def rotate(obj, axis, angle, origin=None): ''' Rotation around unit vector following the right hand rule Input: obj : obj to be rotated (e.g. tree, neuron) axis : unit vector for the axis of rotation angle : rotation angle in rads Returns: A copy of the object with the applied translation. ''' R = _rodriguesToRotationMatrix(axis, angle) if isinstance(obj, Tree): res_tree = make_copy(obj) _affineTransformTree(res_tree, R, np.zeros(3), origin) return res_tree elif isinstance(obj, Neuron): res_nrn = obj.copy() _affineTransformNeuron(res_nrn, R, np.zeros(3), origin) return res_nrn
def test_make_copy(): tree_copy = make_copy(REF_TREE3) # assert that the two trees have the same values # first by total nodes nt.assert_true(len(list(ipreorder(tree_copy))) == len(list(ipreorder(REF_TREE3)))) # then node by node for val1, val2 in izip(val_iter(ipreorder(tree_copy)), val_iter(ipreorder(REF_TREE3))): nt.assert_true(all(val1 == val2)) # assert that the tree values do not have the same identity for val1, val2 in izip(val_iter(ipreorder(tree_copy)), val_iter(ipreorder(REF_TREE3))): nt.assert_false(val1 is val2) # create a deepcopy of the original tree for validation validation_tree = deepcopy(REF_TREE3) # modify copied tree tree_copy.value[0:3] = np.array([1000.0, 1000.0, -1000.0]) tree_copy.children[0].add_child(Tree(np.array([0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0]))) # check if anything changed in REF_TREE3 with respect to the validation deepcopy nt.assert_true(len(list(ipreorder(validation_tree))) == len(list(ipreorder(REF_TREE3)))) for val1, val2 in izip(val_iter(ipreorder(REF_TREE3)), val_iter(ipreorder(validation_tree))): nt.assert_true(all(val1 == val2))
def translate(obj, t): ''' Translate object of supported type. Input : obj : object with one of the following types: 'TreeType', 'Neuron', 'ezyNeuron' Returns: copy of the object with the applied translation ''' if isinstance(obj, Tree): res_tree = make_copy(obj) _affineTransformTree(res_tree, np.identity(3), t) return res_tree elif isinstance(obj, Neuron): res_nrn = obj.copy() _affineTransformNeuron(res_nrn, np.identity(3), t) return res_nrn
def test_affineTransformTree(): # rotate 180 and translate, translate back and rotate 180 # change origin as well new_orig = np.array([10. , 45., 50.]) t = np.array([0.1, - 0.1, 40.3]) R = gtr._rodriguesToRotationMatrix(TEST_UVEC, np.pi) # change origin, rotate 180 and translate m = make_copy(TREE) gtr._affineTransformTree(m, R, t, origin=new_orig) # translate back gtr._affineTransformTree(m, np.identity(3), -t, origin=np.zeros(3)) # rotate back gtr._affineTransformTree(m, R, np.zeros(3), origin=new_orig) _evaluate(TREE, m, lambda x, y: np.allclose(x, y))
def copy(self): '''Return a copy of the Neuron object. ''' return Neuron(deepcopy(self.soma), [make_copy(neu) for neu in self.neurites], self.name)