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

Mccalluc/consistent names #71

Open
wants to merge 2 commits into
base: mccalluc/consistent-arrays
Choose a base branch
from
Open
Show file tree
Hide file tree
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
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ $ pip install .
$ src/vis-preview.py --help
usage: vis-preview.py [-h] (--url URL | --json JSON) [--types_url URL]
[--assets_url URL] [--token TOKEN] [--marker MARKER]
[--to_json] [--conf_index I]
[--to_json] [--config_index I]

Given HuBMAP Dataset JSON, generate a Vitessce viewconf, and load vitessce.io.
Given HuBMAP Dataset JSON, generate a Vitessce view config, and load
vitessce.io.

optional arguments:
-h, --help show this help message and exit
Expand All @@ -30,10 +31,10 @@ optional arguments:
--token TOKEN Globus groups token; Only needed if data is not public
--marker MARKER Marker to highlight in visualization; Only used in some
visualizations.
--to_json Output viewconf, rather than open in browser.
--conf_index I Old untiled imagery produces multiple viewconfs, one for
each tile. This allows you display a viewconf other than
the first.
--to_json Output view config, rather than open in browser.
--config_index I Old untiled imagery produces multiple view configs, one
for each tile. This allows you display a view config other
than the first.
```

## Background
Expand Down
34 changes: 17 additions & 17 deletions src/portal_visualization/builders/anndata_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get_configs(self, marker=None):
if f'{zarr_path}/.zgroup' not in file_paths_found:
message = f'RNA-seq assay with uuid {self._uuid} has no .zarr store at {zarr_path}'
raise FileNotFoundError(message)
vc = VitessceConfig(name=self._uuid)
config = VitessceConfig(name=self._uuid)
adata_url = self._build_assets_url(zarr_path, use_token=False)
# Some of the keys (like marker_genes_for_heatmap) here are from our pipeline
# https://github.com/hubmapconsortium/portal-containers/blob/master/containers/anndata-to-ui
Expand All @@ -55,7 +55,7 @@ def get_configs(self, marker=None):
cell_set_obs_names.append("Predicted ASCT Cell Type")
z = zarr.open(adata_url, mode='r', storage_options={'client_kwargs': request_init})
gene_alias = 'var/hugo_symbol' if 'var' in z and 'hugo_symbol' in z['var'] else None
dataset = vc.add_dataset(name=self._uuid).add_object(AnnDataWrapper(
dataset = config.add_dataset(name=self._uuid).add_object(AnnDataWrapper(
adata_url=adata_url,
mappings_obsm=["X_umap"],
mappings_obsm_names=["UMAP"],
Expand All @@ -75,37 +75,37 @@ def get_configs(self, marker=None):
gene_alias=gene_alias
))

vc = self._setup_anndata_view_config(vc, dataset, marker)
return [vc]
config = self._setup_anndata_view_config(config, dataset, marker)
return [config]

def _setup_anndata_view_config(self, vc, dataset, marker=None):
scatterplot = vc.add_view(
def _setup_anndata_view_config(self, config, dataset, marker=None):
scatterplot = config.add_view(
cm.SCATTERPLOT, dataset=dataset, mapping="UMAP", x=0, y=0, w=self._scatterplot_w, h=6)
cell_sets = vc.add_view(
cell_sets = config.add_view(
cm.CELL_SETS, dataset=dataset, x=9, y=0, w=3, h=3)
gene_list = vc.add_view(
gene_list = config.add_view(
cm.GENES, dataset=dataset, x=9, y=4, w=3, h=3)
cell_sets_expr = vc.add_view(
cell_sets_expr = config.add_view(
cm.CELL_SET_EXPRESSION, dataset=dataset, x=7, y=6, w=5, h=4)
heatmap = vc.add_view(
heatmap = config.add_view(
cm.HEATMAP, dataset=dataset, x=0, y=6, w=7, h=4)
# Adding heatmap to coordination doesn't do anything,
# but it also doesn't hurt anything.
# Vitessce feature request to add it:
# https://github.com/vitessce/vitessce/issues/1298

self._add_spatial_view(dataset, vc)
self._add_spatial_view(dataset, config)

if marker:
vc.link_views(
config.link_views(
[cell_sets, gene_list, scatterplot, cell_sets_expr, heatmap],
[CoordinationType.GENE_SELECTION, CoordinationType.CELL_COLOR_ENCODING],
[[marker], "geneSelection"]
)

return vc
return config

def _add_spatial_view(self, dataset, vc):
def _add_spatial_view(self, dataset, config):
# This class does not have a spatial_view...
# but the subclass does, and overrides this method.
pass
Expand All @@ -124,9 +124,9 @@ def __init__(self, entity, groups_token, assets_endpoint):
self._is_spatial = True
self._scatterplot_w = 4

def _add_spatial_view(self, dataset, vc):
spatial = vc.add_view(cm.SPATIAL, dataset=dataset, x=4, y=0, w=5, h=6)
[cells_layer] = vc.add_coordination('spatialCellsLayer')
def _add_spatial_view(self, dataset, config):
spatial = config.add_view(cm.SPATIAL, dataset=dataset, x=4, y=0, w=5, h=6)
[cells_layer] = config.add_coordination('spatialCellsLayer')
cells_layer.set_value(
{
"visible": True,
Expand Down
18 changes: 9 additions & 9 deletions src/portal_visualization/builders/base_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
ConfigsCells = namedtuple('ConfigsCells', ['configs', 'cells'])


def _get_cells_from_conf_list(confs):
def _get_cells_from_config_list(configs):
cells = []
if len(confs) > 1:
if len(configs) > 1:
cells.append(nbformat.v4.new_markdown_cell('Multiple visualizations are available.'))
for conf in confs:
cells.extend(_get_cells_from_conf(conf))
for config in configs:
cells.extend(_get_cells_from_config(config))
return cells


def _get_cells_from_conf(conf):
imports, conf_expression = conf.to_python()
def _get_cells_from_config(config):
imports, config_expression = config.to_python()
return [
nbformat.v4.new_code_cell(f'from vitessce import {", ".join(imports)}'),
nbformat.v4.new_code_cell(f'conf = {conf_expression}\nconf.widget()'),
nbformat.v4.new_code_cell(f'config = {config_expression}\nconfig.widget()'),
]


Expand Down Expand Up @@ -54,7 +54,7 @@ def get_configs(self): # pragma: no cover
def get_configs_cells(self, marker=None):
kwargs = {'marker': marker} if marker is not None else {}
configs = self.get_configs(**kwargs)
cells = _get_cells_from_conf_list(configs)
cells = _get_cells_from_config_list(configs)
return ConfigsCells(configs, cells)

def _replace_url_in_file(self, file):
Expand Down Expand Up @@ -100,7 +100,7 @@ def _build_assets_url(self, rel_path, use_token=True):
return f"{base_url}?{token_param}" if use_token else base_url

def _get_request_init(self):
"""Get request headers for requestInit parameter in Vitessce conf.
"""Get request headers for requestInit parameter in Vitessce config.
This is needed for non-public zarr stores because the client forms URLs for zarr chunks,
not the above _build_assets_url function.

Expand Down
42 changes: 21 additions & 21 deletions src/portal_visualization/builders/imaging_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ def _get_img_and_offset_url(self, img_path, img_dir):
),
)

def _setup_view_config_raster(self, vc, dataset, disable_3d=[]):
vc.add_view(cm.SPATIAL, dataset=dataset, x=3, y=0, w=9, h=12)
vc.add_view(cm.DESCRIPTION, dataset=dataset, x=0, y=8, w=3, h=4)
vc.add_view(cm.LAYER_CONTROLLER, dataset=dataset, x=0, y=0, w=3, h=8).set_props(
def _setup_view_config_raster(self, config, dataset, disable_3d=[]):
config.add_view(cm.SPATIAL, dataset=dataset, x=3, y=0, w=9, h=12)
config.add_view(cm.DESCRIPTION, dataset=dataset, x=0, y=8, w=3, h=4)
config.add_view(cm.LAYER_CONTROLLER, dataset=dataset, x=0, y=0, w=3, h=8).set_props(
disable3d=disable_3d, disableChannelsIfRgbDetected=True
)
return vc
return config


class ImagePyramidViewConfBuilder(AbstractImagingViewConfBuilder):
Expand All @@ -76,8 +76,8 @@ def get_configs(self):
message = f"Image pyramid assay with uuid {self._uuid} has no matching files"
raise FileNotFoundError(message)

vc = VitessceConfig(name="HuBMAP Data Portal")
dataset = vc.add_dataset(name="Visualization Files")
config = VitessceConfig(name="HuBMAP Data Portal")
dataset = config.add_dataset(name="Visualization Files")
images = []
for img_path in found_images:
img_url, offsets_url = self._get_img_and_offset_url(
Expand All @@ -89,11 +89,11 @@ def get_configs(self):
)
)
dataset = dataset.add_object(MultiImageWrapper(images))
vc = self._setup_view_config_raster(vc, dataset)
conf = vc.to_dict()
config = self._setup_view_config_raster(config, dataset)
config_as_dict = config.to_dict()
# Don't want to render all layers
del conf["datasets"][0]["files"][0]["options"]["renderLayers"]
return [VitessceConfig.from_dict(conf)]
del config_as_dict["datasets"][0]["files"][0]["options"]["renderLayers"]
return [VitessceConfig.from_dict(config_as_dict)]


class IMSViewConfBuilder(ImagePyramidViewConfBuilder):
Expand Down Expand Up @@ -131,13 +131,13 @@ def get_configs(self):
raise FileNotFoundError(message)
# Get all files grouped by PosN names.
images_by_pos = group_by_file_name(found_images)
confs = []
# Build up a conf for each Pos.
configs = []
# Build up a config for each Pos.
for images in images_by_pos:
image_wrappers = []
pos_name = self._get_pos_name(images[0])
vc = VitessceConfig(name=pos_name)
dataset = vc.add_dataset(name=pos_name)
config = VitessceConfig(name=pos_name)
dataset = config.add_dataset(name=pos_name)
sorted_images = sorted(images, key=self._get_hybcycle)
for img_path in sorted_images:
img_url, offsets_url = self._get_img_and_offset_url(
Expand All @@ -151,16 +151,16 @@ def get_configs(self):
)
)
dataset = dataset.add_object(MultiImageWrapper(image_wrappers))
vc = self._setup_view_config_raster(
vc,
config = self._setup_view_config_raster(
config,
dataset,
disable_3d=[self._get_hybcycle(img_path) for img_path in sorted_images]
)
conf = vc.to_dict()
config_as_dict = config.to_dict()
# Don't want to render all layers
del conf["datasets"][0]["files"][0]["options"]["renderLayers"]
confs.append(VitessceConfig.from_dict(conf))
return confs
del config_as_dict["datasets"][0]["files"][0]["options"]["renderLayers"]
configs.append(VitessceConfig.from_dict(config_as_dict))
return configs

def _get_hybcycle(self, image_path):
return re.search(SEQFISH_HYB_CYCLE_REGEX, image_path)[0]
Expand Down
16 changes: 8 additions & 8 deletions src/portal_visualization/builders/scatterplot_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ def get_configs(self):
message = f'Files for uuid "{self._uuid}" not found as expected: ' \
f'Expected: {file_paths_expected}; Found: {file_paths_found}'
raise FileNotFoundError(message)
vc = VitessceConfig(name="HuBMAP Data Portal")
dataset = vc.add_dataset(name="Visualization Files")
config = VitessceConfig(name="HuBMAP Data Portal")
dataset = config.add_dataset(name="Visualization Files")
# The sublcass initializes _files in its __init__ method
for file in self._files:
dataset = dataset.add_file(**(self._replace_url_in_file(file)))
vc = self._setup_scatterplot_view_config(vc, dataset)
return [vc]
config = self._setup_scatterplot_view_config(config, dataset)
return [config]

def _setup_scatterplot_view_config(self, vc, dataset):
vc.add_view(cm.SCATTERPLOT, dataset=dataset, mapping="UMAP", x=0, y=0, w=9, h=12)
vc.add_view(cm.CELL_SETS, dataset=dataset, x=9, y=0, w=3, h=12)
return vc
def _setup_scatterplot_view_config(self, config, dataset):
config.add_view(cm.SCATTERPLOT, dataset=dataset, mapping="UMAP", x=0, y=0, w=9, h=12)
config.add_view(cm.CELL_SETS, dataset=dataset, x=9, y=0, w=3, h=12)
return config


class RNASeqViewConfBuilder(AbstractScatterplotViewConfBuilder):
Expand Down
Loading