Skip to content

Commit aa4064c

Browse files
author
Thinh Nguyen
committed
simplify "activate" no explicit requirements check
1 parent 822c5b7 commit aa4064c

File tree

1 file changed

+51
-46
lines changed

1 file changed

+51
-46
lines changed

elements_ephys/ephys.py

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,83 +6,88 @@
66
import uuid
77
import hashlib
88
import importlib
9-
from collections.abc import Mapping
109

1110
from .readers import neuropixels, kilosort
1211
from . import probe
1312

1413
schema = dj.schema()
1514

16-
required_upstream_tables = ("Session", "SkullReference")
17-
required_functions = ("get_neuropixels_data_directory", "get_paramset_idx", "get_kilosort_output_directory")
15+
_required_module = None
1816

19-
_table_classes = (dj.Manual, dj.Lookup, dj.Imported, dj.Computed)
20-
_required_objects = {}
2117

18+
def activate(ephys_schema_name, probe_schema_name=None, *, create_schema=True,
19+
create_tables=True, required_module=None):
20+
"""
21+
activate(ephys_schema_name, probe_schema_name=None, *, create_schema=True, create_tables=True, dependency=None)
22+
:param ephys_schema_name: schema name to activate the `ephys` element
23+
:param probe_schema_name: schema name to activate the `probe` element
24+
- may be omitted if the `probe` element is already activated
25+
:param create_schema: create the schema if not yet existed (default = True)
26+
:param create_tables: create the tables if not yet existed (default = True)
27+
:param required_module: a module name or a module containing the
28+
required dependencies to activate the `ephys` element:
29+
Upstream tables:
30+
+ Session: parent table to ProbeInsertion, typically identifying a recording session
31+
+ SkullReference:
32+
Functions:
33+
+ get_neuropixels_data_directory(probe_insertion_key: dict) -> str
34+
Retrieve the recorded Neuropixels data directory for a given ProbeInsertion
35+
:param probe_insertion_key: a dictionary of one ProbeInsertion `key`
36+
:return: a string for full path to the resulting Neuropixels data directory
37+
+ get_kilosort_output_directory(clustering_task_key: dict) -> str
38+
Retrieve the Kilosort output directory for a given ClusteringTask
39+
:param clustering_task_key: a dictionary of one ClusteringTask `key`
40+
:return: a string for full path to the resulting Kilosort output directory
41+
+ get_paramset_idx(ephys_rec_key: dict) -> int
42+
Retrieve attribute `paramset_idx` from the ClusteringParamSet record for the given EphysRecording.
43+
:param ephys_rec_key: a dictionary of one EphysRecording `key`
44+
:return: int specifying the `paramset_idx`
45+
"""
2246

23-
def activate(ephys_schema_name, probe_schema_name=None, create_schema=True, create_tables=True, ephys_requirement=None):
24-
global _required_objects
47+
if isinstance(required_module, str):
48+
required_module = importlib.import_module(required_module)
49+
assert inspect.ismodule(required_module), "The argument 'dependency' must be a module's name or a module"
2550

26-
if not isinstance(ephys_requirement, Mapping):
27-
if isinstance(ephys_requirement, str):
28-
ephys_requirement = importlib.import_module(ephys_requirement)
29-
30-
if inspect.ismodule(ephys_requirement):
31-
ephys_requirement = {key: getattr(ephys_requirement, key) for key in dir(ephys_requirement)}
32-
else:
33-
raise ValueError("Argument 'ephys_requirement' must be a dictionary, a module's name or a module")
34-
35-
for name in required_upstream_tables:
36-
assert name in ephys_requirement, "Upstream table %s is required in ephys.activate(ephys_requirement=...)" % name
37-
table = ephys_requirement[name]
38-
if inspect.isclass(table):
39-
table = table()
40-
assert isinstance(table, _table_classes), "Upstream table %s must be a DataJoint table " \
41-
"object in ephys.activate(ephys_requirement=...)" % name
42-
_required_objects[name] = ephys_requirement[name]
43-
44-
for name in required_functions:
45-
assert name in ephys_requirement, "Functions %s is required in ephys.activate(ephys_requirement=...)" % name
46-
assert inspect.isfunction(ephys_requirement[name]), "%s must be a function in ephys.activate(ephys_requirement=...)" % name
47-
_required_objects[name] = ephys_requirement[name]
51+
global _required_module
52+
_required_module = required_module
4853

4954
# activate
50-
if probe.schema.database is not None:
51-
probe.schema.activate(probe_schema_name or ephys_schema_name,
52-
create_schema=create_schema, create_tables=create_tables)
53-
55+
probe.schema.activate(probe_schema_name, create_schema=create_schema, create_tables=create_tables)
5456
schema.activate(ephys_schema_name, create_schema=create_schema,
55-
create_tables=create_tables, add_objects=_required_objects)
57+
create_tables=create_tables, add_objects=_required_module.__dict__)
5658

5759

5860
# -------------- Functions required by the elements-ephys ---------------
5961

6062

6163
def get_neuropixels_data_directory(probe_insertion_key: dict) -> str:
6264
"""
63-
Retrieve the recorded Neuropixels data directory for a given ProbeInsertion
64-
:param probe_insertion_key: a dictionary of one ProbeInsertion `key`
65-
:return: a string for full path to the resulting Neuropixels data directory
65+
get_neuropixels_data_directory(probe_insertion_key: dict) -> str
66+
Retrieve the recorded Neuropixels data directory for a given ProbeInsertion
67+
:param probe_insertion_key: a dictionary of one ProbeInsertion `key`
68+
:return: a string for full path to the resulting Neuropixels data directory
6669
"""
67-
return _required_objects['get_neuropixels_data_directory'](probe_insertion_key)
70+
return _required_module.get_neuropixels_data_directory(probe_insertion_key)
6871

6972

7073
def get_kilosort_output_directory(clustering_task_key: dict) -> str:
7174
"""
72-
Retrieve the Kilosort output directory for a given ClusteringTask
73-
:param clustering_task_key: a dictionary of one ClusteringTask `key`
74-
:return: a string for full path to the resulting Kilosort output directory
75+
get_kilosort_output_directory(clustering_task_key: dict) -> str
76+
Retrieve the Kilosort output directory for a given ClusteringTask
77+
:param clustering_task_key: a dictionary of one ClusteringTask `key`
78+
:return: a string for full path to the resulting Kilosort output directory
7579
"""
76-
return _required_objects['get_kilosort_output_directory'](clustering_task_key)
80+
return _required_module.get_kilosort_output_directory(clustering_task_key)
7781

7882

7983
def get_paramset_idx(ephys_rec_key: dict) -> int:
8084
"""
81-
Retrieve attribute `paramset_idx` from the ClusteringParamSet record for the given EphysRecording.
82-
:param ephys_rec_key: a dictionary of one EphysRecording `key`
83-
:return: int specifying the `paramset_idx`
85+
get_paramset_idx(ephys_rec_key: dict) -> int
86+
Retrieve attribute `paramset_idx` from the ClusteringParamSet record for the given EphysRecording.
87+
:param ephys_rec_key: a dictionary of one EphysRecording `key`
88+
:return: int specifying the `paramset_idx`
8489
"""
85-
return _required_objects['get_paramset_idx'](ephys_rec_key)
90+
return _required_module.get_paramset_idx(ephys_rec_key)
8691

8792

8893
# ----------------------------- Table declarations ----------------------

0 commit comments

Comments
 (0)