Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update TransformsList class with to_dict() and from_dict() methods #1956

Merged
merged 1 commit into from
Oct 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 50 additions & 19 deletions starfish/core/image/_registration/transforms_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,67 @@ def _verify_version(cls, semantic_version_str: str) -> None:
f"supports transform list formats from {MIN_SUPPORTED_VERSION} to "
f"{MAX_SUPPORTED_VERSION}")

def to_json(self, filename: str) -> None:
def to_dict(self) -> dict:
"""
Saves the TransformsList to a json file.
Save the TransformsList as a Python dictionary.

Parameters
----------
filename : str
Returns
-------
dict
A Python dictionary with the following keys and values:
{DocumentKeys.TRANSFORMS_LIST}: List[Tuple[str, str, List[List[float]]]]
The value is a tuple of:
selector_string: A string that specifies which starfish dimensions
to apply the transform to (e.g. "{r: 0}")
transform_type: Specifies the transform type (e.g. "similarity")
transform_matrix: A list of lists of floats
{DocumentKeys.VERSION_KEY}: str
"""
transforms_array = []
# Need to convert Axes to str values, and TransformationObjects to array
for selectors, transform_type, transforms_object in self.transforms:
transforms_matrix = transforms_object.params.tolist()
selectors_str = {k.value: v for k, v in selectors.items()}
transforms_array.append((selectors_str, transform_type.value, transforms_matrix))
transforms_document = {
DocumentKeys.TRANSFORMS_LIST: transforms_array,
DocumentKeys.VERSION_KEY: str(CURRENT_VERSION)
}
transforms_document = {
DocumentKeys.TRANSFORMS_LIST: transforms_array,
DocumentKeys.VERSION_KEY: str(CURRENT_VERSION)
}
return transforms_document

def to_json(self, filename: str) -> None:
"""
Saves the TransformsList to a json file.

Parameters
----------
filename : str
"""
transforms_document = self.to_dict()
with open(filename, 'w') as f:
json.dump(transforms_document, f)

@classmethod
def from_dict(cls, transforms_document: dict) -> "TransformsList":
"""
Load a TransformsList from a Python dictionary.

Returns
-------
TransformsList
"""
version_str = transforms_document[DocumentKeys.VERSION_KEY]
cls._verify_version(version_str)
transforms_array = transforms_document[DocumentKeys.TRANSFORMS_LIST]

transforms_list: List[Tuple[Mapping[Axes, int], TransformType, GeometricTransform]] = list()
for selectors_str, transform_type_str, transforms_matrix in transforms_array:
selectors = {Axes(k): v for k, v in selectors_str.items()}
transform_type = TransformType(transform_type_str)
transform_object = transformsTypeMapping[transform_type](np.array(transforms_matrix))
transforms_list.append((selectors, transform_type, transform_object))
return cls(transforms_list)

@classmethod
def from_json(cls, url_or_path: str) -> "TransformsList":
"""
Expand All @@ -118,16 +158,7 @@ def from_json(cls, url_or_path: str) -> "TransformsList":
TransformsList
"""
config = StarfishConfig()
transforms_list: List[Tuple[Mapping[Axes, int], TransformType, GeometricTransform]] = list()
backend, name, _ = resolve_path_or_url(url_or_path, backend_config=config.slicedimage)
with backend.read_contextmanager(name) as fh:
transforms_document = json.load(fh)
version_str = transforms_document[DocumentKeys.VERSION_KEY]
cls._verify_version(version_str)
transforms_array = transforms_document[DocumentKeys.TRANSFORMS_LIST]
for selectors_str, transform_type_str, transforms_matrix in transforms_array:
selectors = {Axes(k): v for k, v in selectors_str.items()}
transform_type = TransformType(transform_type_str)
transform_object = transformsTypeMapping[transform_type](np.array(transforms_matrix))
transforms_list.append((selectors, transform_type, transform_object))
return cls(transforms_list)
return cls.from_dict(transforms_document=transforms_document)