<s> """<STR_LIT>""" <EOL> from textwrap import dedent <EOL> import numpy as np <EOL> from six import string_types <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> def _wrap_apply ( f ) : <EOL> def wrapped ( arr , ** kwargs ) : <EOL> if arr is None or not len ( arr ) : <EOL> return arr <EOL> arr = np . atleast_2d ( arr ) <EOL> assert arr . ndim == <NUM_LIT:2> <EOL> assert arr . dtype == np . float64 <EOL> out = f ( arr , ** kwargs ) <EOL> assert out . dtype == np . float64 <EOL> out = np . atleast_2d ( out ) <EOL> assert out . ndim == <NUM_LIT:2> <EOL> assert out . shape [ <NUM_LIT:1> ] == arr . shape [ <NUM_LIT:1> ] <EOL> return out <EOL> return wrapped <EOL> def _wrap_glsl ( f ) : <EOL> def wrapped ( var , ** kwargs ) : <EOL> out = f ( var , ** kwargs ) <EOL> out = dedent ( out ) . strip ( ) <EOL> return out <EOL> return wrapped <EOL> def _glslify ( r ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( r , string_types ) : <EOL> return r <EOL> else : <EOL> assert <NUM_LIT:2> <= len ( r ) <= <NUM_LIT:4> <EOL> return '<STR_LIT>' . format ( len ( r ) , '<STR_LIT:U+002CU+0020>' . join ( map ( str , r ) ) ) <EOL> def _minus ( value ) : <EOL> if isinstance ( value , np . ndarray ) : <EOL> return - value <EOL> else : <EOL> assert len ( value ) == <NUM_LIT:2> <EOL> return - value [ <NUM_LIT:0> ] , - value [ <NUM_LIT:1> ] <EOL> def _inverse ( value ) : <EOL> if isinstance ( value , np . ndarray ) : <EOL> return <NUM_LIT:1.> / value <EOL> elif hasattr ( value , '<STR_LIT>' ) : <EOL> assert len ( value ) == <NUM_LIT:2> <EOL> return <NUM_LIT:1.> / value [ <NUM_LIT:0> ] , <NUM_LIT:1.> / value [ <NUM_LIT:1> ] <EOL> else : <EOL> return <NUM_LIT:1.> / value <EOL> def _normalize ( arr , m , M ) : <EOL> d = float ( M - m ) <EOL> b = <NUM_LIT> / d <EOL> a = - <NUM_LIT:1> - m / d <EOL> arr *= b <EOL> arr += a <EOL> return arr <EOL> def subplot_bounds ( shape = None , index = None ) : <EOL> i , j = index <EOL> n_rows , n_cols = shape <EOL> assert <NUM_LIT:0> <= i <= n_rows - <NUM_LIT:1> <EOL> assert <NUM_LIT:0> <= j <= n_cols - <NUM_LIT:1> <EOL> width = <NUM_LIT> / n_cols <EOL> height = <NUM_LIT> / n_rows <EOL> x = - <NUM_LIT:1.0> + j * width <EOL> y = + <NUM_LIT:1.0> - ( i + <NUM_LIT:1> ) * height <EOL> return [ x , y , x + width , y + height ] <EOL> def subplot_bounds_glsl ( shape = None , index = None ) : <EOL> x0 = '<STR_LIT>' . format ( s = shape , i = index ) <EOL> y0 = '<STR_LIT>' . format ( s = shape , i = index ) <EOL> x1 = '<STR_LIT>' . format ( s = shape , i = index ) <EOL> y1 = '<STR_LIT>' . format ( s = shape , i = index ) <EOL> return '<STR_LIT>' . format ( x0 = x0 , y0 = y0 , x1 = x1 , y1 = y1 ) <EOL> def pixels_to_ndc ( pos , size = None ) : <EOL> """<STR_LIT>""" <EOL> pos = np . asarray ( pos , dtype = np . float64 ) <EOL> size = np . asarray ( size , dtype = np . float64 ) <EOL> pos = pos / ( size / <NUM_LIT> ) - <NUM_LIT:1> <EOL> pos [ <NUM_LIT:1> ] = - pos [ <NUM_LIT:1> ] <EOL> return pos <EOL> """<STR_LIT>""" <EOL> NDC = ( - <NUM_LIT:1.0> , - <NUM_LIT:1.0> , + <NUM_LIT:1.0> , + <NUM_LIT:1.0> ) <EOL> class BaseTransform ( object ) : <EOL> def __init__ ( self , value = None ) : <EOL> self . value = value <EOL> self . apply = _wrap_apply ( self . apply ) <EOL> self . glsl = _wrap_glsl ( self . glsl ) <EOL> def apply ( self , arr ) : <EOL> raise NotImplementedError ( ) <EOL> def glsl ( self , var ) : <EOL> raise NotImplementedError ( ) <EOL> def inverse ( self ) : <EOL> raise NotImplementedError ( ) <EOL> class Translate ( BaseTransform ) : <EOL> def apply ( self , arr , value = None ) : <EOL> assert isinstance ( arr , np . ndarray ) <EOL> value = value if value is not None else self . value <EOL> return arr + np . asarray ( value ) <EOL> def glsl ( self , var ) : <EOL> assert var <EOL> return """<STR_LIT>""" . format ( var = var , <EOL> translate = self . value ) <EOL> def inverse ( self ) : <EOL> if isinstance ( self . value , string_types ) : <EOL> return Translate ( '<STR_LIT:->' + self . value ) <EOL> else : <EOL> return Translate ( _minus ( self . value ) ) <EOL> class Scale ( BaseTransform ) : <EOL> def apply ( self , arr , value = None ) : <EOL> value = value if value is not None else self . value <EOL> return arr * np . asarray ( value ) <EOL> def glsl ( self , var ) : <EOL> assert var <EOL> return """<STR_LIT>""" . format ( var = var , scale = self . value ) <EOL> def inverse ( self ) : <EOL> if isinstance ( self . value , string_types ) : <EOL> return Scale ( '<STR_LIT>' + self . value ) <EOL> else : <EOL> return Scale ( _inverse ( self . value ) ) <EOL> class Range ( BaseTransform ) : <EOL> def __init__ ( self , from_bounds = None , to_bounds = None ) : <EOL> super ( Range , self ) . __init__ ( ) <EOL> self . from_bounds = from_bounds if from_bounds is not None else NDC <EOL> self . to_bounds = to_bounds if to_bounds is not None else NDC <EOL> def apply ( self , arr , from_bounds = None , to_bounds = None ) : <EOL> from_bounds = np . asarray ( from_bounds if from_bounds is not None <EOL> else self . from_bounds , dtype = np . float64 ) <EOL> to_bounds = np . asarray ( to_bounds if to_bounds is not None <EOL> else self . to_bounds , dtype = np . float64 ) <EOL> f0 = from_bounds [ ... , : <NUM_LIT:2> ] <EOL> f1 = from_bounds [ ... , <NUM_LIT:2> : ] <EOL> t0 = to_bounds [ ... , : <NUM_LIT:2> ] <EOL> t1 = to_bounds [ ... , <NUM_LIT:2> : ] <EOL> d = ( f1 - f0 ) <EOL> d [ d == <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> out = arr . copy ( ) <EOL> out -= f0 <EOL> out *= ( t1 - t0 ) / d <EOL> out += t0 <EOL> return out <EOL> def glsl ( self , var ) : <EOL> assert var <EOL> from_bounds = _glslify ( self . from_bounds ) <EOL> to_bounds = _glslify ( self . to_bounds ) <EOL> return ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( var = var , f = from_bounds , t = to_bounds ) <EOL> def inverse ( self ) : <EOL> return Range ( from_bounds = self . to_bounds , <EOL> to_bounds = self . from_bounds ) <EOL> class Clip ( BaseTransform ) : <EOL> def __init__ ( self , bounds = None ) : <EOL> super ( Clip , self ) . __init__ ( ) <EOL> self . bounds = bounds or NDC <EOL> def apply ( self , arr , bounds = None ) : <EOL> bounds = bounds if bounds is not None else self . bounds <EOL> index = ( ( arr [ : , <NUM_LIT:0> ] >= bounds [ <NUM_LIT:0> ] ) & <EOL> ( arr [ : , <NUM_LIT:1> ] >= bounds [ <NUM_LIT:1> ] ) & <EOL> ( arr [ : , <NUM_LIT:0> ] <= bounds [ <NUM_LIT:2> ] ) & <EOL> ( arr [ : , <NUM_LIT:1> ] <= bounds [ <NUM_LIT:3> ] ) ) <EOL> return arr [ index , ... ] <EOL> def glsl ( self , var ) : <EOL> assert var <EOL> bounds = _glslify ( self . bounds ) <EOL> return """<STR_LIT>""" . format ( bounds = bounds , var = var ) <EOL> def inverse ( self ) : <EOL> return self <EOL> class Subplot ( Range ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , shape , index = None ) : <EOL> super ( Subplot , self ) . __init__ ( ) <EOL> self . shape = shape <EOL> self . index = index <EOL> self . from_bounds = NDC <EOL> if isinstance ( self . shape , tuple ) and isinstance ( self . index , tuple ) : <EOL> self . to_bounds = subplot_bounds ( shape = self . shape , index = self . index ) <EOL> elif ( isinstance ( self . shape , string_types ) and <EOL> isinstance ( self . index , string_types ) ) : <EOL> self . to_bounds = subplot_bounds_glsl ( shape = self . shape , <EOL> index = self . index ) <EOL> class TransformChain ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . transformed_var_name = None <EOL> self . cpu_transforms = [ ] <EOL> self . gpu_transforms = [ ] <EOL> def add_on_cpu ( self , transforms ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( transforms , list ) : <EOL> transforms = [ transforms ] <EOL> self . cpu_transforms . extend ( transforms or [ ] ) <EOL> return self <EOL> def add_on_gpu ( self , transforms ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( transforms , list ) : <EOL> transforms = [ transforms ] <EOL> self . gpu_transforms . extend ( transforms or [ ] ) <EOL> return self <EOL> def get ( self , class_name ) : <EOL> """<STR_LIT>""" <EOL> for transform in self . cpu_transforms + self . gpu_transforms : <EOL> if transform . __class__ . __name__ == class_name : <EOL> return transform <EOL> def _remove_transform ( self , transforms , name ) : <EOL> return [ t for t in transforms if t . __class__ . __name__ != name ] <EOL> def remove ( self , name ) : <EOL> """<STR_LIT>""" <EOL> cpu_transforms = self . _remove_transform ( self . cpu_transforms , name ) <EOL> gpu_transforms = self . _remove_transform ( self . gpu_transforms , name ) <EOL> return ( TransformChain ( ) . add_on_cpu ( cpu_transforms ) . <EOL> add_on_gpu ( gpu_transforms ) ) <EOL> def apply ( self , arr ) : <EOL> """<STR_LIT>""" <EOL> for t in self . cpu_transforms : <EOL> arr = t . apply ( arr ) <EOL> return arr <EOL> def inverse ( self ) : <EOL> """<STR_LIT>""" <EOL> transforms = self . cpu_transforms + self . gpu_transforms <EOL> inv_transforms = [ transform . inverse ( ) <EOL> for transform in transforms [ : : - <NUM_LIT:1> ] ] <EOL> return TransformChain ( ) . add_on_cpu ( inv_transforms ) <EOL> def __add__ ( self , tc ) : <EOL> assert isinstance ( tc , TransformChain ) <EOL> assert tc . transformed_var_name == self . transformed_var_name <EOL> self . cpu_transforms . extend ( tc . cpu_transforms ) <EOL> self . gpu_transforms . extend ( tc . gpu_transforms ) <EOL> return self </s>
