Skip to content

Commit

Permalink
tqdm
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnnsrs committed Sep 1, 2023
1 parent a0e7a70 commit 57876ed
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 11 deletions.
17 changes: 17 additions & 0 deletions graphql/stage.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@ fragment ExportStage on Stage {
positions {
name
id
x
z
y
omeros {
timepoints {
era {
name
}
deltaT
}
views {

}
acquisitionDate
representation {
store
Expand All @@ -17,6 +29,11 @@ fragment ExportStage on Stage {
id
store
name
metrics {
id
key
value
}
}
}
}
Expand Down
71 changes: 61 additions & 10 deletions gucker/api/schema.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from rath.scalars import ID
from mikro.funcs import execute, aexecute
from pydantic import Field, BaseModel
from mikro.traits import Position, Representation, Omero, Stage
from mikro.rath import MikroRath
from typing import Tuple, Literal, Optional
from enum import Enum
from mikro.traits import Representation, Stage, Position, Omero
from mikro.scalars import Store, File
from pydantic import BaseModel, Field
from mikro.scalars import Store, MetricValue
from rath.scalars import ID
from datetime import datetime
from typing import Tuple, Literal, Optional
from mikro.rath import MikroRath
from mikro.funcs import aexecute, execute


class OmeroFileType(str, Enum):
Expand All @@ -24,16 +24,50 @@ class OmeroFileType(str, Enum):
"Unwknon File Format"


class ExportStageFragmentPositionsOmerosTimepointsEra(BaseModel):
"""Era(id, created_by, created_through, created_while, name, start, end, created_at)"""

typename: Optional[Literal["Era"]] = Field(alias="__typename", exclude=True)
name: str
"The name of the era"

class Config:
frozen = True


class ExportStageFragmentPositionsOmerosTimepoints(BaseModel):
"""The relative position of a sample on a microscope stage"""

typename: Optional[Literal["Timepoint"]] = Field(alias="__typename", exclude=True)
era: ExportStageFragmentPositionsOmerosTimepointsEra
delta_t: Optional[float] = Field(alias="deltaT")

class Config:
frozen = True


class ExportStageFragmentPositionsOmerosRepresentationFileorigins(BaseModel):
typename: Optional[Literal["OmeroFile"]] = Field(alias="__typename", exclude=True)
id: ID
file: Optional[File]
file: str
"The file"

class Config:
frozen = True


class ExportStageFragmentPositionsOmerosRepresentationDerivedMetrics(BaseModel):
typename: Optional[Literal["Metric"]] = Field(alias="__typename", exclude=True)
id: ID
key: str
"The Key"
value: Optional[MetricValue]
"Value"

class Config:
frozen = True


class ExportStageFragmentPositionsOmerosRepresentationDerived(
Representation, BaseModel
):
Expand Down Expand Up @@ -76,6 +110,13 @@ class ExportStageFragmentPositionsOmerosRepresentationDerived(
store: Optional[Store]
name: Optional[str]
"Cleartext name"
metrics: Optional[
Tuple[
Optional[ExportStageFragmentPositionsOmerosRepresentationDerivedMetrics],
...,
]
]
"Associated metrics of this Imasge"

class Config:
frozen = True
Expand Down Expand Up @@ -142,6 +183,10 @@ class ExportStageFragmentPositionsOmeros(Omero, BaseModel):
"""

typename: Optional[Literal["Omero"]] = Field(alias="__typename", exclude=True)
timepoints: Optional[
Tuple[Optional[ExportStageFragmentPositionsOmerosTimepoints], ...]
]
"Associated Timepoints"
acquisition_date: Optional[datetime] = Field(alias="acquisitionDate")
representation: ExportStageFragmentPositionsOmerosRepresentation

Expand All @@ -156,6 +201,12 @@ class ExportStageFragmentPositions(Position, BaseModel):
name: str
"The name of the possition"
id: ID
x: float
"pixelSize for x in microns"
z: float
"pixelSize for z in microns"
y: float
"pixelSize for y in microns"
omeros: Optional[Tuple[Optional[ExportStageFragmentPositionsOmeros], ...]]
"Associated images through Omero"

Expand All @@ -180,7 +231,7 @@ class ExportDatasetFragmentOmerofiles(BaseModel):
"The name of the file"
type: OmeroFileType
"The type of the file"
file: Optional[File]
file: str
"The file"

class Config:
Expand All @@ -206,7 +257,7 @@ class Arguments(BaseModel):
id: ID

class Meta:
document = "fragment ExportStage on Stage {\n name\n positions {\n name\n id\n omeros {\n acquisitionDate\n representation {\n store\n name\n id\n fileOrigins {\n id\n file\n }\n derived(flatten: 4) {\n id\n store\n name\n }\n }\n }\n }\n}\n\nquery GetExportStage($id: ID!) {\n stage(id: $id) {\n ...ExportStage\n }\n}"
document = "fragment ExportStage on Stage {\n name\n positions {\n name\n id\n x\n z\n y\n omeros {\n timepoints {\n era {\n name\n }\n deltaT\n }\n acquisitionDate\n representation {\n store\n name\n id\n fileOrigins {\n id\n file\n }\n derived(flatten: 4) {\n id\n store\n name\n metrics {\n id\n key\n value\n }\n }\n }\n }\n }\n}\n\nquery GetExportStage($id: ID!) {\n stage(id: $id) {\n ...ExportStage\n }\n}"


class GetExportDatasetQuery(BaseModel):
Expand Down
20 changes: 19 additions & 1 deletion gucker/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from mikro import Stage, Image
import tifffile
from arkitekt.tqdm import tqdm
import json

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -235,6 +236,15 @@ def export_representation(self, representation: Image, dir: str) -> None:
os.path.join(dir, f"ID({representation.id}) {representation.name}.tiff"),
representation.data,
)
with open(
os.path.join(
dir, f"ID({representation.id}) {representation.name} meta.json"
),
"w",
) as f:
f.write(
json.dumps(representation.dict(), indent=4, sort_keys=True, default=str)
)

def export_stage(self, stage: Stage) -> None:
"""Export Stage
Expand All @@ -250,15 +260,23 @@ def export_stage(self, stage: Stage) -> None:

stage_dir = os.path.join(self.export_dir, f"ID({stage.id}) {export_stage.name}")
os.makedirs(stage_dir, exist_ok=True)
for item in export_stage.positions:
for item in tqdm(export_stage.positions):
pos_dir = os.path.join(stage_dir, f"ID({item.id}) {item.name}")
os.makedirs(pos_dir, exist_ok=True)
with open(os.path.join(pos_dir, "position.json"), "w") as f:
f.write(
json.dumps(item.dict(), indent=4, sort_keys=True, default=str),
)
for image in item.omeros:
image_dir = os.path.join(
pos_dir,
f"ID({image.representation.id}) {image.representation.name} {image.acquisition_date}",
)
os.makedirs(image_dir, exist_ok=True)
with open(os.path.join(image_dir, "raw.json"), "w") as f:
f.write(
json.dumps(image.dict(), indent=4, sort_keys=True, default=str)
)
self.export_representation(image.representation, image_dir)

for file in image.representation.derived:
Expand Down

0 comments on commit 57876ed

Please sign in to comment.