<s> import numpy as np <EOL> def collapse ( j , data ) : <EOL> """<STR_LIT>""" <EOL> uniques = np . unique ( j ) <EOL> return uniques , np . array ( [ data [ j == u ] . sum ( ) for u in uniques ] ) <EOL> def nearest ( coords , shape , ** kwargs ) : <EOL> valid = ~ ( np . isnan ( coords ) . all ( <NUM_LIT:1> ) ) <EOL> valid = np . logical_and ( valid , np . logical_and ( coords [ : , <NUM_LIT:0> ] > - <NUM_LIT> , coords [ : , <NUM_LIT:0> ] < shape [ <NUM_LIT:2> ] + <NUM_LIT> ) ) <EOL> valid = np . logical_and ( valid , np . logical_and ( coords [ : , <NUM_LIT:1> ] > - <NUM_LIT> , coords [ : , <NUM_LIT:1> ] < shape [ <NUM_LIT:1> ] + <NUM_LIT> ) ) <EOL> valid = np . logical_and ( valid , np . logical_and ( coords [ : , <NUM_LIT:2> ] > - <NUM_LIT> , coords [ : , <NUM_LIT:2> ] < shape [ <NUM_LIT:0> ] + <NUM_LIT> ) ) <EOL> rcoords = coords [ valid ] . round ( ) . astype ( int ) <EOL> j = np . ravel_multi_index ( rcoords . T [ : : - <NUM_LIT:1> ] , shape , mode = '<STR_LIT>' ) <EOL> return np . nonzero ( valid ) [ <NUM_LIT:0> ] , j , np . ones ( ( valid . sum ( ) , ) ) <EOL> def trilinear ( coords , shape , ** kwargs ) : <EOL> valid = ~ ( np . isnan ( coords ) . all ( <NUM_LIT:1> ) ) <EOL> ( x , y , z ) , floor = np . modf ( coords [ valid ] . T ) <EOL> floor = floor . astype ( int ) <EOL> ceil = floor + <NUM_LIT:1> <EOL> x [ x < <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> y [ y < <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> z [ z < <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> i000 = np . array ( [ floor [ <NUM_LIT:2> ] , floor [ <NUM_LIT:1> ] , floor [ <NUM_LIT:0> ] ] ) <EOL> i100 = np . array ( [ floor [ <NUM_LIT:2> ] , floor [ <NUM_LIT:1> ] , ceil [ <NUM_LIT:0> ] ] ) <EOL> i010 = np . array ( [ floor [ <NUM_LIT:2> ] , ceil [ <NUM_LIT:1> ] , floor [ <NUM_LIT:0> ] ] ) <EOL> i001 = np . array ( [ ceil [ <NUM_LIT:2> ] , floor [ <NUM_LIT:1> ] , floor [ <NUM_LIT:0> ] ] ) <EOL> i101 = np . array ( [ ceil [ <NUM_LIT:2> ] , floor [ <NUM_LIT:1> ] , ceil [ <NUM_LIT:0> ] ] ) <EOL> i011 = np . array ( [ ceil [ <NUM_LIT:2> ] , ceil [ <NUM_LIT:1> ] , floor [ <NUM_LIT:0> ] ] ) <EOL> i110 = np . array ( [ floor [ <NUM_LIT:2> ] , ceil [ <NUM_LIT:1> ] , ceil [ <NUM_LIT:0> ] ] ) <EOL> i111 = np . array ( [ ceil [ <NUM_LIT:2> ] , ceil [ <NUM_LIT:1> ] , ceil [ <NUM_LIT:0> ] ] ) <EOL> v000 = ( <NUM_LIT:1> - x ) * ( <NUM_LIT:1> - y ) * ( <NUM_LIT:1> - z ) <EOL> v100 = x * ( <NUM_LIT:1> - y ) * ( <NUM_LIT:1> - z ) <EOL> v010 = ( <NUM_LIT:1> - x ) * y * ( <NUM_LIT:1> - z ) <EOL> v110 = x * y * ( <NUM_LIT:1> - z ) <EOL> v001 = ( <NUM_LIT:1> - x ) * ( <NUM_LIT:1> - y ) * z <EOL> v101 = x * ( <NUM_LIT:1> - y ) * z <EOL> v011 = ( <NUM_LIT:1> - x ) * y * z <EOL> v111 = x * y * z <EOL> i = np . tile ( np . nonzero ( valid ) [ <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:8> ] ) . ravel ( ) <EOL> j = np . hstack ( [ i000 , i100 , i010 , i001 , i101 , i011 , i110 , i111 ] ) <EOL> data = np . vstack ( [ v000 , v100 , v010 , v001 , v101 , v011 , v110 , v111 ] ) . ravel ( ) <EOL> return i , np . ravel_multi_index ( j , shape , mode = '<STR_LIT>' ) , data <EOL> def distance_func ( func , coords , shape , renorm = True , mp = True ) : <EOL> """<STR_LIT>""" <EOL> nZ , nY , nX = shape <EOL> dx = coords [ : , <NUM_LIT:0> ] - np . atleast_2d ( np . arange ( nX ) ) . T <EOL> dy = coords [ : , <NUM_LIT:1> ] - np . atleast_2d ( np . arange ( nY ) ) . T <EOL> dz = coords [ : , <NUM_LIT:2> ] - np . atleast_2d ( np . arange ( nZ ) ) . T <EOL> Lx , Ly , Lz = func ( dx ) , func ( dy ) , func ( dz ) <EOL> ix , jx = np . nonzero ( Lx ) <EOL> iy , jy = np . nonzero ( Ly ) <EOL> iz , jz = np . nonzero ( Lz ) <EOL> ba = np . broadcast_arrays <EOL> def func ( v ) : <EOL> mx , my , mz = ix [ jx == v ] , iy [ jy == v ] , iz [ jz == v ] <EOL> idx , idy , idz = [ i . ravel ( ) for i in ba ( * np . ix_ ( mx , my , mz ) ) ] <EOL> vx , vy , vz = [ i . ravel ( ) for i in ba ( * np . ix_ ( Lx [ mx , v ] , Ly [ my , v ] , Lz [ mz , v ] ) ) ] <EOL> i = v * np . ones ( ( len ( idx , ) ) ) <EOL> j = np . ravel_multi_index ( ( idz , idy , idx ) , shape , mode = '<STR_LIT>' ) <EOL> data = vx * vy * vz <EOL> if renorm : <EOL> data /= data . sum ( ) <EOL> return i , j , data <EOL> if mp : <EOL> from . . import mp <EOL> ijdata = mp . map ( func , range ( len ( coords ) ) ) <EOL> else : <EOL> ijdata = map ( func , range ( len ( coords ) ) ) <EOL> return np . hstack ( ijdata ) <EOL> def gaussian ( coords , shape , sigma = <NUM_LIT:1> , window = <NUM_LIT:3> , ** kwargs ) : <EOL> raise NotImplementedError <EOL> def gaussian ( x ) : <EOL> pass <EOL> return distance_func ( gaussian , coords , shape , ** kwargs ) <EOL> def lanczos ( coords , shape , window = <NUM_LIT:3> , ** kwargs ) : <EOL> def lanczos ( x ) : <EOL> out = np . zeros_like ( x ) <EOL> sel = np . abs ( x ) < window <EOL> selx = x [ sel ] <EOL> out [ sel ] = np . sin ( np . pi * selx ) * np . sin ( np . pi * selx / window ) * ( window / ( np . pi ** <NUM_LIT:2> * selx ** <NUM_LIT:2> ) ) <EOL> return out <EOL> return distance_func ( lanczos , coords , shape , ** kwargs ) </s>
