Example #1
0
def get_data_from_redcap(folder_name, config):
    # Enter the path for filters_config
    try:
        token = config.get('cappy', 'token')
        redcap_url = config.get('cappy', 'redcap_server')
    except Exception as e:
        print >> sys.stderr, "Please check the config file and validate all the proper fields exist"
        print e
        raise e

    redcap_access_api = API(token, redcap_url, 'master.yaml')
    res = redcap_access_api.export_records(
        adhoc_redcap_options={'format': 'csv'})
    try:
        rawdata = str(res.content).encode("utf-8")
        myreader = csv.reader(rawdata.splitlines())
        try:
            with open(os.path.join(folder_name, "redcap_input.csv"),
                      "w") as file:
                writer = csv.writer(file, delimiter=',')
                for row in myreader:
                    writer.writerow(row)
        except Exception as e:
            print("Error in Writing")
            print e

    except:
        print("Error in CSV file")

    return
Example #2
0
def validate_config(config, metadata=None):
    """
    Instantiates a Validator and uses that with each form config along
    with the metadata to make sure that the field names in the form are
    the correct field names.
    """
    if not metadata:
        api = API(config['token'], config['redcap_url'], 'v6.16.0.json')
        response = api.export_metadata()
        check_metadata_export_permission(response)
        metadata = json.loads(str(response.content, 'utf-8'))
    for form in config['forms']:
        validator = Validator(metadata, form)
        is_valid, field_name, form_name = validator.validate()
        if not is_valid:
            print('Your configuration is not valid.')
            print("""
            The form {form_name} has {field_name} in it's configuration but
            {field_name} does not belong to {form_name} according to the metadata.
            """.format(form_name=form_name, field_name=field_name))
            exit()
Example #3
0
def main(argv):
    api = API(config.token, config.endpoint, config.versions[0])
    project_name = argv[1]
    if project_name:
        try:
            os.mkdir(os.path.join(config.outfile_dir, project_name))
        except:
            pass

        write_project_config(api, project_name)

        path = os.path.join(config.outfile_dir, project_name, 'event_map.json')
        with open(path, 'r') as event_map:
            data = json.loads(event_map.read())

        with open(os.path.join(config.outfile_dir, project_name, 'formEvents.xml'), 'w') as form_events_file:
            form_events_file.write(xml_util.form_events_render(data))

        with open(os.path.join(config.outfile_dir, project_name, 'translationTable.xml'), 'w') as trans_file:
            trans_file.write(xml_util.translation_table_render('./xml_util/translation.yaml'))
Example #4
0
class Batcher(file_util):
    """
    This class is responsible for pulling metadata and data from redcap as well
    as writing the files to the batch root under the quail install directory
    """
    def __init__(self, batch_root, name, token, url):
        self.batch_root = batch_root
        self.api = API(token, url, 'v6.16.0.json')
        self.event_key = 'redcap_event_name'

    def pull_metadata(self, metadata_type=None):
        calls = [('project_info', self.api.export_project_info),
                 ('arms', self.api.export_arms),
                 ('events', self.api.export_events),
                 ('instruments', self.api.export_instruments),
                 ('instrument_event',
                  self.api.export_instrument_event_mapping),
                 ('metadata', self.api.export_metadata),
                 ('records', self.api.export_records)]
        if metadata_type:
            calls = [(m_type, call) for m_type, call in calls
                     if m_type == metadata_type]
        for m_type, call in calls:
            data = json.loads(str(call().content, 'utf-8'))
            today = str(datetime.date.today())
            self.date = today
            file_path = self.join(
                [self.batch_root, today, 'redcap_metadata', m_type + '.json'])
            self.write(file_path, data, 'json')

    def pull_data(self):
        """
        The only sneaky thing in here is that the field that determines the primary
        key of a subject in redcap is given by the very first metadata item
        """
        newest_metadata = self.get_most_recent_date_path(self.batch_root)
        metadata_path = self.join([newest_metadata, 'redcap_metadata'])
        metadata = self.read(self.join([metadata_path, 'metadata.json']),
                             'json')
        instrument_event = self.read(
            self.join([metadata_path, 'instrument_event.json']), 'json')
        today = str(datetime.date.today())
        self.metadata_date = self.path_split(newest_metadata)[1]
        self.date = today

        self.unique_field = metadata[0]
        self.unique_field_name = metadata[0]['field_name']
        self.instruments = list(set([item['form_name'] for item in metadata]))
        self.event_instrument_mapping = {}
        for item in instrument_event:
            form = self.event_instrument_mapping.setdefault(
                item['form'], set())
            form.add(item['unique_event_name'])

        for instrument in self.instruments:
            print('Downloading Instrument {}'.format(instrument))
            event_list = self.event_instrument_mapping.get(instrument)
            if not event_list:
                continue
            res = self.api.export_records(
                fields=[self.unique_field_name, self.event_key],
                events=list(event_list),
                forms=[instrument],
                adhoc_redcap_options={'format': 'csv'})
            try:
                json_data = csv_to_json(str(res.content, 'utf-8'))
            except:
                print('Received non-utf8 data for instrument {}'.format(
                    instrument))
                repr_data = repr(res.content)[2:-1].replace('\\n', '\n')
                json_data = csv_to_json(repr_data)
            data = json.loads(json_data)
            if instrument != self.unique_field['form_name']:
                data = [
                    record for record in data if record_has_data(
                        record,
                        unique_field_name=self.unique_field_name,
                        form_record_name=instrument)
                ]
            else:
                data = [
                    record for record in data
                    if record_has_data(record, form_record_name=instrument)
                ]
            data_path = self.join([
                self.batch_root, today, 'redcap_data_files',
                instrument + '.json'
            ])
            self.write(data_path, data, 'json')
            print('Wrote Instrument {} to path {}'.format(
                instrument, data_path))
Example #5
0
 def __init__(self, batch_root, name, token, url):
     self.batch_root = batch_root
     self.api = API(token, url, 'v6.16.0.json')
     self.event_key = 'redcap_event_name'
Example #6
0
import json

from cappy import API

redcap_token = 'F53EA8B9D58456B722945F4B274E6B4C'
redcap_api_endpoint = 'http://redi2.dev/redcap/api/'
cappy_version = 'lineman.json'

api = API(redcap_token, redcap_api_endpoint, cappy_version)

# only gets the records for subjects with unique_field 1 and 2
res = api.export_records(records=[1, 2])

cappy_version = 'master.yaml'
api = API(redcap_token,
          redcap_api_endpoint,
          cappy_version,
          requests_options={
              'verify': False,
              'headers': {
                  'TEST': 'THIS IS A TEST'
              }
          })

# exports in eav instead of flat
# resist the urge to use adhoc options
res = api.export_records(adhoc_redcap_options={'type': 'eav'})

# we get back raw requests content
# print(res.content)
# cappy gives us more information about what was passed