def format_vsm(self): data = self.machine_data.out header = self.machine_data.header segments = self.machine_data.segment_info aux = np.array([j for i in data for j in i]) # combine all data arrays a = np.array([(i, v) for i, v in enumerate(np.diff(aux, axis=0)[:, 0])]) sign = np.sign(np.diff(aux, axis=0)[:, 1]) threshold = 3 zero_crossings = [ i + 1 for i in xrange(len(a[:, 1]) - 1) if a[i, 1] > 0 > a[i + 1, 1] and a[i, 1] > 0 > a[i + 2, 1] or a[i, 1] < 0 < a[i + 1, 1] and a[i, 1] < 0 < a[i + 2, 1] ] zero_crossings = [0] + zero_crossings # start with zero index zero_crossings += [len(aux)] # append last index ut = 0 # running number warming dt = 0 # running number cooling for i, v in enumerate(zero_crossings): if v < zero_crossings[-1]: # prevents index Error if sum(a[v:zero_crossings[i + 1], 1]) < 0: # cooling name = 'cool%02i' % (ut) ut += 1 else: name = 'warm%02i' % (dt) dt += 1 data = aux[v:zero_crossings[i + 1] + 1] rpd = RockPyData(column_names=header, data=data) rpd.rename_column('temperature', 'temp') rpd.rename_column('moment', 'mag') self._data.update({name: rpd})
def format_vsm(self): data = self.machine_data.out header = self.machine_data.header segments = self.machine_data.segment_info aux = np.array([j for i in data for j in i]) # combine all data arrays a = np.array([(i, v) for i, v in enumerate(np.diff(aux, axis=0)[:, 0])]) sign = np.sign(np.diff(aux, axis=0)[:, 1]) threshold = 3 zero_crossings = [i+1 for i in xrange(len(a[:, 1]) - 1) if a[i, 1] > 0 > a[i + 1, 1] and a[i, 1] > 0 > a[i + 2, 1] or a[i, 1] < 0 < a[i + 1, 1] and a[i, 1] < 0 < a[i + 2, 1]] zero_crossings = [0] + zero_crossings # start with zero index zero_crossings += [len(aux)] # append last index ut = 0 # running number warming dt = 0 # running number cooling for i, v in enumerate(zero_crossings): if v < zero_crossings[-1]: # prevents index Error if sum(a[v:zero_crossings[i + 1], 1]) < 0: # cooling name = 'cool%02i' % (ut) ut += 1 else: name = 'warm%02i' % (dt) dt += 1 data = aux[v:zero_crossings[i + 1] + 1] rpd = RockPyData(column_names=header, data=data) rpd.rename_column('temperature', 'temp') rpd.rename_column('moment', 'mag') self._data.update({name: rpd})
class TestRockPyData(TestCase): def setUp(self): # run before each test self.testdata = ((1, 2, 3, 4), (1, 6, 7, 8), (1, 2, 11, 12), (1, 6, 55, 66)) self.col_names = ('F', 'Mx', 'My', 'Mz') self.row_names = ('1.Zeile', '2.Zeile_A', '3.Zeile', '4.Zeile_A') self.units = ('T', 'mT', 'fT', 'pT') self.RPD = RockPyData(column_names=self.col_names, row_names=self.row_names, units=self.units, data=self.testdata) def test_column_names(self): self.assertEqual(self.RPD.column_names, list(self.col_names)) def test_column_count(self): self.assertEqual(self.RPD.column_count, len(self.col_names)) def test__find_duplicate_variable_rows(self): # self.assertTrue((self.RPD._find_duplicate_variables()[0] == np.array([0, 1, 2])).all()) self.assertEqual(self.RPD._find_duplicate_variable_rows(), [(0, 1, 2, 3)]) # redefine variabe alias to the first two columns self.RPD.define_alias('variable', ('F', 'Mx')) self.assertEqual(self.RPD._find_duplicate_variable_rows(), [(0, 2), (1, 3)]) def test_rename_column(self): self.RPD.rename_column('Mx', 'M_x') self.assertEqual(self.RPD.column_names, ['F', 'M_x', 'My', 'Mz']) def test_append_rows(self): d1 = [[5, 6, 7, 8], [9, 10, 11, 12]] self.RPD = self.RPD.append_rows(d1, ('5.Zeile', '6.Zeile')) self.assertTrue(np.array_equal(self.RPD.v[-2:, :], np.array(d1))) d2 = [5, 6, 7, 8] self.RPD = self.RPD.append_rows(d2, '5.Zeile') self.assertTrue(np.array_equal(self.RPD.v[-1, :], np.array(d2))) # lets try with other RockPyData object rpd = copy.deepcopy(self.RPD) rpd.rename_column('Mx', 'M_x') self.RPD = self.RPD.append_rows(rpd) # TODO: add assert #print self.RPD def test_delete_rows(self): self.RPD = self.RPD.delete_rows((0, 2)) self.assertTrue(np.array_equal(self.RPD.v, np.array(self.testdata)[(1, 3), :])) def test_eliminate_duplicate_variable_rows(self): # check for one variable column self.RPD = self.RPD.eliminate_duplicate_variable_rows() self.assertTrue(np.array_equal(self.RPD.v, np.array([]).reshape(0, 4))) def test_eliminate_duplicate_variable_rows2(self): # check for two variable columns self.RPD.define_alias('variable', ('F', 'Mx')) rpd = self.RPD.eliminate_duplicate_variable_rows(substfunc='mean') self.assertTrue(np.array_equal(rpd.v, np.array([[1., 2., 7., 8.], [1., 6., 31., 37.]]))) self.assertTrue(np.array_equal(rpd.e, np.array([[0., 0., 4., 4.], [0., 0., 24., 29.]]))) rpd = self.RPD.eliminate_duplicate_variable_rows(substfunc='last') self.assertTrue(np.array_equal(rpd.v, np.array([[1., 2., 11., 12.], [1., 6., 55., 66.]]))) def test_mean(self): self.RPD = self.RPD.mean() self.assertTrue(np.array_equal(self.RPD.v, np.array([[1., 4., 19., 22.5]]))) np.testing.assert_allclose(self.RPD.e, np.array([[0., 2., 20.976, 25.273]]), atol=0.01) def test_max(self): self.RPD = self.RPD.max() self.assertTrue(np.array_equal(self.RPD.v, np.array([[1., 6., 55., 66.]]))) def test_filter_row_names(self): self.assertEqual(self.RPD.filter_row_names(('1.Zeile', '3.Zeile')).row_names, ['1.Zeile', '3.Zeile']) def test_filter_match_row_names(self): # get all rows ending with '_A' self.assertEqual(self.RPD.filter_match_row_names('.*_A').row_names, ['2.Zeile_A', '4.Zeile_A']) def test_append_columns(self): cb = self.RPD.column_count d = (8, 7, 6, 5) self.RPD = self.RPD.append_columns('neue Spalte', d) self.assertEqual(cb + 1, self.RPD.column_count) self.assertTrue(np.array_equal(self.RPD['neue Spalte'].v, np.array(d))) def test_sort(self): self.assertTrue(np.array_equal(self.RPD.sort('Mx')['Mx'].v, np.array((2, 2, 6, 6)))) #def test_interpolate(self): # self.RPD.define_alias('variable', 'My') # iv = (1, 11, 33, 55, 100) # self.assertTrue(np.array_equal((self.RPD.interpolate(iv))['My'].v, np.array(iv))) # self.assertTrue(np.array_equal((self.RPD.interpolate(iv))['Mx'].v[1:-1], np.array([2., 4., 6.]))) def test_magnitude(self): self.RPD.define_alias('m', ('Mx', 'My', 'Mz')) self.RPD = self.RPD.append_columns('mag', self.RPD.magnitude('m')) np.testing.assert_allclose(self.RPD['mag'].v, np.array([5.38516481, 12.20655562, 16.40121947, 86.12200648]), atol=1e-5) def test_column_names_to_indices(self): self.assertEqual( self.RPD.column_names_to_indices(('Mx', 'Mz')), [1,3]) def test_interation(self): # TODO: add proper assertion for l in self.RPD: #print l pass def test_add_errors(self): d = RockPyData(column_names=['A', 'B']) #d['A'].v = 1 # Attribute Error NoneType has no attribute, maybe initialize to np.nan? #d['B'] = 2 #d['A'].e = 4 #d['B'].e = 5 d = d.append_rows([1, 2]) #print d d.e = [[4, 5]] self.assertEqual(5., d['B'].e) def test_data_assignment(self): print self.RPD # set only values self.RPD['Mx'] = [1.1, 1.2, 1.3, 1.4] print self.RPD # set values and errors self.RPD['Mx'] = [[[1.1, 0.11]], [[1.2, 0.12]], [[1.3, 0.13]], [[1.4, 0.14]]] print self.RPD
def test(): # define some data for tutorials.rst testdata = ((1, 2, 3, 4), (2, 6, 7, 8), (9, 10, 11, 12)) testdata2 = ((1, 1), (2, 2), (19, 3)) # create a rockpydata object with named columns and filled with testdata d = RockPyData(column_names=('F', 'Mx', 'My', 'Mz'), row_names=('1.Zeile', '2.Zeile', '3.Zeile'), units=('T', 'mT', 'fT', 'pT'), data=testdata) d_json = numpyson.dumps(d) print d_json dd = numpyson.loads(d_json) print repr(dd) print "dd:", dd #d = d.eliminate_duplicate_variable_rows(substfunc='last') #print d._find_unique_variable_rows() print('d:\n%s' % d) e = RockPyData(column_names=('F', 'Mx'), row_names=('1.Zeile', '2.Zeile', '3.Zeile'), units=('T', 'mT', 'fT', 'pT'), data=testdata2) print('e:\n%s' % e) print('e+d:\n%s' % (e+d)) print('e-d:\n%s' % (e-d)) print('e/d:\n%s' % (e/d)) print('e*d:\n%s' % (e*d)) print('d/e:\n%s' % (d/e)) print('d*e:\n%s' % (d*e)) print('d+e:\n%s' % (d+e)) print('d-e:\n%s' % (d-e)) print d.units # define as many aliases as you want d.define_alias('M', ('Mx', 'My', 'Mz')) d.define_alias('Mzx', ('Mz', 'Mx')) # show some data # aliases 'all', 'variable' and 'dep_var' are predefined print('all:\n%s' % d['all']) print('Mzx:\n%s' % d['Mzx']) # lets alter some data d['Mx'] = np.array((13, 24, 35)) # show M with modified Mx component print('M:\n%s' % d['M']) # show Mx print('Mx:\n%s' % d['Mx']) # we can also alter several columns at once d['M'] = ((2, 3, 4), (18, 88, 98), (39, 89, 99)) print('M:\n%s' % d['M']) # some math fun # calculate magnitude of vector 'M' and save it as new column 'magM' d = d.append_columns('magM', d.magnitude('M')) # calculate values of 'magM' normalized to 100 #d.append_columns('normM', d.normalize('magM', 100)) # we can also add arbitrary data in a new column d = d.append_columns(("T",), np.array((1, 2, 3))) # we can also add an empty column d = d.append_columns(("empty",)) # renaming a column d.rename_column('T', 'temp') # show all data again, now including magM and T as the last two columns print d # do a plot of F vs magM # plt.plot(d['F'], d['magM']) # plt.show() # fancy filtering of data tf_array = (d['Mx'].v > 10) & (d['Mx'].v < 20) print 'filtering:' filtered_d = d.filter(tf_array) print filtered_d['Mx'] # arithmetic operations e = deepcopy(d) # mutlipy one column with value e['Mx'].v *= 2 # calculate difference of two rockpydata objects #c = e - d #print c #c = e + d #print c #c = e / d #print c #c = e * d #print c #print repr(c) # test single line object l = RockPyData(column_names=('A', 'B', 'C', 'D'), row_names=('1.Zeile',), units=('T', 'mT', 'fT', 'pT'), data=((1, 2, 3, 4),)) l = l.append_columns('X', (5,)) print l print l['X'] #print d.mean() print d print d + (1, 2, 3, 4, 5, 6) print d.interpolate(np.arange(2, 10, .5), includesourcedata=True) #d.define_alias('variable', 'Mx') #print d.interpolate(np.arange(0, 10, .5)) print "condense:" print condense([d, d*2, d*3], substfunc='median')