def loadImageAsGreyScale(self, subscriber=0): im = PIL.Image.open(self.paramFilename.value) if im.mode == "I;16": im = im.convert("I") data = scipy.misc.fromimage(im).astype("int16") else: data = scipy.misc.fromimage(im, flatten=True) Ny, Nx = data.shape xUnit = parseFCUnit(self.paramXScale.value) xAxis = DataContainer.FieldContainer( scipy.linspace(0.0, self._getScalar(xUnit), Nx, True), xUnit / self._getScalar(xUnit), longname="x-coordinate", shortname="x", ) if self.paramYScale.value == "link2X": yUnit = xUnit * float(Ny) / Nx else: yUnit = parseFCUnit(self.paramYScale.value) yAxis = DataContainer.FieldContainer( scipy.linspace(0.0, self._getScalar(yUnit), Ny, True), yUnit / self._getScalar(yUnit), longname="y-coordinate", shortname="y", ) fieldUnit = parseFCUnit(self.paramFieldUnit.value) result = DataContainer.FieldContainer( data, fieldUnit, longname="Image", shortname="I", dimensions=[yAxis, xAxis] ) result.seal() return result
def loadImageAsGreyScale(self, subscriber=0): im = PIL.Image.open(self.paramFilename.value) if im.mode == "I;16": im = im.convert("I") data = scipy.misc.fromimage(im).astype("int16") else: data = scipy.misc.fromimage(im, flatten=True) Ny, Nx = data.shape xUnit = parseFCUnit(self.paramXScale.value) xAxis = DataContainer.FieldContainer( scipy.linspace(0.0, self._getScalar(xUnit), Nx, True), xUnit / self._getScalar(xUnit), longname='x-coordinate', shortname='x' ) if self.paramYScale.value == 'link2X': yUnit = xUnit * float(Ny) / Nx else: yUnit = parseFCUnit(self.paramYScale.value) yAxis = DataContainer.FieldContainer( scipy.linspace(0.0, self._getScalar(yUnit), Ny, True), yUnit / self._getScalar(yUnit), longname='y-coordinate', shortname='y' ) fieldUnit = parseFCUnit(self.paramFieldUnit.value) result = DataContainer.FieldContainer( data, fieldUnit, longname="Image", shortname="I", dimensions=[yAxis, xAxis] ) result.seal() return result
def loadImageAsGreyScale(self, subscriber=0): import os import re from scipy.misc import imread import numpy from pyphant.core.DataContainer import FieldContainer path = os.path.realpath(self.paramPath.value) if os.path.isfile(path): path = os.path.dirname(path) pattern = re.compile(self.paramRegex.value) filenames = filter(lambda x: pattern.match(x) is not None, os.listdir(path)) filenames.sort() filenames = [os.path.join(path, fname) for fname in filenames] print path zClip = self.getClip(self.paramZClip.value) filenames = filenames[zClip[0]:zClip[1]] assert len(filenames) >= 1 yClip = self.getClip(self.paramYClip.value) xClip = self.getClip(self.paramXClip.value) dtype = self.paramDtype.value data = [] for i, fn in enumerate(filenames): subscriber %= 1 + 99 * i / len(filenames) data.append(imread(fn, True)[yClip[0]:yClip[1], xClip[0]:xClip[1]]) data = numpy.array(data, dtype=dtype) axes = ['z', 'y', 'x'] dimensions = [ self.getDimension(a, data.shape[i]) for i, a in enumerate(axes) ] unit = parseFCUnit(self.paramFieldUnit.value) longname = self.paramLongname.value shortname = self.paramShortname.value image = FieldContainer(data=data, dimensions=dimensions, unit=unit, longname=longname, shortname=shortname, attributes={ 'yFactor': parseFCUnit(self.paramDy.value), 'xFactor': parseFCUnit(self.paramDx.value) }) image.seal() subscriber %= 100 return image
def loadImageAsGreyScale(self, subscriber=0): import os import re from scipy.misc import imread import numpy from pyphant.core.DataContainer import FieldContainer path = os.path.realpath(self.paramPath.value) if os.path.isfile(path): path = os.path.dirname(path) pattern = re.compile(self.paramRegex.value) filenames = filter( lambda x: pattern.match(x) is not None, os.listdir(path) ) filenames.sort() filenames = [os.path.join(path, fname) for fname in filenames] print path zClip = self.getClip(self.paramZClip.value) filenames = filenames[zClip[0]:zClip[1]] assert len(filenames) >= 1 yClip = self.getClip(self.paramYClip.value) xClip = self.getClip(self.paramXClip.value) dtype = self.paramDtype.value data = [] for i, fn in enumerate(filenames): subscriber %= 1 + 99 * i / len(filenames) data.append(imread(fn, True)[yClip[0]:yClip[1], xClip[0]:xClip[1]]) data = numpy.array(data, dtype=dtype) axes = ['z', 'y', 'x'] dimensions = [ self.getDimension(a, data.shape[i]) for i, a in enumerate(axes) ] unit = parseFCUnit(self.paramFieldUnit.value) longname = self.paramLongname.value shortname = self.paramShortname.value image = FieldContainer( data=data, dimensions=dimensions, unit=unit, longname=longname, shortname=shortname, attributes={'yFactor': parseFCUnit(self.paramDy.value), 'xFactor': parseFCUnit(self.paramDx.value)} ) image.seal() subscriber %= 100 return image
def getDimension(self, axis, length): from pyphant.quantities import Quantity from pyphant.core.DataContainer import FieldContainer import numpy delta = parseFCUnit(self.getParam('d' + axis).value) start = parseFCUnit(self.getParam('start' + axis).value) if isinstance(delta, Quantity): unit = Quantity(1.0, delta.unit) start = start.inUnitsOf(delta.unit).value delta = delta.value else: unit = 1 data = start + numpy.arange(0, length, dtype=float) * delta dim = FieldContainer(data, unit=unit, shortname=axis, longname='%s-Axis' % (axis.upper(), )) return dim
def getDimension(self, axis, length): from pyphant.quantities import Quantity from pyphant.core.DataContainer import FieldContainer import numpy delta = parseFCUnit(self.getParam('d' + axis).value) start = parseFCUnit(self.getParam('start' + axis).value) if isinstance(delta, Quantity): unit = Quantity(1.0, delta.unit) start = start.inUnitsOf(delta.unit).value delta = delta.value else: unit = 1 data = start + numpy.arange(0, length, dtype=float) * delta dim = FieldContainer( data, unit=unit, shortname=axis, longname='%s-Axis' % (axis.upper(), ) ) return dim
def add_noise(self, input_fc, subscriber=0): width = parseFCUnit(self.paramWidth.value) scale = float(width / input_fc.unit) noisy_data = input_fc.data + normal( scale=scale, size=input_fc.data.shape ) output_fc = FieldContainer( data=noisy_data, unit=deepcopy(input_fc.unit), dimensions=deepcopy(input_fc.dimensions), longname=input_fc.longname + u" with noise", shortname=input_fc.shortname, error=deepcopy(input_fc.error), mask=deepcopy(input_fc.mask), attributes=deepcopy(input_fc.attributes) ) output_fc.seal() return output_fc
def testQuantity(self): self.assertEqual(parseFCUnit("120 mm"), Quantity("120 mm"))
def testInt(self): self.assertEqual(parseFCUnit("42"), 42.0)
def testFloat(self): self.assertEqual(parseFCUnit("2.3e-5"), 2.3e-5)