-
Notifications
You must be signed in to change notification settings - Fork 2
/
diglab2ando.py
295 lines (251 loc) · 7.7 KB
/
diglab2ando.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import re
import pathlib
import json
from ando.tools.generator.AnDOGenerator import AnDOData
from redcap_bridge.server_interface import download_records
from ando.checker import is_valid
config_file = pathlib.Path(__file__).parent / 'config.json'
project_name = 'SimpleProject'
with open(config_file) as f:
conf = json.load(f)
PROJECT_DEF = pathlib.Path(conf[project_name]['project_definition'])
OUTPUT_FOLDER = pathlib.Path(conf[project_name]['output_directory'])
if not OUTPUT_FOLDER.exists():
OUTPUT_FOLDER.mkdir()
def get_metadata(conf, format):
"""
Fetch all recorded metadata from the server
Parameters
----------
conf:
format:
Returns
----------
"""
records_file = OUTPUT_FOLDER / f'records.{format}'
download_records(records_file, conf / 'project.json', format=format)
with open(records_file) as f:
records = json.load(f)
return records
def convert_to_bids(records, OUTPUT_FOLDER):
"""
Parameters
----------
records:
OUTPUT_FOLDER:
Returns
----------
"""
for record_dict in records:
sub_id, ses_id = get_sub_ses_ids(record_dict)
gen = AnDOData(sub_id, ses_id, modality='ephys')
gen.register_data_files(get_data_file())
gen.basedir = OUTPUT_FOLDER
gen.generate_structure()
gen.generate_data_files(mode='move')
generate_metadata_files(record_dict, gen.get_data_folder())
def get_sub_ses_ids(record_dict):
"""
Parameters
----------
record_dict:
Returns
----------
"""
sub_id = record_dict['guid']
ses_id = f"{record_dict['date']}"
for sep, key in zip(['id', 'c'], ['ses_number', 'ses_custom_field']):
if key in record_dict:
ses_id += f"{sep}{record_dict[key]}"
# clean strings
sub_id = re.sub(r'[\W_]+', '', sub_id)
ses_id = re.sub(r'[\W_]+', '', ses_id)
if ses_id.isalnum() and sub_id.isalnum():
return sub_id, ses_id
else:
raise Exception("Record dict must only contain alphanumeric characters")
def get_data_file():
"""
Parameters
----------
Returns
----------
TODO: this needs to be replaced by a project-specific functions that converts the data to nix and provides the path to the nix file
"""
dummy_nix_file = OUTPUT_FOLDER / 'dummy_file.nix'
if not dummy_nix_file.exists():
dummy_nix_file.touch()
return dummy_nix_file
def generate_metadata_files(record_dict, save_dir):
"""
Parameters
----------
record_dict:
save_dir:
Returns
----------
TODO: this needs to generate the basic BIDS metadata files and
"""
# these can then be rearranged into the right location by the ando generator
filename = f'sub-{record_dict["guid"]}_ses-{record_dict["date"]}_ephys.json'
with open(save_dir / filename, 'w') as f:
json.dump(record_dict, f)
metadata_filenames = ["dataset_description.json","tasks.json","participants.json"
"tasks.csv", "participants.csv"]
for filename in metadata_filenames:
with open(save_dir / filename, 'w') as f:
pass
if __name__ == '__main__':
# json way of the world
rec = get_metadata(PROJECT_DEF, 'json')
if not rec:
raise ValueError(f'No records found for project {project_name}.')
convert_to_bids(rec, OUTPUT_FOLDER)
ando.is_valid(OUTPUT_FOLDER)
# def convert(project_name, output_directory):
# # open config , find project specifications
# # use Diglabtools -> download_records -> 'records.csv' / 'records.json'
# # make sure essential info is present (exp_name, date, session_id....)
# # run andogenerator using essential infos
# # (use andochecker)
#
# """
# :param pdf_form:
# :param root_directory:
# :return:
# """
#
# ###
# # 1. read diglab form with Diglab Extractor and generate odml metadata
# ###
#
# metadata_collection = Extractor.load_form(pdf_form)
#
# # assert essential AnDO information is present
#
# def flatten_collection(collection):
# '''
# Merging individual dicts of list into single dictionary
# Args:
# collection: list of dictionaries. Only the first entry needs to be a list and is ignored.
#
# Returns:
# result: flattened dictionary
# '''
# if not isinstance(collection, list):
# raise ValueError('Unexpected type of metadata collection. Expection an object of type `list`')
#
# result = {}
#
# if collection:
# if not isinstance(collection[0], list):
# raise ValueError('Unexpected type of metadata collection. First entry is not of type `list`')
# if not all([isinstance(i, dict) for i in collection[1:]]):
# raise ValueError('Unexpected type of metadata collection. All but first entry should be of type `dict`')
#
# for item in collection[1:]:
# result.update(item)
#
# return result
#
# metadata_dict = flatten_collection(metadata_collection)
#
# if 'dateSession' in metadata_dict and 'date' not in metadata_dict:
# metadata_dict['date'] = metadata_dict['dateSession']
#
# required_keys = ['expName', 'guid', 'date', 'sesNumber', 'customSesField']
# params = {}
# for req_key in required_keys:
# if req_key in metadata_dict:
# params[req_key] = metadata_dict[req_key]
# else:
# raise ValueError(f'DigLab form does not contain required key `{req_key}`.')
#
# # TODO: Convert date to datetime object
# ###
# # 2. create AnDO directory
# ###
#
#
# # cheating for testing purposes only
# # TODO: Remove this cheat
# params['expName'] = 'MyCoolExperiment'
# params['guid'] = '012345'
#
#
# from ando.tools.generator.AnDOGenerator import AnDOSession
#
# ses = AnDOSession(**params)
# ses.generate_folders(root_directory)
#
#
# ###
# # 3. store the metadata extracted from the DigLab in the right place in the AnDO structure
# ###
#
# # save a tsv file with odml_tables, that is called ses-YYYYMMDD_XXX_BBBB/metadata/diglab-YYYYMMDD_XXX_BBBB.tsv
#
#
# ###
# # 4. store the data files (if provided) in the AnDO structure
# ###
#
# # copy each of the datafile1, datafileN into ses-YYYYMMDD_XXX_BBBB/rawdata
#
#
# def parse_cli():
# """Load command line arguments"""
# parser = ArgumentParser(description='Convert PDF DigLab Form to AnDO folder structure.')
# parser.add_argument('file', metavar='pdf_form',
# help='PDF form to read')
# parser.add_argument('root_data_dir', help='Root directory to create AnDO structure',
# default=None, metavar='root_dir')
#
# return parser.parse_args()
#
# def main():
# args = parse_cli()
# convert(args.file, args.out)
#
# if __name__ == '__main__':
# main()
#
#
#
#
# def cli():
# """
# Cli
# """
#
# parser = argparse.ArgumentParser(description='')
# parser.add_argument('inputFile',
# help='Path to the input file csv one.')
# parser.add_argument('--outputDirectory',
# help='Path to the output that contains the resumes.')
# return parser
#
#
#
#
# def generate():
# """
# run andogenerator using essential infos
# Parameters
# ----------
#
# Returns
# ----------
# """
#
# if __name__ == "__main__":
#
# arg_parser = cli()
# parsed_args = arg_parser.parse_args(sys.argv[1:])
#
# if os.path.exists(parsed_args.inputDirectory):
# csv_file = download(parsed_args.inputFile)
# sub_id, ses_id = get_info(csv_file)
# generate(sub_id,ses_id,parsed_args.outputDirectory)
# ando.is_valid(parsed_args.outputDirectory)
#