diff options
Diffstat (limited to 'Wrappers')
-rwxr-xr-x | Wrappers/Python/ccpi/framework/Vector.py | 97 | ||||
-rwxr-xr-x | Wrappers/Python/ccpi/framework/__init__.py | 2 | ||||
-rwxr-xr-x | Wrappers/Python/ccpi/framework/framework.py | 222 | ||||
-rw-r--r-- | Wrappers/Python/test/test_Operator.py | 4 |
4 files changed, 68 insertions, 257 deletions
diff --git a/Wrappers/Python/ccpi/framework/Vector.py b/Wrappers/Python/ccpi/framework/Vector.py deleted file mode 100755 index 1885822..0000000 --- a/Wrappers/Python/ccpi/framework/Vector.py +++ /dev/null @@ -1,97 +0,0 @@ -# -*- coding: utf-8 -*- -# CCP in Tomographic Imaging (CCPi) Core Imaging Library (CIL). - -# Copyright 2017 UKRI-STFC -# Copyright 2017 University of Manchester - -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at - -# http://www.apache.org/licenses/LICENSE-2.0 - -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -from __future__ import unicode_literals - -import numpy -import sys -from datetime import timedelta, datetime -import warnings -from functools import reduce -from numbers import Number -from ccpi.framework import DataContainer - -class VectorData(DataContainer): - '''DataContainer to contain 1D array''' - def __init__(self, array=None, **kwargs): - self.geometry = kwargs.get('geometry', None) - self.dtype = kwargs.get('dtype', numpy.float32) - - if self.geometry is None: - if array is None: - raise ValueError('Please specify either a geometry or an array') - else: - if len(array.shape) > 1: - raise ValueError('Incompatible size: expected 1D got {}'.format(array.shape)) - out = array - self.geometry = VectorGeometry(array.shape[0]) - self.length = self.geometry.length - else: - self.length = self.geometry.length - - if array is None: - out = numpy.zeros((self.length,), dtype=self.dtype) - else: - if self.length == array.shape[0]: - out = array - else: - raise ValueError('Incompatible size: expecting {} got {}'.format((self.length,), array.shape)) - deep_copy = True - super(VectorData, self).__init__(out, deep_copy, None) - -class VectorGeometry(object): - '''Geometry describing VectorData to contain 1D array''' - RANDOM = 'random' - RANDOM_INT = 'random_int' - - def __init__(self, - length): - - self.length = length - self.shape = (length, ) - - - def clone(self): - '''returns a copy of VectorGeometry''' - return VectorGeometry(self.length) - - def allocate(self, value=0, **kwargs): - '''allocates an VectorData according to the size expressed in the instance''' - self.dtype = kwargs.get('dtype', numpy.float32) - out = VectorData(geometry=self, dtype=self.dtype) - if isinstance(value, Number): - if value != 0: - out += value - else: - if value == VectorGeometry.RANDOM: - seed = kwargs.get('seed', None) - if seed is not None: - numpy.random.seed(seed) - out.fill(numpy.random.random_sample(self.shape)) - elif value == VectorGeometry.RANDOM_INT: - seed = kwargs.get('seed', None) - if seed is not None: - numpy.random.seed(seed) - max_value = kwargs.get('max_value', 100) - out.fill(numpy.random.randint(max_value,size=self.shape)) - else: - raise ValueError('Value {} unknown'.format(value)) - return out diff --git a/Wrappers/Python/ccpi/framework/__init__.py b/Wrappers/Python/ccpi/framework/__init__.py index cae8988..94788a5 100755 --- a/Wrappers/Python/ccpi/framework/__init__.py +++ b/Wrappers/Python/ccpi/framework/__init__.py @@ -30,10 +30,10 @@ from functools import reduce from .framework import DataContainer from .framework import ImageData, AcquisitionData from .framework import ImageGeometry, AcquisitionGeometry +from .framework import VectorData, VectorGeometry from .framework import find_key, message from .framework import DataProcessor from .framework import AX, PixelByPixelDataProcessor, CastDataContainer from .BlockDataContainer import BlockDataContainer from .BlockGeometry import BlockGeometry from .TestData import TestData -from .Vector import VectorGeometry, VectorData diff --git a/Wrappers/Python/ccpi/framework/framework.py b/Wrappers/Python/ccpi/framework/framework.py index 5768e7d..c27ad1e 100755 --- a/Wrappers/Python/ccpi/framework/framework.py +++ b/Wrappers/Python/ccpi/framework/framework.py @@ -506,8 +506,10 @@ class DataContainer(object): #print ("axes {0}".format(axes)) cleaned = numpy.transpose(cleaned, axes).copy() - - return type(self)(cleaned , True, dimensions) + if cleaned.ndim > 1: + return type(self)(cleaned , True, dimensions) + else: + return VectorData(cleaned) def fill(self, array, **dimension): '''fills the internal numpy array with the one provided''' @@ -1325,165 +1327,71 @@ class PixelByPixelDataProcessor(DataProcessor): return y +class VectorData(DataContainer): + '''DataContainer to contain 1D array''' + def __init__(self, array=None, **kwargs): + self.geometry = kwargs.get('geometry', None) + self.dtype = kwargs.get('dtype', numpy.float32) + if self.geometry is None: + if array is None: + raise ValueError('Please specify either a geometry or an array') + else: + if len(array.shape) > 1: + raise ValueError('Incompatible size: expected 1D got {}'.format(array.shape)) + out = array + self.geometry = VectorGeometry(array.shape[0]) + self.length = self.geometry.length + else: + self.length = self.geometry.length + + if array is None: + out = numpy.zeros((self.length,), dtype=self.dtype) + else: + if self.length == array.shape[0]: + out = array + else: + raise ValueError('Incompatible size: expecting {} got {}'.format((self.length,), array.shape)) + deep_copy = True + super(VectorData, self).__init__(out, deep_copy, None) + +class VectorGeometry(object): + '''Geometry describing VectorData to contain 1D array''' + RANDOM = 'random' + RANDOM_INT = 'random_int' -if __name__ == '__main__': - shape = (2,3,4,5) - size = shape[0] - for i in range(1, len(shape)): - size = size * shape[i] - #print("a refcount " , sys.getrefcount(a)) - a = numpy.asarray([i for i in range( size )]) - print("a refcount " , sys.getrefcount(a)) - a = numpy.reshape(a, shape) - print("a refcount " , sys.getrefcount(a)) - ds = DataContainer(a, False, ['X', 'Y','Z' ,'W']) - print("a refcount " , sys.getrefcount(a)) - print ("ds label {0}".format(ds.dimension_labels)) - subset = ['W' ,'X'] - b = ds.subset( subset ) - print("a refcount " , sys.getrefcount(a)) - print ("b label {0} shape {1}".format(b.dimension_labels, - numpy.shape(b.as_array()))) - c = ds.subset(['Z','W','X']) - print("a refcount " , sys.getrefcount(a)) - - # Create a ImageData sharing the array with c - volume0 = ImageData(c.as_array(), False, dimensions = c.dimension_labels) - volume1 = ImageData(c, False) - - print ("volume0 {0} volume1 {1}".format(id(volume0.array), - id(volume1.array))) - - # Create a ImageData copying the array from c - volume2 = ImageData(c.as_array(), dimensions = c.dimension_labels) - volume3 = ImageData(c) - - print ("volume2 {0} volume3 {1}".format(id(volume2.array), - id(volume3.array))) + def __init__(self, + length): - # single number DataSet - sn = DataContainer(numpy.asarray([1])) - - ax = AX() - ax.scalar = 2 - ax.set_input(c) - #ax.apply() - print ("ax in {0} out {1}".format(c.as_array().flatten(), - ax.get_output().as_array().flatten())) - - cast = CastDataContainer(dtype=numpy.float32) - cast.set_input(c) - out = cast.get_output() - out *= 0 - axm = AX() - axm.scalar = 0.5 - axm.set_input_processor(cast) - axm.get_output(out) - #axm.apply() - print ("axm in {0} out {1}".format(c.as_array(), axm.get_output().as_array())) - - # check out in DataSetProcessor - #a = numpy.asarray([i for i in range( size )]) - + self.length = length + self.shape = (length, ) - # create a PixelByPixelDataProcessor - - #define a python function which will take only one input (the pixel value) - pyfunc = lambda x: -x if x > 20 else x - clip = PixelByPixelDataProcessor() - clip.pyfunc = pyfunc - clip.set_input(c) - #clip.apply() - - print ("clip in {0} out {1}".format(c.as_array(), clip.get_output().as_array())) - - #dsp = DataProcessor() - #dsp.set_input(ds) - #dsp.input = a - # pipeline + + def clone(self): + '''returns a copy of VectorGeometry''' + return VectorGeometry(self.length) - chain = AX() - chain.scalar = 0.5 - chain.set_input_processor(ax) - print ("chain in {0} out {1}".format(ax.get_output().as_array(), chain.get_output().as_array())) - - # testing arithmetic operations - - print (b) - print ((b+1)) - print ((1+b)) - - print (b) - print ((b*2)) - - print (b) - print ((2*b)) - - print (b) - print ((b/2)) - - print (b) - print ((2/b)) - - print (b) - print ((b**2)) - - print (b) - print ((2**b)) - - print (type(volume3 + 2)) - - s = [i for i in range(3 * 4 * 4)] - s = numpy.reshape(numpy.asarray(s), (3,4,4)) - sino = AcquisitionData( s ) - - shape = (4,3,2) - a = [i for i in range(2*3*4)] - a = numpy.asarray(a) - a = numpy.reshape(a, shape) - print (numpy.shape(a)) - ds = DataContainer(a, True, ['X', 'Y','Z']) - # this means that I expect the X to be of length 2 , - # y of length 3 and z of length 4 - subset = ['Y' ,'Z'] - b0 = ds.subset( subset ) - print ("shape b 3,2? {0}".format(numpy.shape(b0.as_array()))) - # expectation on b is that it is - # 3x2 cut at z = 0 - - subset = ['X' ,'Y'] - b1 = ds.subset( subset , Z=1) - print ("shape b 2,3? {0}".format(numpy.shape(b1.as_array()))) - - + def allocate(self, value=0, **kwargs): + '''allocates an VectorData according to the size expressed in the instance''' + self.dtype = kwargs.get('dtype', numpy.float32) + out = VectorData(geometry=self, dtype=self.dtype) + if isinstance(value, Number): + if value != 0: + out += value + else: + if value == VectorGeometry.RANDOM: + seed = kwargs.get('seed', None) + if seed is not None: + numpy.random.seed(seed) + out.fill(numpy.random.random_sample(self.shape)) + elif value == VectorGeometry.RANDOM_INT: + seed = kwargs.get('seed', None) + if seed is not None: + numpy.random.seed(seed) + max_value = kwargs.get('max_value', 100) + out.fill(numpy.random.randint(max_value,size=self.shape)) + else: + raise ValueError('Value {} unknown'.format(value)) + return out - # create VolumeData from geometry - vgeometry = ImageGeometry(voxel_num_x=2, voxel_num_y=3, channels=2) - vol = ImageData(geometry=vgeometry) - - sgeometry = AcquisitionGeometry(dimension=2, angles=numpy.linspace(0, 180, num=20), - geom_type='parallel', pixel_num_v=3, - pixel_num_h=5 , channels=2) - sino = AcquisitionData(geometry=sgeometry) - sino2 = sino.clone() - - a0 = numpy.asarray([i for i in range(2*3*4)]) - a1 = numpy.asarray([2*i for i in range(2*3*4)]) - - - ds0 = DataContainer(numpy.reshape(a0,(2,3,4))) - ds1 = DataContainer(numpy.reshape(a1,(2,3,4))) - - numpy.testing.assert_equal(ds0.dot(ds1), a0.dot(a1)) - - a2 = numpy.asarray([2*i for i in range(2*3*5)]) - ds2 = DataContainer(numpy.reshape(a2,(2,3,5))) - -# # it should fail if the shape is wrong -# try: -# ds2.dot(ds0) -# self.assertTrue(False) -# except ValueError as ve: -# self.assertTrue(True) diff --git a/Wrappers/Python/test/test_Operator.py b/Wrappers/Python/test/test_Operator.py index 1ec7761..775b446 100644 --- a/Wrappers/Python/test/test_Operator.py +++ b/Wrappers/Python/test/test_Operator.py @@ -405,7 +405,7 @@ class TestBlockOperator(unittest.TestCase): u = ig.allocate('random_int') steps = [timer()] i = 0 - n = 2. + n = 10. t1 = t2 = 0 res = B.range_geometry().allocate() @@ -456,7 +456,7 @@ class TestBlockOperator(unittest.TestCase): i += 1 print ("Time difference ", t1,t2) - self.assertGreater(t1,t2) + # self.assertGreater(t1,t2) def test_BlockOperatorLinearValidity(self): print ("test_BlockOperatorLinearValidity") |