Skip to content

Commit

Permalink
Try again
Browse files Browse the repository at this point in the history
  • Loading branch information
keller-mark committed Oct 4, 2024
1 parent f64bd51 commit da88b96
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 196 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ classifiers = [
'Intended Audience :: Developers',
'Intended Audience :: Science/Research',
'Topic :: Multimedia :: Graphics',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
Expand Down
7 changes: 2 additions & 5 deletions tests/test_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ def test_anndata_with_base_dir(self):

file_def_creator = w.make_file_def_creator('A', 0)
file_def = file_def_creator('http://localhost:8000')
print(file_def)
self.assertEqual(file_def, {'fileType': 'anndata.zarr', 'url': 'http://localhost:8000/test.h5ad.zarr',
'options': {
'obsEmbedding': [{'path': 'obsm/X_umap', 'dims': [0, 1], 'embeddingType': 'UMAP'}],
Expand All @@ -249,7 +248,6 @@ def test_anndata_with_base_dir_no_names(self):

file_def_creator = w.make_file_def_creator('A', 0)
file_def = file_def_creator('http://localhost:8000')
print(file_def)
self.assertEqual(file_def, {'fileType': 'anndata.zarr', 'url': 'http://localhost:8000/test.h5ad.zarr',
'options': {
'obsEmbedding': [{'path': 'obsm/X_umap', 'dims': [0, 1], 'embeddingType': 'X_umap'}],
Expand Down Expand Up @@ -396,13 +394,12 @@ def test_multivec_zarr_with_base_dir(self):
def test_spatial_data_with_base_dir(self):

spatial_data_path = 'test.spatialdata.zarr'
w = SpatialDataWrapper(spatialdata_path=spatial_data_path, image_elem="picture", obs_set_paths=['obs/CellType'], obs_set_names=['Cell Type'], obs_embedding_paths=[
w = SpatialDataWrapper(sdata_path=spatial_data_path, image_path="images/picture", obs_set_paths=['obs/CellType'], obs_set_names=['Cell Type'], obs_embedding_paths=[
'obsm/X_umap'], obs_embedding_names=['UMAP'])
w.base_dir = data_path
w.local_dir_uid = 'spatialdata.zarr'

file_def_creator = w.make_file_def_creator('A', 0)
file_def = file_def_creator('http://localhost:8000')
print(file_def)
self.assertEqual(file_def,
{'fileType': 'spatialdata.zarr', 'url': 'http://localhost:8000/test.spatialdata.zarr', 'options': {'obsSets': {'obsSets': [{'name': 'Cell Type', 'path': 'obs/CellType'}]}, 'image': {'path': 'picture'}}})
{'fileType': 'spatialdata.zarr', 'url': 'http://localhost:8000/test.spatialdata.zarr', 'options': {'obsSets': [{'name': 'Cell Type', 'path': 'obs/CellType'}], 'image': {'path': 'images/picture'}}})
130 changes: 130 additions & 0 deletions vitessce/file_def_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
from __future__ import annotations

from functools import partial
from typing import Optional

import numpy as np


def gen_obs_embedding_schema(options: dict, paths: Optional[list[str]] = None, names: Optional[list[str]] = None, dims: Optional[list[list[int]]] = None):
if paths is not None:
if "obsEmbedding" not in options:
options["obsEmbedding"] = []
if names is not None:
for key, mapping in zip(paths, names):
options["obsEmbedding"].append({
"path": key,
"dims": [0, 1],
"embeddingType": mapping
})
else:
for mapping in paths:
mapping_key = mapping.split('/')[-1]
options["obsEmbedding"].append({
"path": mapping,
"dims": [0, 1],
"embeddingType": mapping_key
})
if dims is not None:
if "obsEmbedding" not in options:
options["obsEmbedding"] = []
for dim_i, dim in enumerate(dims):
options["obsEmbedding"][dim_i]['dims'] = dim
return options


def gen_obs_sets_schema(options: dict, paths: Optional[list[str]] = None, names: Optional[list[str]] = None):
if paths is not None:
options["obsSets"] = []
if names is not None:
names = names
else:
names = []
for obs in paths:
obs_end_path = obs.split('/')[-1]
names += [obs_end_path]
for obs, name in zip(paths, names):
options["obsSets"].append({
"name": name,
"path": obs
})
return options


def gen_obs_feature_matrix_schema(options: dict, matrix_path: Optional[str] = None, var_filter_path: Optional[str] = None, init_var_filter_path: Optional[str] = None):
if matrix_path is not None:
options["obsFeatureMatrix"] = {
"path": matrix_path
}
if var_filter_path is not None:
options["obsFeatureMatrix"]["featureFilterPath"] = var_filter_path
if init_var_filter_path is not None:
options["obsFeatureMatrix"]["initialFeatureFilterPath"] = init_var_filter_path
return options


def gen_obs_labels_schema(options: dict, paths: Optional[list[str]] = None, names: Optional[list[str]] = None):
if paths is not None:
if names is not None and len(paths) == len(names):
# A name was provided for each path element, so use those values.
names = names
else:
# Names were not provided for each path element,
# so fall back to using the final part of each path for the names.
names = [labels_path.split('/')[-1] for labels_path in paths]
obs_labels = []
for path, name in zip(paths, names):
obs_labels.append({"path": path, "obsLabelsType": name})
options["obsLabels"] = obs_labels
return options


def gen_path_schema(key: str, path: Optional[str], options: dict):
if path is not None:
options[key] = {
"path": path
}
return options


gen_obs_locations_schema = partial(gen_path_schema, "obsLocations")
gen_obs_segmentations_schema = partial(gen_path_schema, "obsSegmentations")
gen_obs_spots_schema = partial(gen_path_schema, "obsSpots")
gen_obs_points_schema = partial(gen_path_schema, "obsPoints")
gen_feature_labels_schema = partial(gen_path_schema, "featureLabels")

def gen_sdata_image_schema(options, path: str, coordinate_system: Optional[str] = None, affine_transformation: Optional[np.ndarray] = None) -> dict:
if path is not None:
options["image"] = {
"path": path
}
if affine_transformation is not None:
options["image"]['coordinateTransformations'] = affine_transformation
if coordinate_system is not None:
options["image"]['coordinateSystem'] = coordinate_system
return options

def gen_sdata_labels_schema(options, path: str, table_path: str = "tables/table", coordinate_system: Optional[str] = None, affine_transformation: Optional[np.ndarray] = None) -> dict:
if path is not None:
options["labels"] = {
"path": path
}
if table_path is not None:
options["labels"]['tablePath'] = table_path
if affine_transformation is not None:
options["labels"]['coordinateTransformations'] = affine_transformation
if coordinate_system is not None:
options["labels"]['coordinateSystem'] = coordinate_system
return options


def gen_sdata_obs_spots_schema(options: dict, shapes_path: Optional[str] = None, table_path: str = "tables/table", region: Optional[str] = None, coordinate_system: Optional[str] = None) -> dict:
if shapes_path is not None:
options['obsSpots'] = {
"path": shapes_path,
"tablePath": table_path,
"region": region
}
if coordinate_system is not None:
options['obsSpots']['coordinateSystem'] = coordinate_system
return options
116 changes: 0 additions & 116 deletions vitessce/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
from __future__ import annotations

from functools import partial
from typing import Optional

import numpy as np


def get_next_scope_numeric(prev_scopes):
next_scope_int = 0
next_scope_str = None
Expand Down Expand Up @@ -42,111 +34,3 @@ def get_initial_coordination_scope_name(dataset_uid, data_type, i=None):
prefix = get_initial_coordination_scope_prefix(dataset_uid, data_type)
return f"{prefix}{0 if i is None else i}"


def gen_obs_embedding_schema(options: dict, paths: Optional[list[str]] = None, names: Optional[list[str]] = None, dims: Optional[list[list[int]]] = None):
if paths is not None:
if "obsEmbedding" not in options:
options["obsEmbedding"] = []
if names is not None:
for key, mapping in zip(paths, names):
options["obsEmbedding"].append({
"path": key,
"dims": [0, 1],
"embeddingType": mapping
})
else:
for mapping in paths:
mapping_key = mapping.split('/')[-1]
options["obsEmbedding"].append({
"path": mapping,
"dims": [0, 1],
"embeddingType": mapping_key
})
if dims is not None:
if "obsEmbedding" not in options:
options["obsEmbedding"] = []
for dim_i, dim in enumerate(dims):
options["obsEmbedding"][dim_i]['dims'] = dim
return options


def gen_obs_sets_schema(options: dict, paths: Optional[list[str]] = None, names: Optional[list[str]] = None):
if paths is not None:
options["obsSets"] = []
if names is not None:
names = names
else:
names = []
for obs in paths:
obs_end_path = obs.split('/')[-1]
first_letter_capitalized = obs_end_path.capitalize()[0]
names = [first_letter_capitalized + obs_end_path[1:]]
for obs, name in zip(paths, names):
options["obsSets"].append({
"name": name,
"path": obs
})
return options


def gen_obs_feature_matrix_schema(options: dict, matrix_path: Optional[str] = None, var_filter_path: Optional[str] = None, init_var_filter_path: Optional[str] = None):
if matrix_path is not None:
options["obsFeatureMatrix"] = {
"path": matrix_path
}
if var_filter_path is not None:
options["obsFeatureMatrix"]["featureFilterPath"] = var_filter_path
if init_var_filter_path is not None:
options["obsFeatureMatrix"]["initialFeatureFilterPath"] = init_var_filter_path
return options


def gen_obs_labels_schema(options: dict, paths: Optional[list[str]] = None, names: Optional[list[str]] = None):
if paths is not None:
if names is not None and len(paths) == len(names):
# A name was provided for each path element, so use those values.
names = names
else:
# Names were not provided for each path element,
# so fall back to using the final part of each path for the names.
names = [labels_path.split('/')[-1] for labels_path in paths]
obs_labels = []
for path, name in zip(paths, names):
obs_labels.append({"path": path, "obsLabelsType": name})
options["obsLabels"] = obs_labels
return options


def gen_path_schema(key: str, path: Optional[str], options: dict):
if path is not None:
options[key] = {
"path": path
}
return options


gen_obs_locations_schema = partial(gen_path_schema, "obsLocations")
gen_obs_segmentations_schema = partial(gen_path_schema, "obsSegmentations")
gen_obs_spots_schema = partial(gen_path_schema, "obsSpots")
gen_obs_points_schema = partial(gen_path_schema, "obsPoints")
gen_feature_labels_schema = partial(gen_path_schema, "featureLabels")


def gen_image_schema(options, path: str, affine_transformation: Optional[np.ndarray] = None) -> dict:
if path is not None:
options["image"] = {
"path": path
}
if affine_transformation is not None:
options['coordinateTransformations'] = affine_transformation
return options


def gen_obs_spots_from_shapes_schema(options: dict, shapes_path: Optional[str] = None, table_path: str = "tables/table") -> dict:
if shapes_path is not None:
options['obsSpots'] = {
"path": shapes_path,
"tablePath": table_path,
"region": "region"
}
return options
Loading

0 comments on commit da88b96

Please sign in to comment.