capsul.datasel module¶
- Data selection, parsing, filtering from
Ex: find all raw T1 MRIs in a Bids sourcedata or rawdata directory:
from capsul.datasel.data_selector import get_data_selector
data_dir = '/home/hericart/raw_data'
sel = get_data_selector(data_dir, fom='raw-bids-1.0',
upstream_selector={'type': 'exhaustive'})
data = sel.get_data(
data_types=['morphologist.capsul.import_t1_mri.ImportT1Mri.input'])
Here sel is a chain of 2 selectors: a DataParser which takes inputs from a ExhaustiveDataSelector. It is needed to parse every file in the data directory, otherwise only files matching the first possible pattern for the ImportT1Mri.input parameter will be considered.
We could also filter data, using the same selector:
data = sel.get_data(
data_types=['morphologist.capsul.import_t1_mri.ImportT1Mri.input'],
data_filters={'.*': {'session': '2'}})
The returned data is a list. Each element of this list is a tuple
(data_type, items), where data_type is one of the required data_types in the query, and items is a list of dicts. Each dict has a path key, and normally (there are exceptions) an attributes dict containing FOM attributes.
Ex: query some Morphologist outputs in a database:
data_dir = '/home/charles-axel/morphologist_data'
sel = get_data_selector(data_dir, fom='morphologist-auto-1.0')
data = sel.get_data(
data_types=[
'morphologist.capsul.morphologist.Morphologist.split_brain',
'morphologist.capsul.morphologist.Morphologist.left_labelled_graph',
'morphologist.capsul.morphologist.Morphologist.right_labelled_graph'
],
type_labels=['split', 'left graph', 'right graph'],
data_filters={'.*_graph': {'sulci_recognition_session': 'deep_cnn'}}
)
If the database is large, we can build and use a SQLite database for it:
data_dir = '/home/charles-axel/morphologist_data'
sel = get_data_selector(data_dir, fom='morphologist-auto-1.0', type='sqlite')
sel.build_sqlite_index(
data_type='morphologist.capsul.morphologist.Morphologist.split_brain')
# here split_brain does not matter actually, only the process is used.
# then query:
sel = get_data_selector(data_dir, type='sqlite')
data = sel.get_data(
data_types=[
'morphologist.capsul.morphologist.Morphologist.split_brain',
'morphologist.capsul.morphologist.Morphologist.left_labelled_graph',
'morphologist.capsul.morphologist.Morphologist.right_labelled_graph'
],
type_labels=['split', 'left graph', 'right graph'],
data_filters={'.*_graph': {'sulci_recognition_session': 'deep_cnn'}}
)
We can also save the data dict and reuse it in a filter selector:
import json
# save using a dict structure
json_filename = '/tmp/select.json'
with open(json_filename, 'w') as f:
json.dump(dict(data), f)
# then use a filter which reads from the JSON file
# (we could directly use the dict instead of the JSON file, also):
sel = get_data_selector(
data_dir,
type='filter',
upstream_selector={'dict_list': json_filename})
data = sel.get_data(
data_types=['morphologist.capsul.morphologist.Morphologist.left_labelled_graph'],
data_filters={'.*': {'sulci_recognition_session': 'deepcnn'}})
capsul.datasel.data_selector submodule¶
- class capsul.datasel.data_selector.DataSelector(database, engine=None, fom=None, data_types=None, data_filters=None, type_labels=None, status_for_type=None, upstream_selector=None, **kwargs)[source]¶
Data selector, base class. The data selection mechanism allows to parse, store, query and filter data. The base class is abstract and should not be instantiated.
Use
get_data_selector()instead to build the appropriate data selector.The use the
get_data()method to parse/query data.Ex:
parser = get_data_selector( '/home/albert/dataset', database_sqlite='/home/albert/dataset/capsul-2.6.sqlite') data = parser.get_data( data_types=[ 'morphologist.capsul.morphologist.t1mri', 'morphologist.capsul.morphologist.Report_report_json'], data_filters={'.*': {'center': 'subjects'}, 'B': {'file_status': 'ok'}}, type_labels=['A', 'B'])
- classes_registry = {'dict_list': <class 'capsul.datasel.data_selector.DictListSelector'>, 'exhaustive': <class 'capsul.datasel.data_selector.ExhaustiveDataSelector'>, 'files_list': <class 'capsul.datasel.data_selector.FilesListSelector'>, 'metadata': <class 'capsul.datasel.data_selector.MetadataAssociator'>}¶
registry of all DataSelector types, used in
get_data_selector()
- find_items(dfilt, sfilt)[source]¶
This method needs to be overloaded in concrete classes.
Its output is a generator, where each element is a dictionary, with 2 entries:
- path: str
full file path
- attributes: dict
the attributes dictionaty for the file. It generally corresponds to the
FOMattributes. In some special DataSelector subclasses likeExhaustiveDataSelectororFilesListSelectorwhich are meant to be only data sources, the attributes dictionary may be missing.
- get_data(**kwargs)[source]¶
The main method of the DataSelector class. Performs parsing / querying / filtering and returns data found.
The keyword arguments allow to specify selection parameters. The base ones are listed here, but specialized selectors may support more parameters. Some of them may be given upon construction (see
get_data_selector()), and might be overriden here.- database: str
database directory to query
- fom: str
the
FOMmodel to apply (ex:morphologist-bids-2.0,morphologist-auto-1.0,raw-bids-1.0…)- data_types: (mandatory), list of str
each element is a process + parameter name in capsul which determines a data type to look for. Ex:
['morphologist.capsul.morphologist.t1mri', 'morphologist.capsul.morphologist.Report_report_json']
- data_filters: list, dict, or str
if specified as str, will be evaluated as a python expression to form a list or a dict.
- type_labels: list
assigns a label to each data type, to provide shorter names in data_filters.
- static get_qc_status(proc, path)[source]¶
Get the QC status from a Morphologist JSON QC file.
To be used in
status_for_typemapping parameter.
- status_str = {-1: 'undefined', 0: 'ok', 1: 'present', 2: 'warning', 3: 'suspicious', 4: 'bad', 5: 'absent', 8: 'valid_ok', 9: 'valid_present', 10: 'valid_warning', 11: 'valid_suspicious', 12: 'valid_bad', 13: 'valid_absent', 16: 'invalid_ok', 17: 'invalid_present', 18: 'invalid_warning', 19: 'invalid_suspicious', 20: 'invalid_bad', 21: 'invalid_absent'}¶
string identifiers for status enums
- class capsul.datasel.data_selector.DictListSelector(database, dict_list, **kwargs)[source]¶
Iterates over a given list of files. Each element is a dict with the same shape as the
data_parser.DataParseroutput.The
dict_listmay be a list of dicts, or a JSON filename.You normally use it either directly, or as as data source (
upstream_selector) for aDataFilter
- class capsul.datasel.data_selector.ExhaustiveDataSelector(database, engine=None, fom=None, data_types=None, data_filters=None, type_labels=None, status_for_type=None, upstream_selector=None, **kwargs)[source]¶
This DataSelector walks the entire database directory tree and returns every file. Each element is a dict with one item: “path”. No attributes.
You normally use it as as data source (
upstream_selector) for aDataParser
- class capsul.datasel.data_selector.FilesListSelector(database, files_list, **kwargs)[source]¶
Iterates over a given files list. Each element is a dict with one item: “path”. No attributes.
You normally use it as as data source (
upstream_selector) for aDataParser
- class capsul.datasel.data_selector.MetadataAssociator(*args, **kwargs)[source]¶
Associate metadata to each element of a DataSelector.
An upstream_selector needs to be used, and a list (or any iterable) of dicts (metadata) needs to be passed to it. It will add the metadata attributes to each item, so the source selector and the number of elements of the metadata list must match.
Contrarily to
DataFilter, metadata attributes here are not used for selection and filtering, but for assignment to each item.Ex:
sel = data_selector.get_data_selector( data_dir, type='metadata', upstream_selector={'files_list': [ '/home/charles-axel/data/mri1.nii.gz', '/home/charles-axel/data/mri2.nii.gz']}, metadata=[{'subject': 'guillaumot', 'acquisition': '1777'}, {"subject": 'hericart', 'acquisition': '1809'}]) data = sel.get_data( data_types=[ 'morphologist.capsul.morphologist.Morphologist.t1mri'], fom='morphologist-auto-1.0') data >>> [('morphologist.capsul.morphologist.Morphologist.t1mri', [{'path': '/home/charles-axel/data/mri1.nii.gz', 'subject': 'guillaumot', 'acquisition': '1777'}, {'path': '/home/charles-axel/data/mri2.nii.gz', 'subject': 'hericart', 'acquisition': '1809'}])]
- capsul.datasel.data_selector.get_data_selector(database, engine=None, fom=None, data_types=None, data_filters=None, database_sqlite=None, type_labels=None, status_for_type=None, index_status=None, type=None, **kwargs)[source]¶
Factory function to build a data selector corresponding to the given criterions. Mainly, if
database_sqliteprovides a SQLite filename, then a SQLite data selector will be returned.The
typeparameter allows to select the intended data selector. If omitted, the type will be guessed from other parameters, if possible.Currently supported types are:
- sqlite:
SQLiteDataSelector- files_list:
- dict_list:
- exhaustive:
- parser:
DataParser- filter:
DataFilter- metadata:
The
upstream_selectorparameter can be used to build a chain of selectors: the value of this parameter is a dictionary defining the upstream selector in the chain, and it may also conain an upstream_selector, and so on.The status_for_type is a dictionary of {type: function}. Such functions will be called to evaluate the status of a file of the given type. For instance, you may use the
DataSelector.get_qc_status()function to read Morphologist QC status files and get a status code from them.Ex:
sel = data_selector.get_data_selector( '/home/charles-axel/raw_data', fom='raw-bids-1.0', upstream_selector={'type': 'exhaustive'}) data = sel.get_data( data_types=['morphologist.capsul.import_t1_mri.ImportT1Mri.input'], data_filters={'.*': {'subject': '^hericart$'}})