Skip to content

Commit a4e3310

Browse files
committed
Merge branch 'release/1.9.0'
2 parents bef6e4f + e4b1c1c commit a4e3310

File tree

5 files changed

+54
-23
lines changed

5 files changed

+54
-23
lines changed

CHANGELOG

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
Changelog
22
=========
3+
## [1.9.0] - 2022-06-24
4+
5+
### Summary
6+
This release changes our REDCap API call from cappy to PyCap, which allows for
7+
a more flexible data export. This new library makes it so a REDCap project of
8+
any size can be automatically exported; the filters will no longer fail when
9+
attempting to export a REDCap data csv larger than 30mb.
10+
11+
### Updated
12+
* Update run_filters.py to use PyCap instead of cappy
313
## [1.8.2] - 2022-04-26
414

515
### Summary

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ HOW TO Convert from REDCap to NACC
2121

2222
To install NACCulator, run:
2323

24-
$ python3 -m pip install git+https://github.com/ctsit/cappy.git@2.0.0
2524
$ pip3 install git+https://github.com/ctsit/nacculator.git
2625

2726
Once the project data is exported from REDCap to the CSV file `data.csv`, run:

nacc/run_filters.py

+41-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import csv
44
import datetime
55
import configparser
6-
from cappy import API
6+
from redcap import Project
77
from nacc.uds3.filters import *
88

99

@@ -88,29 +88,54 @@ def read_config(config_path):
8888
return config
8989

9090

91-
# Getting Data From RedCap
92-
def get_data_from_redcap(folder_name, config):
91+
def get_data_from_redcap_pycap(folder_name, config):
9392
# Enter the path for filters_config
9493
try:
95-
token = config.get('cappy', 'token')
96-
redcap_url = config.get('cappy', 'redcap_server')
94+
token = config.get('pycap', 'token')
95+
redcap_url = config.get('pycap', 'redcap_server')
9796
except Exception as e:
9897
print("Please check the config file and validate all the proper fields exist", file=sys.stderr)
9998
print(e)
10099
raise e
101100

102-
redcap_access_api = API(token, redcap_url, 'master.yaml')
103-
res = redcap_access_api.export_records(adhoc_redcap_options={
104-
'format': 'csv'
105-
})
101+
redcap_project = Project(redcap_url, token)
102+
103+
# Get list of all fieldnames in project to create a csv header
104+
assert hasattr(redcap_project, 'field_names')
105+
header_a = getattr(redcap_project, 'field_names')
106+
107+
header_b = []
108+
list_of_fields = redcap_project.export_field_names()
109+
for field in list_of_fields:
110+
header_b.append(field['export_field_name'])
111+
112+
header_full = list(set(header_a + header_b))
113+
header_full.insert(1, 'redcap_event_name')
114+
115+
# Get list of all records present in project to iterate over
116+
list_of_records = []
117+
all_records = redcap_project.export_records(fields=['ptid'])
118+
for record in all_records:
119+
if record['ptid'] not in list_of_records:
120+
list_of_records.append(record['ptid'])
121+
122+
chunked_records = []
123+
# Break the list into chunks of 50
124+
n = 50
125+
for i in range(0, len(list_of_records), n):
126+
chunked_records.append(list_of_records[i:i + n])
127+
106128
try:
107-
rawdata = str(res.text)
108-
myreader = csv.reader(rawdata.splitlines())
129+
109130
try:
110-
with open(os.path.join(folder_name, "redcap_input.csv"), "w") as file:
111-
writer = csv.writer(file, delimiter=',')
112-
for row in myreader:
113-
writer.writerow(row)
131+
with open(os.path.join(folder_name, "redcap_input.csv"), "w") as redcap_export:
132+
writer = csv.DictWriter(redcap_export, fieldnames=header_full)
133+
writer.writeheader()
134+
# header_mapping = next(reader)
135+
for current_record_chunk in chunked_records:
136+
data = redcap_project.export_records(records=current_record_chunk)
137+
for row in data:
138+
writer.writerow(row)
114139
except Exception as e:
115140
print("Error in Writing")
116141
print(e)
@@ -136,7 +161,7 @@ def main():
136161
config_path = sys.argv[1]
137162
config = read_config(config_path)
138163

139-
get_data_from_redcap(folder_name, config)
164+
get_data_from_redcap_pycap(folder_name, config)
140165
run_all_filters(folder_name, config_path)
141166

142167
exit()

nacculator_cfg.ini.example

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[DEFAULT]
22

3-
[cappy]
3+
[pycap]
44
token: Your REDCAP Token
55
redcap_server: Your Redcap Server
66

setup.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from setuptools import setup, find_packages
88

9-
VERSION = "1.8.2"
9+
VERSION = "1.9.0"
1010

1111
setup(
1212
name="nacculator",
@@ -31,11 +31,8 @@
3131
]
3232
},
3333

34-
dependency_links=[
35-
"git+https://github.com/ctsit/cappy.git@2.0.0#egg=cappy-2.0.0"
36-
],
3734
install_requires=[
38-
"cappy==2.0.0"
35+
"PyCap>=2.1.0"
3936
],
4037

4138
python_requires=">=3.6.0",

0 commit comments

Comments
 (0)