<s> """<STR_LIT>""" <EOL> import zlib <EOL> from itertools import product <EOL> import numpy as np <EOL> from scipy . optimize import fmin_l_bfgs_b as minimize <EOL> from skimage . morphology import disk <EOL> from skimage . filter . rank import percentile_autolevel <EOL> from skimage . feature import daisy <EOL> from . field import get_field <EOL> from . util import normalize , ellipse , multiwavelet_from_rgb <EOL> class Extractor ( object ) : <EOL> def __init__ ( self ) : <EOL> self . shp = ( <NUM_LIT> , <NUM_LIT> ) <EOL> self . preprocessors = [ ] <EOL> def _preprocess_rgb ( self , rgb ) : <EOL> for p in self . preprocessors : <EOL> rgb = p ( rgb ) <EOL> return rgb <EOL> def __call__ ( self , lon , l , b , r ) : <EOL> return self . extract ( lon , l , b , r ) <EOL> def extract ( self , lon , l , b , r , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> kwargs . setdefault ( '<STR_LIT>' , [ <NUM_LIT:1> , <NUM_LIT> ] ) <EOL> kwargs . setdefault ( '<STR_LIT>' , self . shp ) <EOL> rgb = get_field ( lon ) . extract_stamp ( l , b , r , ** kwargs ) <EOL> if rgb is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> elif ( rgb [ : , : , <NUM_LIT:1> ] == <NUM_LIT:0> ) . mean ( ) > <NUM_LIT:0.1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> rgb = self . _preprocess_rgb ( rgb ) <EOL> return self . _extract_rgb ( rgb ) <EOL> def _extract_rgb ( self , rgb ) : <EOL> raise NotImplementedError ( ) <EOL> class RGBExtractor ( Extractor ) : <EOL> """<STR_LIT>""" <EOL> def _extract_rgb ( self , rgb ) : <EOL> return rgb <EOL> class MultiWaveletExtractor ( Extractor ) : <EOL> """<STR_LIT>""" <EOL> def _extract_rgb ( self , rgb ) : <EOL> return normalize ( multiwavelet_from_rgb ( rgb ) . reshape ( <NUM_LIT:1> , - <NUM_LIT:1> ) ) <EOL> class CompressionExtractor ( Extractor ) : <EOL> """<STR_LIT>""" <EOL> def _extract_rgb ( self , rgb ) : <EOL> clen = lambda x : np . array ( [ len ( zlib . compress ( x . tostring ( ) ) ) ] ) <EOL> return np . hstack ( map ( clen , [ rgb , rgb [ : , : , <NUM_LIT:0> ] , rgb [ : , : , <NUM_LIT:1> ] , <EOL> rgb [ : , : , <NUM_LIT:0> ] - rgb [ : , : , <NUM_LIT:1> ] ] ) ) . reshape ( <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> class RawStatsExtractor ( Extractor ) : <EOL> """<STR_LIT>""" <EOL> def extract ( self , lon , l , b , r ) : <EOL> rgb = get_field ( lon ) . extract_stamp ( l , b , r , do_scale = False ) <EOL> if rgb is None : <EOL> rgb = np . zeros ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:3> ) , dtype = np . uint8 ) <EOL> elif ( rgb [ : , : , <NUM_LIT:1> ] == <NUM_LIT:0> ) . mean ( ) > <NUM_LIT:0.1> : <EOL> rgb = np . zeros ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:3> ) , dtype = np . uint8 ) <EOL> return self . _extract_rgb ( rgb ) <EOL> def _extract_rgb ( self , rgb ) : <EOL> r = rgb [ : , : , <NUM_LIT:0> ] . astype ( np . float ) . ravel ( ) <EOL> g = rgb [ : , : , <NUM_LIT:1> ] . astype ( np . float ) . ravel ( ) <EOL> r [ ~ np . isfinite ( r ) ] = <NUM_LIT:0> <EOL> g [ ~ np . isfinite ( g ) ] = <NUM_LIT:0> <EOL> sums = np . array ( [ r . sum ( ) , g . sum ( ) ] ) <EOL> ps = [ <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:10> , <NUM_LIT:50> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> rper = np . array ( np . percentile ( r , ps ) ) <EOL> gper = np . array ( np . percentile ( g , ps ) ) <EOL> rdiff = rper [ [ <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ] ] - rper [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] <EOL> gdiff = gper [ [ <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ] ] - gper [ [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] <EOL> return np . hstack ( [ sums , rper , gper , rdiff , gdiff ] ) . reshape ( <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> class EllipseExtractor ( Extractor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( EllipseExtractor , self ) . __init__ ( ) <EOL> aas = np . linspace ( <NUM_LIT:8> , <NUM_LIT> , <NUM_LIT:3> ) <EOL> bs = aas <EOL> x0s = np . linspace ( <NUM_LIT:5> , <NUM_LIT> , <NUM_LIT:5> ) <EOL> y0s = np . linspace ( <NUM_LIT:5> , <NUM_LIT> , <NUM_LIT:5> ) <EOL> drs = [ <NUM_LIT:4> ] <EOL> thetas = np . linspace ( <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:4> ) <EOL> params = np . vstack ( product ( x0s , y0s , aas , bs , drs , thetas ) ) <EOL> templates = np . column_stack ( [ ellipse ( x0 , y0 , a , b , dr , theta ) . ravel ( ) <EOL> for x0 , y0 , a , b , dr , theta <EOL> in params ] ) <EOL> templates /= np . sqrt ( ( templates ** <NUM_LIT:2> ) . sum ( axis = <NUM_LIT:0> ) ) <EOL> self . templates = templates <EOL> self . params = params <EOL> def best_match ( self , arr ) : <EOL> arr = normalize ( arr ) <EOL> dot = np . dot ( arr , self . templates ) <EOL> best = np . argmax ( dot ) <EOL> return self . params [ best ] <EOL> def _fit_ellipse ( self , arr ) : <EOL> narr = normalize ( arr ) <EOL> bounds = [ ( <NUM_LIT:10> , <NUM_LIT:30> ) , ( <NUM_LIT:10> , <NUM_LIT:30> ) , ( <NUM_LIT:3> , <NUM_LIT:30> ) , ( <NUM_LIT:3> , <NUM_LIT:30> ) , ( <NUM_LIT> , <NUM_LIT> ) , ( <NUM_LIT:0> , <NUM_LIT> ) ] <EOL> def fun ( x ) : <EOL> e = ellipse ( <NUM_LIT:20> , <NUM_LIT:20> , * x [ <NUM_LIT:2> : ] ) . ravel ( ) . astype ( np . float ) <EOL> norm = np . sqrt ( ( e ** <NUM_LIT:2> ) . sum ( ) ) <EOL> e = ellipse ( * x ) . ravel ( ) . astype ( np . float ) / norm <EOL> return np . dot ( narr , e ) * ( - <NUM_LIT:1> ) <EOL> x0 = self . best_match ( narr ) <EOL> result = minimize ( fun , x0 , bounds = bounds , approx_grad = True ) <EOL> return result [ <NUM_LIT:0> ] , fun ( result [ <NUM_LIT:0> ] ) <EOL> def _extract_rgb ( self , rgb ) : <EOL> r , g = rgb [ : , : , <NUM_LIT:0> ] , rgb [ : , : , <NUM_LIT:1> ] <EOL> rp , rs = self . _fit_ellipse ( r ) <EOL> gp , gs = self . _fit_ellipse ( g ) <EOL> return np . hstack ( [ rp , rs , gp , gs ] ) . reshape ( <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> class RingExtractor ( Extractor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( RingExtractor , self ) . __init__ ( ) <EOL> self . _template_shp = None <EOL> def _prepare_templates ( self , shp ) : <EOL> if self . _template_shp == shp : <EOL> return <EOL> self . _template_shp = shp <EOL> s = shp [ <NUM_LIT:0> ] <EOL> y , x = np . mgrid [ <NUM_LIT:0> : s , <NUM_LIT:0> : s ] . astype ( np . float ) <EOL> r = np . hypot ( y - s / <NUM_LIT:2> , x - s / <NUM_LIT:2> ) <EOL> rs = np . linspace ( <NUM_LIT:1.> , s / <NUM_LIT:2> , <NUM_LIT:7> ) <EOL> ts = np . array ( [ <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:6> , <NUM_LIT:8> , <NUM_LIT:10> , <NUM_LIT:15> , <NUM_LIT:20> ] ) . astype ( np . float ) <EOL> ts *= ( s / <NUM_LIT:2> ) / <NUM_LIT:20> <EOL> self . rings = np . column_stack ( np . exp ( - ( r - rr ) ** <NUM_LIT:2> / tt ** <NUM_LIT:2> ) . ravel ( ) <EOL> for rr , tt in product ( rs , ts ) ) <EOL> def _extract_rgb ( self , rgb ) : <EOL> self . _prepare_templates ( rgb . shape ) <EOL> r = rgb [ : , : , <NUM_LIT:0> ] . astype ( np . float ) . ravel ( ) <EOL> g = rgb [ : , : , <NUM_LIT:1> ] . astype ( np . float ) . ravel ( ) <EOL> rnorm = r / np . maximum ( r . sum ( ) , <NUM_LIT:1> ) <EOL> gnorm = g / np . maximum ( g . sum ( ) , <NUM_LIT:1> ) <EOL> result = np . hstack ( [ np . dot ( r , self . rings ) , <EOL> np . dot ( g , self . rings ) , <EOL> np . dot ( r , self . rings ) - np . dot ( g , self . rings ) , <EOL> np . dot ( rnorm , self . rings ) , <EOL> np . dot ( gnorm , self . rings ) , <EOL> np . dot ( rnorm - gnorm , self . rings ) ] ) <EOL> return result . reshape ( <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> class DaisyExtractor ( Extractor ) : <EOL> def _extract_rgb ( self , rgb ) : <EOL> kwargs = dict ( step = rgb . shape [ <NUM_LIT:0> ] / <NUM_LIT:5> , radius = rgb . shape [ <NUM_LIT:0> ] / <NUM_LIT:10> , rings = <NUM_LIT:2> , <EOL> histograms = <NUM_LIT:6> , orientations = <NUM_LIT:8> ) <EOL> return np . hstack ( daisy ( rgb [ : , : , i ] , ** kwargs ) . ravel ( ) for i in [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> class MultiViewExtractor ( Extractor ) : <EOL> def __init__ ( self , orig ) : <EOL> self . orig = orig <EOL> def extract ( self , lon , l , b , r ) : <EOL> return np . hstack ( ( self . orig . extract ( lon , l , b , r ) , <EOL> self . orig . extract ( lon , l , b , r / <NUM_LIT:2> ) , <EOL> self . orig . extract ( lon , l , b , r * <NUM_LIT:2> ) , <EOL> self . orig . extract ( lon , l , b + r / <NUM_LIT:2> , r ) ) ) <EOL> class CompositeExtractor ( Extractor ) : <EOL> composite_classes = [ ] <EOL> def __init__ ( self ) : <EOL> super ( CompositeExtractor , self ) . __init__ ( ) <EOL> self . extractors = [ c ( ) for c in self . composite_classes ] <EOL> def _extract_rgb ( self , rgb ) : <EOL> return np . hstack ( e . _extract_rgb ( rgb ) . ravel ( ) for e in self . extractors ) <EOL> class RingWaveletCompositeExtractor ( CompositeExtractor ) : <EOL> composite_classes = [ RingExtractor , MultiWaveletExtractor ] <EOL> class RingWaveletCompressionExtractor ( CompositeExtractor ) : <EOL> composite_classes = [ RingExtractor , MultiWaveletExtractor , <EOL> CompressionExtractor ] <EOL> class RingWaveletCompressionStatExtractor ( CompositeExtractor ) : <EOL> composite_classes = [ RingExtractor , MultiWaveletExtractor , <EOL> CompressionExtractor , RawStatsExtractor ] <EOL> def extract ( self , lon , l , b , r ) : <EOL> return np . hstack ( e . extract ( lon , l , b , r ) . ravel ( ) <EOL> for e in self . extractors ) . reshape ( <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> class ManyManyExtractors ( CompositeExtractor ) : <EOL> composite_classes = [ RingExtractor , MultiWaveletExtractor , <EOL> CompressionExtractor , DaisyExtractor ] <EOL> def enhance_contrast ( rgb ) : <EOL> """<STR_LIT>""" <EOL> s = rgb . shape <EOL> d = disk ( s [ <NUM_LIT:0> ] / <NUM_LIT:5> ) <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> rgb [ : , : , i ] = percentile_autolevel ( rgb [ : , : , i ] , d , p0 = <NUM_LIT> , p1 = <NUM_LIT> ) <EOL> return rgb </s>
