def get_data(self): data = Data('exponential_data') model = self.get_model() function = model.lambdify('exp', ('t', 'a', 'k'), modules='numpy') linspace = numpy.linspace(-1, 1, 300, endpoint=True) data.array = numpy.array([ linspace, function(linspace, 2, 3) ]) return data
def test_load_data(self): for data_file in self.data_files: data = Data() data.path = data_file['filename'] data.genfromtxt_args['delimiter'] = data_file['delimiter'] if data_file['headers']: data.genfromtxt_args['skip_header'] = 1 yield assert_allclose, data.load_data(), self.raw_data
def test_load_data_with_scale(self): data_file = self.data_files[0] data = Data() data.path = data_file['filename'] data.genfromtxt_args['delimiter'] = data_file['delimiter'] data.scale = (2, 5) raw_data_scaled = [ [ 2 * x for x in self.raw_data[0] ], [ 5 * x for x in self.raw_data[1] ], ] assert_allclose(data.load_data(), raw_data_scaled)
def test_error_makes_ndarray(self): data = Data() data.error = (0.1, None) assert_allclose(data.error[0], numpy.array(0.1)) eq_(data.error[1], None) data.error = ([0.1, 0.2], None) assert_allclose(data.error[0], numpy.array([0.1, 0.2])) eq_(data.error[1], None) data.error = (0.1, 0.2) assert_allclose(data.error[0], numpy.array(0.1)) assert_allclose(data.error[1], numpy.array(0.2)) data.error = (0.1, [0.1, 0.2]) assert_allclose(data.error[0], numpy.array(0.1)) assert_allclose(data.error[1], numpy.array([0.1, 0.2]))
def test_load_error(self): raw_error = numpy.array(self.error) data = Data() data.path = self.error_file['filename'] data.error_columns = (1, None) assert_allclose(data.error[0], raw_error[1]) eq_(data.error[1], None) del data._error data.error_columns = (None, 1) eq_(data.error[0], None) assert_allclose(data.error[1], raw_error[1]) del data._error data.error_columns = (2, 1) assert_allclose(data.error[0], raw_error[2]) assert_allclose(data.error[1], raw_error[1]) del data._error data.error_columns = ((1, 2), None) assert_allclose(data.error[0], numpy.array([raw_error[1], raw_error[2]])) eq_(data.error[1], None) del data._error data.error_columns = (None, (1, 2)) eq_(data.error[0], None) assert_allclose(data.error[1], numpy.array([raw_error[1], raw_error[2]])) del data._error data.error_columns = ((1, 2), 0) assert_allclose(data.error[0], numpy.array([raw_error[1], raw_error[2]])) assert_allclose(data.error[1], raw_error[0]) del data._error data.error_columns = (0, (2, 1)) assert_allclose(data.error[0], raw_error[0]) assert_allclose(data.error[1], numpy.array([raw_error[2], raw_error[1]])) del data._error data.error_columns = ((1, 3), (0, 2)) assert_allclose(data.error[0], numpy.array([raw_error[1], raw_error[3]])) assert_allclose(data.error[1], numpy.array([raw_error[0], raw_error[2]]))
def test_load_error_with_scale(self): raw_error = numpy.array(self.error) data = Data() data.path = self.error_file['filename'] data.scale = (2, 10) data.error_columns = ((1, 2), 0) assert_allclose(data.error[0], 2 * numpy.array([raw_error[1], raw_error[2]])) assert_allclose(data.error[1], 10 * raw_error[0]) del data._error data.error_columns = (0, (2, 1)) assert_allclose(data.error[0], 2 * raw_error[0]) assert_allclose(data.error[1], 10 * numpy.array([raw_error[2], raw_error[1]])) del data._error data.error_columns = ((1, 3), (0, 2)) assert_allclose(data.error[0], 2 * numpy.array([raw_error[1], raw_error[3]])) assert_allclose(data.error[1], 10 * numpy.array([raw_error[0], raw_error[2]]))
def test_scale_with_mixed_gives_numbers(self): data = Data() data.scale = (2, 'Avogadro constant') assert_array_almost_equal(data.scale, (2, 6.022140857e+23))
def test_scale_with_strings_gives_numbers(self): data = Data() data.scale = ('kilo', 'milli') assert_almost_equal(data.scale, (1000, 0.001))
def test_scale_with_numbers_gives_numbers(self): data = Data() data.scale = (1, 2) eq_(data.scale, (1, 2))
import os import numpy from example_helper import save_example_fit from scipy_data_fitting import Data, Model, Fit # # Example of a basic linear fit. # This example demonstrates how to use a custom `fit_function`. # name = 'linear_polyfit' # Load data from a csv file. data = Data(name) data.path = os.path.join('examples', 'data', 'linear.csv') data.genfromtxt_args['skip_header'] = 1 # Create a linear model. model = Model(name) model.add_symbols('t', 'v', 'x_0') t, v, x_0 = model.get_symbols('t', 'v', 'x_0') model.expressions['line'] = v * t + x_0 # Create the fit using the data and model. fit = Fit(name, data=data, model=model) fit.expression = 'line' fit.independent = {'symbol': 't', 'name': 'Time', 'units': 's'} fit.dependent = {'name': 'Distance', 'units': 'm'} fit.parameters = [ {'symbol': 'v', 'guess': 1, 'units': 'm/s'},
import os import sympy from example_helper import save_example_fit from scipy_data_fitting import Data, Model, Fit # # Example of a fit to a sine wave with error bars. # name = 'wave' # Load data from a csv file. data = Data(name) data.path = os.path.join('examples','data', 'wave.csv') data.genfromtxt_args['skip_header'] = 1 data.error = (0.1, 0.05) # Create a wave model. model = Model(name) model.add_symbols('t', 'A', 'ω', 'δ') A, t, ω, δ = model.get_symbols('A', 't', 'ω', 'δ') model.expressions['wave'] = A * sympy.functions.sin(ω * t + δ) model.expressions['frequency'] = ω / (2 * sympy.pi) # Create the fit using the data and model. fit = Fit(name, data=data, model=model) fit.expression = 'wave' fit.independent = {'symbol': 't', 'name': 'Time', 'units': 's'} fit.dependent = {'name': 'Voltage', 'prefix': 'kilo', 'units': 'kV'} fit.parameters = [
def test_limits_symmetric(self): data = Data() data.array = numpy.array([ [-2, 1], [3, 4] ]) fit = Fit(data=data) eq_(fit.limits, (-2, 2))
def test_limits(self): data = Data() data.array = numpy.array([ [1, 2], [3, 4] ]) fit = Fit(data=data) eq_(fit.limits, (0, 2))
import os from example_helper import save_example_fit from scipy_data_fitting import Data, Model, Fit # # Example of a basic linear fit with error bars. # name = 'linear' # Load data from a csv file. data = Data(name) data.path = os.path.join('examples', 'data', 'linear.csv') data.genfromtxt_args['skip_header'] = 1 data.error_columns = (2, 3) # Create a linear model. model = Model(name) model.add_symbols('t', 'v', 'x_0') t, v, x_0 = model.get_symbols('t', 'v', 'x_0') model.expressions['line'] = v * t + x_0 # Create the fit using the data and model. fit = Fit(name, data=data, model=model) fit.expression = 'line' fit.independent = {'symbol': 't', 'name': 'Time', 'units': 's'} fit.dependent = {'name': 'Distance', 'units': 'm'} fit.parameters = [ {'symbol': 'v', 'guess': 1, 'units': 'm/s'}, {'symbol': 'x_0', 'value': 1, 'units': 'm'},
import os from example_helper import save_example_fit from scipy_data_fitting import Data, Model, Fit # # Example of a basic linear fit. # This example demonstrates how to use `prefix` for unit conversions. # name = 'linear_scaled' # Load data from a csv file. data = Data(name) data.path = os.path.join('examples', 'data', 'linear.csv') data.genfromtxt_args['skip_header'] = 1 # Assume the data was not saved in SI base units. data.scale = ('micro', 'kilo') # Create a linear model. model = Model(name) model.add_symbols('t', 'v', 'x_0') t, v, x_0 = model.get_symbols('t', 'v', 'x_0') model.expressions['line'] = v * t + x_0 # Create the fit using the data and model. fit = Fit(name, data=data, model=model) fit.expression = 'line' fit.independent = {'symbol': 't', 'name': 'Time', 'prefix': 'micro', 'units': 'µs'} fit.dependent = {'name': 'Distance', 'prefix': 'kilo', 'units': 'km'}