<s> """<STR_LIT>""" <EOL> from functools import reduce <EOL> import numpy as np <EOL> import param <EOL> from . dimension import Dimension , Dimensioned , ViewableElement <EOL> from . ndmapping import UniformNdMapping <EOL> from . layout import Composable , Layout <EOL> from . util import sanitize_identifier , unique_array <EOL> class Overlayable ( object ) : <EOL> """<STR_LIT>""" <EOL> def __mul__ ( self , other ) : <EOL> if type ( other ) . __name__ == '<STR_LIT>' : <EOL> from . operation import DynamicFunction <EOL> def dynamic_mul ( element ) : <EOL> return self * element <EOL> return DynamicFunction ( other , function = dynamic_mul ) <EOL> if isinstance ( other , UniformNdMapping ) and not isinstance ( other , CompositeOverlay ) : <EOL> items = [ ( k , self * v ) for ( k , v ) in other . items ( ) ] <EOL> return other . clone ( items ) <EOL> self_item = [ ( ( self . group , self . label if self . label else '<STR_LIT:I>' ) , self ) ] <EOL> other_items = ( other . items ( ) if isinstance ( other , Overlay ) <EOL> else [ ( ( other . group , other . label if other . label else '<STR_LIT:I>' ) , other ) ] ) <EOL> return Overlay ( items = Overlay . relabel_item_paths ( list ( self_item ) + list ( other_items ) ) ) <EOL> class CompositeOverlay ( ViewableElement , Composable ) : <EOL> """<STR_LIT>""" <EOL> _deep_indexable = True <EOL> def hist ( self , index = <NUM_LIT:0> , adjoin = True , dimension = None , ** kwargs ) : <EOL> valid_ind = isinstance ( index , int ) and ( <NUM_LIT:0> <= index < len ( self ) ) <EOL> valid_label = index in [ el . label for el in self ] <EOL> if not any ( [ valid_ind , valid_label ] ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> hists = self . get ( index ) . hist ( adjoin = False , dimension = dimension , ** kwargs ) <EOL> if not isinstance ( hists , Layout ) : <EOL> hists = [ hists ] <EOL> if not isinstance ( dimension , list ) : <EOL> dimension = [ '<STR_LIT>' ] <EOL> if adjoin : <EOL> layout = self <EOL> for hist in hists : <EOL> layout = layout << hist <EOL> layout . main_layer = index <EOL> elif len ( dimension ) > <NUM_LIT:1> : <EOL> layout = hists <EOL> else : <EOL> layout = hists [ <NUM_LIT:0> ] <EOL> return layout <EOL> def dimension_values ( self , dimension , expanded = True , flat = True ) : <EOL> values = [ ] <EOL> found = False <EOL> for el in self : <EOL> if dimension in el . dimensions ( label = True ) : <EOL> values . append ( el . dimension_values ( dimension ) ) <EOL> found = True <EOL> if not found : <EOL> return super ( CompositeOverlay , self ) . dimension_values ( dimension , expanded , flat ) <EOL> values = [ v for v in values if v is not None and len ( v ) ] <EOL> if not values : <EOL> return np . array ( ) <EOL> vals = np . concatenate ( values ) <EOL> return vals if expanded else unique_array ( vals ) <EOL> class Overlay ( Layout , CompositeOverlay ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def _from_values ( cls , val ) : <EOL> return reduce ( lambda x , y : x * y , val ) . map ( lambda x : x . display ( '<STR_LIT>' ) , [ Overlay ] ) <EOL> def __init__ ( self , items = None , group = None , label = None , ** params ) : <EOL> view_params = ViewableElement . params ( ) . keys ( ) <EOL> self . __dict__ [ '<STR_LIT>' ] = False <EOL> self . __dict__ [ '<STR_LIT>' ] = group <EOL> self . __dict__ [ '<STR_LIT>' ] = label <EOL> Layout . __init__ ( self , items , <EOL> ** { k : v for k , v in params . items ( ) if k not in view_params } ) <EOL> ViewableElement . __init__ ( self , self . data , <EOL> ** { k : v for k , v in params . items ( ) if k in view_params } ) <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return Overlay ( [ ( k , v [ key ] ) for k , v in self . items ( ) ] ) <EOL> def get ( self , identifier , default = None ) : <EOL> if isinstance ( identifier , int ) : <EOL> values = list ( self . data . values ( ) ) <EOL> if <NUM_LIT:0> <= identifier < len ( values ) : <EOL> return values [ identifier ] <EOL> else : <EOL> return default <EOL> return super ( Overlay , self ) . get ( identifier , default ) <EOL> def __add__ ( self , other ) : <EOL> return Layout . from_values ( self ) + Layout . from_values ( other ) <EOL> def __mul__ ( self , other ) : <EOL> if isinstance ( other , Overlay ) : <EOL> items = list ( self . data . items ( ) ) + list ( other . data . items ( ) ) <EOL> elif isinstance ( other , ViewableElement ) : <EOL> label = other . label if other . label else '<STR_LIT:I>' <EOL> items = list ( self . data . items ( ) ) + [ ( ( other . group , label ) , other ) ] <EOL> elif isinstance ( other , UniformNdMapping ) : <EOL> raise NotImplementedError <EOL> return Overlay ( items = self . relabel_item_paths ( items ) ) . display ( '<STR_LIT:all>' ) <EOL> def collapse ( self , function ) : <EOL> """<STR_LIT>""" <EOL> elements = list ( self ) <EOL> types = [ type ( el ) for el in elements ] <EOL> values = [ el . group for el in elements ] <EOL> if not len ( set ( types ) ) == <NUM_LIT:1> and len ( set ( values ) ) == <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> return elements [ <NUM_LIT:0> ] . clone ( types [ <NUM_LIT:0> ] . collapse_data ( [ el . data for el in elements ] , <EOL> function , self . kdims ) ) <EOL> @ property <EOL> def group ( self ) : <EOL> if self . _group : <EOL> return self . _group <EOL> elements = [ el for el in self if not el . _auxiliary_component ] <EOL> values = { el . group for el in elements } <EOL> types = { type ( el ) for el in elements } <EOL> if values : <EOL> group = list ( values ) [ <NUM_LIT:0> ] <EOL> vtype = list ( types ) [ <NUM_LIT:0> ] . __name__ <EOL> else : <EOL> group , vtype = [ ] , '<STR_LIT>' <EOL> if len ( values ) == <NUM_LIT:1> and group != vtype : <EOL> return group <EOL> else : <EOL> return type ( self ) . __name__ <EOL> @ group . setter <EOL> def group ( self , group ) : <EOL> if not sanitize_identifier . allowable ( group ) : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> group ) <EOL> else : <EOL> self . _group = group <EOL> @ property <EOL> def label ( self ) : <EOL> if self . _label : <EOL> return self . _label <EOL> labels = { el . label for el in self <EOL> if not el . _auxiliary_component } <EOL> if len ( labels ) == <NUM_LIT:1> : <EOL> return list ( labels ) [ <NUM_LIT:0> ] <EOL> else : <EOL> return '<STR_LIT>' <EOL> @ label . setter <EOL> def label ( self , label ) : <EOL> if not sanitize_identifier . allowable ( label ) : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> label ) <EOL> self . _label = label <EOL> @ property <EOL> def ddims ( self ) : <EOL> dimensions = [ ] <EOL> dimension_names = [ ] <EOL> for el in self : <EOL> for dim in el . dimensions ( ) : <EOL> if dim . name not in dimension_names : <EOL> dimensions . append ( dim ) <EOL> dimension_names . append ( dim . name ) <EOL> return dimensions <EOL> @ property <EOL> def shape ( self ) : <EOL> raise NotImplementedError <EOL> class NdOverlay ( UniformNdMapping , CompositeOverlay , Overlayable ) : <EOL> """<STR_LIT>""" <EOL> kdims = param . List ( default = [ Dimension ( '<STR_LIT>' ) ] , constant = True , doc = """<STR_LIT>""" ) <EOL> _deep_indexable = True <EOL> def __init__ ( self , overlays = None , ** params ) : <EOL> super ( NdOverlay , self ) . __init__ ( overlays , ** params ) <EOL> __all__ = list ( set ( [ _k for _k , _v in locals ( ) . items ( ) <EOL> if isinstance ( _v , type ) and issubclass ( _v , Dimensioned ) ] ) ) + [ '<STR_LIT>' ] </s>
