1- import os
2- from pathlib import Path
3- from collections import OrderedDict
4- import json
5- import click
6-
7- from collections import OrderedDict
81import json
92import os
103import shutil
4+ from collections import OrderedDict
115from pathlib import Path
6+
7+ import click
8+ import pandas as pd
9+ from fhir .resources .questionnaire import Questionnaire
10+
1211from . import __version__ , get_logger , set_logger_level
1312from .migrate import migrate2newschema
1413from .redcap2reproschema import redcap2reproschema as redcap2rs
14+ from .reproschema2fhir import QuestionnaireGenerator
1515from .reproschema2redcap import reproschema2redcap as rs2redcap
1616from .reproschemaui2redcap import parse_survey
17- from .reproschema2fhir import QuestionnaireGenerator
18- from fhir .resources .questionnaire import Questionnaire
19- import pandas as pd
20-
2117
2218lgr = get_logger ()
2319
@@ -198,26 +194,27 @@ def reproschema2redcap(input_path, output_csv_path):
198194def reproschema_ui_to_redcap (survey_file , redcap_csv ):
199195 """
200196 Generates redcap csv given the audio and survey data from reproschema ui
201-
197+
202198 survey_file is the location of the surveys generated from reproschem ui
203199 redcap_csv is the path to store the newly generated redcap csv
204-
200+
205201 """
206202 merged_questionnaire_data = []
207203 # load each file recursively within the folder into its own key
208204 content = OrderedDict ()
209205 for file in Path (survey_file ).rglob ("*" ):
210206 if file .is_file ():
211207 filename = str (file .relative_to (survey_file ))
212- with open (f"{ survey_file } /{ filename } " , 'r' ) as f :
208+ with open (f"{ survey_file } /{ filename } " , "r" ) as f :
213209 content [filename ] = json .load (f )
214210
215- for questionnaire in content .keys (): # activity files
211+ for questionnaire in content .keys (): # activity files
216212 try :
217213 record_id = (survey_file .split ("/" )[- 1 ]).split ()[0 ]
218214 survey_data = content [questionnaire ]
219215 merged_questionnaire_data += parse_survey (
220- survey_data , record_id , questionnaire )
216+ survey_data , record_id , questionnaire
217+ )
221218 except Exception :
222219 continue
223220
@@ -230,13 +227,14 @@ def reproschema_ui_to_redcap(survey_file, redcap_csv):
230227 f"Converted reproschema-ui output from { survey_file } to Redcap CSV at { redcap_csv } "
231228 )
232229
230+
233231@main .command ()
234232@click .argument ("reproschema_questionnaire" , type = str )
235233@click .argument ("output" , type = str )
236234def reproschema_to_fhir (reproschema_questionnaire , output ):
237235 """
238236 Generates redcap csv given the audio and survey data from reproschema ui
239-
237+
240238 reproschema_questionnaire is the location of all reproschema activities
241239 output is the path to store the newly generated fhir json
242240 """
@@ -246,7 +244,9 @@ def reproschema_to_fhir(reproschema_questionnaire, output):
246244 raise FileNotFoundError (
247245 f"{ reproschema_folders } does not exist. Please check if folder exists and is located at the correct directory"
248246 )
249- reproschema_folders = [Path (f ) for f in reproschema_folders .iterdir () if f .is_dir ()]
247+ reproschema_folders = [
248+ Path (f ) for f in reproschema_folders .iterdir () if f .is_dir ()
249+ ]
250250 for reproschema_folder in reproschema_folders :
251251 # load each file recursively within the folder into its own key in the reproschema_content dict
252252 reproschema_content = OrderedDict ()
@@ -259,26 +259,36 @@ def reproschema_to_fhir(reproschema_questionnaire, output):
259259 reproschema_content [filename ] = json .loads (f .read ())
260260
261261 schema_name = [
262- name for name in (reproschema_content .keys ())
262+ name
263+ for name in (reproschema_content .keys ())
263264 if name .endswith ("_schema" )
264265 ][0 ]
265266 reproschema_schema = reproschema_content [schema_name ]
266267
267- if ("schema:version" in reproschema_schema and
268- reproschema_schema ["schema:version" ] not in ("0.0.1" , "1.0.0-rc1" , "1.0.0" )
269- ) or "schemaVersion" in reproschema_schema and reproschema_schema [
270- "schemaVersion" ] not in ("0.0.1" , "1.0.0-rc1" , "1.0.0-rc4" , "1.0.0" ):
268+ if (
269+ (
270+ "schema:version" in reproschema_schema
271+ and reproschema_schema ["schema:version" ]
272+ not in ("0.0.1" , "1.0.0-rc1" , "1.0.0" )
273+ )
274+ or "schemaVersion" in reproschema_schema
275+ and reproschema_schema ["schemaVersion" ]
276+ not in ("0.0.1" , "1.0.0-rc1" , "1.0.0-rc4" , "1.0.0" )
277+ ):
271278 raise ValueError (
272- ' Unable to work with reproschema versions other than 0.0.1, 1.0.0-rc1, and 1.0.0-rc4'
279+ " Unable to work with reproschema versions other than 0.0.1, 1.0.0-rc1, and 1.0.0-rc4"
273280 )
274281
275282 questionnaire_generator = QuestionnaireGenerator ()
276283 fhir_questionnaire = questionnaire_generator .convert_to_fhir (
277- reproschema_content )
284+ reproschema_content
285+ )
278286
279287 # validate the json using fhir resources
280288 try :
281- Questionnaire .model_validate (fhir_questionnaire )
289+ questionnaire_json = Questionnaire .model_validate (
290+ fhir_questionnaire
291+ )
282292 except Exception :
283293 raise Exception ("Fhir Questionnaire is not valid" )
284294
@@ -290,14 +300,10 @@ def reproschema_to_fhir(reproschema_questionnaire, output):
290300 if dirpath .exists () and dirpath .is_dir ():
291301 shutil .rmtree (dirpath )
292302
293- paths = [
294- output_path / file_name
295- ]
303+ paths = [output_path / file_name ]
296304
297305 for folder in paths :
298306 folder .mkdir (parents = True , exist_ok = True )
299307
300308 with open (output_path / f"{ file_name } /{ file_name } .json" , "w+" ) as f :
301309 f .write (json .dumps (fhir_questionnaire ))
302-
303-
0 commit comments