@@ -109,24 +109,24 @@ def normalize_condition(condition_str, field_type=None):
109109 return None
110110
111111 try :
112-
112+
113113 # Clean HTML
114114 condition_str = BeautifulSoup (condition_str , "html.parser" ).get_text ()
115115 condition_str = condition_str .strip ()
116-
116+
117117 if not condition_str :
118118 return None
119119
120120 # Common operator normalizations for all types
121121 operator_replacements = [
122- (r"\s*\+\s*" , " + " ), # Normalize spacing around +
123- (r"\s*-\s*" , " - " ), # Normalize spacing around -
124- (r"\s*\*\s*" , " * " ), # Normalize spacing around *
125- (r"\s*\/\s*" , " / " ), # Normalize spacing around /
126- (r"\s*\(\s*" , "(" ), # Remove spaces after opening parenthesis
127- (r"\s*\)\s*" , ")" ), # Remove spaces before closing parenthesis
128- (r"\s*,\s*" , "," ), # Normalize spaces around commas
129- (r"\s+" , " " ), # Normalize multiple spaces
122+ (r"\s*\+\s*" , " + " ), # Normalize spacing around +
123+ (r"\s*-\s*" , " - " ), # Normalize spacing around -
124+ (r"\s*\*\s*" , " * " ), # Normalize spacing around *
125+ (r"\s*\/\s*" , " / " ), # Normalize spacing around /
126+ (r"\s*\(\s*" , "(" ), # Remove spaces after opening parenthesis
127+ (r"\s*\)\s*" , ")" ), # Remove spaces before closing parenthesis
128+ (r"\s*,\s*" , "," ), # Normalize spaces around commas
129+ (r"\s+" , " " ), # Normalize multiple spaces
130130 ]
131131
132132 # Apply operator normalizations first
@@ -145,7 +145,7 @@ def normalize_condition(condition_str, field_type=None):
145145 (r"\[([^\]]*)\]" , r"\1" ), # Remove brackets and extra spaces
146146 (r"\bor\b" , "||" ),
147147 (r"\band\b" , "&&" ),
148- (r'"' , "'" )
148+ (r'"' , "'" ),
149149 ]
150150 for pattern , repl in replacements :
151151 condition_str = re .sub (pattern , repl , condition_str )
@@ -816,7 +816,9 @@ def process_csv(csv_file, abs_folder_path, protocol_name):
816816
817817 try :
818818 df = pd .read_csv (csv_file , encoding = "utf-8-sig" )
819- df .columns = df .columns .map (lambda x : x .strip ().strip ('"' ).lstrip ("\ufeff " ))
819+ df .columns = df .columns .map (
820+ lambda x : x .strip ().strip ('"' ).lstrip ("\ufeff " )
821+ )
820822
821823 required_columns = ["Form Name" , "Variable / Field Name" , "Field Type" ]
822824 missing_columns = [
@@ -881,29 +883,42 @@ def process_csv(csv_file, abs_folder_path, protocol_name):
881883
882884 # Case 1: Field is calc type
883885 if field_type in COMPUTE_LIST :
884- calc_value = row_dict .get ("Choices, Calculations, OR Slider Labels" , "" )
886+ calc_value = row_dict .get (
887+ "Choices, Calculations, OR Slider Labels" , ""
888+ )
885889 if calc_value and str (calc_value ).strip ():
886- compute_expression = normalize_condition (calc_value , field_type = field_type )
890+ compute_expression = normalize_condition (
891+ calc_value , field_type = field_type
892+ )
887893 if compute_expression :
888894 is_compute = True
889- compute [form_name ].append ({
890- "variableName" : field_name ,
891- "jsExpression" : compute_expression
892- })
895+ compute [form_name ].append (
896+ {
897+ "variableName" : field_name ,
898+ "jsExpression" : compute_expression ,
899+ }
900+ )
893901 else :
894- print (f"Warning: Could not normalize calc expression for { field_name } : { calc_value } " )
902+ print (
903+ f"Warning: Could not normalize calc expression for { field_name } : { calc_value } "
904+ )
895905
896906 # Case 2: Field has @CALCTEXT
897- elif field_annotation and "@CALCTEXT" in str (field_annotation ).upper ():
907+ elif (
908+ field_annotation
909+ and "@CALCTEXT" in str (field_annotation ).upper ()
910+ ):
898911 match = re .search (r"@CALCTEXT\((.*)\)" , field_annotation )
899912 if match :
900913 compute_expression = normalize_condition (match .group (1 ))
901914 if compute_expression :
902915 is_compute = True
903- compute [form_name ].append ({
904- "variableName" : field_name ,
905- "jsExpression" : compute_expression
906- })
916+ compute [form_name ].append (
917+ {
918+ "variableName" : field_name ,
919+ "jsExpression" : compute_expression ,
920+ }
921+ )
907922
908923 # Add to order list only if not a compute field
909924 if not is_compute :
@@ -915,6 +930,7 @@ def process_csv(csv_file, abs_folder_path, protocol_name):
915930 print (f"Error processing CSV: { str (e )} " )
916931 raise
917932
933+
918934# todo adding output path
919935def redcap2reproschema (
920936 csv_file , yaml_file , output_path , schema_context_url = None
0 commit comments