@@ -666,7 +666,7 @@ def make_mapping_array(N, data, gamma=1.0, inverse=False):
666666 return lut
667667
668668
669- class _Colormap ():
669+ class _Colormap (object ):
670670 """Mixin class used to add some helper methods."""
671671 def _get_data (self , ext ):
672672 """
@@ -1191,7 +1191,7 @@ def updated(
11911191 return cmap
11921192
11931193 @staticmethod
1194- def from_file (path ):
1194+ def from_file (path , warn_on_failure = False ):
11951195 """
11961196 Load colormap from a file.
11971197 Valid file extensions are described in the below table.
@@ -1210,8 +1210,11 @@ def from_file(path):
12101210 ----------
12111211 path : str
12121212 The file path.
1213+ warn_on_failure : bool, optional
1214+ If ``True``, issue a warning when loading fails rather than
1215+ raising an error.
12131216 """ # noqa
1214- return _from_file (path , listed = False )
1217+ return _from_file (path , listed = False , warn_on_failure = warn_on_failure )
12151218
12161219 @staticmethod
12171220 def from_list (name , colors , ratios = None , ** kwargs ):
@@ -1434,7 +1437,7 @@ def updated(self, colors=None, name=None, N=None, *, alpha=None):
14341437 return cmap
14351438
14361439 @staticmethod
1437- def from_file (path ):
1440+ def from_file (path , warn_on_failure = False ):
14381441 """
14391442 Load color cycle from a file.
14401443 Valid file extensions are described in the below table.
@@ -1453,8 +1456,11 @@ def from_file(path):
14531456 ----------
14541457 path : str
14551458 The file path.
1459+ warn_on_failure : bool, optional
1460+ If ``True``, issue a warning when loading fails rather than
1461+ raising an error.
14561462 """ # noqa
1457- return _from_file (path , listed = True )
1463+ return _from_file (path , listed = True , warn_on_failure = warn_on_failure )
14581464
14591465
14601466class PerceptuallyUniformColormap (LinearSegmentedColormap , _Colormap ):
@@ -2792,12 +2798,20 @@ def _get_data_paths(dirname):
27922798 ]
27932799
27942800
2795- def _from_file (filename , listed = False ):
2801+ def _from_file (filename , listed = False , warn_on_failure = False ):
27962802 """Read generalized colormap and color cycle files."""
27972803 filename = os .path .expanduser (filename )
27982804 if os .path .isdir (filename ): # no warning
27992805 return
28002806
2807+ # Warn if loading failed during `register_cmaps` or `register_cycles`
2808+ # but raise error if user tries to load a file.
2809+ def _warn_or_raise (msg ):
2810+ if warn_on_failure :
2811+ _warn_proplot (msg )
2812+ else :
2813+ raise RuntimeError (msg )
2814+
28012815 # Directly read segmentdata json file
28022816 # NOTE: This is special case! Immediately return name and cmap
28032817 N = rcParams ['image.lut' ]
@@ -2828,19 +2842,19 @@ def _from_file(filename, listed=False):
28282842 try :
28292843 data = [[float (num ) for num in line ] for line in data ]
28302844 except ValueError :
2831- _warn_proplot (
2845+ _warn_or_raise (
28322846 f'Failed to load { filename !r} . Expected a table of comma '
28332847 'or space-separated values.'
28342848 )
2835- return None , None
2849+ return
28362850 # Build x-coordinates and standardize shape
28372851 data = np .array (data )
28382852 if data .shape [1 ] != len (ext ):
2839- _warn_proplot (
2853+ _warn_or_raise (
28402854 f'Failed to load { filename !r} . Got { data .shape [1 ]} columns, '
28412855 f'but expected { len (ext )} .'
28422856 )
2843- return None , None
2857+ return
28442858 if ext [0 ] != 'x' : # i.e. no x-coordinates specified explicitly
28452859 x = np .linspace (0 , 1 , data .shape [0 ])
28462860 else :
@@ -2853,13 +2867,15 @@ def _from_file(filename, listed=False):
28532867 try :
28542868 doc = ElementTree .parse (filename )
28552869 except IOError :
2856- _warn_proplot (f'Failed to load { filename !r} .' )
2870+ _warn_or_raise (
2871+ f'Failed to load { filename !r} .'
2872+ )
28572873 return
28582874 x , data = [], []
28592875 for s in doc .getroot ().findall ('.//Point' ):
28602876 # Verify keys
28612877 if any (key not in s .attrib for key in 'xrgb' ):
2862- _warn_proplot (
2878+ _warn_or_raise (
28632879 f'Failed to load { filename !r} . Missing an x, r, g, or b '
28642880 'specification inside one or more <Point> tags.'
28652881 )
@@ -2875,7 +2891,7 @@ def _from_file(filename, listed=False):
28752891 # Convert to array
28762892 if not all (len (data [0 ]) == len (color )
28772893 and len (color ) in (3 , 4 ) for color in data ):
2878- _warn_proplot (
2894+ _warn_or_raise (
28792895 f'Failed to load { filename !r} . Unexpected number of channels '
28802896 'or mixed channels across <Point> tags.'
28812897 )
@@ -2887,15 +2903,15 @@ def _from_file(filename, listed=False):
28872903 string = open (filename ).read () # into single string
28882904 data = re .findall ('#[0-9a-fA-F]{6}' , string ) # list of strings
28892905 if len (data ) < 2 :
2890- _warn_proplot (
2906+ _warn_or_raise (
28912907 f'Failed to load { filename !r} . Hex strings not found.'
28922908 )
28932909 return
28942910 # Convert to array
28952911 x = np .linspace (0 , 1 , len (data ))
28962912 data = [to_rgb (color ) for color in data ]
28972913 else :
2898- _warn_proplot (
2914+ _warn_or_raise (
28992915 f'Colormap or cycle file { filename !r} has unknown extension.'
29002916 )
29012917 return
@@ -2936,7 +2952,9 @@ def register_cmaps():
29362952 """
29372953 for i , path in enumerate (_get_data_paths ('cmaps' )):
29382954 for filename in sorted (glob .glob (os .path .join (path , '*' ))):
2939- cmap = LinearSegmentedColormap .from_file (filename )
2955+ cmap = LinearSegmentedColormap .from_file (
2956+ filename , warn_on_failure = True
2957+ )
29402958 if not cmap :
29412959 continue
29422960 if i == 0 and cmap .name .lower () in ('phase' , 'graycycle' ):
@@ -2958,7 +2976,9 @@ def register_cycles():
29582976 """
29592977 for path in _get_data_paths ('cycles' ):
29602978 for filename in sorted (glob .glob (os .path .join (path , '*' ))):
2961- cmap = ListedColormap .from_file (filename )
2979+ cmap = ListedColormap .from_file (
2980+ filename , warn_on_failure = True
2981+ )
29622982 if not cmap :
29632983 continue
29642984 if isinstance (cmap , LinearSegmentedColormap ):
0 commit comments