<s> """<STR_LIT>""" <EOL> from functools import reduce <EOL> from itertools import chain <EOL> import numpy as np <EOL> import param <EOL> from . dimension import Dimension , Dimensioned , ViewableElement <EOL> from . ndmapping import OrderedDict , NdMapping , UniformNdMapping <EOL> from . tree import AttrTree <EOL> from . util import ( int_to_roman , sanitize_identifier , group_sanitizer , <EOL> label_sanitizer , unique_array ) <EOL> from . import traversal <EOL> class Composable ( object ) : <EOL> """<STR_LIT>""" <EOL> def __add__ ( self , obj ) : <EOL> return Layout . from_values ( self ) + Layout . from_values ( obj ) <EOL> def __lshift__ ( self , other ) : <EOL> if isinstance ( other , ( ViewableElement , NdMapping ) ) : <EOL> return AdjointLayout ( [ self , other ] ) <EOL> elif isinstance ( other , AdjointLayout ) : <EOL> return AdjointLayout ( other . data . values ( ) + [ self ] ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' . format ( type ( other ) . __name__ ) ) <EOL> class Empty ( Dimensioned , Composable ) : <EOL> """<STR_LIT>""" <EOL> group = param . String ( default = '<STR_LIT>' ) <EOL> def __init__ ( self ) : <EOL> super ( Empty , self ) . __init__ ( None ) <EOL> class AdjointLayout ( Dimensioned ) : <EOL> """<STR_LIT>""" <EOL> kdims = param . List ( default = [ Dimension ( '<STR_LIT>' ) ] , constant = True ) <EOL> layout_order = [ '<STR_LIT>' , '<STR_LIT:right>' , '<STR_LIT>' ] <EOL> _deep_indexable = True <EOL> _auxiliary_component = False <EOL> def __init__ ( self , data , ** params ) : <EOL> self . main_layer = <NUM_LIT:0> <EOL> if data and len ( data ) > <NUM_LIT:3> : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if data is not None and all ( isinstance ( v , tuple ) for v in data ) : <EOL> data = dict ( data ) <EOL> if isinstance ( data , dict ) : <EOL> wrong_pos = [ k for k in data if k not in self . layout_order ] <EOL> if wrong_pos : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> elif isinstance ( data , list ) : <EOL> data = dict ( zip ( self . layout_order , data ) ) <EOL> else : <EOL> data = OrderedDict ( ) <EOL> super ( AdjointLayout , self ) . __init__ ( data , ** params ) <EOL> @ property <EOL> def group ( self ) : <EOL> if self . main and self . main . group != type ( self . main ) . __name__ : <EOL> return self . main . group <EOL> else : <EOL> return '<STR_LIT>' <EOL> @ property <EOL> def label ( self ) : <EOL> return self . main . label if self . main else '<STR_LIT>' <EOL> @ group . setter <EOL> def group ( self , group ) : pass <EOL> @ label . setter <EOL> def label ( self , label ) : pass <EOL> def relabel ( self , label = None , group = None , depth = <NUM_LIT:1> ) : <EOL> return super ( AdjointLayout , self ) . relabel ( label = label , group = group , depth = depth ) <EOL> def get ( self , key , default = None ) : <EOL> return self . data [ key ] if key in self . data else default <EOL> def dimension_values ( self , dimension , expanded = True , flat = True ) : <EOL> dimension = self . get_dimension ( dimension , strict = True ) . name <EOL> return self . main . dimension_values ( dimension , expanded , flat ) <EOL> def __getitem__ ( self , key ) : <EOL> if key is ( ) : <EOL> return self <EOL> data_slice = None <EOL> if isinstance ( key , tuple ) : <EOL> data_slice = key [ <NUM_LIT:1> : ] <EOL> key = key [ <NUM_LIT:0> ] <EOL> if isinstance ( key , int ) and key <= len ( self ) : <EOL> if key == <NUM_LIT:0> : data = self . main <EOL> if key == <NUM_LIT:1> : data = self . right <EOL> if key == <NUM_LIT:2> : data = self . top <EOL> if data_slice : data = data [ data_slice ] <EOL> return data <EOL> elif isinstance ( key , str ) and key in self . data : <EOL> if data_slice is None : <EOL> return self . data [ key ] <EOL> else : <EOL> self . data [ key ] [ data_slice ] <EOL> elif isinstance ( key , slice ) and key . start is None and key . stop is None : <EOL> return self if data_slice is None else self . clone ( [ el [ data_slice ] <EOL> for el in self ] ) <EOL> else : <EOL> raise KeyError ( "<STR_LIT>" . format ( key ) ) <EOL> def __setitem__ ( self , key , value ) : <EOL> if key in [ '<STR_LIT>' , '<STR_LIT:right>' , '<STR_LIT>' ] : <EOL> if isinstance ( value , ( ViewableElement , UniformNdMapping ) ) : <EOL> self . data [ key ] = value <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' % key ) <EOL> def __lshift__ ( self , other ) : <EOL> views = [ self . data . get ( k , None ) for k in self . layout_order ] <EOL> return AdjointLayout ( [ v for v in views if v is not None ] + [ other ] ) <EOL> @ property <EOL> def ddims ( self ) : <EOL> return self . main . dimensions ( ) <EOL> @ property <EOL> def main ( self ) : <EOL> return self . data . get ( '<STR_LIT>' , None ) <EOL> @ property <EOL> def right ( self ) : <EOL> return self . data . get ( '<STR_LIT:right>' , None ) <EOL> @ property <EOL> def top ( self ) : <EOL> return self . data . get ( '<STR_LIT>' , None ) <EOL> @ property <EOL> def last ( self ) : <EOL> items = [ ( k , v . last ) if isinstance ( v , NdMapping ) else ( k , v ) <EOL> for k , v in self . data . items ( ) ] <EOL> return self . __class__ ( dict ( items ) ) <EOL> def keys ( self ) : <EOL> return list ( self . data . keys ( ) ) <EOL> def items ( self ) : <EOL> return list ( self . data . items ( ) ) <EOL> def __iter__ ( self ) : <EOL> i = <NUM_LIT:0> <EOL> while i < len ( self ) : <EOL> yield self [ i ] <EOL> i += <NUM_LIT:1> <EOL> def __add__ ( self , obj ) : <EOL> return Layout . from_values ( self ) + Layout . from_values ( obj ) <EOL> def __len__ ( self ) : <EOL> return len ( self . data ) <EOL> class NdLayout ( UniformNdMapping ) : <EOL> """<STR_LIT>""" <EOL> data_type = ( ViewableElement , AdjointLayout , UniformNdMapping ) <EOL> def __init__ ( self , initial_items = None , ** params ) : <EOL> self . _max_cols = <NUM_LIT:4> <EOL> self . _style = None <EOL> super ( NdLayout , self ) . __init__ ( initial_items = initial_items , ** params ) <EOL> @ property <EOL> def uniform ( self ) : <EOL> return traversal . uniform ( self ) <EOL> @ property <EOL> def shape ( self ) : <EOL> num = len ( self . keys ( ) ) <EOL> if num <= self . _max_cols : <EOL> return ( <NUM_LIT:1> , num ) <EOL> nrows = num // self . _max_cols <EOL> last_row_cols = num % self . _max_cols <EOL> return nrows + ( <NUM_LIT:1> if last_row_cols else <NUM_LIT:0> ) , min ( num , self . _max_cols ) <EOL> def grid_items ( self ) : <EOL> """<STR_LIT>""" <EOL> if list ( self . keys ( ) ) == [ ] : return { } <EOL> cols = self . _max_cols <EOL> return { ( idx // cols , idx % cols ) : ( key , item ) <EOL> for idx , ( key , item ) in enumerate ( self . data . items ( ) ) } <EOL> def cols ( self , n ) : <EOL> self . _max_cols = n <EOL> return self <EOL> def __add__ ( self , obj ) : <EOL> return Layout . from_values ( self ) + Layout . from_values ( obj ) <EOL> @ property <EOL> def last ( self ) : <EOL> """<STR_LIT>""" <EOL> last_items = [ ] <EOL> for ( k , v ) in self . items ( ) : <EOL> if isinstance ( v , NdMapping ) : <EOL> item = ( k , v . clone ( ( v . last_key , v . last ) ) ) <EOL> elif isinstance ( v , AdjointLayout ) : <EOL> item = ( k , v . last ) <EOL> else : <EOL> item = ( k , v ) <EOL> last_items . append ( item ) <EOL> return self . clone ( last_items ) <EOL> class Warning ( param . Parameterized ) : pass <EOL> collate_deprecation = Warning ( name = '<STR_LIT>' ) <EOL> class Layout ( AttrTree , Dimensioned ) : <EOL> """<STR_LIT>""" <EOL> group = param . String ( default = '<STR_LIT>' , constant = True ) <EOL> _deep_indexable = True <EOL> @ classmethod <EOL> def collate ( cls , data , kdims = None , key_dimensions = None ) : <EOL> kdims = key_dimensions if ( kdims is None ) else kdims <EOL> if kdims is None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> collate_deprecation . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> from . element import Collator <EOL> layouts = { k : ( v if isinstance ( v , Layout ) else Layout . from_values ( [ v ] ) ) <EOL> for k , v in data . items ( ) } <EOL> return Collator ( layouts , kdims = kdims ) ( ) <EOL> @ classmethod <EOL> def new_path ( cls , path , item , paths , count ) : <EOL> sanitizers = [ sanitize_identifier , group_sanitizer , label_sanitizer ] <EOL> path = tuple ( fn ( p ) for ( p , fn ) in zip ( path , sanitizers ) ) <EOL> while any ( path [ : i ] in paths or path in [ p [ : i ] for p in paths ] <EOL> for i in range ( <NUM_LIT:1> , len ( path ) + <NUM_LIT:1> ) ) : <EOL> path = path [ : <NUM_LIT:2> ] <EOL> pl = len ( path ) <EOL> if ( pl == <NUM_LIT:1> and not item . label ) or ( pl == <NUM_LIT:2> and item . label ) : <EOL> new_path = path + ( int_to_roman ( count - <NUM_LIT:1> ) , ) <EOL> if path in paths : <EOL> paths [ paths . index ( path ) ] = new_path <EOL> path = path + ( int_to_roman ( count ) , ) <EOL> else : <EOL> path = path [ : - <NUM_LIT:1> ] + ( int_to_roman ( count ) , ) <EOL> count += <NUM_LIT:1> <EOL> return path , count <EOL> @ classmethod <EOL> def relabel_item_paths ( cls , items ) : <EOL> """<STR_LIT>""" <EOL> paths , path_items = [ ] , [ ] <EOL> count = <NUM_LIT:2> <EOL> for path , item in items : <EOL> new_path , count = cls . new_path ( path , item , paths , count ) <EOL> new_path = tuple ( '<STR_LIT>' . join ( ( p [ <NUM_LIT:0> ] . upper ( ) , p [ <NUM_LIT:1> : ] ) ) for p in new_path ) <EOL> path_items . append ( item ) <EOL> paths . append ( new_path ) <EOL> return list ( zip ( paths , path_items ) ) <EOL> @ classmethod <EOL> def _from_values ( cls , val ) : <EOL> return reduce ( lambda x , y : x + y , val ) . display ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def from_values ( cls , val ) : <EOL> """<STR_LIT>""" <EOL> collection = isinstance ( val , ( list , tuple ) ) <EOL> if type ( val ) is cls : <EOL> return val <EOL> elif collection and len ( val ) > <NUM_LIT:1> : <EOL> return cls . _from_values ( val ) <EOL> elif collection : <EOL> val = val [ <NUM_LIT:0> ] <EOL> group = group_sanitizer ( val . group ) <EOL> group = '<STR_LIT>' . join ( [ group [ <NUM_LIT:0> ] . upper ( ) , group [ <NUM_LIT:1> : ] ] ) <EOL> label = label_sanitizer ( val . label if val . label else '<STR_LIT:I>' ) <EOL> label = '<STR_LIT>' . join ( [ label [ <NUM_LIT:0> ] . upper ( ) , label [ <NUM_LIT:1> : ] ] ) <EOL> return cls ( items = [ ( ( group , label ) , val ) ] ) <EOL> def __init__ ( self , items = None , identifier = None , parent = None , ** kwargs ) : <EOL> self . __dict__ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . __dict__ [ '<STR_LIT>' ] = <NUM_LIT:4> <EOL> if items and all ( isinstance ( item , Dimensioned ) for item in items ) : <EOL> items = self . from_values ( items ) . data <EOL> params = { p : kwargs . pop ( p ) for p in list ( self . params ( ) . keys ( ) ) + [ '<STR_LIT:id>' ] if p in kwargs } <EOL> AttrTree . __init__ ( self , items , identifier , parent , ** kwargs ) <EOL> Dimensioned . __init__ ( self , self . data , ** params ) <EOL> @ property <EOL> def uniform ( self ) : <EOL> return traversal . uniform ( self ) <EOL> @ property <EOL> def shape ( self ) : <EOL> num = len ( self ) <EOL> if num <= self . _max_cols : <EOL> return ( <NUM_LIT:1> , num ) <EOL> nrows = num // self . _max_cols <EOL> last_row_cols = num % self . _max_cols <EOL> return nrows + ( <NUM_LIT:1> if last_row_cols else <NUM_LIT:0> ) , min ( num , self . _max_cols ) <EOL> def relabel ( self , label = None , group = None , depth = <NUM_LIT:0> ) : <EOL> relabelled = super ( Layout , self ) . relabel ( label = label , group = group , depth = depth ) <EOL> relabelled . __dict__ [ '<STR_LIT>' ] = self . __dict__ [ '<STR_LIT>' ] <EOL> relabelled . __dict__ [ '<STR_LIT>' ] = self . __dict__ [ '<STR_LIT>' ] <EOL> return relabelled <EOL> def clone ( self , * args , ** overrides ) : <EOL> """<STR_LIT>""" <EOL> clone = super ( Layout , self ) . clone ( * args , ** overrides ) <EOL> clone . _display = self . _display <EOL> clone . _cols = self . _cols <EOL> clone . id = self . id <EOL> return clone <EOL> def dimension_values ( self , dimension , expanded = True , flat = True ) : <EOL> "<STR_LIT>" <EOL> dimension = self . get_dimension ( dimension , strict = True ) . name <EOL> all_dims = self . traverse ( lambda x : [ d . name for d in x . dimensions ( ) ] ) <EOL> if dimension in chain . from_iterable ( all_dims ) : <EOL> values = [ el . dimension_values ( dimension ) for el in self <EOL> if dimension in el . dimensions ( label = True ) ] <EOL> vals = np . concatenate ( values ) <EOL> return vals if expanded else unique_array ( vals ) <EOL> else : <EOL> return super ( Layout , self ) . dimension_values ( dimension , <EOL> expanded , flat ) <EOL> def cols ( self , ncols ) : <EOL> self . _max_cols = ncols <EOL> return self <EOL> def display ( self , option ) : <EOL> "<STR_LIT>" <EOL> options = [ '<STR_LIT>' , '<STR_LIT:all>' ] <EOL> if option not in options : <EOL> raise Exception ( "<STR_LIT>" % <EOL> '<STR_LIT:U+002C>' . join ( repr ( el ) for el in options ) ) <EOL> self . _display = option <EOL> return self <EOL> def select ( self , selection_specs = None , ** selections ) : <EOL> return super ( Layout , self ) . select ( selection_specs , ** selections ) . display ( self . _display ) <EOL> def grid_items ( self ) : <EOL> return { tuple ( np . unravel_index ( idx , self . shape ) ) : ( path , item ) <EOL> for idx , ( path , item ) in enumerate ( self . items ( ) ) } <EOL> def regroup ( self , group ) : <EOL> """<STR_LIT>""" <EOL> new_items = [ el . relabel ( group = group ) for el in self . data . values ( ) ] <EOL> return reduce ( lambda x , y : x + y , new_items ) <EOL> def __getitem__ ( self , key ) : <EOL> if isinstance ( key , int ) : <EOL> if key < len ( self ) : <EOL> return self . data . values ( ) [ key ] <EOL> raise KeyError ( "<STR_LIT>" ) <EOL> if len ( key ) == <NUM_LIT:2> and not any ( [ isinstance ( k , str ) for k in key ] ) : <EOL> if key == ( slice ( None ) , slice ( None ) ) : return self <EOL> row , col = key <EOL> idx = row * self . _max_cols + col <EOL> keys = list ( self . data . keys ( ) ) <EOL> if idx >= len ( keys ) or col >= self . _max_cols : <EOL> raise KeyError ( '<STR_LIT>' % str ( key ) ) <EOL> key = keys [ idx ] <EOL> return super ( Layout , self ) . __getitem__ ( key ) <EOL> def __len__ ( self ) : <EOL> return len ( self . data ) <EOL> def __add__ ( self , other ) : <EOL> other = self . from_values ( other ) <EOL> items = list ( self . data . items ( ) ) + list ( other . data . items ( ) ) <EOL> return Layout ( items = self . relabel_item_paths ( items ) ) . display ( '<STR_LIT:all>' ) <EOL> __all__ = list ( set ( [ _k for _k , _v in locals ( ) . items ( ) <EOL> if isinstance ( _v , type ) and ( issubclass ( _v , Dimensioned ) <EOL> or issubclass ( _v , Layout ) ) ] ) ) </s>
