Skip to content

Commit

Permalink
TST: fix utility tests
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Dec 16, 2023
1 parent 89aeab5 commit 1c1956a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
22 changes: 12 additions & 10 deletions cmasher/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,19 @@ def test_standalone_rainforest(self):
cmap_old = mplcm.get_cmap('cmr.rainforest')

# Create standalone module for rainforest
cmap_path = create_cmap_mod('rainforest')
cmap_path = create_cmap_mod('rainforest', _copy_name='rainforest_copy')

# Try to import this module
spec = spec_from_file_location('rainforest', cmap_path)
spec = spec_from_file_location('rainforest_copy', cmap_path)
mod = module_from_spec(spec)
spec.loader.exec_module(mod)

# Check if the colormap in MPL has been updated
cmap_new = mplcm.get_cmap('cmr.rainforest')
assert cmap_new is mod.cmap
assert cmap_old is not cmap_new
cmap_new = mplcm.get_cmap('cmr.rainforest_copy')

# identity equality isn't achievable since mplcm.get_cmap may return a copy
assert cmap_new == mod.cmap
assert cmap_old == cmap_new

# Check if the values in both colormaps are the same
assert np.allclose(cmap_old.colors, cmap_new.colors)
Expand All @@ -79,17 +81,17 @@ def test_standalone_infinity(self):
cmap_old = mplcm.get_cmap('cmr.infinity')

# Create standalone module for infinity
cmap_path = create_cmap_mod('infinity')
cmap_path = create_cmap_mod('infinity', _copy_name='inifinity_copy')

# Try to import this module
spec = spec_from_file_location('infinity', cmap_path)
spec = spec_from_file_location('infinity_copy', cmap_path)
mod = module_from_spec(spec)
spec.loader.exec_module(mod)

# Check if the colormap in MPL has been updated
cmap_new = mplcm.get_cmap('cmr.infinity')
assert cmap_new is mod.cmap
assert cmap_old is not cmap_new
assert cmap_new == mod.cmap
assert cmap_old == cmap_new

# Check if the values in both colormaps are the same
assert np.allclose(cmap_old.colors, cmap_new.colors)
Expand Down Expand Up @@ -230,7 +232,7 @@ def test_CMasher_cmaps(self, cm_name):

# Test if providing a cmap .txt-file works
def test_cmap_file_txt(self):
import_cmaps(path.join(dirpath, '../colormaps/cm_rainforest.txt'))
import_cmaps(path.join(dirpath, '../colormaps/cm_rainforest.txt'), _skip_registration=True)

# Test if providing a cmap .txt-file with 8-bit values works
def test_cmap_file_8bit(self):
Expand Down
18 changes: 10 additions & 8 deletions cmasher/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def _get_cmap_perceptual_rank(cmap: CMAP) -> Tuple[

# %% FUNCTIONS
# This function creates a standalone module of a CMasher colormap
def create_cmap_mod(cmap: str, *, save_dir: str = '.') -> str:
def create_cmap_mod(cmap: str, *, save_dir: str = '.', _copy_name: Optional[str] = None) -> str:
"""
Creates a standalone Python module of the provided *CMasher* `cmap` and
saves it in the given `save_dir` as '<`cmap`>.py'.
Expand Down Expand Up @@ -348,11 +348,11 @@ def create_cmap_mod(cmap: str, *, save_dir: str = '.') -> str:
""")

# Format py-file string
cm_py_file = cm_py_file.format(cm_type, array_str, name, len(rgb),
cm_py_file = cm_py_file.format(cm_type, array_str, _copy_name or name, len(rgb),
len(rgb)//2)

# Obtain the path to the module
cmap_path = path.join(save_dir, f"{name}.py")
cmap_path = path.join(save_dir, f"{_copy_name or name}.py")

# Create Python module
with open(cmap_path, 'w') as f:
Expand Down Expand Up @@ -988,7 +988,7 @@ def get_sub_cmap(


# Function to import all custom colormaps in a file or directory
def import_cmaps(cmap_path: str) -> None:
def import_cmaps(cmap_path: str, *, _skip_registration: bool=False) -> None:
"""
Reads in custom colormaps from a provided file or directory `cmap_path`;
transforms them into :obj:`~matplotlib.colors.ListedColormap` objects; and
Expand Down Expand Up @@ -1098,8 +1098,9 @@ def import_cmaps(cmap_path: str) -> None:
rgb = np.genfromtxt(cm_file_path, dtype=None, comments='//',
encoding=None)

# Register colormap
register_cmap(cm_name, rgb)
if not _skip_registration:
# Register colormap
register_cmap(cm_name, rgb)

# Check if provided cmap is a cyclic colormap
# If so, obtain its shifted (reversed) versions as well
Expand All @@ -1110,8 +1111,9 @@ def import_cmaps(cmap_path: str) -> None:
# Shift the entire colormap by this index
rgb_s = np.r_[rgb[idx:], rgb[:idx]]

# Register this colormap as well
register_cmap(cm_name+'_s', rgb_s)
if not _skip_registration:
# Register this colormap as well
register_cmap(cm_name+'_s', rgb_s)

# If any error is raised, reraise it
except Exception as error:
Expand Down

0 comments on commit 1c1956a

Please sign in to comment.