|
6 | 6 | import uuid |
7 | 7 | import hashlib |
8 | 8 | import importlib |
9 | | -from collections.abc import Mapping |
10 | 9 |
|
11 | 10 | from .readers import neuropixels, kilosort |
12 | 11 | from . import probe |
13 | 12 |
|
14 | 13 | schema = dj.schema() |
15 | 14 |
|
16 | | -required_upstream_tables = ("Session", "SkullReference") |
17 | | -required_functions = ("get_neuropixels_data_directory", "get_paramset_idx", "get_kilosort_output_directory") |
| 15 | +_required_module = None |
18 | 16 |
|
19 | | -_table_classes = (dj.Manual, dj.Lookup, dj.Imported, dj.Computed) |
20 | | -_required_objects = {} |
21 | 17 |
|
| 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 | + """ |
22 | 46 |
|
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" |
25 | 50 |
|
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 |
48 | 53 |
|
49 | 54 | # 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) |
54 | 56 | 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__) |
56 | 58 |
|
57 | 59 |
|
58 | 60 | # -------------- Functions required by the elements-ephys --------------- |
59 | 61 |
|
60 | 62 |
|
61 | 63 | def get_neuropixels_data_directory(probe_insertion_key: dict) -> str: |
62 | 64 | """ |
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 |
66 | 69 | """ |
67 | | - return _required_objects['get_neuropixels_data_directory'](probe_insertion_key) |
| 70 | + return _required_module.get_neuropixels_data_directory(probe_insertion_key) |
68 | 71 |
|
69 | 72 |
|
70 | 73 | def get_kilosort_output_directory(clustering_task_key: dict) -> str: |
71 | 74 | """ |
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 |
75 | 79 | """ |
76 | | - return _required_objects['get_kilosort_output_directory'](clustering_task_key) |
| 80 | + return _required_module.get_kilosort_output_directory(clustering_task_key) |
77 | 81 |
|
78 | 82 |
|
79 | 83 | def get_paramset_idx(ephys_rec_key: dict) -> int: |
80 | 84 | """ |
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` |
84 | 89 | """ |
85 | | - return _required_objects['get_paramset_idx'](ephys_rec_key) |
| 90 | + return _required_module.get_paramset_idx(ephys_rec_key) |
86 | 91 |
|
87 | 92 |
|
88 | 93 | # ----------------------------- Table declarations ---------------------- |
|
0 commit comments