예제 #1
0
def parse_sample(row, dayfirst_dict, source=None):
    """Parse a row into a Sample.

    Parameters
    ----------
    row : dict-like 
        Object whose keys are column headings and values are the row values.
    dayfirst_dict : dict
        Dictionary whose keys are names of columns containing date/time data 
        and values are boolean indicating whether the dates in that column 
        should be interpreted as having a day as the first component (True)
        or a month or year as the first component (False).
    source : model.Source
        Source for the returned sample.

    Returns
    -------
    Sample
    """
    sample = Sample()
    sample.age_units = get_age_units(row, ureg.years)
    sample.age = get_age(row)
    sample.latitude = get_latitude(row)
    sample.longitude = get_longitude(row)
    sample.elevation = get_elevation(row)
    sample.height_units = get_height_units(row, ureg.metres)
    sample.height = get_height(row)
    sample.weight_units = get_weight_units(row, ureg.kilograms)
    sample.weight = get_weight(row)
    sample.bmi = get_bmi(row)
    (d, t) = get_collection_datetime(row, dayfirst_dict)
    sample.sample_date = d
    sample.sample_time = t
    sample.sampling_time = parse_sampling_time(sample)
    sample.sampling_site = parse_sampling_site(row)

    # Initialize equality attrs
    if not source:
        sample.source = parse_source(row)
    elif isinstance(source, Source):
        sample.source = source
    else:
        raise TypeError(f'Given source was not of type {type(Source())!r}.')
    sample.orig_study_id = get_study_id(row)
    sample.orig_subject_id = get_subject_id(row)
    sample.orig_sample_id = get_sample_id(row)
    return sample
예제 #2
0
def parse_sample(row, attr_map, site_attrs_map=None, time_attr=None):
    """Parse a row of an indexable collection into a Sample.

    Parameters
    ----------
    row : indexable collection
        A row to be parsed into a Sample.
    attr_map : dict
        A dictionary mapping indexes for the indexable collection `row` to
        attribute names of the Sample object.
    """
    sample = Sample()
    # print(row)
    for index, attr in attr_map.items():
        setattr(sample, attr, row[index])
    if site_attrs_map:
        sample.sampling_site = parse_sample_site(row, site_attrs_map)
    if time_attr:
        sample.sampling_time = parse_sample_time(row, time_attr)
    return sample