hanslovsky

more dask imglyb

May 24th, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.07 KB | None | 0 0
  1. import numpy as np
  2. import os
  3. import pathlib
  4. import subprocess
  5. import tempfile
  6. import time
  7. import threading
  8.  
  9. tmp_dir = tempfile.mkdtemp()
  10.  
  11. import jnius_config
  12. jnius_config.add_classpath( tmp_dir )
  13.  
  14.  
  15. import dask.array as da
  16. import imglyb
  17. import imglyb.types
  18. import imglyb.util
  19. from jnius import PythonJavaClass, java_method, autoclass, JavaException, cast
  20.  
  21. chunks_dict = {}
  22.  
  23. IntUnsafe = autoclass('net.imglib2.img.basictypelongaccess.unsafe.IntUnsafe')
  24. Cell      = autoclass('net.imglib2.img.cell.Cell')
  25.  
  26. make_cell_grid_code = """
  27. import java.util.Arrays;
  28.  
  29. import net.imglib2.img.cell.Cell;
  30. import net.imglib2.img.cell.CellGrid;
  31. import net.imglib2.img.cell.LazyCellImg;
  32. import net.imglib2.img.cell.LazyCellImg.Get;
  33. import net.imglib2.type.NativeType;
  34.  
  35. public class Helpers {
  36.  
  37.    public static CellGrid makeGrid( long[] dims, int[] cellSize ) {
  38.        return new CellGrid( dims, cellSize );
  39.    }
  40.  
  41.    public static < T extends NativeType< T >, A > LazyCellImg< T, A > lazyCellImg( final CellGrid grid, final T type, final Get< Cell< A > > get )
  42.    {
  43.        return new LazyCellImg<>( grid, type, get );
  44.    }
  45.  
  46.    public static long[] cellMin( final CellGrid grid, long index )
  47.    {
  48.        long[] min = new long[ grid.numDimensions() ];
  49.        grid.getCellGridPositionFlat( index, min );
  50.        Arrays.setAll( min, d -> min[ d ] * grid.cellDimension( d ) );
  51. //        for ( int d = 0; d < min.length; ++d )
  52. //            min[ d ] *= grid.cellDimension( d );
  53.        return min;
  54.    }
  55.    
  56. }
  57. """
  58.  
  59. fp = pathlib.Path( tmp_dir ) / 'Helpers.java'
  60. print( tmp_dir )
  61. with open( fp, 'w' ) as f:
  62.     f.write( make_cell_grid_code )
  63.  
  64. javac = pathlib.Path( os.environ[ 'JAVA_HOME' ] ) / 'bin' / 'javac'
  65. proc = subprocess.run(
  66.     [ javac, '-cp', jnius_config.split_char.join( jnius_config.get_classpath() ), fp ],
  67.     stdout=subprocess.PIPE,
  68.     stderr=subprocess.PIPE)
  69. if proc.returncode != 0:
  70.     print("Failed!")
  71.     print ( proc.stderr )
  72.  
  73. Helpers = autoclass('Helpers')
  74.  
  75. class LazyCellImgGetFromDaskArray(PythonJavaClass):
  76.     __javainterfaces__ = ['net/imglib2/img/cell/LazyCellImg$Get']
  77.  
  78.     def __init__(self, dask_array, cell_grid):
  79.         super(LazyCellImgGetFromDaskArray, self).__init__()
  80.         self.dask_array = dask_array
  81.         self.slices     = da.core.slices_from_chunks(self.dask_array.chunks)
  82.         self.cell_grid  = cell_grid
  83.        
  84.  
  85.     @java_method('(J)Ljava/lang/Object;', name='get')
  86.     def get(self, index):
  87.         if not index in chunks_dict:
  88.             chunk = np.ascontiguousarray(self.dask_array[self.slices[index]].compute())
  89.             chunks_dict[index] = chunk
  90.         else:
  91.             chunk = chunks_dict[index]
  92.         # TODO: instead of adding chunk to global list, pass reference to wrapper around IntUnsafe (or any other unsafe access)
  93.         address = chunk.ctypes.data
  94.         # TODO: replace 2nd chunk.shape by actual min of chunk
  95.         # TODO: do we need to use chunk.shape[::-1] instead?
  96.         print("Getting chunk", chunk.shape, index, address)
  97.         return Cell(chunk.shape[::-1], Helpers.cellMin(self.cell_grid, index), IntUnsafe(address))
  98.    
  99.  
  100. LazyCellImg = autoclass('net.imglib2.img.cell.LazyCellImg')
  101. CellGrid    = autoclass('net.imglib2.img.cell.CellGrid')
  102. ARGBType    = autoclass('net.imglib2.type.numeric.ARGBType')
  103.  
  104. chunks = [100, 200, 290]
  105. shape  = [10, 20, 30]
  106. array  = da.random.randint(low=0, high=2**31, size=shape, chunks=chunks)
  107. slices = da.core.slices_from_chunks(array.chunks)
  108.  
  109.  
  110. grid = Helpers.makeGrid(shape[::-1], chunks[::-1])
  111. get  = LazyCellImgGetFromDaskArray(array, grid)
  112. img  = Helpers.lazyCellImg(grid, ARGBType(), get)
  113.  
  114. print("Going to open bdv window")
  115. bdv = imglyb.util.BdvFunctions.show(img, 'random')
  116. print("Showing bdv")
  117. vp = bdv.getBdvHandle().getViewerPanel()
  118.  
  119. # Keep Python running until user closes Bdv window
  120. check = autoclass( 'net.imglib2.python.BdvWindowClosedCheck' )()
  121. frame = cast( 'javax.swing.JFrame', autoclass( 'javax.swing.SwingUtilities' ).getWindowAncestor( vp ) )
  122. frame.addWindowListener( check )
  123.  
  124. def sleeper():
  125.     while check.isOpen():
  126.         time.sleep( 0.1 )
  127.                
  128.  
  129. t = threading.Thread( target=sleeper )
  130. t.start()
  131. t.join()
  132. print(len(chunk_list_tmp))
Advertisement
Add Comment
Please, Sign In to add comment