<s> from __future__ import print_function <EOL> import os . path <EOL> import json <EOL> import webbrowser <EOL> from jcvi . apps . base import datadir as _DATADIR <EOL> try : <EOL> from matplotlib . colors import LinearSegmentedColormap <EOL> except ImportError : <EOL> HAVE_MPL = False <EOL> else : <EOL> HAVE_MPL = True <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> _DATAFILE = os . path . join ( _DATADIR , '<STR_LIT>' ) <EOL> with open ( _DATAFILE , '<STR_LIT:r>' ) as f : <EOL> COLOR_MAPS = json . load ( f ) <EOL> MAP_TYPES = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def print_maps ( map_type = None , number = None ) : <EOL> """<STR_LIT>""" <EOL> if not map_type and not number : <EOL> print_all_maps ( ) <EOL> elif map_type : <EOL> print_maps_by_type ( map_type , number ) <EOL> else : <EOL> s = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> raise ValueError ( s ) <EOL> def print_all_maps ( ) : <EOL> """<STR_LIT>""" <EOL> for t in MAP_TYPES : <EOL> print_maps_by_type ( t ) <EOL> def print_maps_by_type ( map_type , number = None ) : <EOL> """<STR_LIT>""" <EOL> map_type = map_type . lower ( ) . capitalize ( ) <EOL> if map_type not in MAP_TYPES : <EOL> s = '<STR_LIT>' . format ( MAP_TYPES ) <EOL> raise ValueError ( s ) <EOL> print ( map_type ) <EOL> map_keys = sorted ( COLOR_MAPS [ map_type ] . keys ( ) ) <EOL> format_str = '<STR_LIT>' <EOL> for mk in map_keys : <EOL> num_keys = sorted ( COLOR_MAPS [ map_type ] [ mk ] . keys ( ) , key = int ) <EOL> if not number or str ( number ) in num_keys : <EOL> num_str = '<STR_LIT:{>' + '<STR_LIT:U+002CU+0020>' . join ( num_keys ) + '<STR_LIT:}>' <EOL> print ( format_str . format ( mk , num_str ) ) <EOL> class _ColorMap ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , map_type , colors ) : <EOL> self . name = name <EOL> self . type = map_type <EOL> self . number = len ( colors ) <EOL> self . colors = colors <EOL> @ property <EOL> def hex_colors ( self ) : <EOL> """<STR_LIT>""" <EOL> hc = [ ] <EOL> for color in self . colors : <EOL> h = '<STR_LIT:#>' + '<STR_LIT>' . join ( '<STR_LIT>' . format ( hex ( c ) [ <NUM_LIT:2> : ] . upper ( ) ) <EOL> for c in color ) <EOL> hc . append ( h ) <EOL> return hc <EOL> @ property <EOL> def mpl_colors ( self ) : <EOL> """<STR_LIT>""" <EOL> mc = [ ] <EOL> for color in self . colors : <EOL> mc . append ( tuple ( [ x / <NUM_LIT> for x in color ] ) ) <EOL> return mc <EOL> @ property <EOL> def mpl_colormap ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get_mpl_colormap ( ) <EOL> def get_mpl_colormap ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not HAVE_MPL : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> cmap = LinearSegmentedColormap . from_list ( self . name , <EOL> self . mpl_colors , ** kwargs ) <EOL> return cmap <EOL> def show_as_blocks ( self , block_size = <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> from ipythonblocks import BlockGrid <EOL> grid = BlockGrid ( self . number , <NUM_LIT:1> , block_size = block_size ) <EOL> for block , color in zip ( grid , self . colors ) : <EOL> block . rgb = color <EOL> grid . show ( ) <EOL> class BrewerMap ( _ColorMap ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def colorbrewer2_url ( self ) : <EOL> """<STR_LIT>""" <EOL> url = '<STR_LIT>' <EOL> return url . format ( self . type . lower ( ) , self . name , self . number ) <EOL> def colorbrewer2 ( self ) : <EOL> """<STR_LIT>""" <EOL> webbrowser . open_new_tab ( self . colorbrewer2_url ) <EOL> def get_map ( name , map_type , number , reverse = False ) : <EOL> """<STR_LIT>""" <EOL> number = str ( number ) <EOL> map_type = map_type . lower ( ) . capitalize ( ) <EOL> if map_type not in MAP_TYPES : <EOL> s = '<STR_LIT>' . format ( MAP_TYPES ) <EOL> raise ValueError ( s ) <EOL> map_names = dict ( ( k . lower ( ) , k ) for k in COLOR_MAPS [ map_type ] . keys ( ) ) <EOL> if name . lower ( ) not in map_names : <EOL> s = '<STR_LIT>' <EOL> s = s . format ( name , map_type ) <EOL> valid_names = [ str ( k ) for k in COLOR_MAPS [ map_type ] . keys ( ) ] <EOL> valid_names . sort ( ) <EOL> s += '<STR_LIT>' . format ( valid_names ) <EOL> raise ValueError ( s ) <EOL> name = map_names [ name . lower ( ) ] <EOL> if number not in COLOR_MAPS [ map_type ] [ name ] : <EOL> s = '<STR_LIT>' <EOL> s = s . format ( map_type , str ( name ) ) <EOL> valid_numbers = [ int ( k ) for k in COLOR_MAPS [ map_type ] [ name ] . keys ( ) ] <EOL> valid_numbers . sort ( ) <EOL> s += '<STR_LIT>' . format ( valid_numbers ) <EOL> raise ValueError ( s ) <EOL> colors = COLOR_MAPS [ map_type ] [ name ] [ number ] [ '<STR_LIT>' ] <EOL> if reverse : <EOL> name += '<STR_LIT>' <EOL> colors = [ x for x in reversed ( colors ) ] <EOL> return BrewerMap ( name , map_type , colors ) <EOL> def _load_maps_by_type ( map_type ) : <EOL> """<STR_LIT>""" <EOL> seq_maps = COLOR_MAPS [ map_type ] <EOL> loaded_maps = { } <EOL> for map_name in seq_maps : <EOL> loaded_maps [ map_name ] = { } <EOL> for num in seq_maps [ map_name ] : <EOL> inum = int ( num ) <EOL> colors = seq_maps [ map_name ] [ num ] [ '<STR_LIT>' ] <EOL> bmap = BrewerMap ( map_name , map_type , colors ) <EOL> loaded_maps [ map_name ] [ inum ] = bmap <EOL> max_num = int ( max ( seq_maps [ map_name ] . keys ( ) , key = int ) ) <EOL> loaded_maps [ map_name ] [ '<STR_LIT>' ] = loaded_maps [ map_name ] [ max_num ] <EOL> return loaded_maps </s>
