def norm_group(pos, _, **kargs): """Takes the drain current for each file in group and builds an analysis file and works out the mean drain""" if "signal" in kargs: signal = kargs["signal"] else: signal = "fluo" lfit = kargs["lfit"] rfit = kargs["rfit"] posfile = Data() posfile.metadata = pos[0].metadata posfile = posfile & pos[0].column(0) posfile.column_headers = ["Energy"] for f in pos: print(str(f["run"]) + str(f.find_col(signal))) posfile = posfile & f.column(signal) posfile.add_column(lambda r: np.mean(r[1:]), "mean drain") ec = posfile.find_col("Energy") md = posfile.find_col("mean drain") linearfit = scipy.poly1d( posfile.polyfit(ec, md, 1, lambda x, y: lfit[0] <= x <= lfit[1])) posfile.add_column(lambda r: r[md] - linearfit(r[ec]), "minus linear") highend = posfile.mean("minus", lambda r: rfit[0] <= r[ec] <= rfit[1]) ml = posfile.find_col("minus linear") posfile.add_column(lambda r: r[ml] / highend, "normalised") if "group_key" in kargs: posfile[kargs["group_key"]] = pos.key return posfile
def norm_group(pos,_,**kargs): """Takes the drain current for each file in group and builds an analysis file and works out the mean drain""" if "signal" in kargs: signal=kargs["signal"] else: signal="fluo" lfit=kargs["lfit"] rfit=kargs["rfit"] posfile=Data() posfile.metadata=pos[0].metadata posfile=posfile&pos[0].column(0) posfile.column_headers=['Energy'] for f in pos: print(str(f["run"])+str(f.find_col(signal))) posfile=posfile&f.column(signal) posfile.add_column(lambda r:np.mean(r[1:]),"mean drain") ec=posfile.find_col('Energy') md=posfile.find_col('mean drain') linearfit=scipy.poly1d(posfile.polyfit(ec,md,1,lambda x,y:lfit[0]<=x<=lfit[1])) posfile.add_column(lambda r:r[md]-linearfit(r[ec]),'minus linear') highend=posfile.mean('minus',lambda r:rfit[0]<=r[ec]<=rfit[1]) ml=posfile.find_col('minus linear') posfile.add_column(lambda r:r[ml]/highend,"normalised") if "group_key" in kargs: posfile[kargs["group_key"]]=pos.key return posfile
class Analysis_test(unittest.TestCase): """Path to sample Data File""" datadir=path.join(pth,"sample-data") def setUp(self): self.d1=Data(path.join(self.datadir,"OVF1.ovf")) self.d2=Data(path.join(self.datadir,"TDI_Format_RT.txt")) self.d3=Data(path.join(self.datadir,"New-XRay-Data.dql")) self.d4=Data(np.column_stack([np.ones(100),np.ones(100)*2]),setas="xy") def test_functions(self): #Test section: self.s1=self.d1.section(z=(12,13)) self.assertTrue(142.710<self.d2.mean("Temp")<142.711,"Failed on the mean test.") self.assertTrue(round(self.d2.span("Temp")[0],1)==4.3 and round(self.d2.span("Temp")[1],1)==291.6,"Span test failed.") f=self.d2.split("Temp",lambda x,r:x<150) self.assertTrue(len(f[0])==838,"Split failed to work.") self.assertEqual(len(self.d3.threshold(2000,rising=True,falling=True,all_vals=True)),5,"Threshold failure.") self.d4.add(0,1,"Add") self.d4.subtract(1,0,header="Subtract") self.d4.multiply(0,1,header="Multiply") self.d4.divide(0,1,header="Divide") self.d4.diffsum(0,1,header="Diffsum") self.assertTrue(np.all(self.d4[0]==np.array([-0.5,-1,-3,3,-1,2])),"Test column ops failed.") def test_peaks(self): d=self.d3.clone d.peaks(width=8,poly=4,significance=40,modify=True) self.assertEqual(len(d),10,"Failed on peaks test.")
class ColumnOps_test(unittest.TestCase): def setUp(self): """Create a test data set.""" x = np.linspace(1, 10, 10) y = 2 * x - 3 dy = np.abs(y / 100) z = x + 4 dz = np.abs(z / 100) self.data = Data( np.column_stack((x, y, dy, z, dz)), column_headers=["Tine", "Signal 1", "d1", "Signal 2", "d2"]) def test_add(self): self.data.add(1, 3, header="Add") self.assertTrue( np.all(self.data // "Add" == (self.data // "Signal 1" + self.data // "Signal 2")), "Failed to add column correctly") self.data.add((1, 2), (3, 4), header="Add", index="Add", replace=True) d_man = np.sqrt((self.data // "d1")**2 + (self.data // "d2")**2) self.assertTrue(np.allclose(self.data // -1, d_man), "Failed to calculate error in add") self.data.add(1, 3.0, index="Add", replace=True) self.assertTrue(np.all(self.data // 5 == self.data // 1 + 3), "Failed to add with a constant") self.data.add(1, np.ones(10), index=5, replace=True) self.assertTrue(np.all(self.data // 5 == self.data // 1 + 1), "Failed to add with a array") def test_subtract(self): self.data.subtract(1, 3, header="Subtract") self.assertTrue( np.all(self.data // "Subtract" == (self.data // "Signal 1" - self.data // "Signal 2")), "Failed to add column correctly") self.data.subtract((1, 2), (3, 4), header="Subtract", index="Subtract", replace=True) d_man = np.sqrt((self.data // "d1")**2 + (self.data // "d2")**2) self.assertTrue(np.allclose(self.data // -1, d_man), "Failed to calculate error in add") self.data.subtract(1, 3.0, index="Subtract", replace=True) self.assertTrue(np.all(self.data // 5 == self.data // 1 - 3), "Failed to subtract with a constant") self.data.subtract(1, np.ones(10), index=5, replace=True) self.assertTrue(np.all(self.data // 5 == self.data // 1 - 1), "Failed to subtract with a array") def test_multiply(self): self.data.multiply(1, 3, header="Multiply") self.assertTrue( np.all((self.data // "Multiply") == ((self.data // "Signal 1") * (self.data // "Signal 2"))), "Failed to add column correctly") self.data.multiply((1, 2), (3, 4), header="Multiply", index="Multiply", replace=True) d_man = np.sqrt(2) * 0.01 * np.abs(self.data // -2) self.assertTrue(np.allclose(self.data // -1, d_man), "Failed to calculate error in add") self.data.multiply(1, 3.0, index="Multiply", replace=True) self.assertTrue(np.all(self.data // 5 == (self.data // 1) * 3), "Failed to multiply with a constant") self.data.multiply(1, 2 * np.ones(10), index=5, replace=True) self.assertTrue(np.all(self.data // 5 == (self.data // 1) * 2), "Failed to multiply with a array") def test_divide(self): self.data.divide(1, 3, header="Divide") self.assertTrue( np.all((self.data // "Divide") == ((self.data // "Signal 1") / (self.data // "Signal 2"))), "Failed to add column correctly") self.data.divide((1, 2), (3, 4), header="Divide", index="Divide", replace=True) d_man = np.sqrt(2) * 0.01 * np.abs(self.data // -2) self.assertTrue(np.allclose(self.data // -1, d_man), "Failed to calculate error in diffsum") self.data.divide(1, 3.0, index="Divide", replace=True) self.assertTrue(np.all(self.data // 5 == (self.data // 1) / 3), "Failed to add with a constant") self.data.divide(1, 2 * np.ones(10), index=5, replace=True) self.assertTrue(np.all(self.data // 5 == (self.data // 1) / 2), "Failed to add with a array") def test_diffsum(self): self.data.diffsum(1, 3, header="Diffsum") a = self.data // 1 b = self.data // 3 man = (a - b) / (a + b) self.assertTrue(np.all((self.data // "Diffsum") == man), "Failed to diffsum column correctly") self.data.diffsum((1, 2), (3, 4), header="Diffsum", index="Diffsum", replace=True) def test_limits(self): self.data.setas = "x..ye" self.assertEqual(self.data.min(1), (-1, 0), "Minimum method failed") self.assertEqual(self.data.min(), (5.0, 0), "Minimum method failed") self.assertEqual(self.data.min(1, bounds=lambda r: r[0] > 2), (3.0, 2.0), "Max with bounds failed") self.assertEqual(self.data.max(), (14, 9), "Max method failed") self.assertEqual(self.data.max(1), (17, 9), "Max method failed") self.assertEqual(self.data.max(1, bounds=lambda r: r[0] < 5), (5.0, 3.0), "Max with bounds failed") self.assertEqual(self.data.span(), (5.0, 14.0), "span method failed") self.assertEqual(self.data.span(1), (-1.0, 17.0), "span method failed") self.assertEqual(self.data.span(1, bounds=lambda r: 2 < r.i < 8), (5.0, 13.0), "span with bounds failed") def test_stats(self): self.assertEqual(self.data.mean(1), 8.0, "Simple channel mean failed") self.assertTrue( np.allclose(self.data.mean(1, sigma=2), (0.048990998729652346, 0.031144823004794875)), "Channel mean with sigma failed") self.assertAlmostEqual(self.data.std(1), 6.0553007081949835, msg="Simple Standard Deviation failed") self.assertAlmostEqual(self.data.std(1, 2), 2.7067331877422456, msg="Simple Standard Deviation failed")
class Analysis_test(unittest.TestCase): """Path to sample Data File""" datadir=path.join(pth,"sample-data") def setUp(self): self.d1=Data(path.join(self.datadir,"OVF1.ovf")) self.d2=Data(path.join(self.datadir,"TDI_Format_RT.txt")) self.d3=Data(path.join(self.datadir,"New-XRay-Data.dql")) self.d4=Data(np.column_stack([np.ones(100),np.ones(100)*2]),setas="xy") def test_functions(self): #Test section: self.s1=self.d1.section(z=(12,13)) self.assertTrue(142.710<self.d2.mean("Temp")<142.711,"Failed on the mean test.") self.assertTrue(round(self.d2.span("Temp")[0],1)==4.3 and round(self.d2.span("Temp")[1],1)==291.6,"Span test failed.") f=self.d2.split(lambda r:r["Temp"]<150) self.assertTrue(len(f[0])==838,"Split failed to work.") self.assertEqual(len(self.d3.threshold(2000,rising=True,falling=True,all_vals=True)),5,"Threshold failure.") self.d4.add(0,1,"Add") self.d4.subtract(1,0,header="Subtract") self.d4.multiply(0,1,header="Multiply") self.d4.divide(0,1,header="Divide") self.d4.diffsum(0,1,header="Diffsum") self.assertTrue(np.all(self.d4[0]==np.array([-0.5,-1,-3,3,-1,2])),"Test column ops failed.") d=Data(np.zeros((100,1))) d.add(0,1.0) self.assertEqual(np.sum(d[:,0]),100.,"Add with a flot didn't work") d.add(0,np.ones(100)) self.assertEqual(np.sum(d[:,0]),200.,"Add with an array failed.") def test_peaks(self): d=self.d3.clone d.peaks(width=8,poly=4,significance=100,modify=True) self.assertEqual(len(d),11,"Failed on peaks test.") def test_threshold(self): #set up some zigzag data #mins at 0,100,200,300,400, max at 50, 150, 250, 350 and zeros in between ar = np.zeros((400,2)) ar[:,0]=np.arange(0,len(ar)) for i in range(4): ar[i*100:i*100+50,1] = np.linspace(-1,1,50) for i in range(4): ar[i*100+50:i*100+100,1] = np.linspace(1,-1,50) d = Data(ar, setas='xy') self.assertTrue(len(d.threshold(0,rising=True,falling=False,all_vals=True)==4)) self.assertTrue(len(d.threshold(0,rising=False,falling=True,all_vals=True)==4)) self.assertTrue(len(d.threshold(0,interpolate=False,rising=False,falling=True,all_vals=True)==4)) self.assertTrue(d.threshold(0,all_vals=True)[1]==124.5) self.thresh=d self.assertTrue(np.sum(d.threshold([0.0,0.5,1.0])-np.array([[24.5,36.74999999, 49.]]))<1E-6,"Multiple threshold failed.") self.assertAlmostEqual(d.threshold(0,interpolate=False,all_vals=True)[1],124.5,6,"Threshold without interpolation failed.") result=d.threshold(0,interpolate=False,all_vals=True,xcol=False) self.assertTrue(np.allclose(result,np.array([[ 24.5, 0. ],[124.5, 0. ],[224.5, 0. ],[324.5, 0. ]])), "Failed threshold with False scol - result was {}".format(result)) def test_apply(self): self.app=Data(np.zeros((100,1)),setas="y") self.app.apply(lambda r:r.i[0],header="Counter") def calc(r,omega=1.0,k=1.0): return np.sin(r.y*omega) self.app.apply(calc,replace=False,header="Sin",_extra={"omega":0.1},k=1.0) self.app.apply(lambda r:r.__class__([r[1],r[0]]),replace=True,header=["Index","Sin"]) self.app.setas="xy" self.assertAlmostEqual(self.app.integrate(),-64.1722191259037,msg="Integrate after aplies failed.")
filename = '../sample-data/6221-Lockin-DAQ Temperature Control !0001.txt' t_col=": T2" # Temperature column label r_cols=("Sample 4::R","Sample 7::R") #Resistance Column Labels iterator="iterator" #Temperature ramp iteration column label threshold=0.85 #Fraction of transition to fit to data=Data(filename) #Use FALSE to get a dialog box to the file containing Tc data #Define my working x and y axes #Split one file into a folder of two files by the iterator column fldr=data.split(iterator) result=Data() for data in fldr: #For each iteration ramp in the Tc data row=[data.mean(iterator)] data.figure(figsize=(8,4)) for i,r_col in enumerate(r_cols): data.setas(x=t_col,y=r_col) data.del_rows(isnan(data.y)) #Normalise data on y axis between +/- 1 data.normalise(base=(-1.,1.), replace=True) #Swap x and y axes around so that R is x and T is y data=~data #Curve fit a straight line, using only the central 90% of the resistance transition data.curve_fit(linear,bounds=lambda x,r:-threshold<x<threshold,result=True,p0=[7.0,0.0]) #result=True to record fit into metadata #Plot the results
class Analysis_test(unittest.TestCase): """Path to sample Data File""" datadir = path.join(pth, "sample-data") def setUp(self): self.d1 = Data(path.join(self.datadir, "OVF1.ovf")) self.d2 = Data(path.join(self.datadir, "TDI_Format_RT.txt")) self.d3 = Data(path.join(self.datadir, "New-XRay-Data.dql")) self.d4 = Data(np.column_stack([np.ones(100), np.ones(100) * 2]), setas="xy") def test_functions(self): #Test section: self.s1 = self.d1.section(z=(12, 13)) self.assertTrue(142.710 < self.d2.mean("Temp") < 142.711, "Failed on the mean test.") self.assertTrue( round(self.d2.span("Temp")[0], 1) == 4.3 and round(self.d2.span("Temp")[1], 1) == 291.6, "Span test failed.") f = self.d2.split(lambda r: r["Temp"] < 150) self.assertTrue(len(f[0]) == 838, "Split failed to work.") self.assertEqual( len( self.d3.threshold(2000, rising=True, falling=True, all_vals=True)), 5, "Threshold failure.") self.d4.add(0, 1, "Add") self.d4.subtract(1, 0, header="Subtract") self.d4.multiply(0, 1, header="Multiply") self.d4.divide(0, 1, header="Divide") self.d4.diffsum(0, 1, header="Diffsum") self.assertTrue( np.all(self.d4[0] == np.array([-0.5, -1, -3, 3, -1, 2])), "Test column ops failed.") d = Data(np.zeros((100, 1))) d.add(0, 1.0) self.assertEqual(np.sum(d[:, 0]), 100., "Add with a flot didn't work") d.add(0, np.ones(100)) self.assertEqual(np.sum(d[:, 0]), 200., "Add with an array failed.") def test_peaks(self): d = self.d3.clone d.peaks(width=8, poly=4, significance=100, modify=True) self.assertEqual(len(d), 11, "Failed on peaks test.") def test_threshold(self): #set up some zigzag data #mins at 0,100,200,300,400, max at 50, 150, 250, 350 and zeros in between ar = np.zeros((400, 2)) ar[:, 0] = np.arange(0, len(ar)) for i in range(4): ar[i * 100:i * 100 + 50, 1] = np.linspace(-1, 1, 50) for i in range(4): ar[i * 100 + 50:i * 100 + 100, 1] = np.linspace(1, -1, 50) d = Data(ar, setas='xy') self.assertTrue( len( d.threshold(0, rising=True, falling=False, all_vals=True) == 4)) self.assertTrue( len( d.threshold(0, rising=False, falling=True, all_vals=True) == 4)) self.assertTrue( len( d.threshold(0, interpolate=False, rising=False, falling=True, all_vals=True) == 4)) self.assertTrue(d.threshold(0, all_vals=True)[1] == 124.5) self.thresh = d self.assertTrue( np.sum( d.threshold([0.0, 0.5, 1.0]) - np.array([[24.5, 36.74999999, 49.]])) < 1E-6, "Multiple threshold failed.") self.assertAlmostEqual( d.threshold(0, interpolate=False, all_vals=True)[1], 124.5, 6, "Threshold without interpolation failed.") result = d.threshold(0, interpolate=False, all_vals=True, xcol=False) self.assertTrue( np.allclose( result, np.array([[24.5, 0.], [124.5, 0.], [224.5, 0.], [324.5, 0.]])), "Failed threshold with False scol - result was {}".format(result)) def test_apply(self): self.app = Data(np.zeros((100, 1)), setas="y") self.app.apply(lambda r: r.i[0], header="Counter") def calc(r, omega=1.0, k=1.0): return np.sin(r.y * omega) self.app.apply(calc, replace=False, header="Sin", _extra={"omega": 0.1}, k=1.0) self.app.apply(lambda r: r.__class__([r[1], r[0]]), replace=True, header=["Index", "Sin"]) self.app.setas = "xy" self.assertAlmostEqual(self.app.integrate(), -64.1722191259037, msg="Integrate after aplies failed.")
class Analysis_test(unittest.TestCase): """Path to sample Data File""" datadir = path.join(pth, "sample-data") def setUp(self): self.d1 = Data(path.join(self.datadir, "OVF1.ovf")) self.d2 = Data(path.join(self.datadir, "TDI_Format_RT.txt")) self.d3 = Data(path.join(self.datadir, "New-XRay-Data.dql")) self.d4 = Data(np.column_stack([np.ones(100), np.ones(100) * 2]), setas="xy") def test_functions(self): #Test section: self.s1 = self.d1.section(z=(12, 13)) self.assertTrue(142.710 < self.d2.mean("Temp") < 142.711, "Failed on the mean test.") self.assertTrue( round(self.d2.span("Temp")[0], 1) == 4.3 and round(self.d2.span("Temp")[1], 1) == 291.6, "Span test failed.") f = self.d2.split(lambda r: r["Temp"] < 150) self.assertTrue(len(f[0]) == 838, "Split failed to work.") self.assertEqual( len( self.d3.threshold(2000, rising=True, falling=True, all_vals=True)), 5, "Threshold failure.") self.d4.add(0, 1, "Add") self.d4.subtract(1, 0, header="Subtract") self.d4.multiply(0, 1, header="Multiply") self.d4.divide(0, 1, header="Divide") self.d4.diffsum(0, 1, header="Diffsum") self.assertTrue( np.all(self.d4[0] == np.array([-0.5, -1, -3, 3, -1, 2])), "Test column ops failed.") def test_peaks(self): d = self.d3.clone d.peaks(width=8, poly=4, significance=40, modify=True) self.assertEqual(len(d), 10, "Failed on peaks test.") def test_threshold(self): #set up some zigzag data #mins at 0,100,200,300,400, max at 50, 150, 250, 350 and zeros in between ar = np.zeros((400, 2)) ar[:, 0] = np.arange(0, len(ar)) for i in range(4): ar[i * 100:i * 100 + 50, 1] = np.linspace(-1, 1, 50) for i in range(4): ar[i * 100 + 50:i * 100 + 100, 1] = np.linspace(1, -1, 50) d = Data(ar, setas='xy') self.assertTrue( len( d.threshold(0, rising=True, falling=False, all_vals=True) == 4)) self.assertTrue( len( d.threshold(0, rising=False, falling=True, all_vals=True) == 4)) self.assertTrue( len( d.threshold(0, interpolate=False, rising=False, falling=True, all_vals=True) == 4)) self.assertTrue(d.threshold(0, all_vals=True)[1] == 124.5)
class Analysis_test(unittest.TestCase): """Path to sample Data File""" datadir=path.join(pth,"sample-data") def setUp(self): self.d1=Data(path.join(self.datadir,"OVF1.ovf")) self.d2=Data(path.join(self.datadir,"TDI_Format_RT.txt")) self.d3=Data(path.join(self.datadir,"New-XRay-Data.dql")) self.d4=Data(np.column_stack([np.ones(100),np.ones(100)*2]),setas="xy") def test_functions(self): #Test section: self.s1=self.d1.section(z=(12,13)) self.assertTrue(142.710<self.d2.mean("Temp")<142.711,"Failed on the mean test.") self.assertTrue(round(self.d2.span("Temp")[0],1)==4.3 and round(self.d2.span("Temp")[1],1)==291.6,"Span test failed.") f=self.d2.split(lambda r:r["Temp"]<150) self.assertTrue(len(f[0])==838,"Split failed to work.") self.assertEqual(len(self.d3.threshold(2000,rising=True,falling=True,all_vals=True)),5,"Threshold failure.") self.d4.add(0,1,"Add") self.d4.subtract(1,0,header="Subtract") self.d4.multiply(0,1,header="Multiply") self.d4.divide(0,1,header="Divide") self.d4.diffsum(0,1,header="Diffsum") self.assertTrue(np.all(self.d4[0]==np.array([-0.5,-1,-3,3,-1,2])),"Test column ops failed.") d=Data(np.zeros((100,1))) d.add(0,1.0) self.assertEqual(np.sum(d[:,0]),100.,"Add with a flot didn't work") d.add(0,np.ones(100)) self.assertEqual(np.sum(d[:,0]),200.,"Add with an array failed.") def test_peaks(self): d=self.d3.clone d.peaks(width=8,poly=4,significance=100,modify=True) self.assertEqual(len(d),11,"Failed on peaks test.") def test_threshold(self): #set up some zigzag data #mins at 0,100,200,300,400, max at 50, 150, 250, 350 and zeros in between ar = np.zeros((400,2)) ar[:,0]=np.arange(0,len(ar)) for i in range(4): ar[i*100:i*100+50,1] = np.linspace(-1,1,50) for i in range(4): ar[i*100+50:i*100+100,1] = np.linspace(1,-1,50) d = Data(ar, setas='xy') self.assertTrue(len(d.threshold(0,rising=True,falling=False,all_vals=True)==4)) self.assertTrue(len(d.threshold(0,rising=False,falling=True,all_vals=True)==4)) self.assertTrue(len(d.threshold(0,interpolate=False,rising=False,falling=True,all_vals=True)==4)) self.assertTrue(d.threshold(0,all_vals=True)[1]==124.5) self.thresh=d self.assertTrue(np.sum(d.threshold([0.0,0.5,1.0])-np.array([[24.5,36.74999999, 49.]]))<1E-6,"Multiple threshold failed.") self.assertAlmostEqual(d.threshold(0,interpolate=False,all_vals=True)[1],124.5,6,"Threshold without interpolation failed.") result=d.threshold(0,interpolate=False,all_vals=True,xcol=False) self.assertTrue(np.allclose(result,np.array([[ 24.5, 0. ],[124.5, 0. ],[224.5, 0. ],[324.5, 0. ]])), "Failed threshold with False scol - result was {}".format(result)) def test_apply(self): self.app=Data(np.zeros((100,1)),setas="y") self.app.apply(lambda r:r.i[0],header="Counter") def calc(r,omega=1.0,k=1.0): return np.sin(r.y*omega) self.app.apply(calc,replace=False,header="Sin",_extra={"omega":0.1},k=1.0) self.app.apply(lambda r:r.__class__([r[1],r[0]]),replace=True,header=["Index","Sin"]) self.app.setas="xy" self.assertAlmostEqual(self.app.integrate(output="result"),18.87616564214,msg="Integrate after aplies failed.") def test_scale(self): x=np.linspace(-5,5,101) y=np.sin(x) orig=Data(x+np.random.normal(size=101,scale=0.025),y+np.random.normal(size=101,scale=0.01)) orig.setas="xy" XTests=[[(0,0,0.5),(0,2,-0.1)], [(0,0,0.5)], [(0,2,-0.2)]] YTests=[[(1,1,0.5),(1,2,-0.1)], [(1,1,0.5)], [(1,2,-0.2)]] for xmode,xdata,xtests in zip(["linear","scale","offset"],[x*2+0.2,x*2,x+0.2],XTests): for ymode,ydata,ytests in zip(["linear","scale","offset"],[y*2+0.2,y*2,y+0.2],YTests): to_scale=Data(xdata+np.random.normal(size=101,scale=0.025),ydata+np.random.normal(size=101,scale=0.01)) to_scale.setas="xy" to_scale.scale(orig,xmode=xmode,ymode=ymode) transform=to_scale["Transform"] t_err=to_scale["Transform Err"] for i,j,v in xtests+ytests: self.assertLessEqual(np.abs(transform[i,j]-v),5*t_err[i,j],"Failed to get correct trandorm factor for {}:{} ({} vs {}".format(xmode,ymode,transform[i,j],v)) to_scale=Data(x*5+0.1+np.random.normal(size=101,scale=0.025),y*0.5+0.1+0.5*x+np.random.normal(size=101,scale=0.01)) to_scale.setas="xy" to_scale.scale(orig,xmode="affine") a_tranform=np.array([[0.2,0.,-0.02],[-0.2, 2.,-0.17]]) t_delta=np.abs(to_scale["Transform"]-a_tranform) t_in_range=t_delta<to_scale["Transform Err"]*5 self.assertTrue(np.all(t_in_range),"Failed to produce correct affine scaling {} vs {}".format(to_scale["Transform"],a_tranform)) def test_clip(self): x=np.linspace(0,np.pi*10,1001) y=np.sin(x) z=np.cos(x) d=Data(x,y,z,setas="xyz") d.clip((-0.1,0.2),"Column 2") self.assertTrue((d.z.min()>=-0.1) and (d.z.max()<=0.2),"Clip with a column specified failed.") d=Data(x,y,z,setas="xyz") d.clip((-0.5,0.7)) self.assertTrue((d.y.min()>=-0.5) and (d.y.max()<=0.7),"Clip with no column specified failed.") def test_integrate(self): d=Data(path.join(self.datadir,"SLD_200919.dat")) d.setas="x..y" d.integrate(result=True,header="Total_M") result=d["Total_M"] self.assertAlmostEqual(result,4.19687459365,7,"Integrate returned the wrong result!") d.setas[-1]="y" d.plot(multiple="y2") self.assertEqual(len(d.axes),2,"Failed to produce plot with double y-axis") d.close("all") d.setas="x..y" fx=d.interpolate(None) self.assertEqual(fx(np.linspace(1,1500,101)).shape,(101,7),"Failed to get the interpolated shape right") def test_sg_filter(self): x=np.linspace(0,10*np.pi,1001) y=np.sin(x)+np.random.normal(size=1001,scale=0.05) d=Data(x,y,column_headers=["Time","Signal"],setas="xy") d.SG_Filter(order=1,result=True) d.setas="x.y" d.y=d.y-np.cos(x) self.assertAlmostEqual(d.y[5:-5].mean(), 0,places=2,msg="Failed to differentiate correctly")