Skip to content

Commit 25a0694

Browse files
authored
NickAkhmetov/Add allow_multiple_scopes_per_type boolean arg to use_coordination and link_views (#274)
* Add `allow_multiple_scopes_per_type` boolean arg to `use_coordination` and `link_views` * Remove lint-failing whitespace * Safely fetch existing value using `.get` * Use concatenation instead of appending
1 parent d3677fd commit 25a0694

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

vitessce/config.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,13 @@ def get_coordination_scope(self, c_type):
370370
"""
371371
return self.view["coordinationScopes"].get(c_type)
372372

373-
def use_coordination(self, *c_scopes):
373+
def use_coordination(self, *c_scopes, allow_multiple_scopes_per_type=False):
374374
"""
375375
Attach a coordination scope to this view instance. All views using the same coordination scope for a particular coordination type will effectively be linked together.
376376
377377
:param \\*c_scopes: A variable number of coordination scope instances can be passed.
378378
:type \\*c_scopes: VitessceConfigCoordinationScope
379+
:param bool allow_multiple_scopes_per_type: Whether to allow multiple coordination scopes per coordination type. If true, multiple values for the same coordination type are treated as a list. If false, latest value for same coordination type is used. Defaults to False.
379380
380381
:returns: Self, to allow chaining.
381382
:rtype: VitessceConfigView
@@ -402,7 +403,15 @@ def use_coordination(self, *c_scopes):
402403
"""
403404
for c_scope in c_scopes:
404405
assert isinstance(c_scope, VitessceConfigCoordinationScope)
405-
self.view["coordinationScopes"][c_scope.c_type] = c_scope.c_scope
406+
existing_value = self.view["coordinationScopes"].get(c_scope.c_type)
407+
new_value = c_scope.c_scope
408+
if (existing_value is not None and allow_multiple_scopes_per_type):
409+
if (isinstance(existing_value, list)):
410+
self.view["coordinationScopes"][c_scope.c_type] = existing_value + [new_value]
411+
else:
412+
self.view["coordinationScopes"][c_scope.c_type] = [existing_value, new_value]
413+
else:
414+
self.view["coordinationScopes"][c_scope.c_type] = new_value
406415
return self
407416

408417
def set_xywh(self, x, y, w, h):
@@ -813,7 +822,7 @@ def set_coordination_value(self, c_type, c_scope, c_value):
813822
self.config["coordinationSpace"][scope.c_type][scope.c_scope] = scope
814823
return scope
815824

816-
def link_views(self, views, c_types, c_values=None):
825+
def link_views(self, views, c_types, c_values=None, allow_multiple_scopes_per_type=False):
817826
"""
818827
A convenience function for setting up new coordination scopes across a set of views.
819828
@@ -822,14 +831,15 @@ def link_views(self, views, c_types, c_values=None):
822831
:param c_types: The coordination types on which to coordinate the views.
823832
:type c_types: list of str or list of vitessce.constants.CoordinationType
824833
:param list c_values: Initial values corresponding to each coordination type. Should have the same length as the c_types array. Optional.
834+
:param bool allow_multiple_scopes_per_type: Whether to allow multiple coordination scopes per coordination type. If true, multiple values for the same coordination type are treated as a list. If false, latest value for same coordination type is used. Defaults to False.
825835
826836
:returns: Self, to allow chaining.
827837
:rtype: VitessceConfig
828838
"""
829839
c_scopes = self.add_coordination(*c_types)
830840
for view in views:
831841
for c_scope in c_scopes:
832-
view.use_coordination(c_scope)
842+
view.use_coordination(c_scope, allow_multiple_scopes_per_type=allow_multiple_scopes_per_type)
833843

834844
if c_values is not None and len(c_values) == len(c_types):
835845
for i, c_scope in enumerate(c_scopes):

0 commit comments

Comments
 (0)