def normalize_nedm_val(nedm, range_check = True):
    """Normalize the data within neuroelectro.models NeuronEphysDataMap to standard units and range
    """
    data_mean_value = nedm.val
    data_err_value = nedm.err

    # initialize output dictionary
    key_list = ['value', 'error']
    output_dict = dict.fromkeys(key_list)

    unit_reg = UnitRegistry()
    ecm = nedm.ephys_concept_map
    ephys_prop = nedm.ephys_concept_map.ephys_prop
    natural_unit = unicode(ephys_prop.units)

    # try to get unit from table header, if can't, assume unit is natural unit
    found_unit = ecm.identified_unit
    if found_unit is None:
        found_unit = get_units_from_table_header(ecm.ref_text)
    if found_unit is None:
        parsed_nedm = resolve_data_float(nedm.ref_text, initialize_dict = True)
        found_unit = parsed_nedm['units']
        # need to save new unit to ecm now
    if found_unit is None:
        found_unit = natural_unit

    # normalize mean value
    conv_mean_value = convert_units(found_unit, natural_unit, data_mean_value)
    if conv_mean_value:
        # custom normalization for negative and ratio values
        conv_mean_value = convert_negative_value(conv_mean_value, ephys_prop)
        conv_mean_value = convert_percent_to_ratio(conv_mean_value, ephys_prop, ecm.ref_text)

        # check whether mean value in appropriate range
        if range_check:
            if check_data_val_range(conv_mean_value, ephys_prop) is False:
                print 'neuron ephys data map %s, with pk %s out of appropriate range' % (data_mean_value, nedm.pk)
                print conv_mean_value, ephys_prop
                conv_mean_value = None
        output_dict['value'] = conv_mean_value


    # normalize error term
    # TODO: address if errors represented as standard deviations

    if data_err_value:
        conv_err_value = convert_units(found_unit, natural_unit, data_err_value)
        if conv_err_value:
            conv_err_value = convert_percent_to_ratio(conv_err_value, ephys_prop, ecm.ref_text)
            #print 'reported err val: %s, norm err val: %s' % (nedm.err, conv_err_value)

            # really basic check for error term validity
            if conv_err_value < 0:
                conv_err_value = None
            output_dict['error'] = conv_err_value

    return output_dict
Example #2
0
    def test_unit_conversion_megaohm(self):

        input_unit = u'MΩ'
        desired_unit = u'GΩ'

        converted_value = convert_units(input_unit, desired_unit, 23000)
        expected_converted_value = 23

        self.assertAlmostEqual(expected_converted_value, converted_value)
Example #3
0
    def test_unit_conversion_amps(self):

        input_unit = u'nA'
        desired_unit = u'pA'

        converted_value = convert_units(input_unit, desired_unit, 23)
        expected_converted_value = 23000

        self.assertAlmostEqual(expected_converted_value, converted_value)