Example #1
0
    def setUp(self):
        super(SampleContainerInSampleContainerTestCase, self).setUp()
        sample1 = SampleContainer([self.dependent, self.field],
                                  longname='First Sample',
                                  shortname='X',
                                  attributes=copy.copy(self.attributes).update(
                                      {'isSample': 'It seems so.'}))
        self.independent2 = FieldContainer(
            0.3 * numpy.linspace(0, 1, self.testData.shape[0] * 10),
            longname='independent variable',
            shortname='x',
            unit=Quantity('1 mg'),
            attributes=copy.copy(self.attributes).update({'independent':
                                                          True}))
        self.dependent2 = FieldContainer(9.81 * self.independent2.data,
                                         dimensions=[self.independent2],
                                         longname='dependent variable',
                                         shortname='f',
                                         unit=Quantity('9.81 nN'),
                                         attributes=copy.copy(
                                             self.attributes).update(
                                                 {'independent': False}))

        sample2 = SampleContainer([self.dependent2, self.independent2],
                                  longname='Second Sample',
                                  shortname='Y',
                                  attributes=copy.copy(self.attributes).update(
                                      {'sample Nr.': 2}))
        self.sample = SampleContainer([sample1, sample2],
                                      longname='SampleContainer with Samples',
                                      shortname='(X,Y)',
                                      attributes=copy.copy(
                                          self.attributes).update(
                                              {'isSample': 'It seems so.'}))
        self.sample.seal()
Example #2
0
 def testCommaSeparated(self):
     section = self.field1d[[1, 3, 7],]
     afoot = FieldContainer(numpy.array([0.2, 0.4, 0.8]), longname="voltage", shortname="U", unit="1V")
     afoot.dimensions[0] = self.xDim[[1, 3, 7],]
     self.assertEqual(section, afoot)
     section = self.field1d[[[1, 3, 7]]]
     self.assertEqual(section, afoot)
Example #3
0
    def gradientWorker(self, image, subscriber=0):
        d0 = image.dimensions[0]
        dims = [d0] + [d.inUnitsOf(d0) for d in image.dimensions[1:]]
        data = image.data.astype(float)
        gradient = np.gradient(data)
        magnitude = np.zeros_like(data)
        for i, (dim, grad) in enumerate(zip(dims, gradient)):
            shape = [1, ] * len(dims)
            shape[i] = dim.data.shape[0]
            grad /= np.gradient(dim.data).reshape(shape)
            magnitude += grad ** 2
        magnitude = np.sqrt(magnitude)

        longname = "Gradient"
        from pyphant.core.DataContainer import FieldContainer
        result = FieldContainer(
            magnitude,
            image.unit / d0.unit,
            None,
            copy.deepcopy(image.mask),
            copy.deepcopy(image.dimensions),
            longname,
            image.shortname,
            copy.deepcopy(image.attributes),
            False)
        result.seal()
        return result
Example #4
0
 def findMaskPoints(self, image, mask, subscriber=0):
     """
     Returns a table of masked points with each row
     giving a tuple (coordinate_1, ..., coordindate_n, value).
     """
     self.check(image, mask)
     subscriber %= 10.0
     index = (mask.data == FEATURE_COLOR).nonzero()
     zVal = image.data[index]
     subscriber %= 60.0
     fields = []
     for dim, coord in enumerate(index):
         newField = FieldContainer(
             image.dimensions[dim].data[coord],
             image.dimensions[dim].unit,
             longname=image.dimensions[dim].longname + " %i" % dim,
             shortname=image.dimensions[dim].shortname)
         fields.append(newField)
     fields.append(
         FieldContainer(zVal,
                        image.unit,
                        longname=image.longname,
                        shortname=image.shortname))
     res = SampleContainer(
         fields, u"Points from %s at %s" % (image.longname, mask.longname),
         u"X1")
     res.seal()
     subscriber %= 100.0
     return res
Example #5
0
 def testLoadOneRow(self):
     result = self.load('onerow.fmf')
     t = FieldContainer(numpy.array([1.0]), unit=Quantity('1 s'),
                        shortname='t', longname='time')
     s = FieldContainer(numpy.array([2.0]), unit=Quantity('1 m'),
                        shortname='s', longname='distance')
     self.checkExpected([t, s], result)
Example #6
0
 def testNonUniformAxes(self):
     im = np.array(
         [
             [0., 1., 2.],
             [30., 10., 50.],
             [8., 9., 6.],
             [-10., 0., 22.]
             ]
         )
     x = FieldContainer(np.array([1., 10., 200.]), unit=Quantity('1 m'))
     y = FieldContainer(np.array([0., 2., 4., 8.]), unit=Quantity('1 cm'))
     fc = FieldContainer(im, unit=Quantity('5 V'), dimensions=[y, x])
     fc.seal()
     grad_y, grad_x = np.gradient(fc.data)
     grad_y /= np.gradient(y.data).reshape((4, 1))
     grad_x /= np.gradient(x.data).reshape((1, 3))
     grad_y = FieldContainer(
         grad_y, unit=fc.unit / y.unit,
         dimensions=copy.deepcopy(fc.dimensions)
         )
     grad_x = FieldContainer(
         grad_x, unit=fc.unit / x.unit,
         dimensions=copy.deepcopy(fc.dimensions)
         )
     grad_x = grad_x.inUnitsOf(grad_y)
     expected_result = FieldContainer(
         (grad_x.data ** 2 + grad_y.data ** 2) ** 0.5,
         unit=copy.deepcopy(grad_y.unit),
         dimensions=copy.deepcopy(fc.dimensions)
         )
     result = Gradient().gradientWorker(fc)
     self.assertEqual(expected_result, result)
Example #7
0
class CoverageTestCase(unittest.TestCase):
    def setUp(self):
        from pyphant.core.DataContainer import FieldContainer
        data = numpy.arange(0, 256, 1).reshape((16, 16))
        self.image = FieldContainer(data, unit=1e10)
        self.image.seal()

    def testRatiosUpToTenPercentError(self):
        delta = .1
        from ImageProcessing.CoverageWorker import CoverageWorker
        from ImageProcessing import FEATURE_COLOR, BACKGROUND_COLOR
        from pyphant.quantities import Quantity
        cworker = CoverageWorker()
        for rho1Fac in [0.1, 0.25, 0.5, 1.0, 1.5, 2.0, 5.0, 10.0]:
            rho1 = '%s mC / m ** 3' % (3000. * rho1Fac, )
            rho2 = '3 C / m ** 3'
            cworker.paramRho1.value = rho1
            cworker.paramRho2.value = rho2
            rho1 = Quantity(rho1)
            rho2 = Quantity(rho2)
            for ratio in numpy.arange(0.0, 1.01, 0.01):
                cworker.paramW1.value = '%s%%' % (ratio * 100., )
                result = cworker.threshold(self.image)
                self.assertEqual(result.dimensions, self.image.dimensions)
                stuff1 = numpy.where(result.data == FEATURE_COLOR,
                                     True, False).sum()
                stuff2 = numpy.where(result.data == BACKGROUND_COLOR,
                                     True, False).sum()
                self.assertEqual(stuff1 + stuff2, result.data.size)
                stuff1 = (stuff1 * rho1).inUnitsOf('C / m ** 3').value
                stuff2 = (stuff2 * rho2).inUnitsOf('C / m ** 3').value
                actualRatio = stuff1 / (stuff1 + stuff2)
                self.assertTrue(abs(ratio - actualRatio) <= delta)
Example #8
0
class CoverageTestCase(unittest.TestCase):
    def setUp(self):
        from pyphant.core.DataContainer import FieldContainer
        data = numpy.arange(0, 256, 1).reshape((16, 16))
        self.image = FieldContainer(data, unit=1e10)
        self.image.seal()

    def testRatiosUpToTenPercentError(self):
        delta = .1
        from ImageProcessing.CoverageWorker import CoverageWorker
        from ImageProcessing import FEATURE_COLOR, BACKGROUND_COLOR
        from pyphant.quantities import Quantity
        cworker = CoverageWorker()
        for rho1Fac in [0.1, 0.25, 0.5, 1.0, 1.5, 2.0, 5.0, 10.0]:
            rho1 = '%s mC / m ** 3' % (3000. * rho1Fac, )
            rho2 = '3 C / m ** 3'
            cworker.paramRho1.value = rho1
            cworker.paramRho2.value = rho2
            rho1 = Quantity(rho1)
            rho2 = Quantity(rho2)
            for ratio in numpy.arange(0.0, 1.01, 0.01):
                cworker.paramW1.value = '%s%%' % (ratio * 100., )
                result = cworker.threshold(self.image)
                self.assertEqual(result.dimensions, self.image.dimensions)
                stuff1 = numpy.where(result.data == FEATURE_COLOR, True,
                                     False).sum()
                stuff2 = numpy.where(result.data == BACKGROUND_COLOR, True,
                                     False).sum()
                self.assertEqual(stuff1 + stuff2, result.data.size)
                stuff1 = (stuff1 * rho1).inUnitsOf('C / m ** 3').value
                stuff2 = (stuff2 * rho2).inUnitsOf('C / m ** 3').value
                actualRatio = stuff1 / (stuff1 + stuff2)
                self.assertTrue(abs(ratio - actualRatio) <= delta)
Example #9
0
 def testLoadOneRowDep(self):
     result = self.load('onerow_dep.fmf')
     t = FieldContainer(numpy.array([1.0]), unit=Quantity('1 s'),
                        shortname='t', longname='time')
     s = FieldContainer(numpy.array([5.0]), unit=Quantity('1 m'),
                        shortname='s', longname='distance')
     s.dimensions[0] = deepcopy(t)
     self.checkExpected([t, s], result)
Example #10
0
 def testLoadOneRowDep(self):
     result = self.load('onerow_dep.fmf')
     t = FieldContainer(numpy.array([1.0]), unit=Quantity('1 s'),
                        shortname='t', longname='time')
     s = FieldContainer(numpy.array([5.0]), unit=Quantity('1 m'),
                        shortname='s', longname='distance')
     s.dimensions[0] = deepcopy(t)
     self.checkExpected([t, s], result)
Example #11
0
 def testCommaSeparated(self):
     section = self.field2d[[1, 3, 7],]
     afoot = FieldContainer(self.field2d.data[[1, 3, 7],], longname="voltage", shortname="U", unit="1V")
     afoot.dimensions[0] = self.yDim[[1, 3, 7],]
     afoot.dimensions[1] = self.xDim
     self.assertEqual(section, afoot)
     section = self.field2d[[[1, 3, 7]]]
     self.assertEqual(section, afoot)
Example #12
0
 def testCompleteRightOpenIntervall(self):
     dim = self.xDim
     intStart = dim.data.min() * dim.unit
     intEnd = dim.data.max() * dim.unit
     unitname = dim.unit.unit.name()
     section = self.field1d["%.4f%s:%.4f%s" % (intStart.value, unitname, intEnd.value, unitname)]
     afoot = FieldContainer(numpy.linspace(0.1, 0.9, 9), longname="voltage", shortname="U", unit="1V")
     afoot.dimensions[0] = self.xDim[0:-1]
     self.assertEqual(section, afoot)
Example #13
0
 def testRegionIndex(self):
     section = self.field1d[1:4]
     afoot = FieldContainer(numpy.linspace(0.2, 0.4, 3), longname="voltage", shortname="U", unit="1V")
     afoot.dimensions[0] = self.xDim[1:4]
     self.assertEqual(section, afoot)
     section = self.field1d[1:-1]
     afoot = FieldContainer(numpy.linspace(0.2, 0.9, 8), longname="voltage", shortname="U", unit="1V")
     afoot.dimensions[0] = self.xDim[1:-1]
     self.assertEqual(section, afoot)
Example #14
0
 def testSeal(self):
     field = FieldContainer(self.testData, 1, longname=self.longname, shortname=self.shortname)
     field.seal()
     self.assertNotEqual(field.id, None)
     self.assertNotEqual(field.hash, None)
     try:
         field.data = scipy.array([1, 2, 3])
     except TypeError, e:
         pass
Example #15
0
 def testUnits(self):
     data = (np.arange(0, 256, .01)).reshape((80, 320))
     image = FieldContainer(data, unit=Quantity('1 mJ'))
     for dim in image.dimensions:
         dim.unit = Quantity('1 cm')
     image.seal()
     gradient = Gradient()
     result = gradient.gradientWorker(image)
     self.assertEqual(result.dimensions, image.dimensions)
     self.assertEqual(result.unit, Quantity('1 mJ / cm'))
Example #16
0
 def setUp(self):
     data = NPArray([10.0, -103.5, 1000.43, 0.0, 10.0])
     unit = PQ('3s')
     error = NPArray([0.1, 0.2, 4.5, 0.1, 0.2])
     longname = u'Test: FieldContainer H5FileHandler'
     shortname = u'TestH5FC'
     attributes = {'custom1': u'testing1...', 'custom2': u'testing2...'}
     self.fc = FieldContainer(data, unit, error, None, None, longname,
                              shortname, attributes)
     self.fc.seal()
Example #17
0
class FieldContainerTestCase(unittest.TestCase):
    def setUp(self):
        data = NPArray([10.0, -103.5, 1000.43, 0.0, 10.0])
        unit = PQ('3s')
        error = NPArray([0.1, 0.2, 4.5, 0.1, 0.2])
        longname = u'Test: FieldContainer H5FileHandler'
        shortname = u'TestH5FC'
        attributes = {'custom1':u'testing1...', 'custom2':u'testing2...'}
        self.fc = FieldContainer(data, unit, error, None, None, longname,
                                 shortname, attributes)
        self.fc.seal()
Example #18
0
 def testDeepcopy(self):
     field = FieldContainer(self.testData, 1, longname=self.longname, shortname=self.shortname)
     copiedField = copy.deepcopy(field)
     self.assertEqual(field, copiedField)
     field.seal()
     copiedField.seal()
     self.assertEqual(field, copiedField)
     # equal because only real data is considered:
     self.assertEqual(field.hash, copiedField.hash)
     # unique due to timestamp in dimension ids:
     self.assertNotEqual(field.id, copiedField.id)
Example #19
0
 def testDateTime(self):
     """Test the correct saving and  restoring of object arrays composed from datetime objects."""
     objectArray = numpy.array([datetime.datetime.now() for i in range(10)])
     objectField = FieldContainer(objectArray,longname=u"timestamp",
                                 shortname='t')
     objectField.seal()
     self.eln.createGroup(self.eln.root,'testObjectFields')
     saveField(self.eln,self.eln.root.testObjectFields,objectField)
     restoredField = loadField(self.eln,self.eln.root.testObjectFields)
     for i,j in zip(restoredField.data.tolist(),objectField.data.tolist()):
         self.assertEqual(i,j,'Expected %s but got %s!' % (j,i))
Example #20
0
 def find(self, image, subscriber=0):
     newdata = self.findExtrema(image.data)
     longname = "FindLocalExtrema"
     from pyphant.core.DataContainer import FieldContainer
     result = FieldContainer(newdata, copy.deepcopy(image.unit),
                             copy.deepcopy(image.error),
                             copy.deepcopy(image.mask),
                             copy.deepcopy(image.dimensions),
                             longname, image.shortname,
                             copy.deepcopy(image.attributes), False)
     result.seal()
     return result
Example #21
0
 def testUnits(self):
     from ImageProcessing.Gradient import Gradient
     from pyphant.core.DataContainer import FieldContainer
     from pyphant.quantities import Quantity
     data = (numpy.arange(0, 256, .01)).reshape((80, 320))
     image = FieldContainer(data, unit=Quantity('1 mJ'))
     for dim in image.dimensions:
         dim.unit = Quantity('1 cm')
     image.seal()
     gradient = Gradient()
     result = gradient.gradientWorker(image)
     self.assertEqual(result.dimensions, image.dimensions)
     self.assertEqual(result.unit, Quantity('1 mJ / cm'))
Example #22
0
 def invert(self, image, subscriber=0):
     max = scipy.amax(image.data)
     min = scipy.amin(image.data)
     data = max + min - image.data
     from pyphant.core.DataContainer import FieldContainer
     result = FieldContainer(data, unit=image.unit,
                             dimensions=copy.deepcopy(image.dimensions),
                             mask=copy.deepcopy(image.mask),
                             error=copy.deepcopy(image.error),
                             longname=image.longname,
                             shortname=image.shortname)
     result.seal()
     return result
Example #23
0
 def testUnicodeFields(self):
     self.field.seal()
     unicodeArray = numpy.array([u'Hallo World!',u'Hallo Wörld!'])
     unicodeField = FieldContainer(unicodeArray,longname=u"blabla",
                                 shortname=self.shortname,
                                 unit = 1,
                                 attributes = self.attributes
                                 )
     unicodeField.seal()
     self.eln.createGroup(self.eln.root,'testUnicodeFields')
     saveField(self.eln,self.eln.root.testUnicodeFields,unicodeField)
     restoredField = loadField(self.eln,self.eln.root.testUnicodeFields)
     self.assertEqual(restoredField,unicodeField,
                      "Restored unicode string is %s (%s) but is expected to be %s (%s)." % (restoredField.data,restoredField.data.dtype,unicodeField.data,unicodeField.data.dtype))
Example #24
0
 def testSCwithSCColumn(self):
     fc_child1 = FieldContainer(longname='fc_child1', data=N.ones((10, 10)))
     fc_child2 = FieldContainer(longname='fc_child2', data=N.ones((20, 20)))
     sc_child = SampleContainer(longname='sc_child', columns=[fc_child1])
     sc_parent = SampleContainer(longname='sc_parent',
                                 columns=[sc_child, fc_child2])
     sc_parent.seal()
     km = KnowledgeManager.getInstance()
     km.registerDataContainer(sc_parent, temporary=True)
     lnlist = km.search(['longname'], {'col_of': {'longname': 'sc_parent'}})
     lnlist = [entry[0] for entry in lnlist]
     assert len(lnlist) == 2
     assert 'fc_child2' in lnlist
     assert 'sc_child' in lnlist
Example #25
0
 def invert(self, image, subscriber=0):
     max = scipy.amax(image.data)
     min = scipy.amin(image.data)
     data = max + min - image.data
     from pyphant.core.DataContainer import FieldContainer
     result = FieldContainer(data,
                             unit=image.unit,
                             dimensions=copy.deepcopy(image.dimensions),
                             mask=copy.deepcopy(image.mask),
                             error=copy.deepcopy(image.error),
                             longname=image.longname,
                             shortname=image.shortname)
     result.seal()
     return result
Example #26
0
 def loadImageAsGreyScale(self, subscriber=0):
     import os
     import re
     from scipy.misc import imread
     import numpy
     from pyphant.core.DataContainer import FieldContainer
     from pyphant.quantities import Quantity
     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)
     ]
     try:
         unit = Quantity(self.paramFieldUnit.value)
     except AttributeError:
         unit = 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': Quantity(self.paramDy.value),
                                'xFactor': Quantity(self.paramDx.value)
                            })
     image.seal()
     subscriber %= 100
     return image
Example #27
0
 def setUp(self):
     super(SampleContainerTestCase,self).setUp()
     self.independent = FieldContainer(0.3*numpy.linspace(0,1,self.testData.shape[0]),
                                       longname='independent variable',
                                       shortname='x',
                                       unit = Quantity('1 mg'),
                                       attributes = copy.copy(self.attributes).update({'independent':True}))
     self.dependent = FieldContainer(9.81*self.independent.data,
                                     dimensions=[self.independent],
                                     longname='dependent variable',
                                     shortname='f',
                                     unit = Quantity('9.81 nN'),
                                     attributes = copy.copy(self.attributes).update({'independent':False}))
     self.sample = SampleContainer([self.dependent,self.field],longname='Sample',shortname='X',
                                   attributes = copy.copy(self.attributes).update({'isSample':'It seems so.'}))
     self.sample.seal()
Example #28
0
 def testStrings(self):
     column = ['Hello', 'World']
     result = FMFLoader.column2FieldContainer('simple string', column)
     expectedResult = FieldContainer(
         numpy.array(column), longname='simple string'
         )
     assertEqual(result, expectedResult)
Example #29
0
 def testCall(self):
     from ImageProcessing.FitBackground import FitBackground
     from pyphant.core.DataContainer import FieldContainer
     data = (numpy.arange(0, 256, .001)).reshape((800, 320))
     image = FieldContainer(data)
     fbw = FitBackground()
     fbw.fit_background(image)
Example #30
0
 def find(self, image, subscriber=0):
     newdata = self.findExtrema(image.data)
     longname = "FindLocalExtrema"
     from pyphant.core.DataContainer import FieldContainer
     result = FieldContainer(
         newdata,
         copy.deepcopy(image.unit),
         copy.deepcopy(image.error),
         copy.deepcopy(image.mask),
         copy.deepcopy(image.dimensions),
         longname,
         image.shortname,
         copy.deepcopy(image.attributes),
         False)
     result.seal()
     return result
Example #31
0
 def testInvert(self):
     from ImageProcessing.InvertWorker import InvertWorker
     from pyphant.core.DataContainer import FieldContainer
     from pyphant.quantities import Quantity
     data = numpy.ones((100, 100), dtype='uint8') * 112
     data[0][0] = 0
     image = FieldContainer(data)
     for dim in image.dimensions:
         dim.unit = Quantity('1 cm')
     image.seal()
     invert = InvertWorker()
     result = invert.invert(image)
     self.assertEqual(result.dimensions, image.dimensions)
     expected = numpy.zeros((100, 100), dtype='uint8')
     expected[0][0] = 112
     self.assertTrue((result.data == expected).all())
Example #32
0
 def testLoadOneColumn(self):
     result = self.load('onecolumn.fmf')
     t = FieldContainer(numpy.array([1, 2, 3, 4]),
                        unit=Quantity('1 s'),
                        shortname='t',
                        longname='time')
     self.checkExpected([t], result)
Example #33
0
 def testLoadOneValue(self):
     result = self.load('onevalue.fmf')
     t = FieldContainer(numpy.array([1.0]),
                        unit=Quantity('1 s'),
                        shortname='t',
                        longname='time')
     self.checkExpected([t], result)
Example #34
0
 def testListofStrings(self):
     column = ['World', ['Hello', 'World'], 'World']
     result = LoadFMF.column2FieldContainer('simple string', column)
     expectedResult = FieldContainer(numpy.array(
         ['World', 'Hello, World', 'World']),
                                     longname='simple string')
     assertEqual(result, expectedResult)
Example #35
0
 def testInvert(self):
     from ImageProcessing.InvertWorker import InvertWorker
     from pyphant.core.DataContainer import FieldContainer
     from pyphant.quantities import Quantity
     data = numpy.ones((100, 100), dtype='uint8') * 112
     data[0][0] = 0
     image = FieldContainer(data)
     for dim in image.dimensions:
         dim.unit = Quantity('1 cm')
     image.seal()
     invert = InvertWorker()
     result = invert.invert(image)
     self.assertEqual(result.dimensions, image.dimensions)
     expected = numpy.zeros((100, 100), dtype='uint8')
     expected[0][0] = 112
     self.assertTrue((result.data == expected).all())
Example #36
0
 def loadImageAsGreyScale(self, subscriber=0):
     import os
     import re
     from scipy.misc import imread
     import numpy
     from pyphant.core.DataContainer import FieldContainer
     from pyphant.quantities import Quantity
     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)
         ]
     try:
         unit = Quantity(self.paramFieldUnit.value)
     except AttributeError:
         unit = 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':Quantity(self.paramDy.value),
                     'xFactor':Quantity(self.paramDx.value)}
         )
     image.seal()
     subscriber %= 100
     return image
Example #37
0
 def testFindExtrPoint(self):
     from ImageProcessing.FindLocalExtrema import FindLocalExtrema
     from pyphant.core.DataContainer import FieldContainer
     from pyphant.quantities import Quantity
     data = numpy.ones((100, 100), dtype='uint8') * 112
     data[10][20] = 255
     image = FieldContainer(data)
     for dim in image.dimensions:
         dim.unit = Quantity('2 mum')
     image.seal()
     fle = FindLocalExtrema()
     fle.paramExcolor.value = 1
     result = fle.find(image)
     self.assertEqual(result.dimensions, image.dimensions)
     expected = numpy.zeros((100, 100), dtype='uint8')
     expected[10][20] = 1
     self.assertTrue((result.data == expected).all())
Example #38
0
 def testFindExtrPoint(self):
     from ImageProcessing.FindLocalExtrema import FindLocalExtrema
     from pyphant.core.DataContainer import FieldContainer
     from pyphant.quantities import Quantity
     data = numpy.ones((100, 100), dtype='uint8') * 112
     data[10][20] = 255
     image = FieldContainer(data)
     for dim in image.dimensions:
         dim.unit = Quantity('2 mum')
     image.seal()
     fle = FindLocalExtrema()
     fle.paramExcolor.value = 1
     result = fle.find(image)
     self.assertEqual(result.dimensions, image.dimensions)
     expected = numpy.zeros((100, 100), dtype='uint8')
     expected[10][20] = 1
     self.assertTrue((result.data == expected).all())
Example #39
0
 def testNormalize(self):
     from ImageProcessing.EnhanceContrast import EnhanceContrast
     from pyphant.core.DataContainer import FieldContainer
     data = (numpy.arange(0, 256, 1) * .5 + 20.).reshape((16, 16))
     image = FieldContainer(data)
     enhanceContrast = EnhanceContrast()
     result = enhanceContrast.enhance(image)
     self.assertEqual(result.data.min(), 0)
     self.assertEqual(result.data.max(), 255)
Example #40
0
 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
Example #41
0
 def setUp(self):
     data = NPArray([10.0, -103.5, 1000.43, 0.0, 10.0])
     unit = PQ('3s')
     error = NPArray([0.1, 0.2, 4.5, 0.1, 0.2])
     longname = u'Test: FieldContainer H5FileHandler'
     shortname = u'TestH5FC'
     attributes = {'custom1':u'testing1...', 'custom2':u'testing2...'}
     self.fc = FieldContainer(data, unit, error, None, None, longname,
                              shortname, attributes)
     self.fc.seal()
     fc2 = FieldContainer(NPArray([4003.2, 5.3, 600.9]), PQ('0.2m'), None,
                          None, None, 'FieldContainer 2', 'FC2')
     fc2.seal()
     columns = [self.fc, fc2]
     longname = u'Test: SampleContainer H5FileHandler'
     shortname = u'TestH5SC'
     self.sc = SampleContainer(columns, longname, shortname, attributes)
     self.sc.seal()
Example #42
0
 def testETFR(self):
     from ImageProcessing.EdgeTouchingFeatureRemover \
          import EdgeTouchingFeatureRemover
     from pyphant.core.DataContainer import FieldContainer
     from pyphant.quantities import Quantity
     from ImageProcessing import BACKGROUND_COLOR, FEATURE_COLOR
     data = numpy.ones((10, 10), dtype='uint8') * BACKGROUND_COLOR
     data[5:10,3:8] = FEATURE_COLOR
     data[1:4,3:8] = FEATURE_COLOR
     image = FieldContainer(data)
     for dim in image.dimensions:
         dim.unit = Quantity('2 mum')
     image.seal()
     etfr = EdgeTouchingFeatureRemover()
     result = etfr.fillFeatures(image)
     self.assertEqual(result.dimensions, image.dimensions)
     expected = numpy.ones((10, 10), dtype='uint8') * BACKGROUND_COLOR
     expected[1:4,3:8] = FEATURE_COLOR
     self.assertTrue((expected == result.data).all())
Example #43
0
 def threshold(self, image, subscriber=0):
     from pyphant.quantities.ParseQuantities import parseQuantity
     w1 = parseQuantity(self.paramW1.value)[0]
     rho1 = parseQuantity(self.paramRho1.value)[0]
     rho2 = parseQuantity(self.paramRho2.value)[0]
     coveragePercent = weight2Coverage(w1, rho1, rho2)
     th = calculateThreshold(image, coveragePercent)
     import scipy
     from ImageProcessing import (FEATURE_COLOR, BACKGROUND_COLOR)
     import copy
     from pyphant.core.DataContainer import FieldContainer
     resultArray = scipy.where(image.data < th,
                               FEATURE_COLOR,
                               BACKGROUND_COLOR)
     result = FieldContainer(resultArray,
                             dimensions=copy.deepcopy(image.dimensions),
                             longname=u"Binary Image", shortname=u"B")
     result.seal()
     return result
Example #44
0
class SampleContainerTestCase(unittest.TestCase):
    def setUp(self):
        data = NPArray([10.0, -103.5, 1000.43, 0.0, 10.0])
        unit = PQ('3s')
        error = NPArray([0.1, 0.2, 4.5, 0.1, 0.2])
        longname = u'Test: FieldContainer H5FileHandler'
        shortname = u'TestH5FC'
        attributes = {'custom1': u'testing1...', 'custom2': u'testing2...'}
        self.fc = FieldContainer(data, unit, error, None, None, longname,
                                 shortname, attributes)
        self.fc.seal()
        fc2 = FieldContainer(NPArray([4003.2, 5.3, 600.9]), PQ('0.2m'), None,
                             None, None, 'FieldContainer 2', 'FC2')
        fc2.seal()
        columns = [self.fc, fc2]
        longname = u'Test: SampleContainer H5FileHandler'
        shortname = u'TestH5SC'
        self.sc = SampleContainer(columns, longname, shortname, attributes)
        self.sc.seal()
Example #45
0
 def threshold(self, image, subscriber=0):
     from pyphant.quantities.ParseQuantities import parseQuantity
     w1 = parseQuantity(self.paramW1.value)[0]
     rho1 = parseQuantity(self.paramRho1.value)[0]
     rho2 = parseQuantity(self.paramRho2.value)[0]
     coveragePercent = weight2Coverage(w1, rho1, rho2)
     th = calculateThreshold(image, coveragePercent)
     import scipy
     from ImageProcessing import (FEATURE_COLOR, BACKGROUND_COLOR)
     import copy
     from pyphant.core.DataContainer import FieldContainer
     resultArray = scipy.where(image.data < th, FEATURE_COLOR,
                               BACKGROUND_COLOR)
     result = FieldContainer(resultArray,
                             dimensions=copy.deepcopy(image.dimensions),
                             longname=u"Binary Image",
                             shortname=u"B")
     result.seal()
     return result
Example #46
0
 def gradientWorker(self, image, subscriber=0):
     for dim in image.dimensions:
         assert dim.unit == image.dimensions[0].unit, \
                "Non-uniform dimensions!"
     newdata = gradient(image.data.astype(float))
     longname = "Gradient"
     from pyphant.core.DataContainer import FieldContainer
     result = FieldContainer(
         newdata,
         image.unit / image.dimensions[0].unit,
         None,
         copy.deepcopy(image.mask),
         copy.deepcopy(image.dimensions),
         longname,
         image.shortname,
         copy.deepcopy(image.attributes),
         False)
     result.seal()
     return result
Example #47
0
 def testETFR(self):
     from ImageProcessing.EdgeTouchingFeatureRemover \
          import EdgeTouchingFeatureRemover
     from pyphant.core.DataContainer import FieldContainer
     from pyphant.quantities import Quantity
     from ImageProcessing import BACKGROUND_COLOR, FEATURE_COLOR
     data = numpy.ones((10, 10), dtype='uint8') * BACKGROUND_COLOR
     data[5:10, 3:8] = FEATURE_COLOR
     data[1:4, 3:8] = FEATURE_COLOR
     image = FieldContainer(data)
     for dim in image.dimensions:
         dim.unit = Quantity('2 mum')
     image.seal()
     etfr = EdgeTouchingFeatureRemover()
     result = etfr.fillFeatures(image)
     self.assertEqual(result.dimensions, image.dimensions)
     expected = numpy.ones((10, 10), dtype='uint8') * BACKGROUND_COLOR
     expected[1:4, 3:8] = FEATURE_COLOR
     self.assertTrue((expected == result.data).all())
Example #48
0
 def testUltimatePoints(self):
     from ImageProcessing.UltimatePointsCalculator \
          import UltimatePointsCalculator
     from pyphant.core.DataContainer import FieldContainer
     from pyphant.quantities import Quantity
     data = numpy.zeros((10, 10), dtype='uint8')
     data[1][2] = 1
     data[5][3] = 2
     image = FieldContainer(data)
     for dim in image.dimensions:
         dim.unit = Quantity('1 mum')
     image.seal()
     upc = UltimatePointsCalculator()
     result = upc.findUltimatePoints(image)
     self.assertEqual(result['i'].unit, Quantity('1 mum'))
     self.assertEqual(result['j'].unit, Quantity('1 mum'))
     indices = zip(result['j'].data, result['i'].data)
     self.assertTrue((1, 2) in indices)
     self.assertTrue((5, 3) in indices)
     self.assertEqual(len(indices), 2)
Example #49
0
    def testCall(self):
        """
        test call to fillImage

        no test for reasonable results
        """
        from ImageProcessing.EdgeFillWorker import EdgeFillWorker
        from pyphant.core.DataContainer import FieldContainer
        image = FieldContainer(numpy.zeros((100, 200), dtype='uint8'))
        efw = EdgeFillWorker()
        efw.fillImage(image)
Example #50
0
 def testUltimatePoints(self):
     from ImageProcessing.UltimatePointsCalculator \
          import UltimatePointsCalculator
     from pyphant.core.DataContainer import FieldContainer
     from pyphant.quantities import Quantity
     data = numpy.zeros((10, 10), dtype='uint8')
     data[1][2] = 1
     data[5][3] = 2
     image = FieldContainer(data)
     for dim in image.dimensions:
         dim.unit = Quantity('1 mum')
     image.seal()
     upc = UltimatePointsCalculator()
     result = upc.findUltimatePoints(image)
     self.assertEqual(result['i'].unit, Quantity('1 mum'))
     self.assertEqual(result['j'].unit, Quantity('1 mum'))
     indices = zip(result['j'].data, result['i'].data)
     self.assertTrue((1, 2) in indices)
     self.assertTrue((5, 3) in indices)
     self.assertEqual(len(indices), 2)
Example #51
0
 def testWatershed(self):
     from ImageProcessing.Watershed import Watershed
     from pyphant.core.DataContainer import FieldContainer
     from pyphant.quantities import Quantity
     data = numpy.zeros((10, 10), dtype='uint8')
     data[2:8, 2:8] = 1
     image = FieldContainer(data)
     for dim in image.dimensions:
         dim.unit = Quantity('1 mum')
     image.seal()
     data = numpy.zeros((10, 10), dtype='uint8')
     data[3][3] = 1
     data[4][6] = 2
     markers = FieldContainer(data)
     for dim in markers.dimensions:
         dim.unit = Quantity('1 mum')
     wshed = Watershed()
     result = wshed.wsworker(image, markers)
     self.assertEqual(result.dimensions, image.dimensions)
     from scipy.ndimage import label
     self.assertEqual(label(result.data)[1], 2)
Example #52
0
 def testRegionIndex(self):
     section = self.field2d[1:4]
     afoot = FieldContainer(self.field2d.data[1:4], longname="afoot", shortname="U", unit="1V")
     afoot.dimensions[0] = self.yDim[1:4]
     afoot.dimensions[1] = self.xDim
     self.assertEqual(section, afoot)
     section = self.field2d[1:-1]
     afoot = FieldContainer(self.field2d.data[1:-1], longname="voltage", shortname="U", unit="1V")
     afoot.dimensions[0] = self.yDim[1:-1]
     afoot.dimensions[1] = self.xDim
     self.assertEqual(section, afoot)
Example #53
0
class IsValidFieldContainer(unittest.TestCase):
    def setUp(self):
        self.field = FieldContainer(numpy.random.randn(7, 13), longname="voltage", shortname="U", unit="1V")

    def testWrongDimension(self):
        self.field.dimensions[0].data = self.field.dimensions[0].data[:-1]
        self.assertFalse(self.field.isValid())

    def testWrongDimensionNumber(self):
        self.field.dimensions.append(copy.deepcopy(self.field.dimensions[0]))
        self.assertFalse(self.field.isValid())
        self.field.dimensions = [self.field.dimensions[0]]
        self.assertFalse(self.field.isValid())

    def testWrongMask(self):
        shape = list(self.field.data.shape)
        self.field.mask = numpy.ones(shape)
        self.assertTrue(self.field.isValid())
        shape[0] = shape[0] + 1
        self.field.mask = numpy.ones(shape)
        self.assertFalse(self.field.isValid())

    def testWrongError(self):
        shape = list(self.field.data.shape)
        self.field.error = numpy.zeros(shape)
        self.assertTrue(self.field.isValid())
        shape[0] = shape[0] + 1
        self.field.error = numpy.ones(shape)
        self.assertFalse(self.field.isValid())

    def testDimension0HasSameShapeAsField(self):
        self.field.dimensions[0] = copy.deepcopy(self.field)
        self.assertFalse(self.field.isValid())

    def testDimension1HasSameShapeAsField(self):
        self.field.dimensions[1] = copy.deepcopy(self.field)
        self.assertFalse(self.field.isValid())
Example #54
0
 def setUp(self):
     from pyphant.core.DataContainer import FieldContainer
     data = numpy.arange(0, 256, 1).reshape((16, 16))
     self.image = FieldContainer(data, unit=1e10)
     self.image.seal()