def parse_date(text, month=None, day=None, complete_date_bool=False): if len(text) == 5: dt = datetime.fromordinal( datetime(1900, 1, 1).toordinal() + int(text) - 2 ).date() parser = dateparser("%Y-%m-%d", ISOFormat.DAY) date_string = str(dt) elif len(text) < 4: return None elif month and day and text: parser = dateparser("%m/%d/%Y", ISOFormat.DAY) date_string = "{0}/{1}/{2}".format(month, day, text) elif day and text: parser = dateparser("%Y", ISOFormat.YEAR) date_string = "{0}".format(text) elif month and text and not day: parser = dateparser("%m/%Y", ISOFormat.MONTH) date_string = "{0}/{1}".format(month, text) elif not complete_date_bool: parser = dateparser("%Y", ISOFormat.YEAR) date_string = "{0}".format(text) else: parser = dateparser("%m/%d/%Y", ISOFormat.DAY) date_string = "{0}".format(text) return parser(date_string)
'ASIAN': ('http://hl7.org/fhir/v3/Race', '2028-9', 'Asian'), 'BLACK OR AFRICAN AMERICAN': ('http://hl7.org/fhir/v3/Race', '2054-5', 'Black or African American'), 'HISPANIC OR LATINO': ('http://hl7.org/fhir/v3/Race', '2106-3', 'White'), 'WHITE': ('http://hl7.org/fhir/v3/Race', '2106-3', 'White'), 'NATIVE HAWAIIAN AND OTHER PACIFIC ISLANDER': ('http://hl7.org/fhir/v3/Race', '2076-8', 'Native Hawaiian or Other Pacific Islander') }.get(race, None) patients = (etl.io.csv.fromcsv(resolve('work/Patient.csv')) .fieldmap({ 'id': 'ID', 'STUDYID': 'STUDYID', 'subject_id': ('STUDYID', lambda x: 'CASE-' + x), 'race': ('RACE', map_race), 'gender': ('SEX', {'F': 'female', 'M': 'male'}), 'birth_date': ('BIRTH_YR', year), 'index_date': ('INDEX_YEAR', dateparser('%Y', ISOFormat.DAY)), 'tag': lambda rec: ('subject-type', 'case') }, True)) index = (patients .cut('STUDYID', 'id', 'index_date') .rename('id', 'subject')) procedures = (etl.io.csv.fromcsv(resolve('work/Procedure.csv')) .hashjoin(index, lkey='STUDYID', rkey='STUDYID') .fieldmap({ 'id': 'ID', 'date': lambda rec: rec['index_date'] + timedelta(int(rec['DAYS_VIS_INDEX'])), 'code': lambda rec: ('http://www.ama-assn.org/go/cpt', rec['PROC_CODE'], rec['NAME'].strip('" ')), 'subject': 'subject' }, True))
def index_date(rec): birth = number(rec['BIRTH_YR']) index_age = number(rec['INDEX_AGE']) index_date = str(birth + index_age) return dateparser('%Y', ISOFormat.DAY)(index_date)
def sample_date(text): parser = dateparser("%m/%d/%y %H:%M", ISOFormat.DAY) return parser(text)
from datetime import timedelta import petl as etl import math from fhir_petl.fhir import to_json from fhir_petl.util import resolve, mkdirp, number, year, dateparser, ISOFormat from datetime import datetime date = dateparser("%m/%d/%y", ISOFormat.DAY) def map_race(race): return { "Asian": ("http://hl7.org/fhir/v3/Race", "2028-9", "Asian"), "Black": ("http://hl7.org/fhir/v3/Race", "2054-5", "Black or African American"), "African American": ( "http://hl7.org/fhir/v3/Race", "2054-5", "Black or African American", ), "White": ("http://hl7.org/fhir/v3/Race", "2106-3", "White"), "Other": ("http://hl7.org/fhir/v3/Race", "2131-1", "Other Race"), "Unknown": ("http://hl7.org/fhir/v3/NullFlavor", "UNK", "Unknown"), "Native American / Alaskan": ( "http://hl7.org/fhir/v3/Race", "1002-5", "American Indian or Alaska Native", ), "Native Hawaiian / Pacific Islander": ( "http://hl7.org/fhir/v3/Race", "2076-8", "Native Hawaiian or Other Pacific Islander",
def test_dateparser(): year = util.dateparser('%Y', util.ISOFormat.YEAR) assert year('1994').isoformat() == '1994' second = util.dateparser('%Y', util.ISOFormat.SECOND) assert second('1994').isoformat() == '1994-01-01T00:00:00'
from datetime import timedelta import petl as etl from fhir_petl.fhir import to_json from fhir_petl.util import resolve, mkdirp, number, year, dateparser, ISOFormat date = dateparser('%Y-%m-%d %H:%M:%S', ISOFormat.DAY) def map_race(race): return { 'ASIAN': ('http://hl7.org/fhir/v3/Race', '2028-9', 'Asian'), 'BLACK': ('http://hl7.org/fhir/v3/Race', '2054-5', 'Black or African American'), 'HISPANIC/LATINO': ('http://hl7.org/fhir/v3/Race', '2106-3', 'White'), 'WHITE': ('http://hl7.org/fhir/v3/Race', '2106-3', 'White'), 'NATIVE HAWAIIAN/PACIFIC ISLANDER': ('http://hl7.org/fhir/v3/Race', '2076-8', 'Native Hawaiian or Other Pacific Islander') }.get(race, None) def sample_date(text): parser = dateparser('%m/%d/%y %H:%M', ISOFormat.DAY) return parser(text) def birth_date(rec): sample_age = timedelta(int(rec['Age (Sample)']) * 365.25) return sample_date(rec['SAMPLE_DATE']) - sample_age