Skip to content

Commit

Permalink
Start conversion of fake morphologist schema
Browse files Browse the repository at this point in the history
  • Loading branch information
sapetnioc committed Nov 28, 2023
1 parent 6437abd commit 802629e
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 141 deletions.
38 changes: 26 additions & 12 deletions capsul/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ def __init__(
super().__setattr__("_metadata", metadata)
if actions is None:
actions = []
super().__setattr__("_executable", executable)
super().__setattr__("executable", executable)
if parameters_equivalence is None:
parameters_equivalence = {}
super().__setattr__("_parameters_equivalence", parameters_equivalence)
Expand All @@ -686,14 +686,14 @@ def __init__(
metadata.setdefault(field.name, {})["path_generation"] = False

def set_executable(self, executable):
super().__setattr__("_executable", executable)
super().__setattr__("executable", executable)

def __getitem__(self, key):
if self._parameter is None:
return MetadataModification(
unused=self._unused,
metadata=self._metadata,
executable=self._executable,
executable=self.executable,
parameters_equivalence=self._parameters_equivalence,
parameter=key,
item=None,
Expand All @@ -711,6 +711,16 @@ def __getitem__(self, key):
def __getattr__(self, attribute):
return self[attribute]

def value(self):
result = self._metadata
if self._parameter:
result = result[self._parameter]
if self._item:
result = result[self._item]
super().__setattr__("_parameter", None)
super().__setattr__("_item", None)
return result

def __setitem__(self, key, value):
if isinstance(value, MetadataModification):
if self._item:
Expand Down Expand Up @@ -755,20 +765,20 @@ def __setattr__(self, attribute, value):
self[attribute] = value

def _parameters(self, parameters):
# print("!parameters!", self._executable.name, parameters)
# print("!parameters!", self.executable.name, parameters)
if isinstance(parameters, str):
selection = (re.compile(fnmatch.translate(parameters)),)
else:
selection = tuple(re.compile(fnmatch.translate(i)) for i in parameters)
for field in self._executable.user_fields():
for field in self.executable.user_fields():
# print(
# "!parameters! ?",
# field.name,
# any(i.match(field.name) for i in selection),
# )
if any(i.match(field.name) for i in selection):
exported_name = self._parameters_equivalence.get(
self._executable, {}
self.executable, {}
).get(field.name)
# print("!parameters! ->", exported_name)
if exported_name:
Expand All @@ -785,7 +795,7 @@ def unused(self, value=True):
raise Exception("invalid metdata modification, no parameter")
if not self._item:
raise Exception("invalid metdata modification, no item")
if self._executable.activated:
if self.executable.activated:
for parameter in self._parameters(self._parameter):
for item in self._items(self._item):
self._unused.setdefault(parameter, {})[item] = value
Expand All @@ -797,7 +807,9 @@ def append(self, value, sep="_"):
raise Exception("invalid metdata modification, no parameter")
if not self._item:
raise Exception("invalid metdata modification, no item")
if self._executable.activated:
if isinstance(value, MetadataModification):
value = value.value()
if self.executable.activated:
# print("!apply_append!", executable.name, parameters, items, value)
for parameter in self._parameters(self._parameter):
for item in self._items(self._item):
Expand All @@ -814,7 +826,9 @@ def prepend(self, value, sep="_"):
raise Exception("invalid metdata modification, no parameter")
if not self._item:
raise Exception("invalid metdata modification, no item")
if self._executable.activated:
if isinstance(value, MetadataModification):
value = value.value()
if self.executable.activated:
# print("!apply_prepend!", executable.name, parameters, items, value)
for parameter in self._parameters(self._parameter):
for item in self._items(self._item):
Expand All @@ -827,7 +841,7 @@ def prepend(self, value, sep="_"):
super().__setattr__("_item", None)

def _set(self, parameters, items, value):
if self._executable.activated:
if self.executable.activated:
for parameter in self._parameters(parameters):
for item in self._items(items):
self._metadata.setdefault(parameter, {})[item] = value
Expand All @@ -839,7 +853,7 @@ def _copy_item(
source_parameters,
source_items,
):
if self._executable.activated:
if self.executable.activated:
# print(
# "!apply_copy_item!",
# executable.name,
Expand All @@ -866,7 +880,7 @@ def _copy_all(
parameters,
source_parameters,
):
if self._executable.activated:
if self.executable.activated:
# print(
# "!apply_copy_all!",
# executable.name,
Expand Down
235 changes: 114 additions & 121 deletions capsul/schemas/brainvisa.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from capsul.dataset import MetadataSchema, SchemaMapping
from capsul.dataset import MetadataSchema, SchemaMapping, process_schema
from soma.controller import undefined
import importlib
import copy
Expand Down Expand Up @@ -267,133 +267,126 @@ def declare_morpho_schemas(morpho_module):
sulcigraphmorphometrybysubject.sulcigraphmorphometrybysubject
)

bv_acq_meta = {
"unused": [
"subject_only",
"analysis",
"seg_directory",
"side",
"sidebis",
"sulci_graph_version",
"sulci_recognition_session",
]
}
bv_t1_meta = copy.deepcopy(bv_acq_meta)
bv_t1_meta["unused"] += ["suffix"]
bv_ref_meta = copy.deepcopy(bv_acq_meta)
bv_ref_meta["unused"].remove("seg_directory")
bv_acq_unused = [
"subject_only",
"analysis",
"seg_directory",
"side",
"sidebis",
"sulci_graph_version",
"sulci_recognition_session",
]
bv_t1_unused = bv_acq_meta + ["suffix"]
bv_ref_unused = list(bv_acq_meta)
bv_ref_unused.remove("seg_directory")

def updated(d, *args):
res = copy.deepcopy(d)
for d2 in args:
res.update(d2)
return res

# class MorphologistBrainVISA(
# ProcessSchema, schema="brainvisa", process=Morphologist
# ):
# _ = {
# "*": {"process": None, "modality": "t1mri"},
# "*_pass1": Append("suffix", "pass1"),
# "*_labelled_graph": {
# "suffix": lambda **kwargs: f'{kwargs["metadata"].sulci_recognition_session}'
# f'_{kwargs["metadata"].sulci_recognition_type}',
# },
# "left_*": {"side": "L"},
# "right_*": {"side": "R"},
# }

# _nodes = {
# "GreyWhiteClassification": {"*": {"side": "L"}},
# "GreyWhiteTopology": {"*": {"side": "L"}},
# "GreyWhiteMesh": {"*": {"sidebis": "L"}},
# "PialMesh": {"*": {"sidebis": "L"}},
# "SulciSkeleton": {"*": {"side": "L"}},
# "CorticalFoldsGraph": {"*": {"side": "L"}},
# "SulciRecognition": {"*": {"side": "L"}},
# "*_1": {"*": {"side": "R"}},
# "GreyWhiteMesh_1": {"*": {"sidebis": "R", "side": None}},
# "PialMesh_1": {"*": {"sidebis": "R", "side": None}},
# "SulciRecognition*": {
# "*": {
# "sulci_graph_version": lambda **kwargs: f'{kwargs["process"].CorticalFoldsGraph_graph_version}',
# "prefix": None,
# "sidebis": None,
# }
# },
# "*.ReorientAnatomy": {
# "_meta_links": {
# "transformation": {
# "*": [],
# },
# },
# },
# "*.Convert*normalizationToAIMS": {
# "_meta_links": {
# "*": {
# "*": [],
# },
# },
# },
# }

# metadata_per_parameter = {
# "*": {
# "unused": [
# "subject_only",
# "sulci_graph_version",
# "sulci_recognition_session",
# ]
# },
# "*_graph": {"unused": ["subject_only", "sulci_recognition_session"]},
# "*_labelled_graph": {"unused": ["subject_only"]},
# "t1mri": bv_t1_meta,
# "imported_t1mri": bv_t1_meta,
# "reoriented_t1mri": bv_t1_meta,
# "normalized_t1mri": bv_acq_meta,
# "normalization_spm_native_transformation": bv_acq_meta,
# "commissure_coordinates": bv_t1_meta,
# "t1mri_referential": bv_ref_meta,
# "Talairach_transform": bv_ref_meta,
# "MNI_transform": bv_ref_meta,
# "subject": {"used": ["subject_only", "subject"]},
# "sulcal_morpho_measures": {"unused": ["subject_only"]},
# }

# normalization_spm_native_transformation = {"prefix": None}
# commissure_coordinates = {
# "extension": "APC",
# }
# t1mri_referential = {
# "seg_directory": "registration",
# "short_prefix": "RawT1-",
# "suffix": lambda **kwargs: f'{kwargs["metadata"].acquisition}',
# "extension": "referential",
# }
# Talairach_transform = {
# "seg_directory": "registration",
# "prefix": "",
# "short_prefix": "RawT1-",
# "suffix": lambda **kwargs: f'{kwargs["metadata"].acquisition}_TO_Talairach-ACPC',
# "extension": "trm",
# }
# MNI_transform = {
# "seg_directory": "registration",
# "prefix": "",
# "short_prefix": "RawT1-",
# "suffix": lambda **kwargs: f'{kwargs["metadata"].acquisition}_TO_Talairach-MNI',
# "extension": "trm",
# }
# left_graph = {
# "prefix": None,
# "suffix": None,
# }
# right_graph = {
# "prefix": None,
# "suffix": None,
# }
# subject = {"subject_only": True}
# sulcal_morpho_measures = {"subject_only": False}
@process_schema("brainvisa", Morphologist)
def brainvisa_Morphologist(metadata):
metadata["*"].process = None
metadata["*"].modality = "t1mri"
metadata["*_pass1"].suffix.append("pass1")
metadata["*_labelled_graph"].suffix.append(
metadata["left_labelled_graph"].sulci_recognition_session
)
metadata["*_labelled_graph"].suffix.append(
metadata["left_labelled_graph"].sulci_recognition_type
)
metadata["left_*"].side = "L"
metadata["right_*"].side = "R"
# _nodes = {
# "GreyWhiteClassification": {"*": {"side": "L"}},
# "GreyWhiteTopology": {"*": {"side": "L"}},
# "GreyWhiteMesh": {"*": {"sidebis": "L"}},
# "PialMesh": {"*": {"sidebis": "L"}},
# "SulciSkeleton": {"*": {"side": "L"}},
# "CorticalFoldsGraph": {"*": {"side": "L"}},
# "SulciRecognition": {"*": {"side": "L"}},
# "*_1": {"*": {"side": "R"}},
# "GreyWhiteMesh_1": {"*": {"sidebis": "R", "side": None}},
# "PialMesh_1": {"*": {"sidebis": "R", "side": None}},
# "SulciRecognition*": {
# "*": {
# "sulci_graph_version": lambda **kwargs: f'{kwargs["process"].CorticalFoldsGraph_graph_version}',
# "prefix": None,
# "sidebis": None,
# }
# },
# "*.ReorientAnatomy": {
# "_meta_links": {
# "transformation": {
# "*": [],
# },
# },
# },
# "*.Convert*normalizationToAIMS": {
# "_meta_links": {
# "*": {
# "*": [],
# },
# },
# },
# }

metadata["*"][
"subject_only", "sulci_graph_version", "sulci_recognition_session"
].unused()
metadata["*_graph"]["sulci_graph_version"].unused(False)
metadata["*_graph"]["sulci_graph_version", "sulci_recognition_session"].unused(
False
)

metadata[
"t1mri", "imported_t1mri", "reoriented_t1mri", "commissure_coordinates"
][bv_t1_unused].unused()
metadata[
"normalized_t1mri",
"normalization_spm_native_transformation",
][bv_acq_unused].unused()
metadata[
"t1mri_referential",
"Talairach_transform",
"MNI_transform",
][bv_ref_unused].unused()
metadata["subject"]["subject_only", "subject"].unused(False)
metadata["sulcal_morpho_measures"]["subject_only"].unused()

metadata.normalization_spm_native_transformation.prefix = None
metadata.commissure_coordinates.extension = "APC"

metadata.t1mri_referential.seg_directory = "registration"
metadata.t1mri_referential.short_prefix = "RawT1-"
metadata.t1mri_referential.suffix = (
metadata.t1mri_referential.acquisition.value()
)
metadata.t1mri_referential.extension = "referential"

metadata.Talairach_transform.seg_directory = "registration"
metadata.Talairach_transform.prefix = ""
metadata.Talairach_transform.short_prefix = "RawT1-"
metadata.Talairach_transform.suffix = (
f"{metadata.Talairach_transform.acquisition.value()}_TO_Talairach-ACPC"
)
metadata.Talairach_transform.extension = "trm"

metadata.MNI_transform.seg_directory = "registration"
metadata.MNI_transform.prefix = ""
metadata.MNI_transform.short_prefix = "RawT1-"
metadata.MNI_transform.suffix = (
f"{metadata.MNI_transform.acquisition.value()}_TO_Talairach-MNI"
)
metadata.MNI_transform.extension = "trm"

metadata["left_graph", "right_graph"].prefix = None
metadata["left_graph", "right_graph"].suffix = None

metadata.subject.subject_only = True
metadata.sulcal_morpho_measures.subject_only = False

# class MorphologistBIDS(
# MorphologistBrainVISA, schema="morphologist_bids", process=Morphologist
Expand Down
Loading

0 comments on commit 802629e

Please sign in to comment.