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

Add repr() #66

Closed
wants to merge 10 commits into from
33 changes: 28 additions & 5 deletions vitessce/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ class AbstractWrapper:
"""
An abstract class that can be extended when
implementing custom dataset object wrapper classes.

TODO: Add some useful tests.

>>> assert True

"""

def __init__(self, **kwargs):
Expand All @@ -57,6 +52,9 @@ def __init__(self, **kwargs):
self.is_remote = False
self.file_def_creators = []

def __repr__(self):
return self._repr

def convert_and_save(self, dataset_uid, obj_i):
"""
Fill in the file_def_creators array.
Expand Down Expand Up @@ -132,6 +130,27 @@ def auto_view_config(self, vc):
"""
raise NotImplementedError("Auto view configuration has not yet been implemented for this data object wrapper class.")


def _make_repr(init_locals):
'''
>>> orig = MultiImageWrapper('IMAGE_WRAPPERS', foo='bar')
>>> orig_repr = repr(orig)
>>> print(orig_repr)
MultiImageWrapper(image_wrappers='IMAGE_WRAPPERS', use_physical_size_scaling=False, foo='bar')
>>> evalled = eval(orig_repr)
>>> assert orig_repr == repr(evalled)
'''
del init_locals['self']
clazz = init_locals.pop('__class__') # Requires superclass to be initialized.
kwargs = init_locals.pop('kwargs')
args = {
**init_locals,
**kwargs
}
params = ', '.join([f'{k}={repr(v)}' for k, v in args.items()])
return f'{clazz.__name__}({params})'


class MultiImageWrapper(AbstractWrapper):
"""
Wrap multiple imaging datasets by creating an instance of the ``MultiImageWrapper`` class.
Expand All @@ -141,6 +160,7 @@ class MultiImageWrapper(AbstractWrapper):
"""
def __init__(self, image_wrappers, use_physical_size_scaling=False, **kwargs):
super().__init__(**kwargs)
self._repr = _make_repr(locals())
self.image_wrappers = image_wrappers
self.use_physical_size_scaling = use_physical_size_scaling

Expand Down Expand Up @@ -198,6 +218,7 @@ class OmeTiffWrapper(AbstractWrapper):
def __init__(self, img_path=None, offsets_path=None, img_url=None, offsets_url=None, name="", transformation_matrix=None, is_bitmask=False,
**kwargs):
super().__init__(**kwargs)
self._repr = _make_repr(locals())
self.name = name
self._img_path = img_path
self._img_url = img_url
Expand Down Expand Up @@ -395,6 +416,7 @@ def __init__(self, adata=None, adata_url=None, expression_matrix=None, matrix_ge
:param \\*\\*kwargs: Keyword arguments inherited from :class:`~vitessce.wrappers.AbstractWrapper`
"""
super().__init__(**kwargs)
self._repr = _make_repr(locals())
self._adata = adata
self._adata_url = adata_url
if adata is not None:
Expand Down Expand Up @@ -561,6 +583,7 @@ class SnapWrapper(AbstractWrapper):

def __init__(self, in_mtx, in_barcodes_df, in_bins_df, in_clusters_df, starting_resolution=5000, **kwargs):
super().__init__(**kwargs)
self._repr = _make_repr(locals())
self.in_mtx = in_mtx # scipy.sparse.coo.coo_matrix (filtered_cell_by_bin.mtx)
self.in_barcodes_df = in_barcodes_df # pandas dataframe (barcodes.txt)
self.in_bins_df = in_bins_df # pandas dataframe (bins.txt)
Expand Down