Esempio n. 1
0
def _test():
  import numpy as np
  import ClearMap.Test.Files as tst
  import ClearMap.Visualization.Plot3d as p3d
  
  import ClearMap.ImageProcessing.Differentiation as dif
  
  from importlib import reload
  reload(dif);
  
  source = tst.init('v')[:20, :20, :20]; 
  
  sink = dif.eigenvalues(source, sigma = 1.0);
  p3d.plot([source] + list(sink.transpose([3,0,1,2])));
    
  sink = np.zeros(source.shape + (3,), order = 'C');
  dif.eigenvalues(source, sink, sigma = 1.0);
  p3d.plot([source] + list(sink.transpose([3,0,1,2])));

  sink = np.zeros(source.shape + (3,), order = 'F');
  dif.eigenvalues(source, sink, sigma = 1.0);
  p3d.plot([source] + list(sink.transpose([3,0,1,2])));
  
  sink = np.zeros(source.shape);
  dif.lambda123(source, sink, sigma = 1.0);
  p3d.plot([source, sink])
  
  sink = dif.lambda123(source, sigma = 1.0, threshold = 0.2);
  p3d.plot([source, sink]);
Esempio n. 2
0
def _test():
    """Tests"""
    import ClearMap.Tests.Files as tsf
    import ClearMap.Visualization.Plot3d as p3d

    import ClearMap.ImageProcessing.LightsheetCorrection as ls
    from importlib import reload
    reload(ls)

    s = tsf.source('vasculature_lightsheet_raw')
    #p3d.plot(s)

    import ClearMap.ImageProcessing.Experts.Vasculature as vasc
    clipped, mask, high, low = vasc.clip(s[:, :, 80:120],
                                         clip_range=(400, 60000))

    corrected = ls.correct_lightsheet(clipped,
                                      mask=mask,
                                      percentile=0.25,
                                      lightsheet=dict(selem=(150, 1, 1)),
                                      background=dict(selem=(200, 200, 1),
                                                      spacing=(25, 25, 1),
                                                      step=(2, 2, 1),
                                                      interpolate=1),
                                      lightsheet_vs_background=2)

    p3d.plot([clipped, corrected])
Esempio n. 3
0
def _test():
  """Tests."""
  import numpy as np
  import ClearMap.Visualization.Plot3d as p3d
  import ClearMap.Tests.Files as tsf
  import ClearMap.ImageProcessing.Experts.Vasculature as vasc
  
  source = np.array(tsf.source('vls')[:300,:300,80:120]);
  source[:,:,[0,-1]] = 0;
  source[:,[0,-1],:] = 0;
  source[[0,-1],:,:] = 0;
    
  bpar = vasc.default_binarization_parameter.copy();
  bpar['clip']['clip_range'] = (150, 7000)
  bpar['as_memory'] = True
  #bpar['binary_status'] = 'binary_status.npy'
  
  ppar = vasc.default_processing_parameter.copy();
  ppar['processes'] = 10;
  ppar['size_max'] = 10;
  
  sink='binary.npy'
  #sink=None;
  
  binary = vasc.binarize(source, sink=sink, binarization_parameter=bpar, processing_parameter = ppar) 
  p3d.plot([source, binary])

  import ClearMap.IO.IO as io
  io.delete_file(sink)
  
  pppar = vasc.default_postprocessing_parameter.copy();
  pppar['smooth']['iterations'] = 3;
  smoothed = vasc.postprocess(binary, postprocessing_parameter=pppar)
  p3d.plot([binary, smoothed])
Esempio n. 4
0
def _test():
  """Tests"""
  import ClearMap.Visualization.Plot3d as p3d
  import ClearMap.ImageProcessing.IlluminationCorrection as ic
  
  ff = ic.flatfield_from_line(ic.default_flat_field_line_file_name, 100);
  p3d.plot(ff)
Esempio n. 5
0
def _test():
  import numpy as np
  import ClearMap.ImageProcessing.Binary.Smoothing as sm
  #reload(sm)
  
  lut = sm.initialize_lookup_table() #analysis:ignore
  #lut = sm.generate_lookup_table(verbose=True);
  
  shape = (30,40,50);
  binary = np.zeros(shape, dtype = bool, order='F');
  grid = np.meshgrid(*[range(s) for s in shape], indexing='ij');
  center = tuple(s/2 for s in shape);
  distance = np.sum([(g-c)**2 for g,c in zip(grid, center)], axis=0);
  binary[distance <= 10**2] = True
  
  import scipy.ndimage as ndi
  border = ndi.convolve(np.asarray(binary, dtype=int), np.ones((3,3,3)))
  border = np.logical_and(border > 0, border < 27)
  
  noisy = binary.copy();
  noisy[np.logical_and(border, np.random.rand(*shape) > 0.925)] = True
  smoothed  = sm.smooth_by_configuration(noisy, iterations=3, verbose=True, processes='serial')
  
  import ClearMap.Visualization.Plot3d as p3d
  p3d.plot([noisy, smoothed])
Esempio n. 6
0
def _test():
    import numpy as np
    import ClearMap.ImageProcessing.GreyReconstruction as gr
    import ClearMap.Visualization.Plot3d as p3d

    x = np.random.rand(*(200, 200, 10))
    r = gr.grey_reconstruct(x, mask=0.5 * x, method='erosion', shape=3)

    p3d.plot([x, r])
Esempio n. 7
0
def _test():
    import ClearMap.Visualization.Mesh.IcoSphere as ics
    from importlib import reload
    reload(ics)

    vertices, faces = ics.icosphere(n_refinements=2)

    import ClearMap.Visualization.Plot3d as p3d
    p3d.plot_mesh_3d(vertices, faces)
Esempio n. 8
0
def _test():
    import ClearMap.ImageProcessing.Filter.StructureElement as se

    from importlib import reload
    reload(se)

    d = se.sphere((150, 50, 10))

    import ClearMap.Visualization.Plot3d as p3d
    p3d.plot(d)
Esempio n. 9
0
def _test():
    """Tests"""
    import ClearMap.ImageProcessing.Filter.FilterKernel as fk
    import ClearMap.Visualization.Plot3d as p3d

    k = fk.filter_kernel(ftype='dog',
                         shape=(15, 15, 15),
                         sigma=None,
                         radius=None,
                         sigma2=None)
    p3d.plot(k)
Esempio n. 10
0
def _test():
    import numpy as np
    import ClearMap.Visualization.Plot3d as p3d
    import ClearMap.ImageProcessing.Thresholding.Thresholding as th

    r = np.arange(50)
    x, y, z = np.meshgrid(r, r, r)
    x, y, z = [i - 25 for i in (x, y, z)]
    d = np.exp(-(x * x + y * y + z * z) / 10.0**2)

    t = th.threshold(d, sink=None, threshold=0.9, hysteresis_threshold=0.5)
    p3d.plot([[d, t]])
Esempio n. 11
0
def _test():
    """Tests."""
    import numpy as np
    import ClearMap.Visualization.Plot3d as p3d

    import ClearMap.ImageProcessing.LocalStatistics as ls
    from importlib import reload
    reload(ls)

    source = np.random.rand(100, 200, 150) + np.arange(100)[:, None, None]
    p = ls.local_percentile(source,
                            percentile=0.5,
                            selem=(30, 30, 30),
                            interpolate=1)
    p3d.plot([source, p])
Esempio n. 12
0
def test():
    import numpy as np
    import ClearMap.IO.IO as io
    import ClearMap.Analysis.Measurements.MeasureRadius as mr

    data = 10 - np.abs(10 - np.arange(0, 21))
    search = mr.search_indices_sphere(radius=[10, 10, 10])
    print(search)

    points = np.array([10])
    d, i = mr.measure_radius(data,
                             points,
                             fraction=0.75,
                             max_radius=10,
                             scale=2,
                             verbose=True,
                             processes=4,
                             return_indices=True)

    data = np.random.rand(*(30, 40, 50))
    io.write('data.npy', data)

    points = np.array([np.random.randint(0, s, size=10) for s in data.shape]).T
    d, i = mr.measure_radius(data,
                             points,
                             value=0.5,
                             max_radius=10,
                             scale=2,
                             verbose=True,
                             processes=4,
                             return_indices=True)

    data = np.zeros((30, 40, 50), dtype=int)
    data[10:20, 15:25, 10:20] = 1
    data[15, 20, 15] = 2

    import ClearMap.Visualization.Plot3d as p3d
    p3d.plot(data)

    points = np.array([[15, 20, 15], [4, 4, 4]])
    d, i = mr.measure_radius(data,
                             points,
                             value=0.0,
                             max_radius=10,
                             scale=None,
                             verbose=True,
                             processes=None,
                             return_indices=True)
Esempio n. 13
0
def _test():
    """Tests."""
    import numpy as np
    import ClearMap.Visualization.Plot3d as p3d
    import ClearMap.ImageProcessing.Binary.Filling as bf

    from importlib import reload
    reload(bf)

    test = np.zeros((50, 50, 50), dtype=bool)
    test[20:30, 30:40, 25:35] = True
    test[25:27, 34:38, 27:32] = False
    test[5:15, 5:15, 23:35] = True
    test[8:12, 8:12, 27:32] = False

    filled = bf.fill(test, sink=None, processes=10)
    p3d.plot([test, filled])
Esempio n. 14
0
def _test():
    import numpy as np
    import ClearMap.IO.IO as io
    import ClearMap.Visualization.Plot3d as p3d
    import ClearMap.Tests.Files as tsf
    import ClearMap.ImageProcessing.Skeletonization.PK12 as PK12
    from importlib import reload
    reload(PK12)

    #Lookup tables
    #lut = PK12.generate_lookup_table();
    #np.save(PK12.filename, lut);
    #lut.sum()

    #lutnr = PK12.generate_lookup_table(function=PK12.match_non_removable, verbose = True);
    #np.save(PK12.filename_non_removable, lutnr);
    #lutnr.sum()

    #Skeletonization
    reload(PK12)
    binary = tsf.skeleton_binary
    binary_array = np.array(io.as_source(binary))

    #default version
    skeleton = PK12.skeletonize(binary_array.copy(),
                                delete_border=True,
                                verbose=True)
    p3d.plot([[binary_array, skeleton]])

    #fast index version
    skeleton = PK12.skeletonize_index(binary_array.copy(),
                                      delete_border=True,
                                      verbose=True)
    p3d.plot([[binary_array, skeleton]])

    # plotting
    import ClearMap.Visualization.Plot3d as p3d
    p3d.plot_3d(binary_array[:150, :150, :150], cmap=p3d.grays_alpha(0.05))
    p3d.plot_3d(skeleton[:150, :150, :150],
                cmap=p3d.single_color_colormap('red', alpha=0.8))

    #save for figure
    import scipy.io as sio
    sio.savemat('binary.mat', {'binary': binary_array[:100, :100, :100]})
    sio.savemat('binary_skeleton.mat',
                {'skeleton': skeleton[:100, :100, :100]})
Esempio n. 15
0
def _test():
    import numpy as np
    import ClearMap.Visualization.Plot3d as p3d
    import ClearMap.ImageProcessing.Filter.SeedThresholding.SeedThresholding as sth

    from importlib import reload
    reload(sth)

    r = np.arange(50)
    x, y, z = np.meshgrid(r, r, r)
    x, y, z = [i - 25 for i in (x, y, z)]
    d = np.exp(-(x * x + y * y + z * z) / 10.0**2)
    m = sth.local_max(d, shape=(3, 3, 3))
    x, y, z = np.where(m)
    t = sth.threshold(d,
                      sink=None,
                      seeds_x=x,
                      seeds_y=y,
                      seeds_z=z,
                      percentage=0.5,
                      absolute=0.2)
    p3d.plot([[d, m, t]])

    import ClearMap.Test.Files as tst
    d = tst.init('v')[:200, :200, :100]
    import ClearMap.ImageProcessing.Filter.Rank as rnk
    d = np.array(d)
    d = rnk.median(d, np.ones((3, 3, 3), dtype=bool))

    m = sth.local_max(d, shape=(5, 5, 1))
    m = np.logical_and(m, d > 10)
    x, y, z = np.where(m)
    v = d[x, y, z]
    s = np.argsort(v)[::-1]
    x, y, z = [i[s] for i in (x, y, z)]
    t = sth.threshold(d,
                      sink=None,
                      seeds_x=x,
                      seeds_y=y,
                      seeds_z=z,
                      percentage=0.25,
                      absolute=5)
    p3d.plot([d, [d, m, t]])
Esempio n. 16
0
def _test():
    """Test the statistics array"""
    import numpy as np
    import ClearMap.Analysis.Statistics.GroupStatistics as st

    s = np.ones((5, 4, 20))
    s[:, 0:3, :] = -1

    x = np.random.rand(4, 4, 20)
    y = np.random.rand(5, 4, 20) + s

    pvals, psign = st.t_test_voxelization(x, y, signed=True)

    pvalscol = st.color_p_values(pvals,
                                 psign,
                                 positive=[255, 0, 0],
                                 negative=[0, 255, 0])

    import ClearMap.Visualization.Plot3d as p3d
    p3d.plot(pvalscol)
Esempio n. 17
0
def _test():
    import numpy as np
    import ClearMap.IO.IO as io
    import ClearMap.ImageProcessing.Skeletonization.Skeletonization as skl
    import ClearMap.Visualization.Plot3d as p3d
    import ClearMap.Tests.Files as tsf

    from importlib import reload
    reload(skl)

    binary = tsf.skeleton_binary

    #default version
    skeleton = skl.skeletonize(binary, delete_border=True, verbose=True)
    p3d.plot([[binary, skeleton]])

    #fast index version
    skeleton = skl.PK12.skeletonize_index(binary.copy(),
                                          delete_border=True,
                                          verbose=True)
    p3d.plot([[binary, skeleton]])
Esempio n. 18
0
def plotTracingResult(path,
                      data_nbh,
                      mask_nbh,
                      center,
                      radius,
                      tubeness_nbh,
                      skeleton=None,
                      distance_nbh=None):
    global viewer

    img_nbh = np.asarray(data_nbh, dtype=float)
    bin_nbh = np.zeros(mask_nbh.shape, dtype=int)

    if skeleton is not None:
        bin_nbh += extractNeighbourhood(skeleton, center, radius)

    addPathToMask(bin_nbh, path, value=4)
    if points is not None:
        pts = points - center + radius
        for d in range(3):
            ids = np.logical_and(0 <= pts[:, d], pts[:, d] < data_nbh.shape[d])
            pts = pts[ids]
        addPathToMask(bin_nbh, pts, value=3)

    addPathToMask(img_nbh, path, value=512)
    addPathToMask(tubeness_nbh, path, value=50)
    if distance_nbh is not None:
        addPathToMask(distance_nbh, path, value=50)

    try:
        viewer[0].setSource(bin_nbh)
        viewer[1].setSource(img_nbh)
        viewer[2].setSource(tubeness_nbh)
        if distance_nbh is not None:
            viewer[3].setSource(distance_nbh)
    except:
        if distance_nbh is not None:
            viewer = p3d.plot([bin_nbh, img_nbh, tubeness_nbh, distance_nbh])
        else:
            viewer = p3d.plot([bin_nbh, img_nbh, tubeness_nbh])
Esempio n. 19
0
def _test():
    """Tests"""
    import numpy as np
    import ClearMap.Visualization.Plot3d as p3d
    import ClearMap.ParallelProcessing.DataProcessing.DevolvePointList as dpl

    from importlib import reload
    reload(dpl)

    points = np.array([1, 9, 10], dtype=float)
    indices = [-2, -1, 0, 1]
    v = dpl.devolve(points, shape=(11, ), indices=indices)
    print(v.array)

    points = np.array([[10, 10, 18]])
    indices = [[-1, -1, -1], [0, 0, 0], [1, 1, 1]]
    v = dpl.devolve(points, shape=(20, 20, 20), indices=indices, weights=None)
    p3d.plot(v)

    points = np.array([[10, 10, 10]])
    weights = np.random.rand(len(points))
    v = dpl.devolve(points,
                    shape=(20, 20, 20),
                    indices=indices,
                    weights=weights)
    p3d.plot(v)

    kernel = np.random.rand(len(indices))
    v = dpl.devolve(points,
                    shape=(20, 20, 20),
                    indices=indices,
                    weights=None,
                    kernel=kernel)
    p3d.plot(v)
Esempio n. 20
0
def plotData(data, skel, binary, ends=None, isolated=None, replot=True):
    global viewer

    img = np.zeros(data.shape, dtype=int)
    img[:] = img + binary + skel

    if ends is not None:
        xyz = np.array(np.unravel_index(ends, skel.shape, order='C')).T
        for x, y, z in xyz:
            img[x, y, z] = 3

    if isolated is not None:
        xyz = np.array(np.unravel_index(isolated, skel.shape, order='C')).T
        for x, y, z in xyz:
            img[x, y, z] = 4

    if replot:
        try:
            viewer[0].setSource(data)
            viewer[1].setSource(data)
        except:
            viewer = p3d.plot([data, img])
    else:
        return p3d.plot([data, img])
Esempio n. 21
0
def _test():
    """Tests"""
    import numpy as np
    import ClearMap.Visualization.Plot3d as p3d
    import ClearMap.Analysis.Measurements.Voxelization as vox

    #points = np.random.rand(20,3) * 15;
    #points = np.asarray(points, dtype=int);
    points = np.array([[10, 1, 1]])
    v = vox.voxelize(points, shape=(20, 20, 20), radius=(0, 0, 0))
    w = vox.voxelize(points, shape=(20, 20, 20), radius=(2, 2, 2))
    p3d.plot([[v, w]])

    indices, kernel = vox.search_indices_sphere((2, 2, 2), kernel=None)
    print(indices)

    points = np.array([[10, 10, 18]])
    v = vox.voxelize(points,
                     shape=(20, 20, 20),
                     weights=None,
                     radius=(5, 7, 10))
    p3d.plot(v)

    points = np.array([[10, 10, 10]])
    weights = np.random.rand(len(points))
    v = vox.voxelize(points,
                     shape=(20, 20, 20),
                     weights=weights,
                     radius=(2, 2, 2),
                     method='rectangle')
    p3d.plot(v)

    def kernel(d):
        return np.exp(-d)

    v = vox.voxelize(points,
                     shape=(20, 20, 20),
                     weights=None,
                     radius=(5, 5, 8),
                     kernel=kernel,
                     method='sphere')
    p3d.plot(v)
Esempio n. 22
0
    reload(sth)

    r = np.arange(50)
    x, y, z = np.meshgrid(r, r, r)
    x, y, z = [i - 25 for i in (x, y, z)]
    d = np.exp(-(x * x + y * y + z * z) / 10.0**2)
    m = sth.local_max(d, shape=(3, 3, 3))
    x, y, z = np.where(m)
    t = sth.threshold(d,
                      sink=None,
                      seeds_x=x,
                      seeds_y=y,
                      seeds_z=z,
                      percentage=0.5,
                      absolute=0.2)
    p3d.plot([[d, m, t]])

    import ClearMap.Test.Files as tst
    d = tst.init('v')[:200, :200, :100]
    import ClearMap.ImageProcessing.Filter.Rank as rnk
    d = np.array(d)
    d = rnk.median(d, np.ones((3, 3, 3), dtype=bool))

    m = sth.local_max(d, shape=(5, 5, 1))
    m = np.logical_and(m, d > 10)
    x, y, z = np.where(m)
    v = d[x, y, z]
    s = np.argsort(v)[::-1]
    x, y, z = [i[s] for i in (x, y, z)]
    t = sth.threshold(d,
                      sink=None,
Esempio n. 23
0
def _test():
    import numpy as np
    import scipy.ndimage as ndi

    import ClearMap.Visualization.Plot3d as p3d
    import ClearMap.ImageProcessing.Filter.Curvature.Curvature as cur
    import ClearMap.ImageProcessing.Tracing.Trace as trc

    from importlib import reload
    reload(trc)

    #x = np.random.rand(50,50,50);
    #x = ndi.gaussian_filter(x, sigma = 2);

    x = np.load('/home/ckirst/Desktop/data.npy')[:50, :50, :50]
    x = np.ascontiguousarray(x, dtype=float)

    start, stop = [37, 20, 24], [27, 32, 38]

    #  from collections import defaultdict
    #  import gc
    #  gc.collect();
    #before = defaultdict(int)
    #after = defaultdict(int)
    #for i in gc.get_objects():
    #  before[type(i)] += 1

    t = cur.tubeness(x)

    #reload(trc);
    p = trc.trace(x, t, start, stop, verbose=False)

    #gc.collect();

    #for i in gc.get_objects():
    #   after[type(i)] += 1
    #print [(k, after[k] - before[k]) for k in after if after[k] - before[k]]

    #import mem_top as mt;
    #print mt.mem_top(width = 150);

    #%%
    xp = np.asarray(x, dtype=int)
    for pp in p:
        xp[pp[0], pp[1], pp[2]] = 512

    try:
        dd[0].setSource(xp)
        dd[1].setSource(t)
    except:
        dd = p3d.plot([xp, t])

    import scipy.ndimage as ndi

    mask = np.zeros_like(x)
    mask[:, :, :40] = 1
    # zeros are the goal !!
    dist = ndi.distance_transform_edt(mask, sampling=[1, 1, 1])

    start = [37, 20, 25]
    path = trc.traceToMask(x, t, start, dist)

    xp = np.asarray(x, dtype=int)
    for pp in path:
        xp[pp[0], pp[1], pp[2]] = 512

    try:
        dd[0].setSource(xp)
        dd[1].setSource(t)
    except:
        dd = p3d.plot([xp, t])
Esempio n. 24
0
def _test():
  """Tests"""
  import ClearMap.Settings as settings 
  import ClearMap.IO.IO as io
  import ClearMap.Visualization.Plot3d as p3d;
  
  import ClearMap.Alignment.Resampling as res
  from importlib import reload
  reload(res)
  
  r = res.resample_shape(source_shape=(100,200,300), sink_shape=(50,50,30));
  print('resampled source_shape=%r, sink_shape=%r, source_resolution=%r, sink_resolution=%r' % r)
 
  r = res.resample_shape(source_shape=(100,200,300), source_resolution=(2,2,2), sink_resolution=(10,2,1))
  print('resampled source_shape=%r, sink_shape=%r, source_resolution=%r, sink_resolution=%r' % r)
  
  
  source = io.join(settings.test_data_path, 'Resampling/test.tif')
  sink   = io.join(settings.test_data_path, "Resampling/resampled.npy")
  
  source = io.join(settings.test_data_path, 'Tif/sequence/sequence<Z,4>.tif')
  sink = io.join(settings.test_data_path, "Resampling/resampled_sequence.tif")  
  
  
  source_shape, sink_shape, source_res, sink_res = res.resample_shape(source_shape=io.shape(source), source_resolution=(1.,1.,1.), sink_resolution=(1.6,1.6,2))
  axes_order = res._axes_order(None, source_shape, sink_shape);
  print(axes_order)
  resampled = res.resample(source, sink, source_resolution=(1.,1.,1.), sink_resolution = (1.6,1.6,2), orientation=None, processes=None);
  p3d.plot(resampled)
  p3d.plot(source)
  
  inverse = res.resample_inverse(resampled, sink=None, resample_source=source, source_resolution=(1,1,1), sink_resolution = (10,10,2), orientation=None, processes='serial')
  p3d.plot([source, inverse])

  
  resampled = res.resample(source, sink, source_resolution=(1,1,1), sink_resolution = (10,10,2), orientation=(2,-1,3), processes=None);
  p3d.plot(resampled)
  
  inverse = res.resample_inverse(resampled, sink=None, resample_source=source, source_resolution=(1,1,1), sink_resolution = (10,10,2), orientation=(2,-1,3), processes=None)
  p3d.plot([source, inverse])
  
  resampled = res.resample(source, sink=None, source_resolution=(1.6,1.6,2), sink_shape=(10,20,30), orientation=None, processes=None)
  p3d.plot(resampled)
  
  
  # ponints
  points = res.np.array([[0,0,0], [1,1,1], [1,2,3]], dtype=float);
  resampled_points = res.resample_points(points, resample_source=source , resample_sink=sink, orientation=None)
  print(resampled_points)

  inverse_points = res.resample_points_inverse(resampled_points, resample_source=source , resample_sink=sink, orientation=None)
  print(inverse_points)
  print(res.np.allclose(points, inverse_points))
Esempio n. 25
0
def test():
    """Test voxelization module"""
    import numpy as np

    import ClearMap.Analysis.Orientation as ort

    from importlib import reload
    reload(ort)

    n = 200

    #points = np.random.rand(n,3) * 20;
    #orientations = np.random.rand(n,3);
    #orientations = (orientations.T / np.linalg.norm(orientations, axis = 1)).T;

    phi = np.linspace(0, 2 * np.pi, n)
    psi = np.linspace(0, 2 * np.pi, n)
    phi, psi = np.meshgrid(phi, psi)

    orientations = np.array(
        [np.cos(psi) * np.cos(phi),
         np.sin(phi),
         np.sin(psi) * np.cos(phi)])

    points = np.meshgrid(np.arange(n), np.arange(n))
    points = np.array(points)

    ox, oy, oz = orientations
    px, py = points

    orientations_f = np.array([ox.flatten(), oy.flatten(), oz.flatten()]).T
    points_f = np.array([px.flatten(), py.flatten()]).T

    import mayavi.mlab as mlab
    mlab.quiver3d(points_f[:, 0],
                  points_f[:, 1],
                  np.zeros(points_f.shape[0]),
                  orientations_f[:, 0],
                  orientations_f[:, 1],
                  orientations_f[:, 2],
                  color='black',
                  opacity=0.2,
                  scale_factor=1.5)

    vo = ort.voxelizeOrientations(points_f,
                                  orientations_f,
                                  dataSize=(20, 20, 20),
                                  size=(5, 5, 5))

    #normalize
    nrm = np.linalg.norm(vo, axis=-1)
    nrm[nrm < 10e-5] = 1.0
    vo = (vo.transpose((3, 0, 1, 2)) / nrm).transpose((1, 2, 3, 0))

    import ClearMap.Visualization.ColorMaps as cmaps
    reload(cmaps)

    vo2 = cmaps.boys2rgb(vo.transpose([3, 0, 1, 2]))

    import ClearMap.Visualization.Plot3d as p3d
    p3d.plot([[vo2[:, :, :, 0], vo2[:, :, :, 1], vo2[:, :, :, 2]]])

    import ClearMap.Visualization.ColorMaps as cmaps
    reload(cmaps)

    ob = cmaps.boys2rgb(orientations)

    ob.shape = ob.shape + (1, )

    p3d.plot([[ob[0], ob[1], ob[2]]])

    from mayavi import mlab
    import numpy as np

    import ClearMap.Visualization.ColorMaps as cmaps
    reload(cmaps)

    # Make sphere, choose colors
    phi, theta = np.mgrid[0:np.pi:101j, 0:2 * np.pi:101j]
    x, y, z = np.sin(phi) * np.cos(theta), np.sin(phi) * np.sin(theta), np.cos(
        phi)
    xr, yr, zr = x.ravel(), y.ravel(), z.ravel()

    rgb = cmaps.boys2rgb([xr, yr, zr])
    rgb = np.abs(np.array([xr, yr, zr])).T

    color = np.zeros((rgb.shape[0], 4), dtype='uint8')
    color[:, :3] = rgb * 255
    color[:, 3] = 255

    #s = cmaps.rgbToLUT(rgb);
    #si = np.array(s, dtype = int);

    si = np.arange(len(xr))
    si.shape = x.shape

    #lut = cmaps.rgbLUT();
    lut = 0.8 * color
    lut[:, 3] = 255

    # Display
    mlab.figure(1, bgcolor=(1, 1, 1), fgcolor=(0, 0, 0), size=(600, 500))
    mm = mlab.mesh(x,
                   y,
                   z,
                   scalars=si,
                   colormap='Spectral',
                   vmin=0,
                   vmax=lut.shape[0])

    #mm = mlab.points3d(xr, yr, zr, si, mode = 'point')

    #magic to modify lookup table
    mm.module_manager.scalar_lut_manager.lut._vtk_obj.SetTableRange(
        0, lut.shape[0])
    mm.module_manager.scalar_lut_manager.lut.number_of_colors = lut.shape[0]
    mm.module_manager.scalar_lut_manager.lut.table = lut

    mlab.view()
    mlab.show()
Esempio n. 26
0
def _test():
    #%%
    import numpy as np
    import scipy.ndimage as ndi
    import ClearMap.DataProcessing.LargeData as ld
    import ClearMap.Visualization.Plot3d as p3d
    import ClearMap.DataProcessing.ConvolvePointList as cpl
    import ClearMap.ImageProcessing.Skeletonization.Topology3d as t3d
    import ClearMap.ImageProcessing.Skeletonization.SkeletonCleanUp as scu

    import ClearMap.ImageProcessing.Tracing.Connect as con
    reload(con)

    data = np.load('/home/ckirst/Desktop/data.npy')
    binary = np.load('/home/ckirst/Desktop/binarized.npy')
    skel = np.load('/home/ckirst/Desktop/skel.npy')
    #points = np.load('/home/ckirst/Desktop/pts.npy');

    data = np.copy(data, order='F')
    binary = np.copy(binary, order='F')
    skel = np.copy(skel, order='F')
    skel_copy = np.copy(skel, order='F')
    points = np.ravel_multi_index(np.where(skel), skel.shape, order='F')

    skel, points = scu.cleanOpenBranches(skel,
                                         skel_copy,
                                         points,
                                         length=3,
                                         clean=True)
    deg = cpl.convolve3DIndex(skel, t3d.n26, points)

    ends, isolated = con.findEndpoints(skel, points, border=25)
    special = np.sort(np.hstack([ends, isolated]))

    ends_xyz = np.array(np.unravel_index(ends, data.shape, order='F')).T
    isolated_xyz = np.array(np.unravel_index(isolated, data.shape,
                                             order='F')).T
    special_xyz = np.vstack([ends_xyz, isolated_xyz])

    #%%
    import ClearMap.ParallelProcessing.SharedMemoryManager as smm
    data_s = smm.asShared(data, order='F')
    binary_s = smm.asShared(binary.view('uint8'), order='F')
    skel_s = smm.asShared(skel.view('uint8'), order='F')

    smm.clean()
    res = con.addConnections(data_s,
                             binary_s,
                             skel_s,
                             points,
                             radius=20,
                             start_points=None,
                             add_to_skeleton=True,
                             add_to_mask=True,
                             verbose=True,
                             processes=4,
                             debug=False,
                             block_size=10)

    skel_s = skel_s.view(bool)
    binary_s = binary_s.view(bool)

    #%%
    mask_img = np.asarray(binary, dtype=int, order='A')
    mask_img[:] = mask_img + binary_s
    mask_img[:] = mask_img + skel

    data_img = np.copy(data, order='A')
    data_img[skel] = 120

    mask_img_f = np.reshape(mask_img, -1, order='A')
    data_img_f = np.reshape(data_img, -1, order='A')

    mask_img_f[res] = 7
    data_img_f[res] = 512

    mask_img_f[special] = 8
    data_img_f[special] = 150

    for d in [3, 4, 5]:
        mask_img_f[points[deg == d]] = d + 1

    try:
        con.viewer[0].setSource(mask_img)
        con.viewer[1].setSource(data_img)
    except:
        con.viewer = p3d.plot([mask_img, data_img])

    con.viewer[0].setMinMax([0, 8])
    con.viewer[1].setMinMax([24, 160])

    #%%
    mask = binary
    data_new = np.copy(data, order='A')
    data_new[skel] = 120

    skel_new = np.asarray(skel, dtype=int, order='A')
    skel_new[:] = skel_new + binary

    binary_new = np.copy(binary, order='A')
    qs = []
    for i, e in enumerate(special):
        print('------')
        print('%d / %d' % (i, len(special)))
        path, quality = con.connectPoint(data,
                                         mask,
                                         special,
                                         i,
                                         radius=25,
                                         skeleton=skel,
                                         tubeness=None,
                                         remove_local_mask=True,
                                         min_quality=15.0,
                                         verbose=True,
                                         maxSteps=15000,
                                         costPerDistance=1.0)

        #print path, quality
        if len(path) > 0:
            qs.append(quality * 1.0 / len(path))

            q = con.addPathToMask(skel_new, path, value=7)
            q = con.addPathToMask(data_new, path, value=512)
            binary_new = con.addDilatedPathToMask(binary_new,
                                                  path,
                                                  iterations=1)

    skel_new[:] = skel_new + binary_new
    q = con.addPathToMask(skel_new, special_xyz, value=6)
    for d in [3, 4, 5]:
        xyz = np.array(
            np.unravel_index(points[deg == d], data.shape, order='F')).T
        q = con.addPathToMask(skel_new, xyz, value=d)
    q = con.addPathToMask(data_new, special_xyz, value=150)

    try:
        con.viewer[0].setSource(skel_new)
        con.viewer[1].setSource(data_new)
    except:
        con.viewer = p3d.plot([skel_new, data_new])

    con.viewer[0].setMinMax([0, 8])
    con.viewer[1].setMinMax([24, 160])

    #%%
    import matplotlib.pyplot as plt
    plt.figure(1)
    plt.clf()
    #plt.plot(qs);
    plt.hist(qs)

    #%%
    i = 20
    i = 21
    i = 30
    i = 40
    r = 25
    center = np.unravel_index(ends[i], data.shape)
    print(center, data.shape)
    mask = binary
    path = con.tracePointToMask(data,
                                mask,
                                center,
                                radius=r,
                                points=special_xyz,
                                plot=True,
                                skel=skel,
                                binary=binary,
                                tubeness=None,
                                removeLocalMask=True,
                                maxSteps=None,
                                verbose=False,
                                costPerDistance=0.0)

    #%%

    nbs = ap.findNeighbours(ends, i, skel.shape, skel.strides, r)
    center = np.unravel_index(ends[i], skel.shape)

    nbs_xyz = np.array(np.unravel_index(nbs, skel.shape)).T
    dists = nbs_xyz - center
    dists = np.sum(dists * dists, axis=1)

    nb = np.argmin(dists)

    center = np.unravel_index(ends[i], data.shape)
    print(center, data.shape)
    mask = binary
    path = con.tracePointToNeighbor(data,
                                    mask,
                                    center,
                                    nbs_xyz[nb],
                                    radius=r,
                                    points=special_xyz,
                                    plot=True,
                                    skel=skel,
                                    binary=binary,
                                    tubeness=None,
                                    removeLocalMask=True,
                                    maxSteps=None,
                                    verbose=False,
                                    costPerDistance=0.0)

    #%%

    import ClearMap.ImageProcessing.Filter.FilterKernel as fkr
    dog = fkr.filterKernel('DoG', size=(13, 13, 13))
    dv.plot(dog)

    data_filter = ndi.correlate(np.asarray(data, dtype=float), dog)
    data_filter -= data_filter.min()
    data_filter = data_filter / 3.0
    #dv.dualPlot(data, data_filter);

    #%%add all paths
    reload(con)

    r = 25
    mask = binary
    data_new = data.copy()
    data_new[skel] = 120

    skel_new = np.asarray(skel, dtype=int)
    skel_new = skel_new + binary

    binary_new = binary.copy()

    for i, e in enumerate(special):
        center = np.unravel_index(e, data.shape)

        print(i, e, center)
        path = con.tracePointToMask(data,
                                    mask,
                                    center,
                                    radius=r,
                                    points=special_xyz,
                                    plot=False,
                                    skel=skel,
                                    binary=binary,
                                    tubeness=None,
                                    removeLocalMask=True,
                                    maxSteps=15000,
                                    costPerDistance=1.0)

        q = con.addPathToMask(skel_new, path, value=7)
        q = con.addPathToMask(data_new, path, value=512)
        binary_new = con.addDilatedPathToMask(binary_new, path, iterations=1)

    q = con.addPathToMask(skel_new, special_xyz, value=6)
    for d in [3, 4, 5]:
        xyz = np.array(np.unravel_index(points[deg == d], data.shape)).T
        q = con.addPathToMask(skel_new, xyz, value=d)
    q = con.addPathToMask(data_new, special_xyz, value=150)

    skel_new = skel_new + binary_new
    try:
        con.viewer[0].setSource(skel_new)
        con.viewer[1].setSource(data_new)
    except:
        con.viewer = dv.dualPlot(skel_new, data_new)

    con.viewer[0].setMinMax([0, 8])
    con.viewer[1].setMinMax([24, 160])

    #%%

    import ClearMap.ImageProcessing.Skeletonization.Skeletonize as skl

    skel_2 = skl.skeletonize3D(binary_new.copy())

    #%%

    np.save('/home/ckirst/Desktop/binarized_con.npy', binary_new)
    #%%

    # write image

    import ClearMap.IO.IO as io

    #r = np.asarray(128 * binary_new, dtype = 'uint8');
    #g = r.copy(); b = r.copy();
    #r[:] = r + 127 * skel_2[0];
    #g[:] = g - 128 * skel_2[0];
    #b[:] = b - 128 * skel_2[0];
    #img = np.stack((r,g,b), axis = 3)

    img = np.asarray(128 * binary_new, dtype='uint8')
    img[:] = img + 127 * skel_2[0]

    io.writeData('/home/ckirst/Desktop/3d.tif', img)
Esempio n. 27
0
def _test():
    import numpy as np
    import ClearMap.ParallelProcessing.DataProcessing.ArrayProcessing as ap

    ## Lookup table processing

    #apply_lut
    x = np.random.randint(0, 100, size=(20, 30))
    lut = np.arange(100) + 1
    y = ap.apply_lut(x, lut)
    assert np.all(y == x + 1)

    #apply_lut_to_index
    import ClearMap.ImageProcessing.Topology.Topology3d as t3d
    kernel = t3d.index_kernel(dtype=int)

    import ClearMap.ImageProcessing.Binary.Smoothing as sm
    lut = sm.initialize_lookup_table()

    data = np.array(np.random.rand(150, 30, 40) > 0.75, order='F')

    result = ap.apply_lut_to_index(data, kernel, lut, sink=None, verbose=True)

    import ClearMap.Visualization.Plot3d as p3d
    p3d.plot([[data, result]])

    ### Correlation

    #correlate1d
    kernel = np.array(range(11), dtype='uint32')
    data = np.array(np.random.randint(0,
                                      2**27, (300, 400, 1500),
                                      dtype='uint32'),
                    order='F')
    #data = np.array(np.random.rand(3,4,5), order='F');

    data = np.empty((300, 400, 1500), order='F')
    kernel = np.array([1, 2, 3, 4, 5], dtype='uint8')

    sink = 'test.npy'

    import ClearMap.Utils.Timer as tmr
    import scipy.ndimage as ndi
    timer = tmr.Timer()
    for axis in range(3):
        print(axis)
        corr_ndi = ndi.correlate1d(data, axis=axis, mode='constant', cval=0)
    timer.print_elapsed_time('ndi')

    timer = tmr.Timer()
    for axis in range(3):
        print(axis)
        corr = ap.correlate1d(data,
                              sink=sink,
                              kernel=kernel,
                              axis=axis,
                              verbose=False,
                              processes=None)
    timer.print_elapsed_time('ap')

    assert np.allclose(corr.array, corr_ndi)

    # IO
    import ClearMap.ParallelProcessing.DataProcessing.ArrayProcessing as ap
    import numpy as np
    reload(ap)

    data = np.random.rand(10, 200, 10)

    sink = ap.write('test.npy', data, verbose=True)
    assert (np.all(sink.array == data))

    read = ap.read('test.npy', verbose=True)
    assert (np.all(read.array == data))

    ap.io.delete_file('test.npy')

    # where
    reload(ap)
    data = np.random.rand(30, 20, 40) > 0.5

    where_np = np.array(np.where(data)).T
    where = ap.where(data, cutoff=2**0)

    check_np = np.zeros(data.shape, dtype=bool)
    check = np.zeros(data.shape, dtype=bool)
    check_np[tuple(where_np.T)] = True
    check[tuple(where.array.T)] = True
    assert (np.all(check_np == check))
Esempio n. 28
0
def _test():
  import numpy as np
  import ClearMap.Tests.Files as tf
  import ClearMap.Analysis.Graphs.GraphProcessing as gp
  #reload(gp)

  skeleton = tf.source('skeleton');
  
  #import ClearMap.Visualization.Plot3d as p3d
  #p3d.plot(skeleton)

  #reload(gp)
  g = gp.graph_from_skeleton(skeleton)

  g.vertex_coordinates()
  
  s = gp.graph_to_skeleton(g)
  assert np.all(s==skeleton)


  gc = gp.clean_graph(g, verbose=True)  
    
  
  gr = gp.reduce_graph(gc, verbose=True)
  
  
  gr2 = gr.copy();
  
  gr2.set_edge_geometry_type('edge')
  
  
  l = gr.edge_geometry_lengths();
  print(l)

  
  g = gp.ggt.Graph(n_vertices=10);
  g.add_edge(np.array([[7,8],[7,9],[1,2],[2,3],[3,1],[1,4],[4,5],[2,6],[6,7]]));
  g.set_vertex_coordinates(np.array([[10,10,10],[0,0,0],[1,1,1],[1,1,0],[5,0,0],[8,0,1],[0,7,1],[0,10,2],[0,12,3],[3,7,7]], dtype=float));
  
  import ClearMap.Visualization.Plot3d as p3d
  p3d.plot_graph_line(g)
  
  gc = gp.clean_graph(g, verbose=True); 
  p3d.plot_graph_line(gc)
  
  gr = gp.reduce_graph(gc, edge_geometry=True, verbose=True)
  #gr.set_edge_geometry(0.1*np.ones(gr.edge_geometry(as_list=False).shape[0]), 'radii')


  import ClearMap.Visualization.Plot3d as p3d
  vertex_colors = np.random.rand(g.n_vertices, 4);
  vertex_colors[:,3] = 1;
  p3d.plot_graph_mesh(gr, default_radius=1, vertex_colors=vertex_colors)
  
  eg = gr.edge_geometry(as_list=False);
  egs = 0.5 * eg;
  gr.set_edge_geometry(name='coordinates', values=egs)
    
  #tracing
  import numpy as np
  import ClearMap.Visualization.Plot3d as p3d
  import ClearMap.Analysis.Graphs.GraphProcessing as gp
  
  g = gp.ggt.Graph(n_vertices=10);
  g.add_edge(np.array([[0,1],[1,2],[2,3],[3,4],[4,0],[0,5],[5,6],[6,7],[0,8],[8,9],[9,0]]));
  g.set_vertex_coordinates(np.array([[0,0,0],[1,0,0],[2,0,0],[2,2,0],[0,1,0],[0,0,1],[0,0,2],[0,0,3],[0,-1,0],[0,-1,-1]], dtype=float));
  g.set_vertex_radii(np.array([10,6,4,6,7,4,2,2,5,5]) * 0.02)
  vertex_colors = np.array([g.vertex_radii()]*4).T; vertex_colors = vertex_colors / vertex_colors.max(); vertex_colors[:,3] = 1;
  p3d.plot_graph_mesh(g, default_radius=1, vertex_colors=vertex_colors)
  
  def condition(graph, vertex):
    r = graph.vertex_radii(vertex=vertex);
    print('condition, vertex=%d, radius=%r' % (vertex,r))
    return r >= 5 * 0.02;
  
  label = np.zeros(g.n_vertices, dtype=bool);
  label[0] = True;
  traced = gp.trace_vertex_label(g, label, condition=condition, steps=1)
  print(traced)  

  vertex_colors = np.array([[1,0,0,1],[1,1,1,1]])[np.asarray(traced, dtype=int)]
  p3d.plot_graph_mesh(g, default_radius=1, vertex_colors=vertex_colors)
  
  from importlib import reload;
  reload(gp)
  
  
  # edge tracing
  import ClearMap.Analysis.Graphs.GraphGt as ggt
  edges = [[0,1],[1,2],[2,3],[4,5],[5,6],[1,7]];
  g = ggt.Graph(edges=edges)
  
  l,m = g.edge_graph(return_edge_map=True);
  
  import numpy as np
  label = np.zeros(len(edges), dtype=bool);
  label[1] = True;
  
  import ClearMap.Analysis.Graphs.GraphProcessing as gp
  
  def condition(graph, edge):
    print('condition, edge=%d' % (edge,))
    return True;
  
  traced = gp.trace_edge_label(g, label, condition=condition);     
  print(traced)
  
  # expansion of edge lengths
  
  import numpy as np
  import ClearMap.Tests.Files as tf
  import ClearMap.Analysis.Graphs.GraphProcessing as gp
  
  graph = gp.ggt.Graph(n_vertices=5);
  graph.add_edge([[0,1],[0,2],[0,3],[2,3],[2,1],[0,4]]);
  graph.add_edge_property('length', np.array([0,1,2,3,4,5])+2);
  
  e, m = gp.expand_graph_length(graph, 'length', True)
  
  import graph_tool.draw as gd
  pos = gp.ggt.vertex_property_map_to_python(gd.sfdp_layout(e.base))
  import matplotlib.pyplot as plt
  plt.figure(1); plt.clf();
  import matplotlib.collections  as mc

  import ClearMap.Visualization.Color as col
  colors = np.array([col.color(c) for c in ['red', 'blue', 'green', 'black', 'purple', 'orange']])

  ec = e.edge_connectivity();
  lines = pos[ec];
  cols  = colors[m];
  lc = mc.LineCollection(lines, linewidths=1, color=cols);
  
  ax = plt.gca();
  ax.add_collection(lc)
  ax.autoscale()
  ax.margins(0.1)  
  plt.scatter(pos[:,0], pos[:,1])
  p =pos[:graph.n_vertices]
  plt.scatter(p[:,0],p[:,1], color='red')
Esempio n. 29
0
 def plot(self, ftype, **kwargs):
   return p3d.plot(self.filename(ftype, **kwargs));
Esempio n. 30
0
def _test():
  import numpy as np  
  import ClearMap.ParallelProcessing.DataProcessing.ArrayProcessing as ap 
  
  from importlib import reload
  reload(ap)
  
  
  ## Lookup table processing
  
  #apply_lut  
  x = np.random.randint(0, 100, size=(20,30));
  lut = np.arange(100) + 1;
  y = ap.apply_lut(x, lut)
  assert np.all(y == x+1)

  #apply_lut_to_index
  import ClearMap.ImageProcessing.Topology.Topology3d as t3d
  kernel = t3d.index_kernel(dtype=int);
  
  import ClearMap.ImageProcessing.Binary.Smoothing as sm
  lut = sm.initialize_lookup_table()
    
  data = np.random.randint(0, 2, (1500,300,400), dtype = bool)
  
  #reload(ap)
  result = ap.apply_lut_to_index(data, kernel, lut, sink=None, verbose=True)

  import ClearMap.Visualization.Plot3d as p3d
  p3d.plot([data, result])    
  
  
  ## Correlation 
  
  #correlate1d
  #reload(ap)
  axis = 1;
  kernel = np.array(range(11), dtype='uint32');  
  data = np.random.randint(0, 2**27, (1000, 1500,100), dtype='uint32');

  corr = ap.correlate1d(data, kernel, axis=axis, verbose=True, processes=10);
  
  import scipy.ndimage as ndi
  import ClearMap.Utils.Timer as tmr
  timer = tmr.Timer();
  corr_ndi = ndi.correlate1d(data, kernel, axis=axis, mode='constant',cval=0);
  timer.print_elapsed_time('ndi')  
  
  assert np.allclose(corr, corr_ndi)
  
  

#
#
#
#
#
#default_blocks_per_process = 10;
#"""Default number of blocks per process to split the data.
#
#Note
#----
#10 blocks per process is a good choice.
#"""
#
#default_cutoff = 20000000;
#"""Default size of array below which ordinary numpy is used.
#
#Note
#----
#Ideally test this on your machine for different array sizes.
#"""
#
#
#
#def blockRanges(data, blocks = None,  processes = defaultProcesses):
#  """Ranges of evenly spaced blocks in array
#  
#  Arguments:
#    data : array
#      array to divide in blocks
#    blocks : int or None
#      number of blocks to split array into
#    processes : None or int
#      number of processes, if None use number of cpus
#    
#  Returns:
#    array
#      list of the range boundaries
#  """
#  if processes is None:
#    processes = defaultProcesses;
#  if blocks is None:
#    blocks = processes * defaultBlocksPerProcess;
#   
#  d = data.reshape(-1, order = 'A'); 
#  blocks = min(blocks, d.shape[0]);
#  return np.array(np.linspace(0,  d.shape[0], blocks + 1), dtype = int);
#
#
#def blockSums(data, blocks = None, processes = defaultProcesses):
#  """Sums of evenly spaced blocks in array
#  
#  Arguments:
#    data : array
#      array to perform the block sums on
#    blocks : int or None
#      number of blocks to split array into
#    processes : None or int
#      number of processes, if None use number of cpus
#    
#  Returns:
#    array
#      sums of the values in the different blocks
#  """
#  if processes is None:
#    processes = defaultProcesses;
#  if blocks is None:
#    blocks = processes * defaultBlocksPerProcess;
#  
#  d = data.reshape(-1, order = 'A');
#  if data.dtype == bool:
#    d = d.view('uint8')
#  
#  return code.blockSums1d(d, blocks = blocks, processes = processes);
#  
#
#def where(data, out = None, blocks = None, cutoff = defaultCutoff, processes = defaultProcesses):
#  """Returns the indices of the non-zero entries of the array
#  
#  Arguments:
#    data : array
#      array to search for nonzero indices
#    out : array or None
#      if not None results is written into this array
#    blocks : int or None
#      number of blocks to split array into for parallel processing
#    cutoff : int
#      number of elements below whih to switch to numpy.where
#    processes : None or int
#      number of processes, if None use number of cpus
#    
#  Returns:
#    array
#      positions of the nonzero entries of the input array
#  
#  Note:
#    Uses numpy.where if there is no match of dimension implemented!
#  """ 
#  if data.ndim != 1 and data.ndim != 3:
#    raise Warning('Using numpy where for dimension %d and type %s!' % (data.ndim, data.dtype))
#    return np.vstack(np.where(data)).T;
#
#  if cutoff is None:
#    cutoff = 1;
#  cutoff = min(1, cutoff);
#  if data.size <= cutoff:
#    return np.vstack(np.where(data)).T;
#
#  if processes is None:
#    processes = defaultProcesses;
#  if blocks is None:
#    blocks = processes * defaultBlocksPerProcess;
#  
#  if data.dtype == bool:
#    d = data.view('uint8')
#  else:
#    d = data;
#  
#  if out is None:
#    if d.ndim == 1:
#      sums = code.blockSums1d(d, blocks = blocks, processes = processes);
#    else:
#      sums = code.blockSums3d(d, blocks = blocks, processes = processes);
#    out = np.squeeze(np.zeros((np.sum(sums), data.ndim), dtype = np.int));
#  else:
#    sums = None;
#  
#  if d.ndim == 1:
#    code.where1d(d, out = out, sums = sums, blocks = blocks, processes = processes);
#  else: # d.ndim == 3:
#    code.where3d(d, out = out, sums = sums, blocks = blocks, processes = processes);
#    
#  return out;
#
#
#
#
#def setValue(data, indices, value, cutoff = defaultCutoff, processes = defaultProcesses):
#  """Set value at specified indices of an array
#  
#  Arguments:
#    data : array
#      array to search for nonzero indices
#    indices : array or None
#      list of indices to set
#    value : numeric or bool
#      value to set elements in data to
#    processes : None or int
#      number of processes, if None use number of cpus
#    
#  Returns:
#    array
#      array with specified entries set to new value
#  
#  Note:
#    Uses numpy if there is no match of dimension implemented!
#  """
#  if data.ndim != 1:
#    raise Warning('Using numpy where for dimension %d and type %s!' % (data.ndim, data.dtype))
#    data[indices] = value;
#    return data;
#    
#  if cutoff is None:
#    cutoff = 1;
#  cutoff = min(1, cutoff);
#  if data.size <= cutoff:
#    data[indices] = value;
#    return data;
#  
#  if processes is None:
#    processes = defaultProcesses;
#  
#  if data.dtype == bool:
#    d = data.view('uint8')
#  else:
#    d = data;
#  
#  code.set1d(d, indices, value, processes = processes);
#  
#  return data;
#
#
#def setArray(data, indices, values, cutoff = defaultCutoff, processes = defaultProcesses):
#  """Set value at specified indices of an array
#  
#  Arguments:
#    data : array
#      array to search for nonzero indices
#    indices : array or None
#      list of indices to set
#    values : array
#      values to set elements in data to
#    processes : None or int
#      number of processes, if None use number of cpus
#    
#  Returns:
#    array
#      array with specified entries set to new value
#  
#  Note:
#    Uses numpy if there is no match of dimension implemented!
#  """
#  if data.ndim != 1:
#    raise Warning('Using numpy where for dimension %d and type %s!' % (data.ndim, data.dtype))
#    data[indices] = values;
#    return data;
#    
#  if cutoff is None:
#    cutoff = 1;
#  cutoff = min(1, cutoff);
#  if data.size <= cutoff:
#    data[indices] = values;
#    return data;
#  
#  if processes is None:
#    processes = defaultProcesses;
#  
#  if data.dtype == bool:
#    d = data.view('uint8')
#  else:
#    d = data;
#  
#  code.set1darray(d, indices, values, processes = processes);
#  
#  return data;
#
#
#
#def take(data, indices, out = None, cutoff = defaultCutoff, processes = defaultProcesses):
#  """Extracts the values at specified indices
#  
#  Arguments:
#    data : array
#      array to search for nonzero indices
#    out : array or None
#      if not None results is written into this array
#    cutoff : int
#      number of elements below whih to switch to numpy.where
#    processes : None or int
#      number of processes, if None use number of cpus
#    
#  Returns:
#    array
#      positions of the nonzero entries of the input array
#  
#  Note:
#    Uses numpy data[indices] if there is no match of dimension implemented!
#  """ 
#  if data.ndim != 1:
#    raise Warning('Using numpy where for dimension %d and type %s!' % (data.ndim, data.dtype))
#    return data[indices];
#
#  if cutoff is None:
#    cutoff = 1;
#  cutoff = min(1, cutoff);
#  if data.size < cutoff:
#    return data[indices];
#
#  if processes is None:
#    processes = defaultProcesses;
#  
#  if data.dtype == bool:
#    d = data.view('uint8')
#  else:
#    d = data;
#
#  if out is None:
#    out = np.empty(len(indices), dtype = data.dtype);
#  if out.dtype == bool:
#    o = out.view('uint8');
#  else:
#    o = out;
#  
#  code.take1d(d, indices, o, processes = processes);
#  
#  return out;
#
#
#def match(match, indices, out = None):
#  """Matches a sorted list of 1d indices to another larger one 
#  
#  Arguments:
#    match : array
#      array of indices to match to indices
#    indices : array or None
#      array of indices
#  
#  Returns:
#    array
#      array with specified entries set to new value
#  
#  Note:
#    Uses numpy if there is no match of dimension implemented!
#  """
#  if match.ndim != 1:
#    raise ValueError('Match array dimension required to be 1d, found %d!' % (match.ndim))
#  if indices.ndim != 1:
#    raise ValueError('Indices array dimension required to be 1d, found %d!' % (indices.ndim))  
#  
#  if out is None:
#    out = np.empty(len(match), dtype = match.dtype);
#  
#  code.match1d(match, indices, out);
#  
#  return out;
#
#
# Find neighbours in an index list
#
#
#def neighbours(indices, offset, processes = defaultProcesses):
#  """Returns all pairs of indices that are apart a specified offset"""
#  return code.neighbours(indices, offset = offset,  processes = processes);
#
#
#def findNeighbours(indices, center, shape, strides, mask):
#  """Finds all indices within a specified kernel region centered at a point"""
#  
#  if len(strides) != 3 or len(shape) != 3 or (strides[0] != 1 and strides[2] != 1):
#    raise RuntimeError('only 3d C or F contiguous arrays suported');
#
#  if isinstance(mask, int):
#    mask = (mask,);
#  if isinstance(mask, tuple):
#    mask = mask * 3;
#    return code.neighbourlistRadius(indices, center, shape[0], shape[1], shape[2], 
#                                                     strides[0], strides[1], strides[2], 
#                                                     mask[0], mask[1], mask[2]);
#  else:
#    if mask.dtype == bool:
#      mask = mask.view(dtype = 'uint8');
#                                                
#    return code.neighbourlistMask(indices, center, shape[0], shape[1], shape[2], strides[0], strides[1], strides[2], mask);
# 
# Loading and saving
#
#def readNumpyHeader(filename):
#  """Read numpy array information including offset to data
#  
#  Arguments:
#    filename : str
#      file name of the numpy file
#      
#  Returns:
#    shape : tuple
#      shape of the array
#    dtype : dtype
#      data type of array 
#    order : str
#      'C' for c and 'F' for fortran order
#    offset : int
#      offset in bytes to data buffer in file
#  """
#  with open(filename, 'rb') as fhandle:
#    major, minor = np.lib.format.read_magic(fhandle);
#    shape, fortran, dtype = np.lib.format.read_array_header_1_0(fhandle);
#    offset = fhandle.tell()
#  
#  order = 'C';
#  if fortran:
#    order = 'F';
#    
#  return (shape, dtype, order, offset)
# 
# 
#def _offsetFromSlice(sourceSlice, order = 'F'):
#  """Checks if slice is compatible with the large data loader and returns z coordiante"""
#   
#  if order == 'C':
#    os = 1; oe = 3; oi = 0;
#  else:
#    os = 0; oe = 2; oi = 2;
#  
#  for s in sourceSlice[os:oe]:
#    if s.start is not None or s.stop is not None or s.step is not None:
#        raise RuntimeError('sub-regions other than in slowest dimension %d not supported!  slice = %r' % (oi, sourceSlice))
#  
#  s = sourceSlice[oi];
#  if s.step is not None:
#      raise RuntimeError('sub-regions with non unity steps not supported')
#  
#  if s.start is None:
#    s = 0;
#  else:
#    s = s.start;
#    
#  return s;
#
#
#def load(filename, region = None, shared = False, blocks = None, processes = cpu_count(), verbose = False):
#  """Load a large npy array into memory in parallel
#  
#  Arguments:
#    filename : str
#      filename of array to load
#    region : Region or None
#      if not None this specifies the sub-region to read
#    shared : bool
#      if True read into shared memory
#    blocks : int or None
#      number of blocks to split array into for parallel processing
#    processes : None or int
#      number of processes, if None use number of cpus
#    verbose : bool
#      print info about the file to be loaded
#    
#  Returns:
#    array 
#      the data as numpy array
#  """
#  if processes is None:
#    processes = cpu_count();
#  if blocks is None:
#    blocks = processes * defaultBlocksPerProcess;
#  
#  #get specs from header specs
#  shape, dtype, order, offset = readNumpyHeader(filename);
#  if verbose:
#    timer = tmr.Timer();
#    print('Loading array of shape = %r, dtype = %r, order = %r, offset = %r' %(shape, dtype, order, offset)); 
#  
#  if region is not None:
#    shape = region.shape();  
#    sourceSlice = region.sourceSlice();
#    off = _offsetFromSlice(sourceSlice, order = order);
#  
#  if shared:
#    data = shm.create(shape, dtype = dtype, order = order);
#  else:
#    data = np.empty(shape, dtype = dtype, order = order);
#  
#  d = data.reshape(-1, order = 'A');
#  if dtype == bool:
#    d = d.view('uint8');  
#  
#  if region is not None:
#    if order == 'F':
#      offset += data.strides[-1] * off;  
#    else:
#      offset += data.strides[1] * off;  
#  
#  code.load(data = d, filename = filename, offset = offset, blocks = blocks, processes = processes);
#  
#  if verbose:
#    timer.printElapsedTime(head = 'Loading array from %s' % filename);
#           
#  return data;
#
#
#
#
#def save(filename, data, region = None, blocks = None, processes = cpu_count(), verbose = False):
#  """Save a large npy array to disk in parallel
#  
#  Arguments:
#    filename : str
#      filename of array to load
#    data : array
#      array to save to disk
#    blocks : int or None
#      number of blocks to split array into for parallel processing
#    processes : None or int
#      number of processes, if None use number of cpus
#    verbose : bool
#      print info about the file to be loaded
#    
#  Returns:
#    str 
#      the filename of the numpy array on disk
#  """
#  if processes is None:
#    processes = cpu_count();
#  if blocks is None:
#    blocks = processes * defaultBlocksPerProcess;
#  
#  if region is None:
#    #create file on disk via memmap
#    memmap = np.lib.format.open_memmap(filename, mode = 'w+', shape = data.shape, dtype = data.dtype, fortran_order = np.isfortran(data));
#    memmap.flush();
#    del(memmap);
#  
#  #get specs from header specs
#  shape, dtype, order, offset = readNumpyHeader(filename);
#  if verbose:
#    timer = tmr.Timer();
#    print('Saving array of shape = %r, dtype = %r, order = %r, offset = %r' %(shape, dtype, order, offset)); 
#  
#  if (np.isfortran(data) and order != 'F') or (not np.isfortran(data) and order != 'C'):
#    raise RuntimeError('Order of arrays do not match isfortran=%r and order=%s' % (np.isfortran(data), order));
#  
#  d = data.reshape(-1, order = 'A');
#  if dtype == bool:
#    d = d.view('uint8');
#    
#  if region is not None:
#    sourceSlice = region.sourceSlice();
#    off = _offsetFromSlice(sourceSlice, order = order);
#    if order == 'F':
#      offset += data.strides[-1] * off;
#    else:
#      offset += data.strides[1] * off;
#  
#  #print d.dtype, filename, offset, blocks, processes
#  
#  code.save(data = d, filename = filename, offset = offset, blocks = blocks, processes = processes);
#  
#  if verbose:
#    timer.printElapsedTime(head = 'Saving array to %s' % filename);
#           
#  return filename;
#
#
#
#
#
#
#if __name__ == "__main__":
#  
#  import numpy as np
#  from ClearMap.Utils.Timer import Timer;
#  import ClearMap.DataProcessing.LargeData as ld
#  reload(ld)
#  
#  
#  #dat = np.random.rand(2000,2000,1000) > 0.5;
#  #dat = np.random.rand(1000,1000,500) > 0.5;
#  dat = np.random.rand(200,300,400) > 0.5;  
#  #datan = io.MMP.writeData('test.npy', dat);
#  
#  dat = np.load('data.npy')
#  xyz1 = np.load('points.npy')
#  
#  s = ld.sum(dat)
#  print(s == np.sum(s))
#
#
#  timer = Timer();
#  xyz = ld.where(dat)
#  timer.printElapsedTime('parallel')
#  #parallel: elapsed time: 0:00:25.807
#  
#  timer = Timer();
#  xyz1 = np.vstack(np.where(dat)).T
#  timer.printElapsedTime('numpy')
#  #numpy: elapsed time: 0:05:45.590
#  
#  
#  d0 = np.zeros(dat.shape, dtype = bool);
#  d1 = np.zeros(dat.shape, dtype = bool);
#  
#  d0[xyz[:,0], xyz[:,1], xyz[:,2]] = True;
#  d1[xyz1[:,0], xyz1[:,1], xyz1[:,2]] = True;
#  np.all(d0 == d1)
#  
#  dat2 = np.array(np.random.rand(1000, 1000, 1000) > 0, dtype = 'bool');
#  filename = 'test.npy';
#  np.save(filename, dat2)
#  
#  filename = '/disque/raid/vasculature/4X-test2/170824_IgG_2/170824_IgG_16-23-46/rank_threshold.npy'
#  
#  timer = Timer();
#  ldat = ld.load(filename, verbose = True);
#  timer.printElapsedTime('load')
#  #load: elapsed time: 0:00:04.867
#  
#  timer = Timer(); 
#  ldat2 = np.load(filename);  
#  timer.printElapsedTime('numpy')
#  #numpy: elapsed time: 0:00:27.982
#  
#  np.all(ldat == ldat2)
#  
#  timer = Timer();
#  xyz = ld.where(ldat)
#  timer.printElapsedTime('parallel')
#  #parallel: elapsed time: 0:07:25.698
#  
#  lldat = ldat.reshape(-1, order = 'A')
#  timer = Timer();
#  xyz = ld.where(lldat)
#  timer.printElapsedTime('parallel 1d')
#  #parallel 1d: elapsed time: 0:00:49.034
#  
#  timer = Timer();
#  xyz = np.where(ldat)
#  timer.printElapsedTime('numpy')
#  
#  
#  import os
#  #os.remove(filename)
#  
#  filename = './ClearMap/Test/Skeletonization/test_bin.npy';
#  timer = Timer();
#  ldat = ld.load(filename, shared = True, verbose = True);
#  timer.printElapsedTime('load')
#  
#  ld.shm.isShared(ldat);
#  
#  
#  
#  import numpy as np
#  from ClearMap.Utils.Timer import Timer;
#  import ClearMap.DataProcessing.LargeData as ld
#  reload(ld)
#  
#  filename = 'test_save.npy';
#  
#  dat = np.random.rand(100,200,100);
#  
#  ld.save(filename, dat)
#  
#  
#  dat2 = ld.load(filename)
#  
#  np.all(dat == dat2)
#  
#  os.remove(filename)
#  
#  
#    
#  import numpy as np
#  from ClearMap.Utils.Timer import Timer;
#  import ClearMap.DataProcessing.LargeData as ld
#  reload(ld)
#  
#  dat = np.zeros(100, dtype = bool);
#  dat2 = dat.copy();
#  
#  indices = np.array([5,6,7,8,13,42])  
#  
#  ld.setValue(dat, indices, True, cutoff = 0);
#  
#  dat2[indices] = True;
#  np.all(dat2 == dat)
#  
#  d = ld.take(dat, indices, cutoff = 0)
#  np.all(d)
#  
#  
#  import numpy as np
#  from ClearMap.Utils.Timer import Timer;
#  import ClearMap.DataProcessing.LargeData as ld
#  reload(ld)
#  
#  
#  pts = np.array([0,1,5,6,10,11], dtype = int);
#  
#  ld.neighbours(pts, -10)
#  
#  
#  import numpy as np
#  from ClearMap.Utils.Timer import Timer;
#  import ClearMap.DataProcessing.LargeData as ld
#  import ClearMap.ImageProcessing.Filter.StructureElement as sel;
#  reload(ld)
#  
#  dat = np.random.rand(30,40,50) > 0.5;
#  mask = sel.structureElement('Disk', (5,5,5));
#  indices = np.where(dat.reshape(-1))[0];
#  c_id = len(indices)/2;
#  c = indices[c_id];
#  xyz = np.unravel_index(c, dat.shape)
#  l = np.array(mask.shape)/2
#  r = np.array(mask.shape) - l;
#  dlo = [max(0,xx-ll) for xx,ll in zip(xyz,l)];
#  dhi = [min(xx+rr,ss) for xx,rr,ss in zip(xyz,r, dat.shape)]
#  mlo = [-min(0,xx-ll) for xx,ll in zip(xyz,l)];
#  mhi = [mm + min(0, ss-xx-rr) for xx,rr,ss,mm in zip(xyz,r, dat.shape, mask.shape)]
#  
#  nbh = dat[dlo[0]:dhi[0], dlo[1]:dhi[1], dlo[2]:dhi[2]];
#  nbhm = np.logical_and(nbh, mask[mlo[0]:mhi[0], mlo[1]:mhi[1], mlo[2]:mhi[2]] > 0);
#  nxyz = np.where(nbhm);
#  nxyz = [nn + dl for nn,dl in zip(nxyz, dlo)];
#  nbi = np.ravel_multi_index(nxyz, dat.shape);
#  
#  nbs = ld.findNeighbours(indices, c_id , dat.shape, dat.strides, mask)
#  
#  nbs.sort();
#  print np.all(nbs == nbi)
#  
#  
#  dat = np.random.rand(30,40,50) > 0.5;
#  indices = np.where(dat.reshape(-1))[0];
#  c_id = len(indices)/2;
#  c = indices[c_id];
#  xyz = np.unravel_index(c, dat.shape)
#  l = np.array([2,2,2]);
#  r = l + 1;
#  dlo = [max(0,xx-ll) for xx,ll in zip(xyz,l)];
#  dhi = [min(xx+rr,ss) for xx,rr,ss in zip(xyz, r, dat.shape)]  
#  nbh = dat[dlo[0]:dhi[0], dlo[1]:dhi[1], dlo[2]:dhi[2]];
#  nxyz = np.where(nbh);
#  nxyz = [nn + dl for nn,dl in zip(nxyz, dlo)];
#  nbi = np.ravel_multi_index(nxyz, dat.shape);
#  
#  nbs = ld.findNeighbours(indices, c_id , dat.shape, dat.strides, tuple(l))
#  
#  nbs.sort();
#  print np.all(nbs == nbi)
#  
#  print nbs
#  print nbi
#  
#  
#  import numpy as np
#  from ClearMap.Utils.Timer import Timer;
#  import ClearMap.DataProcessing.LargeData as ld
#  reload(ld)
#  
#  data = np.random.rand(100);
#  values =np.random.rand(50);
#  indices = np.arange(50);
#  ld.setArray(data, indices, values, cutoff = 1)
#  print np.all(data[:50] == values)
#  
#  import numpy as np
#  from ClearMap.Utils.Timer import Timer;
#  import ClearMap.DataProcessing.LargeData as ld
#  reload(ld)
#  
#  m = np.array([1,3,6,7,10]);
#  i = np.array([1,2,3,4,6,7,8,9]);
#  
#  o = ld.match(m,i)
#  
#  o2 = [np.where(i==l)[0][0] for l in m]
#
#
#