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 gen_func(*args, **kwargs): r = workingfunc(self.clone, *args, **kwargs) #send copy of self as the first arg if isinstance(r, Data): pass #Data return is ok elif isinstance(r, np.ndarray) and np.prod(r.shape) == np.max( r.shape): #1D Array r = Data(r) r.metadata = self.metadata.copy() r.column_headers[0] = workingfunc.__name__ elif isinstance(r, np.ndarray) and not isinstance( r, ImageArray): #make sure we return a ImageArray r = r.view(type=ImageArray) r.metadata = self.metadata.copy() #NB we might not be returning an ndarray at all here ! return r
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 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
def profile_line(img, src, dst, linewidth=1, order=1, mode='constant', cval=0.0): """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" Returns: A :py:class:`Stoner.Data` object containing the line profile data and the metadata from the image. """ scale = img.get("MicronsPerPixel", 1.0) if isinstance(src[0], float): src = (int(src[0] / scale), int(src[1] / scale)) if isinstance(dst[0], float): dst = (int(dst[0] / scale), int(dst[1] / scale)) 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
def test_constructor(self): """Constructor Tests""" d=Data() self.assertTrue(d.shape==(1,0),"Bare constructor failed") d=Data(self.d) self.assertTrue(np.all(d.data==self.d.data),"Constructor from DataFile failed") d=Data([np.ones(100),np.zeros(100)]) self.assertTrue(d.shape==(100,2),"Constructor from iterable list of nd array failed") d=Data([np.ones(100),np.zeros(100)],["X","Y"]) self.assertTrue(d.column_headers==["X","Y"],"Failed to set column headers in constructor: {}".format(d.column_headers)) c=np.zeros(100) d=Data({"X-Data":c,"Y-Data":c,"Z-Data":c}) self.assertEqual(d.shape,(100,3),"Construction from dictionary of columns failed.") d=Data(False) self.assertEqual(self.d,d,"Faked file-dialog test") d=self.d.clone df=d.to_pandas() e=Data(df) self.assertEqual(d,e,"Roundtripping through Pandas DataFrame failed.") d=self.d.clone e=Data(d.dict_records) e.metadata=d.metadata self.assertEqual(d,e,"Copy from dict records failed.")