本文整理汇总了Python中Stoner.Data类的典型用法代码示例。如果您正苦于以下问题:Python Data类的具体用法?Python Data怎么用?Python Data使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Data类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Fit
def Fit(self):
"""Run the fitting code."""
self.Discard().Normalise().offset_correct()
chi2= self.p0.shape[0]>1
method=getattr(self,self.method)
if not chi2: # Single fit mode, consider whether to plot and save etc
fit=method(self.model,p0=self.p0,result=True,header="Fit",output="report")
if self.show_plot:
self.plot_results()
if self.save_fit:
self.save(False)
if self.report:
print(fit.fit_report())
return fit
else: #chi^2 mapping mode
d=Data(self)
fit = d.lmfit(self.model, p0=self.p0, result=True, header="Fit", output="data")
if self.show_plot:
fit.plot(multiple="panels",capsize=3)
fit.yscale = "log" # Adjust y scale for chi^2
fit.tight_layout()
if self.save_fit:
fit.filename=None
fit.save(False)
示例2: hist
def hist(im, *args, **kargs):
"""Pass through to :py:func:`matplotlib.pyplot.hist` function."""
counts, edges = np.histogram(im.ravel(), *args, **kargs)
centres = (edges[1:] + edges[:-1]) / 2
new = Data(np.column_stack((centres, counts)))
new.column_headers = ["Intensity", "Frequency"]
new.setas = "xy"
return new
示例3: test_grouping
def test_grouping(self):
fldr4=SF.DataFolder()
x=np.linspace(-np.pi,np.pi,181)
for phase in np.linspace(0,1.0,5):
for amplitude in np.linspace(1,2,6):
for frequency in np.linspace(1,2,5):
y=amplitude*np.sin(frequency*x+phase*np.pi)
d=Data(x,y,setas="xy",column_headers=["X","Y"])
d["frequency"]=frequency
d["amplitude"]=amplitude
d["phase"]=phase
d["params"]=[phase,frequency,amplitude]
d.filename="test/{amplitude}/{phase}/{frequency}.dat".format(**d)
fldr4+=d
fldr4.unflatten()
self.assertEqual(fldr4.mindepth,3,"Unflattened DataFolder had wrong mindepth.")
self.assertEqual(fldr4.shape, (~~fldr4).shape,"Datafodler changed shape on flatten/unflatten")
fldr5=fldr4.select(amplitude=1.4,recurse=True)
fldr5.prune()
pruned=(0,
{'test': (0,
{'1.4': (0,
{'0.0': (5, {}),
'0.25': (5, {}),
'0.5': (5, {}),
'0.75': (5, {}),
'1.0': (5, {})})})})
selected=(0,
{'test': (0,
{'1.4': (0,
{'0.25': (1, {}), '0.5': (1, {}), '0.75': (1, {}), '1.0': (1, {})})})})
self.assertEqual(fldr5.shape,pruned,"Folder pruning gave an unxpected shape.")
self.assertEqual(fldr5[("test","1.4","0.5",0,"phase")],0.5,"Multilevel indexing of tree failed.")
shape=(~(~fldr4).select(amplitude=1.4).select(frequency=1).select(phase__gt=0.2)).shape
self.fldr4=fldr4
self.assertEqual(shape, selected,"Multi selects and inverts failed.")
g=(~fldr4)/10
self.assertEqual(g.shape,(0,{'Group 0': (15, {}),'Group 1': (15, {}),'Group 2': (15, {}),'Group 3': (15, {}),'Group 4': (15, {}),
'Group 5': (15, {}),'Group 6': (15, {}),'Group 7': (15, {}),'Group 8': (15, {}),'Group 9': (15, {})}),"Dive by int failed.")
g["Group 6"]-=5
self.assertEqual(g.shape,(0,{'Group 0': (15, {}),'Group 1': (15, {}),'Group 2': (15, {}),'Group 3': (15, {}),'Group 4': (15, {}),
'Group 5': (15, {}),'Group 6': (14, {}),'Group 7': (15, {}),'Group 8': (15, {}),'Group 9': (15, {})}),"Sub by int failed.")
remove=g["Group 3"][4]
g["Group 3"]-=remove
self.assertEqual(g.shape,(0,{'Group 0': (15, {}),'Group 1': (15, {}),'Group 2': (15, {}),'Group 3': (14, {}),'Group 4': (15, {}),
'Group 5': (15, {}),'Group 6': (14, {}),'Group 7': (15, {}),'Group 8': (15, {}),'Group 9': (15, {})}),"Sub by object failed.")
d=fldr4["test",1.0,1.0].gather(0,1)
self.assertEqual(d.shape,(181,6),"Gather seems have failed.")
self.assertTrue(np.all(fldr4["test",1.0,1.0].slice_metadata("phase")==
np.ones(5)),"Slice metadata failure.")
d=(~fldr4).extract("phase","frequency","amplitude","params")
self.assertEqual(d.shape,(150,6),"Extract failed to produce data of correct shape.")
self.assertEqual(d.column_headers,['phase', 'frequency', 'amplitude', 'params', 'params', 'params'],"Exctract failed to get correct column headers.")
p=fldr4["test",1.0,1.0]
p=SF.PlotFolder(p)
p.plot()
self.assertEqual(len(plt.get_fignums()),1,"Failed to generate a single plot for PlotFolder.")
plt.close("all")
示例4: test_apply
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.")
示例5: test_saving
def test_saving(self):
fldr4=SF.DataFolder()
x=np.linspace(-np.pi,np.pi,181)
for phase in np.linspace(0,1.0,5):
for amplitude in np.linspace(1,2,6):
for frequency in np.linspace(1,2,5):
y=amplitude*np.sin(frequency*x+phase*np.pi)
d=Data(x,y,setas="xy",column_headers=["X","Y"])
d["frequency"]=frequency
d["amplitude"]=amplitude
d["phase"]=phase
d["params"]=[phase,frequency,amplitude]
d.filename="test/{amplitude}/{phase}/{frequency}.dat".format(**d)
fldr4+=d
fldr4.unflatten()
newdir=tempfile.mkdtemp()
fldr4.save(newdir)
fldr5=SF.DataFolder(newdir)
self.assertEqual(fldr4.shape,fldr5.shape,"Saved DataFolder and loaded DataFolder have different shapes")
示例6: test_functions
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.")
示例7: LoadData
def LoadData(self, data_item_number, filename):
"""LoadData(self, data_item_number, filename) --> none
Loads the data from filename into the data_item_number.
"""
try:
datafile=Data(str(filename),debug=True) # does all the hard work here
except Exception as e:
ShowWarningDialog(self.parent, 'Could not load the file: ' +\
filename + ' \nPlease check the format.\n\n Stoner.Data'\
+ ' gave the following error:\n' + str(e))
else:
# For the freak case of only one data point
try:
if datafile.setas.cols["axes"]==0:
self.x_col=datafile.find_col(self.x_col)
self.y_col=datafile.find_col(self.y_col)
self.e_col=datafile.find_col(self.e_col)
datafile.etsas(x=self.x_col,y=self.y_col,e=self.e_col)
else:
self.x_col=datafile.setas.cols["xcol"]
self.y_col=datafile.setas.cols["ycol"][0]
if len(datafile.setas.cols["yerr"])>0:
self.e_col=datafile.setas.cols["yerr"][0]
else:
datafile.add_column(np.ones(len(datafile)))
datafile.setas[-1]="e"
except Exception as e:
ShowWarningDialog(self.parent, 'The data file does not contain'\
+ 'all the columns specified in the opions\n'+e.message)
# Okay now we have showed a dialog lets bail out ...
return
# The data is set by the default Template.__init__ function, neat hu
# Know the loaded data goes into *_raw so that they are not
# changed by the transforms
datafile.y=np.where(datafile.y==0.0,1E-8,datafile.y)
self.data[data_item_number].x_raw = datafile.x
self.data[data_item_number].y_raw = datafile.y
self.data[data_item_number].error_raw = datafile.e
# Run the commands on the data - this also sets the x,y, error memebers
# of that data item.
self.data[data_item_number].run_command()
# Send an update that new data has been loaded
self.SendUpdateDataEvent()
示例8: collate
def collate(grp,trail,**kargs):
"""Gather all the data up again."""
grp.sort()
final=Data()
final.add_column(grp[0].column('Energy'),'Energy')
for g in grp:
final.add_column(g.column('Asym'),g.title)
if "group_key" in kargs:
final[kargs["group_key"]]=grp.key
final["path"]=trail
if "save" in kargs and kargs["save"]:
final.save(kargs["filename"])
return final
示例9: profile_line
def profile_line(img, src=None, dst=None, linewidth=1, order=1, mode="constant", cval=0.0, constrain=True, **kargs):
"""Wrapper for sckit-image method of the same name to get a line_profile.
Parameters:
img(ImageArray): Image data to take line section of
src, dst (2-tuple of int or float): start and end of line profile. If the co-ordinates
are given as intergers then they are assumed to be pxiel co-ordinates, floats are
assumed to be real-space co-ordinates using the embedded metadata.
linewidth (int): the wideth of the profile to be taken.
order (int 1-3): Order of interpolation used to find image data when not aligned to a point
mode (str): How to handle data outside of the image.
cval (float): The constant value to assume for data outside of the image is mode is "constant"
constrain (bool): Ensure the src and dst are within the image (default True).
Returns:
A :py:class:`Stoner.Data` object containing the line profile data and the metadata from the image.
"""
scale = img.get("MicronsPerPixel", 1.0)
r, c = img.shape
if src is None and dst is None:
if "x" in kargs:
src = (kargs["x"], 0)
dst = (kargs["x"], r)
if "y" in kargs:
src = (0, kargs["y"])
dst = (c, kargs["y"])
if isinstance(src, float):
src = (src, src)
if isinstance(dst, float):
dst = (dst, dst)
dst = _scale(dst, scale)
src = _scale(src, scale)
if not istuple(src, int, int):
raise ValueError("src co-ordinates are not a 2-tuple of ints.")
if not istuple(dst, int, int):
raise ValueError("dst co-ordinates are not a 2-tuple of ints.")
if constrain:
fix = lambda x, mx: int(round(sorted([0, x, mx])[1]))
r, c = img.shape
src = list(src)
src = (fix(src[0], r), fix(src[1], c))
dst = (fix(dst[0], r), fix(dst[1], c))
result = measure.profile_line(img, src, dst, linewidth, order, mode, cval)
points = measure.profile._line_profile_coordinates(src, dst, linewidth)[:, :, 0]
ret = Data()
ret.data = points.T
ret.setas = "xy"
ret &= np.sqrt(ret.x ** 2 + ret.y ** 2) * scale
ret &= result
ret.column_headers = ["X", "Y", "Distance", "Intensity"]
ret.setas = "..xy"
ret.metadata = img.metadata.copy()
return ret
示例10: Plottest
class Plottest(unittest.TestCase):
"""Path to sample Data File"""
datadir=path.join(pth,"sample-data")
def setUp(self):
self.d=Data(path.join(__home__,"..","sample-data","New-XRay-Data.dql"))
def test_set_no_figs(self):
self.assertTrue(Options.no_figs,"Default setting for no_figs option is incorrect.")
Options.no_figs=True
e=self.d.clone
ret=e.plot()
self.assertTrue(ret is None,"Output of Data.plot() was not None when no_figs is True and showfig is not set({})".format(type(ret)))
Options.no_figs=False
e.showfig=False
ret=e.plot()
self.assertTrue(isinstance(ret,Data),"Return value of Data.plot() was not self when Data.showfig=False ({})".format(type(ret)))
e.showfig=True
ret=e.plot()
self.assertTrue(isinstance(ret,Figure),"Return value of Data.plot() was not Figure when Data.showfig=False({})".format(type(ret)))
e.showfig=None
ret=e.plot()
self.assertTrue(ret is None,"Output of Data.plot() was not None when Data.showfig is None ({})".format(type(ret)))
Options.no_figs=True
self.assertTrue(Options.no_figs,"set_option no_figs failed.")
self.d=Data(path.join(__home__,"..","sample-data","New-XRay-Data.dql"))
self.d.showfig=False
ret=self.d.plot()
self.assertTrue(ret is None,"Output of Data.plot() was not None when no_figs is True ({})".format(type(ret)))
Options.no_figs=True
plt.close("all")
def test_template_settings(self):
template=DefaultPlotStyle(font__weight="bold")
self.assertEqual(template["font.weight"],"bold","Setting ytemplate parameter in init failed.")
template(font__weight="normal")
self.assertEqual(template["font.weight"],"normal","Setting ytemplate parameter in call failed.")
template["font.weight"]="bold"
self.assertEqual(template["font.weight"],"bold","Setting ytemplate parameter in setitem failed.")
del template["font.weight"]
self.assertEqual(template["font.weight"],"normal","Resettting template parameter failed.")
keys=sorted([x for x in template])
self.assertEqual(sorted(template.keys()),keys,"template.keys() and template.iter() disagree.")
attrs=[x for x in dir(template) if template._allowed_attr(x)]
length=len(dict(plt.rcParams))+len(attrs)
self.assertEqual(len(template),length,"templa length wrong.")
示例11: test_threshold
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))
示例12: test_set_no_figs
def test_set_no_figs(self):
self.assertTrue(Options.no_figs,"Default setting for no_figs option is incorrect.")
Options.no_figs=True
e=self.d.clone
ret=e.plot()
self.assertTrue(ret is None,"Output of Data.plot() was not None when no_figs is True and showfig is not set({})".format(type(ret)))
Options.no_figs=False
e.showfig=False
ret=e.plot()
self.assertTrue(isinstance(ret,Data),"Return value of Data.plot() was not self when Data.showfig=False ({})".format(type(ret)))
e.showfig=True
ret=e.plot()
self.assertTrue(isinstance(ret,Figure),"Return value of Data.plot() was not Figure when Data.showfig=False({})".format(type(ret)))
e.showfig=None
ret=e.plot()
self.assertTrue(ret is None,"Output of Data.plot() was not None when Data.showfig is None ({})".format(type(ret)))
Options.no_figs=True
self.assertTrue(Options.no_figs,"set_option no_figs failed.")
self.d=Data(path.join(__home__,"..","sample-data","New-XRay-Data.dql"))
self.d.showfig=False
ret=self.d.plot()
self.assertTrue(ret is None,"Output of Data.plot() was not None when no_figs is True ({})".format(type(ret)))
Options.no_figs=True
plt.close("all")
示例13: Data
"""Double y axis plot."""
from Stoner import Data
p = Data("sample.txt", setas="xyy")
# Quick plot
p.plot(multiple="y2")
示例14: Analysis_test
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.")
示例15: setUp
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")