hanslovsky

dask imglyb example

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