Example #1
0
def _getFitParameters(list_of_dicts, key):
    # Load rows of data
    x = _loadRow('doublingTime', list_of_dicts)
    y = _loadRow(key, list_of_dicts)

    # Save and strip units
    y_units = 1
    x_units = 1
    if units.hasUnit(y):
        y_units = units.getUnit(y)
        y = y.asNumber(y_units)
    if units.hasUnit(x):
        x_units = units.getUnit(x)
        x = x.asNumber(x_units)

    # Sort data for spine fitting (must be ascending order)
    idx_order = x.argsort()
    x = x[idx_order]
    y = y[idx_order]

    # Generate fit
    parameters = interpolate.splrep(x, y)
    if np.sum(np.absolute(interpolate.splev(x, parameters) - y)) / y.size > 1.:
        raise Exception(
            "Fitting {} with 3d spline, residuals are huge!".format(key))

    return {
        'parameters': parameters,
        'x_units': x_units,
        'y_units': y_units,
        'dtype': y.dtype
    }
Example #2
0
def _loadRow(key, list_of_dicts):
    if units.hasUnit(list_of_dicts[0][key]):
        row_units = units.getUnit(list_of_dicts[0][key])
        return row_units * np.array(
            [x[key].asNumber(row_units) for x in list_of_dicts])
    else:
        return np.array([x[key] for x in list_of_dicts])
Example #3
0
def _useFitParameters(x_new, parameters, x_units, y_units, dtype):
    # Convert to same unit base
    if units.hasUnit(x_units):
        x_new = x_new.asNumber(x_units)
    elif units.hasUnit(x_new):
        raise Exception("New x value has units but fit does not!")

    # Calculate new interpolated y value
    y_new = interpolate.splev(x_new, parameters)

    # If value should be an integer (i.e. an elongation rate)
    # round to the nearest integer
    if dtype == np.int:
        y_new = int(np.round(y_new))

    return y_units * y_new
Example #4
0
 def _field(self, fieldname):
     if not units_pkg.hasUnit(self.units[fieldname]):
         if self.units[fieldname] == None:
             return self.struct_array[fieldname]
         else:
             raise Exception, 'Field has incorrect units or unitless designation!\n'
     else:
         return self.units[fieldname] * self.struct_array[fieldname]
Example #5
0
    def __setitem__(self, key, value):
        if units_pkg.hasUnit(value):
            try:
                self.units[key].matchUnits(value)
            except unum.IncompatibleUnitsError:
                raise Exception, 'Units do not match!\n'

            self.struct_array[key] = value.asNumber()
            self.units[key] = units_pkg.getUnit(value)

        elif type(value) == list or type(value) == np.ndarray:
            if units_pkg.hasUnit(self.units[key]):
                raise Exception, 'Units do not match! Quantity has units your input does not!\n'
            self.struct_array[key] = value
            self.units[key] = None

        else:
            raise Exception, 'Cant assign data-type other than unum datatype or list/numpy array!\n'