Skip to content
Snippets Groups Projects
Commit 7584f427 authored by Marco Frailis's avatar Marco Frailis
Browse files

Fixing views and serializers. Jupyter notebook with queries

parent 79a835ff
No related branches found
No related tags found
No related merge requests found
from django.contrib import admin from django.contrib import admin
from .models import(Instrument, NispRawFrame, NispDetector, Astrometry) from .models import(Instrument, NispDetector, Astrometry, DataContainer,
NispRawFrame)
# Register your models here. # Register your models here.
admin.site.register(Instrument) admin.site.register(Instrument)
admin.site.register(NispRawFrame)
admin.site.register(NispDetector) admin.site.register(NispDetector)
admin.site.register(Astrometry) admin.site.register(Astrometry)
admin.site.register(DataContainer)
admin.site.register(NispRawFrame)
...@@ -3,18 +3,34 @@ from composite_field.rest_framework_support import CompositeFieldSerializer ...@@ -3,18 +3,34 @@ from composite_field.rest_framework_support import CompositeFieldSerializer
from rest_framework import serializers from rest_framework import serializers
from imagedb.models import Instrument, NispDetector, NispRawFrame from imagedb.models import Instrument, NispDetector, Astrometry, NispRawFrame
class InstrumentSerializer(serializers.ModelSerializer): class InstrumentSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Instrument model = Instrument
fields = '__all__' fields = '__all__'
class AstrometrySerializer(serializers.ModelSerializer):
ctype1 = CompositeFieldSerializer()
ctype2 = CompositeFieldSerializer()
class Meta:
model = Astrometry
exclude = ('ctype1_coordinateType',
'ctype1_projectionType',
'ctype2_coordinateType',
'ctype2_projectionType')
class NispDetectorSerializer(serializers.ModelSerializer): class NispDetectorSerializer(serializers.ModelSerializer):
astrometry = AstrometrySerializer(read_only = True)
class Meta: class Meta:
model = NispDetector model = NispDetector
exclude = ('rawFrame',) exclude = ('rawFrame',)
class NispRawFrameSerializer(serializers.ModelSerializer): class NispRawFrameSerializer(serializers.ModelSerializer):
detectors = NispDetectorSerializer(many = True, read_only = True) detectors = NispDetectorSerializer(many = True, read_only = True)
commandedPointing = CompositeFieldSerializer() commandedPointing = CompositeFieldSerializer()
...@@ -24,7 +40,7 @@ class NispRawFrameSerializer(serializers.ModelSerializer): ...@@ -24,7 +40,7 @@ class NispRawFrameSerializer(serializers.ModelSerializer):
model = NispRawFrame model = NispRawFrame
exclude = ('commandedPointing_rightAscension', exclude = ('commandedPointing_rightAscension',
'commandedPointing_declination', 'commandedPointing_declination',
'commandedPointing_pointingAngle', 'commandedPointing_orientation',
'imageType_category', 'imageType_category',
'imageType_firstType', 'imageType_firstType',
'imageType_secondType') 'imageType_secondType')
......
...@@ -18,7 +18,9 @@ class NispDetectorViewSet(viewsets.ReadOnlyModelViewSet): ...@@ -18,7 +18,9 @@ class NispDetectorViewSet(viewsets.ReadOnlyModelViewSet):
class NispRawFrameFilterSet(ModelFilterSet): class NispRawFrameFilterSet(ModelFilterSet):
class Meta: class Meta:
model = NispRawFrame model = NispRawFrame
fields = ['id','imageType_category'] fields = [f.name for f in NispRawFrame._meta.get_fields()
if hasattr(f, 'serialize') and f.serialize]
class NispRawFrameViewSet(viewsets.ReadOnlyModelViewSet): class NispRawFrameViewSet(viewsets.ReadOnlyModelViewSet):
queryset = NispRawFrame.objects.all() queryset = NispRawFrame.objects.all()
......
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Data insertion and retrieval with Django models # Data insertion and retrieval with Django models
In the following we show some examples in order to perform insertions and retrievals of metadata In the following we show some examples in order to perform insertions and retrievals of metadata
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from imagedb.models import Instrument from imagedb.models import Instrument
instrument = Instrument.objects.get(instrumentName='NISP') instrument = Instrument.objects.get(instrumentName='NISP')
print(instrument.telescopeName) print(instrument.telescopeName)
``` ```
%% Output
Euclid
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Creating new objects ## Creating new objects
In the following we will create a NispRawFrame object. Since the NispRawFrame must have also a DataCotainer (i.e. a file to reference to), we first create a DataContainer instance. In the following we will create a NispRawFrame object. Since the NispRawFrame must have also a DataCotainer (i.e. a file to reference to), we first create a DataContainer instance.
We can see also that the fields that we have defined as composite fields provide some synctactic sugar when we initialize them with simple dictionaries. We can see also that the fields that we have defined as composite fields provide some synctactic sugar when we initialize them with simple dictionaries.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from imagedb.models import ImageType, Pointing, NispDetector, DataContainer, NispRawFrame from imagedb.models import ImageType, Pointing, NispDetector, DataContainer, NispRawFrame
from datetime import datetime from datetime import datetime
dataFile = DataContainer( dataFile = DataContainer(
fileFormat = 'fits', fileFormat = 'fits',
formatIdentifier = 'le1.nisprawframe', formatIdentifier = 'le1.nisprawframe',
formatVersion = '1.0', formatVersion = '1.0',
url = "http://ia2-owncloud.oats.inaf.it/fake/7ff2f203/data/EUC_LE1_NISP_53892-Y-1_20170712T155430.1Z_00.00.fits" url = "http://ia2-owncloud.oats.inaf.it/fake/7ff2f203/data/EUC_LE1_NISP_53892-Y-1_20170712T155430.1Z_00.00.fits"
) )
# We have to save the data container to the DB before assigning it to a NispRawFrame # We have to save the data container to the DB before assigning it to a NispRawFrame
dataFile.save() dataFile.save()
image = NispRawFrame(exposureTime = 105, image = NispRawFrame(exposureTime = 105,
imgNumber = 16, imgNumber = 16,
naxis1 = 2040, naxis1 = 2040,
naxis2 = 2040, naxis2 = 2040,
imageType = {'category':'SCIENCE', imageType = {'category':'SCIENCE',
'firstType':'OBJECT', 'firstType':'OBJECT',
'secondType':'STD'}, 'secondType':'STD'},
observationDateTime = datetime.strptime("2025-06-21T18:27:23.000001", observationDateTime = datetime.strptime("2025-06-21T18:27:23.000001",
"%Y-%m-%dT%H:%M:%S.%f"), "%Y-%m-%dT%H:%M:%S.%f"),
observationId = 53892, observationId = 53892,
ditherNumber = 1, ditherNumber = 1,
instrument = instrument, instrument = instrument,
commandedPointing = {'rightAscension':8.48223045516, commandedPointing = {'rightAscension':8.48223045516,
'declination':8.48223045516, 'declination':8.48223045516,
'orientation':64.8793517547}, 'orientation':64.8793517547},
filterWheelPosition = "Y", filterWheelPosition = "Y",
grismWheelPosition = "OPEN" grismWheelPosition = "OPEN"
) )
image.frameFile = dataFile image.frameFile = dataFile
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Before saving an object, its surrogate key is still empty, i.e. equal to None Before saving an object, its surrogate key is still empty, i.e. equal to None
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
print(image.id) print(image.id)
image.save() image.save()
print(image.id) print(image.id)
``` ```
%% Output
None
2
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# We can start creating a detector # We can start creating a detector
d11 = NispDetector(detectorId = "11", gain = 1.0, readoutNoise = 0.0, rawFrame = image) d11 = NispDetector(detectorId = "11", gain = 1.0, readoutNoise = 0.0, rawFrame = image)
d11.save() d11.save()
# Or we can use the create() in order to create and save immediately the new object # Or we can use the create() in order to create and save immediately the new object
NispDetector.objects.create(detectorId = "12", gain = 1.0, readoutNoise = 0.0, rawFrame = image) NispDetector.objects.create(detectorId = "12", gain = 1.0, readoutNoise = 0.0, rawFrame = image)
# or we can create the detector starting from the NispRawFrame, using the reversed relationship, # or we can create the detector starting from the NispRawFrame, using the reversed relationship,
# using again the create method # using again the create method
image.detectors.create( image.detectors.create(
detectorId = "13", detectorId = "13",
gain = 1.0, gain = 1.0,
readoutNoise = 0.0 readoutNoise = 0.0
) )
``` ```
%% Output %% Cell type:markdown id: tags:
## Objects retrieval
To retrieve objects from your database, construct a **QuerySet** via a **Manager** on your model class.
A **QuerySet** represents a collection of objects from your database. It can have zero, one or many filters. Filters narrow down the query results based on the given parameters. In SQL terms, a **QuerySet** equates to a **SELECT** statement, and a **filter** is a limiting clause such as **WHERE** or **LIMIT**.
You get a **QuerySet** by using your model’s **Manager**. Each model has at least one Manager, and it’s called **objects** by default.
The simplest way to retrieve objects from a table is to get all of them. To do this, use the **all()** method on a **Manager**:
%% Cell type:code id: tags:
``` python
len(NispRawFrame.objects.all())
```
%% Cell type:markdown id: tags:
But usually we want to filter the results. For this purpose we can use the **filter** method, both provided by the **Manager** and the **QuerySet**
%% Cell type:code id: tags:
``` python
# Retrieving all frames with observation id 53877 and filter Y
# and ordering the results by the ditherNumber
result = NispRawFrame.objects.filter(observationId=53877,
filterWheelPosition='Y').order_by('ditherNumber')
for obj in result:
print(obj.observationId, obj.filterWheelPosition, obj.ditherNumber)
```
%% Cell type:markdown id: tags:
<NispDetector: NispDetector object (4)> We can also limit the number of results of a **QuerySet**, using the Python array-slice syntax:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
image.detectors.all() from datetime import datetime
# Retrieving all NISP raw frames with observation date and time
# greater then or equal to 2026-06-22T17:00
len(NispRawFrame.objects.filter(observationDateTime__gte=datetime(2025,6,22,17,0)))
``` ```
%% Output %% Cell type:code id: tags:
``` python
# Now limiting the result to 5 items
len(NispRawFrame.objects.filter(observationDateTime__gte=datetime(2025,6,22,17,0))[:5])
```
%% Cell type:markdown id: tags:
Keyword argument queries – in filter(), etc. – are “AND”ed together. If you need to execute more complex queries (for example, queries with OR statements), you can use **Q objects**.
<QuerySet [<NispDetector: NispDetector object (2)>, <NispDetector: NispDetector object (3)>, <NispDetector: NispDetector object (4)>]> %% Cell type:code id: tags:
``` python
# Now retrieving all frames with observation ids 53877 and 54349 and filter H
from django.db.models import Q
result = NispRawFrame.objects.filter(Q(observationId=53877) | Q(observationId=54349),
filterWheelPosition='H').order_by('observationId', 'ditherNumber')
for obj in result:
print(obj.observationId, obj.filterWheelPosition, obj.ditherNumber)
```
%% Cell type:markdown id: tags:
We can also traverse relations
%% Cell type:code id: tags:
``` python
# Getting all NispDetectors whose frame observation id is 53877 or 54349 and
# filterWheelPosition is Y.
# Notice that here we also use the 'in' operator
result = NispDetector.objects.filter(rawFrame__observationId__in=[53877, 54349],
rawFrame__filterWheelPosition='Y').order_by(
'rawFrame__observationId', 'rawFrame__ditherNumber',
'detectorId')
for obj in result:
print(obj.rawFrame.observationId, obj.rawFrame.ditherNumber, obj.detectorId)
```
%% Cell type:code id: tags:
``` python
# OR we can find the NispRawFrame whose referenced file url contains "LE1_NISP_52926-J-2"
result = NispRawFrame.objects.filter(frameFile__url__contains="LE1_NISP_52926-J-2")
for obj in result:
print(obj.observationId, obj.frameFile.url)
```
%% Cell type:markdown id: tags:
In order to retrive a single object, instead of using **filter** we can use the **get** method. This method returns one object and raise exceptions if no object is found or if multiple objects satisfy the query
%% Cell type:code id: tags:
``` python
obj = NispRawFrame.objects.get(observationId=53892)
# now we delete such object from the database
obj.delete()
```
%% Cell type:code id: tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment