def setUp(self): # make image array = numpy.arange(100).reshape(10, 10) self.image = Image(data=array) # set attributes self.image.xxx = [1, 2, 3] self.image.yyy = [4, 5, 6] large_array = array[1:9, 1:8].copy() self.large = Image(data=large_array) # set absolute path to current dir working_dir = os.getcwd() file_dir, name = os.path.split(__file__) self.dir = os.path.join(working_dir, file_dir) # image file names self.big_file_name = os.path.join(self.dir, '../../io/test/big_head.mrc') self.small_file_name = os.path.join(self.dir, '../../io/test/small.mrc') self.modified_file_name_mrc = os.path.join( self.dir, '../../io/test/modified.mrc') self.modified_file_name_raw = os.path.join( self.dir, '../../io/test/modified.raw')
def test_modify(self): """ Tests modify(), implicitely tests reading and writting mrc header by pyto.io.ImageIO """ # modify mrc image def fun_1(image): dat = image.data + 1 return dat # requires Image.modify(memmap=False) #def fun_1(image): return image.data + 1 Image.modify(old=self.big_file_name, new=self.modified_file_name_mrc, fun=fun_1, memmap=True) new = Image.read(file=self.modified_file_name_mrc, header=True, memmap=True) old = Image.read(file=self.big_file_name, header=True, memmap=True) # check data np_test.assert_equal(new.data[1, 10, :], numpy.arange(11001, 11101)) np_test.assert_equal(new.data[2, :, 15], numpy.arange(20016, 30016, 100)) # check header np_test.assert_almost_equal(new.pixelsize, old.pixelsize) np_test.assert_almost_equal(new.header[0:19], old.header[0:19]) np_test.assert_almost_equal(new.header[22:25], old.header[22:25]) np_test.assert_equal(True, new.header[25] == old.header[25]) header_len = len(new.header) np_test.assert_almost_equal(new.header[26:header_len - 1], old.header[26:header_len - 1]) np_test.assert_equal( True, new.header[header_len - 1] == old.header[header_len - 1]) # modify mrc image and write as raw def fun_v(image, value): data = image.data + value return data modified = Image.modify(old=self.big_file_name, new=self.modified_file_name_raw, fun=fun_v, fun_kwargs={'value': 4}) new = Image.read(file=self.modified_file_name_raw, shape=modified.data.shape, dataType=modified.data.dtype, memmap=True) old = Image.read(file=self.big_file_name, header=True, memmap=True) # check data np_test.assert_equal(new.data[1, 10, :], numpy.arange(11004, 11104)) np_test.assert_equal(new.data[2, :, 15], numpy.arange(20019, 30019, 100))
def test_cut(self): """ Tests cut(), implicitely tests reading and writting mrc header by pyto.io.ImageIO """ # cut image inset = [slice(1, 4), slice(10, 30), slice(50, 60)] Image.cut(old=self.big_file_name, new=self.small_file_name, inset=inset) # check data new = Image.read(file=self.small_file_name, header=True, memmap=True) np_test.assert_equal(new.data[1, 10, :], numpy.arange(22050, 22060)) np_test.assert_equal(new.data[2, 6:16, 8], numpy.arange(31658, 32658, 100)) # check header old = Image.read(file=self.big_file_name, header=True, memmap=True) np_test.assert_almost_equal(new.pixelsize, old.pixelsize) np_test.assert_equal(len(new.header), len(old.header)) np_test.assert_equal(new.header[0:3], [3, 20, 10]) np_test.assert_equal(new.header[7:10], [3, 20, 10]) np_test.assert_almost_equal(new.header[10:13], numpy.array([3, 20, 10]) * old.pixelsize * 10, decimal=5) np_test.assert_equal(new.header[3:7], old.header[3:7]) np_test.assert_almost_equal(new.header[13:19], old.header[13:19]) np_test.assert_almost_equal(new.header[22:25], old.header[22:25]) np_test.assert_equal(True, new.header[25] == old.header[25]) #np_test.assert_string_equal(new.header[25], old.header[25]) header_len = len(new.header) np_test.assert_almost_equal(new.header[26:header_len - 1], old.header[26:header_len - 1]) np_test.assert_equal( True, new.header[header_len - 1] == old.header[header_len - 1])
def testRead(self): """ Tests read() """ # mrc = Image.read(file=os.path.normpath( os.path.join(self.dir, '../../io/test/new-head_int16.mrc'))) np_test.assert_equal(mrc.pixelsize, 0.4) np_test.assert_equal(mrc.fileFormat, 'mrc') np_test.assert_equal(mrc.data[14, 8, 10], -14) np_test.assert_equal(mrc.memmap, False) # mrc with header mrc = Image.read(file=os.path.normpath( os.path.join(self.dir, '../../io/test/new-head_int16.mrc')), header=True) np_test.assert_equal(mrc.header is None, False) np_test.assert_equal(mrc.pixelsize, 0.4) np_test.assert_equal(mrc.fileFormat, 'mrc') np_test.assert_equal(mrc.data[14, 8, 10], -14) np_test.assert_equal(mrc.memmap, False) # em with header em = Image.read(file=os.path.normpath( os.path.join(self.dir, '../../io/test/mac-file.em')), header=True) np_test.assert_equal(em.header is None, False) # with memmap mrc = Image.read(file=os.path.normpath( os.path.join(self.dir, '../../io/test/new-head_int16.mrc')), memmap=True) np_test.assert_equal(mrc.pixelsize, 0.4) np_test.assert_equal(mrc.fileFormat, 'mrc') np_test.assert_equal(mrc.data[14, 8, 10], -14) np_test.assert_equal(mrc.memmap, True)
def setUp(self): # make image self.image_1 = Image(numpy.arange(100).reshape(10,10)) # make cleft 1 cleft_data_1 = numpy.zeros((10,10), dtype=int) cleft_data_1[slice(1,7), slice(3,9)] = numpy.array(\ [[0, 5, 5, 5, 0, 0], [2, 5, 5, 5, 4, 4], [2, 5, 5, 5, 4, 4], [2, 2, 5, 5, 5, 4], [2, 5, 5, 5, 4, 4], [2, 5, 5, 5, 4, 4]]) self.bound_1 = Segment(cleft_data_1) self.cleft_1 = Cleft(data=self.bound_1.data, bound1Id=2, bound2Id=4, cleftId=5, copy=True, clean=False) self.cleft_1_ins = Cleft(data=self.bound_1.data[1:7, 3:9], bound1Id=2, bound2Id=4, cleftId=5, copy=True, clean=False) self.cleft_1_ins.inset = [slice(1,7), slice(3,9)] # make big cleft and image self.cleft_2, self.image_2 = common.make_cleft_layers_example()
def make_cleft_layers_example(): """ Makes and returns Cleft and Image objects that can be used together """ # make image image_2 = numpy.zeros((10,10)) image_2[1,:] = 5 image_2[2,:] = 10 for ind in range(5): image_2[ind+3, :] = ind + 3 image_2[8,:] = 12 image_2[9,:] = 6 image_cleft_2 = Image(image_2) # make cleft cleft_2 = numpy.zeros((10, 10), dtype=int) cleft_2[1:3, 2:8] = 1 cleft_2[3:8, 2:8] = 3 cleft_2[8:, 2:8] = 2 cleft_2 = Cleft(data=cleft_2, cleftId=3, bound1Id=1, bound2Id=2) return cleft_2, image_cleft_2
def findContacts(self, input, boundary, boundaryIds=None, boundaryLists=None, label=True, mask=0, freeSize=0, freeMode='add', count=False, saveLabels=False): """ Finds all contacts between segements of this instance and boundaries. If label is False, the segments are taken directly from input. If label is True, segments are formed by identifying clusters of connected elements of input among the forground (>0) elements (see self.label and scipy.ndimage.label methods). The connectivity is given by structEl or self.structEl structuring element (default connectivity 1). The segmentation is preformed only in the segmentation region defined as the intersection of the region defined by mask and the "free" region. The segmentation region is restricted to bound=mask if mask is an int, or to the positive elements of mask if mask is an array. If freeSize<=0, then the free regions is determined by mask only. If it is >0, areas around each boundary with max (Euclidean) distance freeSize are intersected/summmed (determined by freeMode) to define the free region. Uses self.makeFree method. If boundaryLists is None, the free region is formed using all boundaryIds. Otherwise, for each element of boundaryList (containing an array of boundary ids) a free region (and a segmentation region) is defined and the segmentation is preformed. In this case all segments are added up and then relabeled (to avoid the overwritting of some segments in case different segmentation regions overlap). However, some segments may be formed by addition of separate pieces and therefore be larger than what would be expected based on freeSize and freeMode parameters. Note: the data for input, boundary and mask has to have the same shape, their positioning (inset) is not taken into account, Arguments: - input: (pyto.core.Image, or ndarray) image to be segmented - boundary: ndarray with labeled boundaries, or a Segment instance - boundaryIds: list of ids defining boundaries in boundary - boundaryLists: an iterable (a list, tuple or a generator) containing lists (subsets) of boundaryIds. - label: if True label input, if False input already contains segments - freeSize: size of the free region. Either a list corresponding to ids (different size for each boundary) or a single number (used for all boundaries) - freeMode: Can be 'add' or 'intersect' and specifies that the free region is formed by the addition or intersection of the enlarged boundaries - mask: defines the region where new segments are allowed to be established. If int, the allowed region is where boundary==mask. Otherwise it's an array and the positive or True elements define the allowed region. - saveLabels: flag indicating if the array containing contact elements is saved (in contacts.labels) Sets attributes data, ids and id-related and creates a Contact object. Returns instance of Contacts. """ # wrap input if needed if isinstance(input, numpy.ndarray): input = Image(input) # set boundary object and flat boundaryIds if isinstance(boundary, Segment): if boundaryIds is not None: flat_bids = nested.flatten(boundaryIds) else: flat_bids = boundary.ids else: boundary = Segment(data=boundary, ids=boundaryIds) flat_bids = boundary.ids # make free regions and find segments within free regions if label: if boundaryLists is None: # for all boundaries at the same time free = boundary.makeFree(ids=flat_bids, size=freeSize, mode=freeMode, mask=mask, update=False) self.label(input=input, mask=free) else: # for each group of boundaries separately for bounds in boundaryLists: # ToDo: avoid making free if boundaries are too far free = boundary.makeFree(ids=bounds, size=freeSize, mode=freeMode, mask=mask, update=False) self.label(input=input, mask=free) self.label() else: self.setData(input.data, copy=False) # find all contacts contacts = Contact() contacts.findContacts(segment=self, boundary=boundary, boundaryIds=flat_bids, contactStructEl=self.contactStructEl, saveLabels=saveLabels) return contacts
def label(self, input, mask=None, structEl=None, update=False, relabel=False): """ Segments input by identifying (connected) segments in input. The connected segments are formed from elements of input that are >0 or True (if input is binary) Only the elements of input that are >0 and where mask >0 are labeled. The segmented image has the same offset and inset as mask If update is False self.data is set to the new segments array and the positioning attributes (offset, inset, fullShape) are set to the same as in mask. If update is True the new segments are added to the exisiting self.data, after they are repositioned to fit the current offset and inset. In this case and if relabel is False, new segemnts are simply placed on top the exsisting may overwrite the already existing segments of self.data. Otherwise, the existing and the new segments are combined and relabeled (the old segments are likely to change their ids). See self.add. Arguments: - input: (pyto.core.Image, or ndarray) image to be segmented - mask: (pyto.core.Image or ndarray, mask that defines the segmentation region - structEl: instance of StructEl, structuring elements that defines the connectivity of segments (in None, self.structEl is used) - update: if True new segments are added to the existing self.data - relabel: determines what to do if the segments generated here overlap with the existing segments (self.data). Sets: - self.data: array containing labeled segments with shape of the intersection of input and mask.data with mask.offset. """ # set input and structEl if isinstance(input, numpy.ndarray): input = Image(input) if structEl is None: structEl = self.structEl # mask if isinstance(mask, numpy.ndarray): mask = Image(mask) # restrict input to an inset corresponding to mask adj_input_inst = input.usePositioning(image=mask, new=True) adj_input = adj_input_inst.data #input_inset = mask.findImageSlice(image=input) #adj_input = input.data[input_inset] # mask (but don't overwrite) input adj_input = numpy.where(mask.data > 0, adj_input, 0) # find segments labeled_data, nSegments = \ StructEl.label(input=adj_input, structure=structEl) # add new segments and update if update: # position labeled_slice to this instance adj_input_inst.usePositioning(image=self, intersect=False) #labeled = self.__class__(data=labeled_data) #labeled.copyPositioning(mask) #labeled_slice = self.findImageSlice(labeled) # add labeled_slice self.add(new=adj_input_inst.data, relabel=relabel) #self.add(new=labeled_data[labeled_slice], relabel=relabel) else: # set data to labeled_data and positioning to be the same as # in mask self.setData(data=labeled_data, copy=False) self.copyPositioning(adj_input_inst)
class TestImage(np_test.TestCase): """ """ def setUp(self): # make image array = numpy.arange(100).reshape(10, 10) self.image = Image(data=array) # set attributes self.image.xxx = [1, 2, 3] self.image.yyy = [4, 5, 6] large_array = array[1:9, 1:8].copy() self.large = Image(data=large_array) # set absolute path to current dir working_dir = os.getcwd() file_dir, name = os.path.split(__file__) self.dir = os.path.join(working_dir, file_dir) # image file names self.big_file_name = os.path.join(self.dir, '../../io/test/big_head.mrc') self.small_file_name = os.path.join(self.dir, '../../io/test/small.mrc') self.modified_file_name_mrc = os.path.join( self.dir, '../../io/test/modified.mrc') self.modified_file_name_raw = os.path.join( self.dir, '../../io/test/modified.raw') def testRelativeToAbsoluteInset(self): """ Tests relativeToAbsoluteInset() """ image = deepcopy(self.image) image_inset = [slice(2, 5), slice(4, 6)] image.useInset(inset=image_inset, mode='abs') # intersect inset = [slice(1, 5), slice(-3, -2)] res = image.relativeToAbsoluteInset(inset=inset) np_test.assert_equal(res, [slice(3, 7), slice(1, 2)]) # intersect below 0 inset = [slice(-4, -2), slice(3, 5)] res = image.relativeToAbsoluteInset(inset=inset) np_test.assert_equal(res, [slice(-2, 0), slice(7, 9)]) def testAbsoluteToRelativeInset(self): """ Tests absoluteToRelativeInset() """ image = deepcopy(self.image) image_inset = [slice(2, 5), slice(4, 6)] image.useInset(inset=image_inset, mode='abs') # intersect inset = [slice(3, 7), slice(1, 2)] res = image.absoluteToRelativeInset(inset=inset) np_test.assert_equal(res, [slice(1, 5), slice(-3, -2)]) # intersect below 0 inset = [slice(-2, 0), slice(7, 9)] res = image.absoluteToRelativeInset(inset=inset) np_test.assert_equal(res, [slice(-4, -2), slice(3, 5)]) # intersect below 0 inset = [slice(-2, 3), slice(7, 9)] res = image.absoluteToRelativeInset(inset=inset) np_test.assert_equal(res, [slice(-4, 1), slice(3, 5)]) def testUseInset(self): """ """ # absolute inset image = deepcopy(self.image) inset = [slice(2, 5), slice(4, 6)] image.useInset(inset=inset, mode='abs') desired_array = numpy.array([[24, 25], [34, 35], [44, 45]]) np_test.assert_equal(image.data, desired_array) # absolute inset no update image = deepcopy(self.image) inset = [slice(2, 5), slice(4, 6)] new_data = image.useInset(inset=inset, mode='abs', update=False) desired_array = numpy.array([[24, 25], [34, 35], [44, 45]]) np_test.assert_equal(new_data, desired_array) np_test.assert_equal(image.data, self.image.data) # absolute inset from an inset large = deepcopy(self.image) inset = [slice(2, 5), slice(4, 6)] large.useInset(inset=inset, mode='abs') desired_array = numpy.array([[24, 25], [34, 35], [44, 45]]) np_test.assert_equal(large.data, desired_array) # relative inset large = deepcopy(self.large) inset = [slice(2, 5), slice(4, 6)] large.useInset(inset=inset, mode='rel') desired_array = numpy.array([[35, 36], [45, 46], [55, 56]]) np_test.assert_equal(large.data, desired_array) # use full full_inset = inset = [slice(0, 10), slice(0, 10)] image = deepcopy(self.image) inset = [slice(2, 5), slice(4, 6)] image.saveFull() image.useInset(inset=inset, mode='abs') image.data[0, 0] = 100 image.useInset(inset=full_inset, mode='abs', useFull=True) np_test.assert_equal(image.data[2, 4], 100) np_test.assert_equal(image.data[9, 9], 99) # do not allow to use full full_inset = inset = [slice(0, 10), slice(0, 10)] image = deepcopy(self.image) inset = [slice(2, 5), slice(4, 6)] image.saveFull() image.useInset(inset=inset, mode='abs') kwargs = {'inset': full_inset, 'mode': 'abs', 'useFull': False} self.assertRaises(ValueError, image.useInset, **kwargs) # expand full_inset = inset = [slice(0, 10), slice(0, 10)] image = deepcopy(self.image) inset = [slice(2, 5), slice(4, 6)] image.useInset(inset=inset, mode='abs') image.data[0, 0] = 100 image.useInset(inset=full_inset, mode='abs', expand=True) np_test.assert_equal(image.data[2, 4], 100) np_test.assert_equal(image.data[9, 9], 0) # expand, no update full_inset = [slice(0, 10), slice(0, 10)] med_inset = [slice(1, 5), slice(4, 7)] inset = [slice(2, 5), slice(4, 6)] image = deepcopy(self.image) image.useInset(inset=inset, mode='abs') image.data[0, 0] = 100 new_data = image.useInset(inset=med_inset, mode='abs', expand=True, update=False) np_test.assert_equal(new_data[2, 1], 35) np_test.assert_equal(new_data[0, 2], 0) np_test.assert_equal(image.inset, inset) new_data = image.useInset(inset=full_inset, mode='abs', expand=True, update=False) np_test.assert_equal(new_data[2, 4], 100) np_test.assert_equal(new_data[9, 9], 0) np_test.assert_equal(image.data[0, 0], 100) np_test.assert_equal(image.data[1:, :], self.image.data[tuple(inset)][1:, :]) np_test.assert_equal(image.inset, inset) # use full, expand, update image = deepcopy(self.image) image.useInset(inset=inset, mode='abs') image.useInset(inset=med_inset, mode='abs', useFull=True, expand=True, update=True) np_test.assert_equal(image.data[2, 1], 35) np_test.assert_equal(image.data[0, 2], 16) np_test.assert_equal(image.inset, med_inset) # use full, expand, no update image = deepcopy(self.image) image.useInset(inset=inset, mode='abs') new_data = image.useInset(inset=med_inset, mode='abs', useFull=True, expand=True, update=False) np_test.assert_equal(new_data[2, 1], 35) np_test.assert_equal(new_data[0, 2], 16) np_test.assert_equal(image.inset, inset) # use full, expand, update, no overlap image = deepcopy(self.image) inset = [slice(2, 5), slice(4, 6)] inset2 = [slice(4, 6), slice(6, 9)] image.useInset(inset=inset, mode='abs') image.useInset(inset=inset2, mode='abs', useFull=True, expand=True, update=True) np_test.assert_equal(image.inset, inset2) desired = numpy.array([[46, 47, 48], [56, 57, 58]]) np_test.assert_equal(image.data, desired) # no use full, expand, update, no overlap image = deepcopy(self.image) inset = [slice(2, 5), slice(4, 6)] inset2 = [slice(4, 6), slice(6, 9)] image.useInset(inset=inset, mode='abs') image.useInset(inset=inset2, mode='abs', useFull=False, expand=True, update=True) np_test.assert_equal(image.inset, inset2) np_test.assert_equal(image.data, numpy.zeros((2, 3))) # no use full, expand, update, inset 0 image = deepcopy(self.image) inset = [slice(2, 5), slice(4, 6)] inset2 = [slice(0, 0), slice(0, 0)] image.useInset(inset=inset, mode='abs') image.useInset(inset=inset2, mode='abs', useFull=False, expand=True, update=True) np_test.assert_equal(image.inset, inset2) np_test.assert_equal(image.data, numpy.zeros((0, 0))) def testExpandInset(self): """ Tests expandInset() """ # expand, update image = deepcopy(self.image) inset = [slice(2, 5), slice(4, 6)] image.useInset(inset=inset, mode='abs') new_inset = [slice(1, 6), slice(2, 6)] image.expandInset(inset=new_inset, update=True) np_test.assert_equal(image.inset, new_inset) new_data = numpy.array([[0, 0, 0, 0], [0, 0, 24, 25], [0, 0, 34, 35], [0, 0, 44, 45], [0, 0, 0, 0]]) np_test.assert_equal(image.data, new_data) # expand, no update image = deepcopy(self.image) inset = [slice(2, 5), slice(4, 6)] image.useInset(inset=inset, mode='abs') new_inset = [slice(1, 6), slice(2, 6)] new_data = image.expandInset(inset=new_inset, update=False) np_test.assert_equal(image.inset, inset) desired_data = numpy.array([[24, 25], [34, 35], [44, 45]]) np_test.assert_equal(image.data, desired_data) desired_data = numpy.array([[0, 0, 0, 0], [0, 0, 24, 25], [0, 0, 34, 35], [0, 0, 44, 45], [0, 0, 0, 0]]) np_test.assert_equal(new_data, desired_data) # partial overlap image = deepcopy(self.image) inset = [slice(2, 5), slice(4, 6)] image.useInset(inset=inset, mode='abs') new_inset = [slice(3, 6), slice(2, 5)] new_data = image.expandInset(inset=new_inset, update=True, value=9) np_test.assert_equal(image.inset, new_inset) desired_data = numpy.array([[9, 9, 34], [9, 9, 44], [9, 9, 9]]) np_test.assert_equal(image.data, desired_data) # completely inside image = deepcopy(self.image) inset = [slice(2, 5), slice(4, 6)] image.useInset(inset=inset, mode='abs') new_inset = [slice(3, 5), slice(4, 5)] new_data = image.expandInset(inset=new_inset, update=True) np_test.assert_equal(image.inset, new_inset) desired_data = numpy.array([[34], [44]]) np_test.assert_equal(image.data, desired_data) # completely inside image = deepcopy(self.image) inset = [slice(2, 5), slice(3, 6)] image.useInset(inset=inset, mode='abs') new_inset = [slice(2, 3), slice(4, 6)] new_data = image.expandInset(inset=new_inset, update=True) np_test.assert_equal(image.inset, new_inset) desired_data = numpy.array([[24, 25]]) np_test.assert_equal(image.data, desired_data) # completely outside image = deepcopy(self.image) inset = [slice(2, 5), slice(3, 6)] image.useInset(inset=inset, mode='abs') new_inset = [slice(5, 7), slice(7, 10)] new_data = image.expandInset(inset=new_inset, update=True) np_test.assert_equal(image.inset, new_inset) desired_data = numpy.zeros((2, 3)) np_test.assert_equal(image.data, desired_data) # 0 image = deepcopy(self.image) inset = [slice(2, 5), slice(3, 6)] image.useInset(inset=inset, mode='abs') new_inset = [slice(0, 0), slice(0, 0)] new_data = image.expandInset(inset=new_inset, update=True) np_test.assert_equal(image.inset, new_inset) desired_data = numpy.zeros((0, 0)) np_test.assert_equal(image.data, desired_data) def testIsInside(self): """ Tests isInside() """ # inset inside self.inset image = deepcopy(self.image) inset = [slice(2, 5), slice(3, 7)] inset2 = [slice(3, 5), slice(3, 4)] image.useInset(inset=inset, mode='abs') res = image.isInside(inset=inset2) np_test.assert_equal(res, True) # self.inset inside inset image = deepcopy(self.image) inset = [slice(2, 5), slice(3, 7)] inset2 = [slice(3, 5), slice(3, 4)] image.useInset(inset=inset2, mode='abs') res = image.isInside(inset=inset) np_test.assert_equal(res, False) # overlap inset = [slice(2, 5), slice(3, 7)] inset2 = [slice(3, 8), slice(2, 4)] image = deepcopy(self.image) image.useInset(inset=inset2, mode='abs') res = image.isInside(inset=inset) np_test.assert_equal(res, False) # overlap inset = [slice(2, 5), slice(3, 7)] inset2 = [slice(1, 6), slice(4, 7)] image = deepcopy(self.image) image.useInset(inset=inset2, mode='abs') res = image.isInside(inset=inset) np_test.assert_equal(res, False) # no overlap inset = [slice(2, 5), slice(3, 7)] inset2 = [slice(5, 6), slice(6, 10)] image = deepcopy(self.image) image.useInset(inset=inset2, mode='abs') res = image.isInside(inset=inset) np_test.assert_equal(res, False) def testHasOvelap(self): """ Tests hasOverlap() """ # inset inside self.inset image = deepcopy(self.image) inset = [slice(2, 5), slice(3, 7)] inset2 = [slice(3, 5), slice(3, 4)] image.useInset(inset=inset, mode='abs') res = image.hasOverlap(inset=inset2) np_test.assert_equal(res, True) # self.inset inside inset image = deepcopy(self.image) inset = [slice(2, 5), slice(3, 7)] inset2 = [slice(3, 5), slice(3, 4)] image.useInset(inset=inset2, mode='abs') res = image.hasOverlap(inset=inset) np_test.assert_equal(res, True) # overlap inset = [slice(2, 5), slice(3, 7)] inset2 = [slice(4, 8), slice(2, 4)] image = deepcopy(self.image) image.useInset(inset=inset2, mode='abs') res = image.hasOverlap(inset=inset) np_test.assert_equal(res, True) # overlap inset = [slice(2, 5), slice(3, 7)] inset2 = [slice(1, 6), slice(4, 7)] image = deepcopy(self.image) image.useInset(inset=inset, mode='abs') res = image.hasOverlap(inset=inset2) np_test.assert_equal(res, True) # no overlap inset = [slice(2, 5), slice(3, 7)] inset2 = [slice(5, 6), slice(5, 10)] image = deepcopy(self.image) image.useInset(inset=inset, mode='abs') res = image.hasOverlap(inset=inset2) np_test.assert_equal(res, False) # no overlap inset = [slice(2, 5), slice(3, 7)] inset2 = [slice(5, 6), slice(8, 10)] image = deepcopy(self.image) image.useInset(inset=inset, mode='abs') res = image.hasOverlap(inset=inset2) np_test.assert_equal(res, False) # no overlap inset = [slice(2, 5), slice(3, 7)] inset2 = [slice(1, 5), slice(7, 10)] image = deepcopy(self.image) image.useInset(inset=inset, mode='abs') res = image.hasOverlap(inset=inset2) np_test.assert_equal(res, False) def testFindEnclosingInset(self): """ Tests findEnclosingInset() """ # inset2 inset = self.image.findEnclosingInset( inset=[slice(2, 5), slice(1, 3)], inset2=[slice(1, 4), slice(3, 6)]) np_test.assert_equal(inset, [slice(1, 5), slice(1, 6)]) # self.inset image = deepcopy(self.image) image.useInset(inset=[slice(4, 6), slice(0, 4)], mode='abs') inset = image.findEnclosingInset(inset=[slice(2, 5), slice(1, 3)]) np_test.assert_equal(inset, [slice(2, 6), slice(0, 4)]) # inset2 inside inset inset = [slice(2, 5), slice(3, 7)] inset2 = [slice(3, 5), slice(3, 4)] res = self.image.findEnclosingInset(inset=inset, inset2=inset2) np_test.assert_equal(res, inset) # inset inside inset2 inset = [slice(2, 5), slice(3, 7)] inset2 = [slice(3, 5), slice(3, 4)] res = self.image.findEnclosingInset(inset=inset2, inset2=inset) np_test.assert_equal(res, inset) # overlap inset = [slice(2, 5), slice(3, 7)] inset2 = [slice(3, 8), slice(2, 4)] res = self.image.findEnclosingInset(inset=inset, inset2=inset2) np_test.assert_equal(res, [slice(2, 8), slice(2, 7)]) # overlap inset = [slice(2, 5), slice(3, 7)] inset2 = [slice(1, 6), slice(4, 7)] res = self.image.findEnclosingInset(inset=inset, inset2=inset2) np_test.assert_equal(res, [slice(1, 6), slice(3, 7)]) # no overlap inset = [slice(2, 5), slice(3, 7)] inset2 = [slice(5, 6), slice(8, 10)] res = self.image.findEnclosingInset(inset=inset, inset2=inset2) np_test.assert_equal(res, [slice(2, 6), slice(3, 10)]) def testFindIntersectingInset(self): """ Tests findIntersectingInset() """ # inset2 inset = self.image.findIntersectingInset( inset=[slice(2, 5), slice(1, 3)], inset2=[slice(1, 4), slice(3, 6)]) np_test.assert_equal(inset, [slice(2, 4), slice(3, 3)]) # self.inset image = deepcopy(self.image) image.useInset(inset=[slice(4, 6), slice(0, 4)], mode='abs') inset = image.findIntersectingInset(inset=[slice(2, 5), slice(1, 3)]) np_test.assert_equal(inset, [slice(4, 5), slice(1, 3)]) # no overlap inset = [slice(2, 5), slice(3, 7)] inset2 = [slice(5, 6), slice(8, 10)] res = self.image.findIntersectingInset(inset=inset, inset2=inset2) np_test.assert_equal(res, [slice(5, 5), slice(8, 7)]) def testNewFromInset(self): """ Tests if copy/deepcopy of attrbutes works properly """ # tests default args inset = [slice(2, 5), slice(4, 6)] new = self.image.newFromInset(inset=inset, copyData=True, deepcp=True) new.xxx[1] = 12 new.yyy[1] = 15 desired_array = numpy.array([[24, 25], [34, 35], [44, 45]]) np_test.assert_equal(new.data, desired_array) np_test.assert_equal(new.inset, inset) np_test.assert_equal(self.image.xxx, [1, 2, 3]) np_test.assert_equal(self.image.yyy, [4, 5, 6]) inset = [slice(2, 5), slice(4, 6)] # tests if copy/deepcopy of attrbutes works properly new = self.image.newFromInset(inset=inset, copyData=True, deepcp=True, noDeepcp=['yyy']) new.xxx[1] = 12 new.yyy[1] = 15 desired_array = numpy.array([[24, 25], [34, 35], [44, 45]]) np_test.assert_equal(new.data, desired_array) np_test.assert_equal(new.inset, inset) np_test.assert_equal(self.image.xxx, [1, 2, 3]) np_test.assert_equal(self.image.yyy, [4, 15, 6]) def testRead(self): """ Tests read() """ # mrc = Image.read(file=os.path.normpath( os.path.join(self.dir, '../../io/test/new-head_int16.mrc'))) np_test.assert_equal(mrc.pixelsize, 0.4) np_test.assert_equal(mrc.fileFormat, 'mrc') np_test.assert_equal(mrc.data[14, 8, 10], -14) np_test.assert_equal(mrc.memmap, False) # mrc with header mrc = Image.read(file=os.path.normpath( os.path.join(self.dir, '../../io/test/new-head_int16.mrc')), header=True) np_test.assert_equal(mrc.header is None, False) np_test.assert_equal(mrc.pixelsize, 0.4) np_test.assert_equal(mrc.fileFormat, 'mrc') np_test.assert_equal(mrc.data[14, 8, 10], -14) np_test.assert_equal(mrc.memmap, False) # em with header em = Image.read(file=os.path.normpath( os.path.join(self.dir, '../../io/test/mac-file.em')), header=True) np_test.assert_equal(em.header is None, False) # with memmap mrc = Image.read(file=os.path.normpath( os.path.join(self.dir, '../../io/test/new-head_int16.mrc')), memmap=True) np_test.assert_equal(mrc.pixelsize, 0.4) np_test.assert_equal(mrc.fileFormat, 'mrc') np_test.assert_equal(mrc.data[14, 8, 10], -14) np_test.assert_equal(mrc.memmap, True) def test_modify(self): """ Tests modify(), implicitely tests reading and writting mrc header by pyto.io.ImageIO """ # modify mrc image def fun_1(image): dat = image.data + 1 return dat # requires Image.modify(memmap=False) #def fun_1(image): return image.data + 1 Image.modify(old=self.big_file_name, new=self.modified_file_name_mrc, fun=fun_1, memmap=True) new = Image.read(file=self.modified_file_name_mrc, header=True, memmap=True) old = Image.read(file=self.big_file_name, header=True, memmap=True) # check data np_test.assert_equal(new.data[1, 10, :], numpy.arange(11001, 11101)) np_test.assert_equal(new.data[2, :, 15], numpy.arange(20016, 30016, 100)) # check header np_test.assert_almost_equal(new.pixelsize, old.pixelsize) np_test.assert_almost_equal(new.header[0:19], old.header[0:19]) np_test.assert_almost_equal(new.header[22:25], old.header[22:25]) np_test.assert_equal(True, new.header[25] == old.header[25]) header_len = len(new.header) np_test.assert_almost_equal(new.header[26:header_len - 1], old.header[26:header_len - 1]) np_test.assert_equal( True, new.header[header_len - 1] == old.header[header_len - 1]) # modify mrc image and write as raw def fun_v(image, value): data = image.data + value return data modified = Image.modify(old=self.big_file_name, new=self.modified_file_name_raw, fun=fun_v, fun_kwargs={'value': 4}) new = Image.read(file=self.modified_file_name_raw, shape=modified.data.shape, dataType=modified.data.dtype, memmap=True) old = Image.read(file=self.big_file_name, header=True, memmap=True) # check data np_test.assert_equal(new.data[1, 10, :], numpy.arange(11004, 11104)) np_test.assert_equal(new.data[2, :, 15], numpy.arange(20019, 30019, 100)) def test_cut(self): """ Tests cut(), implicitely tests reading and writting mrc header by pyto.io.ImageIO """ # cut image inset = [slice(1, 4), slice(10, 30), slice(50, 60)] Image.cut(old=self.big_file_name, new=self.small_file_name, inset=inset) # check data new = Image.read(file=self.small_file_name, header=True, memmap=True) np_test.assert_equal(new.data[1, 10, :], numpy.arange(22050, 22060)) np_test.assert_equal(new.data[2, 6:16, 8], numpy.arange(31658, 32658, 100)) # check header old = Image.read(file=self.big_file_name, header=True, memmap=True) np_test.assert_almost_equal(new.pixelsize, old.pixelsize) np_test.assert_equal(len(new.header), len(old.header)) np_test.assert_equal(new.header[0:3], [3, 20, 10]) np_test.assert_equal(new.header[7:10], [3, 20, 10]) np_test.assert_almost_equal(new.header[10:13], numpy.array([3, 20, 10]) * old.pixelsize * 10, decimal=5) np_test.assert_equal(new.header[3:7], old.header[3:7]) np_test.assert_almost_equal(new.header[13:19], old.header[13:19]) np_test.assert_almost_equal(new.header[22:25], old.header[22:25]) np_test.assert_equal(True, new.header[25] == old.header[25]) #np_test.assert_string_equal(new.header[25], old.header[25]) header_len = len(new.header) np_test.assert_almost_equal(new.header[26:header_len - 1], old.header[26:header_len - 1]) np_test.assert_equal( True, new.header[header_len - 1] == old.header[header_len - 1]) def tearDown(self): """ Remove temporary files """ try: os.remove(self.small_file_name) except OSError: pass try: os.remove(self.modified_file_name_mrc) except OSError: pass try: os.remove(self.modified_file_name_raw) except OSError: pass