Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Commit

Permalink
Added children of observations call to constraint namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
jochemb committed May 23, 2018
1 parent b155db5 commit 302f766
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
16 changes: 15 additions & 1 deletion transmart/api/v2/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ def wrapper(*args, **kwargs):
return wrapper


def add_to_queryable(func):
"""
This decorator allows registration a method to an ConstraintsObjects,
so it can be accessed more easily.
"""
func.__query_method__ = True
return func


class Query:
""" Utility to build queries for transmart v2 api. """

Expand Down Expand Up @@ -105,6 +114,7 @@ def query(self, q):
return r.json()

@default_constraint
@add_to_queryable
def observations(self, constraint=None, as_dataframe=False, **kwargs):
"""
Get observations, from the main table in the transmart data model.
Expand Down Expand Up @@ -137,9 +147,10 @@ def func(constraint=None, *args, **kwargs):
return self.query(q)

func.__doc__ = doc
self.observations.__dict__[handle] = default_constraint(func)
self.observations.__dict__[handle] = add_to_queryable(default_constraint(func))

@default_constraint
@add_to_queryable
def patients(self, constraint=None, **kwargs):
"""
Get patients.
Expand All @@ -160,6 +171,7 @@ def patient_sets(self, patient_set_id=None):
return PatientSets(self.query(q))

@default_constraint
@add_to_queryable
def create_patient_set(self, name: str, constraint=None, **kwargs):
"""
Create a patient set that can be reused at a later stage.
Expand Down Expand Up @@ -224,6 +236,7 @@ def tree_nodes(self, root=None, depth=0, counts=False, tags=True, hal=False):
return tree_nodes

@default_constraint
@add_to_queryable
def get_hd_node_data(self, constraint=None, biomarker_constraint=None, biomarkers: list=None,
biomarker_type='genes', projection='all_data', **kwargs):
"""
Expand All @@ -250,6 +263,7 @@ def get_hd_node_data(self, constraint=None, biomarker_constraint=None, biomarker
return ObservationSetHD(self.query(q))

@default_constraint
@add_to_queryable
def dimension_elements(self, dimension, constraint=None, **kwargs):
q = Query(handle='/v2/dimensions/{}/elements'.format(dimension),
method='GET',
Expand Down
32 changes: 32 additions & 0 deletions transmart/api/v2/constraints/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,23 @@ def json(self):
}


def _find_query_methods(obj):
for method_name in dir(obj):
if method_name.startswith('__'):
continue

method = getattr(obj, method_name)
if getattr(method, '__query_method__', False):
yield method_name, method


def override_defaults(method, **defaults):
@wraps(method)
def wrapper(*args, **kwargs):
return method(*args, **defaults, **kwargs)
return wrapper


class ObservationConstraint(Queryable, Grouper):
"""
Represents constraints on observation level. This is the set of
Expand Down Expand Up @@ -224,6 +241,9 @@ def __init__(self,
if api is not None:
self._details_widget = ConstraintWidget(self)

for name, method in _find_query_methods(api):
self.__dict__[name] = self._constraint_method_factory(method)

self.concept = concept
self.trial_visit = trial_visit
self.value_list = value_list
Expand Down Expand Up @@ -303,6 +323,18 @@ def subselect(self, dimension='patient'):
finally:
self.subselection = current

def _constraint_method_factory(self, method):
@wraps(method)
def wrapper(*args, **kwargs):
func = override_defaults(method, api=self.api, constraint=self)
return func(*args, **kwargs)

# Also set defaults of the observation call children
for name, child in _find_query_methods(method):
wrapper.__dict__[name] = override_defaults(child, api=self.api, constraint=self)

return wrapper

@property
def trial_visit(self):
"""
Expand Down

0 comments on commit 302f766

Please sign in to comment.