Skip to content

Commit df7a66f

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 2517c9c commit df7a66f

File tree

2 files changed

+61
-42
lines changed

2 files changed

+61
-42
lines changed

reproschema/redcap2reproschema.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import re
44
from pathlib import Path
55

6+
import pandas as pd
67
import yaml
78
from bs4 import BeautifulSoup
8-
import pandas as pd
99

1010
from .context_url import CONTEXTFILE_URL
1111
from .jsonldutils import get_context_version
@@ -137,10 +137,14 @@ def process_field_properties(data):
137137
condition = normalize_condition(condition)
138138
else:
139139
condition = True
140-
140+
141141
# Check Field Annotation for special flags
142142
annotation = data.get("Field Annotation", "").upper()
143-
if condition and ("@READONLY" in annotation or "@HIDDEN" in annotation or "@CALCTEXT" in annotation):
143+
if condition and (
144+
"@READONLY" in annotation
145+
or "@HIDDEN" in annotation
146+
or "@CALCTEXT" in annotation
147+
):
144148
condition = False
145149

146150
prop_obj = {
@@ -504,24 +508,30 @@ def parse_language_iso_codes(input_string):
504508
]
505509

506510

507-
def process_csv_with_pandas(csv_file, abs_folder_path, schema_context_url, protocol_name):
511+
def process_csv_with_pandas(
512+
csv_file, abs_folder_path, schema_context_url, protocol_name
513+
):
508514
datas = {}
509515
order = {}
510516
compute = {}
511517
languages = []
512518

513519
df = pd.read_csv(csv_file, encoding="utf-8")
514-
df = df.applymap(lambda x: x.strip() if isinstance(x, str) else x) # Clean headers
520+
df = df.applymap(
521+
lambda x: x.strip() if isinstance(x, str) else x
522+
) # Clean headers
515523

516524
for form_name, group in df.groupby("Form Name"):
517525
datas[form_name] = group.to_dict(orient="records")
518526
order[form_name] = []
519527
compute[form_name] = []
520-
os.makedirs(f"{abs_folder_path}/activities/{form_name}/items", exist_ok=True)
528+
os.makedirs(
529+
f"{abs_folder_path}/activities/{form_name}/items", exist_ok=True
530+
)
521531

522532
# TODO: should we bring back the language
523-
# if not languages:
524-
# languages = parse_language_iso_codes(row["Field Label"])
533+
# if not languages:
534+
# languages = parse_language_iso_codes(row["Field Label"])
525535

526536
for _, row in group.iterrows():
527537
field_name = row["Variable / Field Name"]
@@ -531,26 +541,31 @@ def process_csv_with_pandas(csv_file, abs_folder_path, schema_context_url, proto
531541
row["Choices, Calculations, OR Slider Labels"],
532542
field_type=row["Field Type"],
533543
)
534-
compute[form_name].append({
535-
"variableName": field_name,
536-
"jsExpression": condition,
537-
})
544+
compute[form_name].append(
545+
{
546+
"variableName": field_name,
547+
"jsExpression": condition,
548+
}
549+
)
538550
elif "@CALCTEXT" in row.get("Field Annotation", "").upper():
539551
calc_text = row["Field Annotation"]
540552
match = re.search(r"@CALCTEXT\((.*)\)", calc_text)
541553
if match:
542554
js_expression = match.group(1)
543555
js_expression = normalize_condition(js_expression)
544-
compute[form_name].append({
545-
"variableName": field_name,
546-
"jsExpression": js_expression,
547-
})
556+
compute[form_name].append(
557+
{
558+
"variableName": field_name,
559+
"jsExpression": js_expression,
560+
}
561+
)
548562
else:
549563
order[form_name].append(f"items/{field_name}")
550564

551565
os.makedirs(f"{abs_folder_path}/{protocol_name}", exist_ok=True)
552566
return datas, order, compute, languages
553567

568+
554569
# todo adding output path
555570
def redcap2reproschema(
556571
csv_file, yaml_file, output_path, schema_context_url=None
Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import csv
2+
23
import pytest
34

45
from ..redcap2reproschema import process_field_properties
@@ -13,63 +14,65 @@ def test_process_field_properties_calctext():
1314
"Variable / Field Name": "test_var",
1415
"Required Field?": "",
1516
"Field Annotation": "@CALCTEXT",
16-
"Branching Logic (Show field only if...)": ""
17+
"Branching Logic (Show field only if...)": "",
1718
},
1819
"expected": {
1920
"variableName": "test_var",
2021
"isAbout": "items/test_var",
21-
"isVis": False
22-
}
22+
"isVis": False,
23+
},
2324
},
2425
# Complex CALCTEXT with conditional logic
2526
{
2627
"input": {
2728
"Variable / Field Name": "parkinsons_diagnosis",
2829
"Required Field?": "",
2930
"Field Annotation": "@CALCTEXT(if(([diagnosis_parkinsons_gsd_category_1(bradykinesia)] && ([diagnosis_parkinsons_gsd_category_1(tremor)] || [diagnosis_parkinsons_gsd_category_1(rigidity)])), 'Yes', 'No'))",
30-
"Branching Logic (Show field only if...)": "[some_other_condition] = 1"
31+
"Branching Logic (Show field only if...)": "[some_other_condition] = 1",
3132
},
3233
"expected": {
3334
"variableName": "parkinsons_diagnosis",
3435
"isAbout": "items/parkinsons_diagnosis",
35-
"isVis": False
36-
}
36+
"isVis": False,
37+
},
3738
},
3839
# CALCTEXT with numerical operations
3940
{
4041
"input": {
4142
"Variable / Field Name": "bmi",
4243
"Required Field?": "",
4344
"Field Annotation": "@CALCTEXT([weight]/([height]*[height]))",
44-
"Branching Logic (Show field only if...)": "[weight] > 0 and [height] > 0"
45+
"Branching Logic (Show field only if...)": "[weight] > 0 and [height] > 0",
4546
},
4647
"expected": {
4748
"variableName": "bmi",
4849
"isAbout": "items/bmi",
49-
"isVis": False
50-
}
50+
"isVis": False,
51+
},
5152
},
5253
# CALCTEXT with multiple nested conditions
5354
{
5455
"input": {
5556
"Variable / Field Name": "complex_score",
5657
"Required Field?": "",
5758
"Field Annotation": "@CALCTEXT(if([score1] > 10 && [score2] < 5, 'High', if([score1] > 5, 'Medium', 'Low')))",
58-
"Branching Logic (Show field only if...)": ""
59+
"Branching Logic (Show field only if...)": "",
5960
},
6061
"expected": {
6162
"variableName": "complex_score",
6263
"isAbout": "items/complex_score",
63-
"isVis": False
64-
}
65-
}
64+
"isVis": False,
65+
},
66+
},
6667
]
67-
68+
6869
for test_case in test_cases:
6970
result = process_field_properties(test_case["input"])
7071
for key, expected_value in test_case["expected"].items():
71-
assert result[key] == expected_value, \
72-
f"Failed for {key} in test case with annotation: {test_case['input']['Field Annotation']}"
72+
assert (
73+
result[key] == expected_value
74+
), f"Failed for {key} in test case with annotation: {test_case['input']['Field Annotation']}"
75+
7376

7477
def test_process_field_properties_mixed_annotations():
7578
"""Test fields with multiple annotations"""
@@ -80,34 +83,35 @@ def test_process_field_properties_mixed_annotations():
8083
"Variable / Field Name": "test_var",
8184
"Required Field?": "",
8285
"Field Annotation": "@CALCTEXT @READONLY",
83-
"Branching Logic (Show field only if...)": ""
86+
"Branching Logic (Show field only if...)": "",
8487
},
85-
"expected": {"isVis": False}
88+
"expected": {"isVis": False},
8689
},
8790
# CALCTEXT with HIDDEN
8891
{
8992
"input": {
9093
"Variable / Field Name": "test_var",
9194
"Required Field?": "",
9295
"Field Annotation": "@HIDDEN @CALCTEXT(if([var1] > 0, 1, 0))",
93-
"Branching Logic (Show field only if...)": ""
96+
"Branching Logic (Show field only if...)": "",
9497
},
95-
"expected": {"isVis": False}
98+
"expected": {"isVis": False},
9699
},
97100
# Complex CALCTEXT with other annotations
98101
{
99102
"input": {
100103
"Variable / Field Name": "test_var",
101104
"Required Field?": "",
102105
"Field Annotation": "@CALCTEXT(if(([var1] && [var2]), 'Yes', 'No')) @READONLY @HIDDEN-SURVEY",
103-
"Branching Logic (Show field only if...)": "[condition] = 1"
106+
"Branching Logic (Show field only if...)": "[condition] = 1",
104107
},
105-
"expected": {"isVis": False}
106-
}
108+
"expected": {"isVis": False},
109+
},
107110
]
108-
111+
109112
for test_case in test_cases:
110113
result = process_field_properties(test_case["input"])
111114
for key, expected_value in test_case["expected"].items():
112-
assert result[key] == expected_value, \
113-
f"Failed for {key} in test case with annotation: {test_case['input']['Field Annotation']}"
115+
assert (
116+
result[key] == expected_value
117+
), f"Failed for {key} in test case with annotation: {test_case['input']['Field Annotation']}"

0 commit comments

Comments
 (0)