Skip to content

Commit

Permalink
Merge branch 'develop' into feature/CELE-70
Browse files Browse the repository at this point in the history
  • Loading branch information
aranega committed Aug 15, 2024
2 parents be16fcf + ae7aa6a commit a2d13df
Show file tree
Hide file tree
Showing 58 changed files with 3,836 additions and 2,180 deletions.
47 changes: 47 additions & 0 deletions applications/visualizer/api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,13 @@
"title": "Id",
"type": "string"
},
"neuron3DUrl": {
"title": "Neuron3Durl",
"type": "string"
},
"emData": {
"$ref": "#/components/schemas/EMData"
},
"collection": {
"maxLength": 20,
"title": "Collection",
Expand Down Expand Up @@ -452,6 +459,8 @@
},
"required": [
"id",
"neuron3DUrl",
"emData",
"collection",
"name",
"description",
Expand All @@ -462,6 +471,39 @@
"title": "Dataset",
"type": "object"
},
"EMData": {
"properties": {
"min_zoom": {
"title": "Min Zoom",
"type": "integer"
},
"max_zoom": {
"title": "Max Zoom",
"type": "integer"
},
"nb_slices": {
"title": "Nb Slices",
"type": "integer"
},
"resource_url": {
"title": "Resource Url",
"type": "string"
},
"segmentation_url": {
"title": "Segmentation Url",
"type": "string"
}
},
"required": [
"min_zoom",
"max_zoom",
"nb_slices",
"resource_url",
"segmentation_url"
],
"title": "EMData",
"type": "object"
},
"ErrorMessage": {
"properties": {
"detail": {
Expand All @@ -488,6 +530,10 @@
"title": "Datasetids",
"type": "array"
},
"model3DUrl": {
"title": "Model3Durl",
"type": "string"
},
"nclass": {
"maxLength": 30,
"title": "Nclass",
Expand Down Expand Up @@ -522,6 +568,7 @@
"required": [
"name",
"datasetIds",
"model3DUrl",
"nclass",
"neurotransmitter",
"type"
Expand Down
36 changes: 36 additions & 0 deletions applications/visualizer/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,18 @@ components:
description:
title: Description
type: string
emData:
$ref: '#/components/schemas/EMData'
id:
title: Id
type: string
name:
maxLength: 50
title: Name
type: string
neuron3DUrl:
title: Neuron3Durl
type: string
time:
title: Time
type: number
Expand All @@ -65,6 +70,8 @@ components:
type: number
required:
- id
- neuron3DUrl
- emData
- collection
- name
- description
Expand All @@ -73,6 +80,31 @@ components:
- type
title: Dataset
type: object
EMData:
properties:
max_zoom:
title: Max Zoom
type: integer
min_zoom:
title: Min Zoom
type: integer
nb_slices:
title: Nb Slices
type: integer
resource_url:
title: Resource Url
type: string
segmentation_url:
title: Segmentation Url
type: string
required:
- min_zoom
- max_zoom
- nb_slices
- resource_url
- segmentation_url
title: EMData
type: object
ErrorMessage:
properties:
detail:
Expand Down Expand Up @@ -110,6 +142,9 @@ components:
default: false
title: Intail
type: boolean
model3DUrl:
title: Model3Durl
type: string
name:
title: Name
type: string
Expand All @@ -128,6 +163,7 @@ components:
required:
- name
- datasetIds
- model3DUrl
- nclass
- neurotransmitter
- type
Expand Down
63 changes: 43 additions & 20 deletions applications/visualizer/backend/api/api.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from collections import defaultdict
from typing import Optional
from typing import Iterable, Optional

from django.http import HttpResponse
from ninja import NinjaAPI, Router, Schema, Query
from ninja.pagination import paginate, PageNumberPagination
from django.shortcuts import aget_object_or_404
from django.db.models import Q, F, Value, CharField, Func, OuterRef
from django.db.models import Q
from django.db.models.manager import BaseManager
from django.db.models.functions import Coalesce, Concat
from django.conf import settings

from .schemas import Dataset, Neuron, Connection
from .schemas import Dataset, EMData, Neuron, Connection
from .models import (
Dataset as DatasetModel,
Neuron as NeuronModel,
Expand Down Expand Up @@ -50,13 +49,36 @@ def api_operation(self, *args, **kwargs):
# )


def annotate_dataset(datasets: Iterable[DatasetModel]):
for dataset in datasets:
dataset_id = dataset.id
dataset.neuron3D_url = ( # type: ignore
settings.DATASET_NEURON_REPRESENTATION_3D_URL_FORMAT.format(
dataset=dataset_id
)
)
dataset.em_data = EMData( # type: ignore
min_zoom=0,
max_zoom=0,
nb_slices=0,
# resource_url=settings.DATASET_EMDATA_URL_FORMAT.format(dataset=dataset_id),
# segmentation_url=settings.DATASET_EMDATA_SEGMENTATION_URL_FORMAT.format(
# dataset=dataset_id
# ),
resource_url=settings.DATASET_EMDATA_URL_FORMAT,
segmentation_url=settings.DATASET_EMDATA_SEGMENTATION_URL_FORMAT,
)


@api.get("/datasets", response=list[Dataset], tags=["datasets"])
async def get_datasets(request, ids: Optional[list[str]] = Query(None)):
"""Returns all datasets or a filtered list based on provided IDs"""
if ids:
datasets = await to_list(DatasetModel.objects.filter(id__in=ids))
else:
datasets = await to_list(DatasetModel.objects.all())

annotate_dataset(datasets)
return datasets


Expand All @@ -82,10 +104,12 @@ async def get_datasets(request, ids: Optional[list[str]] = Query(None)):
)
async def get_dataset(request, dataset: str):
"""Returns a specific dataset"""
return await aget_object_or_404(DatasetModel, id=dataset)
obj = await aget_object_or_404(DatasetModel, id=dataset)
annotate_dataset((obj,))
return obj


def annotate_neurons_w_dataset_ids(neurons: BaseManager[NeuronModel]) -> None:
def annotate_neurons(neurons: BaseManager[NeuronModel]) -> None:
"""Queries the datasets ids for each neuron."""
neuron_names = neurons.values_list("name", flat=True).distinct()
pre = (
Expand All @@ -104,15 +128,18 @@ def annotate_neurons_w_dataset_ids(neurons: BaseManager[NeuronModel]) -> None:
for neuron, dataset in pre.union(post):
neurons_dataset_ids[neuron].add(dataset)

# Add dataset ids and 3D representation path
for neuron in neurons:
neuron.dataset_ids = neurons_dataset_ids[neuron.name] # type: ignore
name = neuron.name
neuron.dataset_ids = neurons_dataset_ids[name] # type: ignore
neuron.model3D_url = settings.NEURON_REPRESENTATION_3D_URL_FORMAT.format(name=name) # type: ignore


def neurons_from_datasets(
neurons: BaseManager[NeuronModel], dataset_ids: list[str]
) -> BaseManager[NeuronModel]:
"""Filters neurons belonging to specific datasets."""
return neurons.filter(
neurons = neurons.filter(
Q(
name__in=ConnectionModel.objects.filter(
dataset__id__in=dataset_ids
Expand All @@ -124,6 +151,7 @@ def neurons_from_datasets(
).values_list("post", flat=True)
)
)
return neurons


@api.get(
Expand All @@ -134,7 +162,8 @@ def neurons_from_datasets(
def get_dataset_neurons(request, dataset: str):
"""Returns all the neurons of a dedicated dataset"""
neurons = neurons_from_datasets(NeuronModel.objects, [dataset])
annotate_neurons_w_dataset_ids(neurons)
annotate_neurons(neurons)

return neurons


Expand All @@ -144,17 +173,14 @@ def search_cells(
name: Optional[str] = Query(None),
dataset_ids: Optional[list[str]] = Query(None),
):
neurons = NeuronModel.objects
neurons = NeuronModel.objects.all()

if name:
neurons = neurons.filter(name__istartswith=name)

if dataset_ids:
neurons = neurons_from_datasets(neurons, dataset_ids)
else:
neurons = neurons.all()

annotate_neurons_w_dataset_ids(neurons)
annotate_neurons(neurons)

return neurons

Expand All @@ -163,15 +189,12 @@ def search_cells(
@paginate(PageNumberPagination, page_size=50) # BUG: this is not being applied
def get_all_cells(request, dataset_ids: Optional[list[str]] = Query(None)):
"""Returns all the cells (neurons) from the DB"""
neurons = NeuronModel.objects
neurons = NeuronModel.objects.all()

if dataset_ids:
neurons = neurons_from_datasets(neurons, dataset_ids)
else:
neurons = neurons.all()

annotate_neurons_w_dataset_ids(neurons)

annotate_neurons(neurons)
return neurons


Expand Down
19 changes: 19 additions & 0 deletions applications/visualizer/backend/api/schemas.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.conf import settings

from ninja import ModelSchema, Schema
from .models import (
Dataset as DatasetModel,
Expand All @@ -19,8 +21,24 @@ class Config(Schema.Config):
populate_by_name = True


class Artifact(Schema): ...


class Model3D(Artifact): ...


class EMData(Artifact):
min_zoom: int
max_zoom: int
nb_slices: int
resource_url: str
segmentation_url: str


class Dataset(ModelSchema, BilingualSchema):
id: str
neuron3D_url: str
em_data: EMData

class Meta:
model = DatasetModel
Expand All @@ -38,6 +56,7 @@ class Meta:
class Neuron(ModelSchema, BilingualSchema):
name: str
dataset_ids: list[str]
model3D_url: str

class Meta:
model = NeuronModel
Expand Down
Loading

0 comments on commit a2d13df

Please sign in to comment.