def testComparisonFailureTransplantingALeafNode(self): """Remap each leaf node in a complex swc file, onto different internal nodes of the morphology, and check that the morphology is considered different """ testSrcsPath = LocMgr().get_test_srcs_path() srcSWCFile = Join(testSrcsPath, "swc_srcs/28o_spindle20aFI.CNG.swc") m = MorphologyArray.fromSWC(srcSWCFile) # Find the leaf nodes: leaf_nodes = m.get_leaf_vertices_indices() for new_parent in [0, 10, 20, leaf_nodes[-1]]: for l in leaf_nodes[:-1]: v = m._vertices.copy() c = m._connectivity.copy() # Rewrite the connectivity matrix, mapping the # leaf to a new_parent: c[c == l] = new_parent mNew = MorphologyArray(vertices=v, connectivity=c) assert not MorphArrayComparison.are_same( m, mNew, max_node_distance=0.00001)
def testComparisonFailureAddingEpsilon1(self): """Load simple .swc snippets, and change each [x, y, z, r] value in the vertices matrix individually, to check that it is not the same. """ m = MorphologyArray.fromSWC(StringIO(swc_srcs['Ci'])) for i in range(len(m)): for j in range(4): m1 = MorphologyArray.fromSWC(StringIO(swc_srcs['Ci'])) m1._vertices[i, j] = m1._vertices[i, j] + 0.01 assert MorphArrayComparison.are_same(m, m1, max_node_distance= 0.02) assert not MorphArrayComparison.are_same(m, m1, max_node_distance= 0.005) m1 = MorphologyArray.fromSWC(StringIO(swc_srcs['Ci'])) m1._vertices[i, j] = m1._vertices[i, j] - 0.001 assert MorphArrayComparison.are_same(m, m1, max_node_distance= 0.002) assert not MorphArrayComparison.are_same(m, m1, max_node_distance= 0.0005) m1 = MorphologyArray.fromSWC(StringIO(swc_srcs['Cii'])) m1._vertices[i, j] = m1._vertices[i, j] + 0.01 assert MorphArrayComparison.are_same(m, m1, max_node_distance= 0.02) assert not MorphArrayComparison.are_same(m, m1, max_node_distance= 0.005) m1 = MorphologyArray.fromSWC(StringIO(swc_srcs['Cii'])) m1._vertices[i, j] = m1._vertices[i, j] - 0.001 assert MorphArrayComparison.are_same(m, m1, max_node_distance= 0.002) assert not MorphArrayComparison.are_same(m, m1, max_node_distance= 0.0005)
def testComparisonFailureAddingEpsilon2(self): """Load and complex .swc file, and change each [x, y, z, r] value in the vertices matrix individually, to check that it is not the same. """ testSrcsPath = LocMgr().get_test_srcs_path() srcSWCFile = Join(testSrcsPath, "swc_srcs/28o_spindle20aFI.CNG.swc") m = MorphologyArray.fromSWC(srcSWCFile) MFRandom.seed(0) for i in range(len(m)): print i, len(m) for j in range(4): # Only test 2% of cases: if not np.random.rand() < 0.02: continue m1 = MorphologyArray.fromSWC(srcSWCFile) m1._vertices[i, j] = m1._vertices[i, j] + 0.01 assert MorphArrayComparison.are_same(m, m1, max_node_distance=0.02) assert not MorphArrayComparison.are_same( m, m1, max_node_distance=0.005)
def testComparisonFailureDifferentMorphologies(self): """Verify simple .swc snippets are different to each other""" m1 = MorphologyArray.fromSWC(StringIO(swc_srcs['A'])) m2 = MorphologyArray.fromSWC(StringIO(swc_srcs['B'])) m3 = MorphologyArray.fromSWC(StringIO(swc_srcs['Ci'])) assert not MorphArrayComparison.are_same(m1, m2, max_node_distance= 0.00001) assert not MorphArrayComparison.are_same(m1, m3, max_node_distance= 0.00001) assert not MorphArrayComparison.are_same(m2, m3, max_node_distance= 0.00001)
def testComparisonFailureDifferentMorphologies(self): """Verify simple .swc snippets are different to each other""" m1 = MorphologyArray.fromSWC(StringIO(swc_srcs['A'])) m2 = MorphologyArray.fromSWC(StringIO(swc_srcs['B'])) m3 = MorphologyArray.fromSWC(StringIO(swc_srcs['Ci'])) assert not MorphArrayComparison.are_same( m1, m2, max_node_distance=0.00001) assert not MorphArrayComparison.are_same( m1, m3, max_node_distance=0.00001) assert not MorphArrayComparison.are_same( m2, m3, max_node_distance=0.00001)
def load_swc_single(cls, src, name=None): dtype = {'names': ('id', 'type', 'x', 'y', 'z', 'r', 'pid'), 'formats': ('int32', 'int32', 'f4', 'f4', 'f4', 'f4', 'int32') } swc_data_raw = np.loadtxt(src, dtype=dtype) if len(np.nonzero(swc_data_raw['pid'] == -1)) != 1: assert False, "Unexpected number of id'errstr of -1 in file" # We might not nessesarily have continuous indices in the # SWC file, so lets convert them: index_to_id = swc_data_raw['id'] id_to_index_dict = dict([(_id, index) for (index, _id) in enumerate(index_to_id)]) if len(id_to_index_dict) != len(index_to_id): errstr = "Internal Error Loading SWC: Index and ID map are different lengths." errstr += " [ID:%swc_data_raw, Index:%swc_data_raw]" % (len(index_to_id), len(id_to_index_dict)) raise MorphologyImportError(errstr) # Vertices are easy: vertices = swc_data_raw[['x', 'y', 'z', 'r']] vertices = np.vstack([swc_data_raw['x'], swc_data_raw['y'], swc_data_raw['z'], swc_data_raw['r']]).T # Connections need to translate id_to_index: connection_indices = [(id_to_index_dict[ID], id_to_index_dict[parent_id]) for ID, parent_id in swc_data_raw[['id', 'pid']] if parent_id != -1] # Types are specified per connection: section_types = [swctype for ID, swctype, parent_id in swc_data_raw[['id', 'type', 'pid']] if parent_id != -1] return MorphologyArray(vertices=vertices, connectivity=connection_indices, section_types=section_types, dummy_vertex_index=0, name=name)
def testComparisonFailureTransplantingALeafNode(self): """Remap each leaf node in a complex swc file, onto different internal nodes of the morphology, and check that the morphology is considered different """ testSrcsPath = LocMgr().get_test_srcs_path() srcSWCFile = Join(testSrcsPath, "swc_srcs/28o_spindle20aFI.CNG.swc") m = MorphologyArray.fromSWC(srcSWCFile) # Find the leaf nodes: leaf_nodes = m.get_leaf_vertices_indices() for new_parent in [0, 10, 20, leaf_nodes[-1]]: for l in leaf_nodes[:-1]: v = m._vertices.copy() c = m._connectivity.copy() # Rewrite the connectivity matrix, mapping the # leaf to a new_parent: c[c==l]=new_parent mNew = MorphologyArray(vertices=v, connectivity=c) assert not MorphArrayComparison.are_same(m, mNew, max_node_distance= 0.00001)
def testComparisonFailureAddingEpsilon1(self): """Load simple .swc snippets, and change each [x, y, z, r] value in the vertices matrix individually, to check that it is not the same. """ m = MorphologyArray.fromSWC(StringIO(swc_srcs['Ci'])) for i in range(len(m)): for j in range(4): m1 = MorphologyArray.fromSWC(StringIO(swc_srcs['Ci'])) m1._vertices[i, j] = m1._vertices[i, j] + 0.01 assert MorphArrayComparison.are_same(m, m1, max_node_distance=0.02) assert not MorphArrayComparison.are_same( m, m1, max_node_distance=0.005) m1 = MorphologyArray.fromSWC(StringIO(swc_srcs['Ci'])) m1._vertices[i, j] = m1._vertices[i, j] - 0.001 assert MorphArrayComparison.are_same(m, m1, max_node_distance=0.002) assert not MorphArrayComparison.are_same( m, m1, max_node_distance=0.0005) m1 = MorphologyArray.fromSWC(StringIO(swc_srcs['Cii'])) m1._vertices[i, j] = m1._vertices[i, j] + 0.01 assert MorphArrayComparison.are_same(m, m1, max_node_distance=0.02) assert not MorphArrayComparison.are_same( m, m1, max_node_distance=0.005) m1 = MorphologyArray.fromSWC(StringIO(swc_srcs['Cii'])) m1._vertices[i, j] = m1._vertices[i, j] - 0.001 assert MorphArrayComparison.are_same(m, m1, max_node_distance=0.002) assert not MorphArrayComparison.are_same( m, m1, max_node_distance=0.0005)
def testComparisonFailureAddingEpsilon2(self): """Load and complex .swc file, and change each [x, y, z, r] value in the vertices matrix individually, to check that it is not the same. """ testSrcsPath = LocMgr().get_test_srcs_path() srcSWCFile = Join(testSrcsPath, "swc_srcs/28o_spindle20aFI.CNG.swc") m = MorphologyArray.fromSWC(srcSWCFile) MFRandom.seed(0) for i in range(len(m)): print i, len(m) for j in range(4): # Only test 2% of cases: if not np.random.rand() < 0.02: continue m1 = MorphologyArray.fromSWC(srcSWCFile) m1._vertices[i, j] = m1._vertices[i, j] + 0.01 assert MorphArrayComparison.are_same(m, m1, max_node_distance= 0.02) assert not MorphArrayComparison.are_same(m, m1, max_node_distance= 0.005)
def testComparisonSucessful(self): """Verify simple .swc snippets are the same""" # Loading m1 = MorphologyArray.fromSWC(StringIO(swc_srcs['A'])) m2 = MorphologyArray.fromSWC(StringIO(swc_srcs['A'])) assert MorphArrayComparison.are_same(m1, m2, max_node_distance=0.00001) m1 = MorphologyArray.fromSWC(StringIO(swc_srcs['B'])) m2 = MorphologyArray.fromSWC(StringIO(swc_srcs['B'])) assert MorphArrayComparison.are_same(m1, m2, max_node_distance=0.00001) m1 = MorphologyArray.fromSWC(StringIO(swc_srcs['Ci'])) m2 = MorphologyArray.fromSWC(StringIO(swc_srcs['Cii'])) assert MorphArrayComparison.are_same(m1, m2, max_node_distance=0.00001)
def testComparisonSucessful(self): """Verify simple .swc snippets are the same""" # Loading m1 = MorphologyArray.fromSWC(StringIO(swc_srcs['A'])) m2 = MorphologyArray.fromSWC(StringIO(swc_srcs['A'])) assert MorphArrayComparison.are_same(m1, m2, max_node_distance= 0.00001) m1 = MorphologyArray.fromSWC(StringIO(swc_srcs['B'])) m2 = MorphologyArray.fromSWC(StringIO(swc_srcs['B'])) assert MorphArrayComparison.are_same(m1, m2, max_node_distance= 0.00001) m1 = MorphologyArray.fromSWC(StringIO(swc_srcs['Ci'])) m2 = MorphologyArray.fromSWC(StringIO(swc_srcs['Cii'])) assert MorphArrayComparison.are_same(m1, m2, max_node_distance= 0.00001)
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ---------------------------------------------------------------------- """Load SWC data from a string directly into a MorphologyArray. We can load .swc from any file-like object, so we can use StringIO to load directly from strings. """ from morphforge.morphology.core import MorphologyArray from StringIO import StringIO swcSrc = """ 1 0 1.0 2.0 3.0 4.0 -1 2 0 5.0 6.0 7.0 8.0 1 """ m = MorphologyArray.fromSWC(StringIO(swcSrc)) print 'Morphology Vertices:' print m._vertices print 'Morphology Connectivity:' print m._connectivity
from morphforge.morphology.core import MorphologyArray from StringIO import StringIO swcSrc = """ 1 0 1.0 2.0 3.0 4.0 -1 2 0 5.0 6.0 7.0 8.0 1 """ m = MorphologyArray.fromSWC(StringIO(swcSrc)) print 'Morphology Vertices:' print m._vertices print 'Morphology Connectivity:' print m._connectivity