[GRASS-SVN] r68724 - grass-addons/grass7/raster/r.colors.matplotlib

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jun 21 20:38:15 PDT 2016


Author: wenzeslaus
Date: 2016-06-21 20:38:15 -0700 (Tue, 21 Jun 2016)
New Revision: 68724

Added:
   grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_viscm.png
   grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_viscm_edit.png
   grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_viscm_properties.png
Modified:
   grass-addons/grass7/raster/r.colors.matplotlib/r.colors.matplotlib.html
   grass-addons/grass7/raster/r.colors.matplotlib/r.colors.matplotlib.py
Log:
r.colors.matplotlib: consume files created in the viscm tool

Modified: grass-addons/grass7/raster/r.colors.matplotlib/r.colors.matplotlib.html
===================================================================
--- grass-addons/grass7/raster/r.colors.matplotlib/r.colors.matplotlib.html	2016-06-22 02:29:52 UTC (rev 68723)
+++ grass-addons/grass7/raster/r.colors.matplotlib/r.colors.matplotlib.html	2016-06-22 03:38:15 UTC (rev 68724)
@@ -41,7 +41,7 @@
 that the perceptually uniform sequential color tables, namely
 <em>viridis</em>, <em>inferno</em>, <em>plasma</em>, and <em>magma</em>,
 are available in Matplotlib 1.5 and above.
-Color tables are called <em>color maps</em> in Matplotlib
+Color tables are called <em>color maps</em> (or colormaps) in Matplotlib
 and the best overview of available color maps in the
 <a href="http://matplotlib.org/examples/color/colormaps_reference.html">colormaps_reference</a>
 example in Matplotlib documentation.
@@ -114,7 +114,7 @@
 <img src="r_colors_matplotlib_terrain.png" alt="Matplotlib terrain">
 <p><em>
 summer, winter, autumn, cubehelix, and terrain color tables applied
-to elevation raster from North Carolina sample dataset. winter and
+to elevation raster from the North Carolina sample dataset. winter and
 cubehelix are set to be discrete instead of continuous.
 </em>
 </center>
@@ -135,7 +135,56 @@
 
 Color table for 3D raster map can be set in the same way.
 
+<h3>Using color tables generated by the viscm tool</h3>
 
+A <a href="https://pypi.python.org/pypi/viscm/">viscm</a> tool is a
+little tool for analyzing color tables and creating new color tables
+(color maps) for Matplotlib. The tool was used to create perceptually
+uniform color tables for Matplotlib (for example viridis). The new
+color table is stored into a file. In version 0.7, a temporary file
+named <tt>/tmp/new_cm.py</tt> which is a Python source code which
+creates a <em>Colormap</em> object. If this module gets a name of
+existing file instead of a color table name, it assumes that it this
+kind of file and reads object called <tt>test_cm</tt> as Matplotlib
+color table. The possible workflow follows. (Note that you need to
+install the viscm tool, e.g. using <tt>sudo pip install viscm</tt> on
+Linux.)
+
+<p>
+Start the tool, create and save a color table:
+
+<div class="code"><pre>
+python -m viscm edit
+</pre></div>
+
+Now store the color table in GRASS GIS format:
+
+<div class="code"><pre>
+r.colors.matplotlib color=/tmp/new_cm.py rules=from_viscm.txt
+</pre></div>
+
+<center>
+<img src="r_colors_matplotlib_viscm_edit.png" alt="Editing color table in viscm">
+<img src="r_colors_matplotlib_viscm_properties.png" alt="Reviewing color table properties in viscm">
+<p><em>
+Editing color table in viscm (right): the yellow dot on the blue spline must
+stay in the colored area as the red line moves. Reviewing color table
+properties is done using several displays including color blindness
+simulations.
+</em>
+</center>
+<center>
+<img src="r_colors_matplotlib_viscm.png" alt="Applied color table form viscm">
+<p><em>
+A color table from viscm applied to elevation raster
+from the North Carolina sample dataset.
+</em>
+</center>
+
+The same works for any Python files which follow the same schema,
+so it works for example with files from the
+<a href="https://github.com/BIDS/colormap">BIDS/colormap</a> repository.
+
 <h2>SEE ALSO</h2>
 
 <em>

Modified: grass-addons/grass7/raster/r.colors.matplotlib/r.colors.matplotlib.py
===================================================================
--- grass-addons/grass7/raster/r.colors.matplotlib/r.colors.matplotlib.py	2016-06-22 02:29:52 UTC (rev 68723)
+++ grass-addons/grass7/raster/r.colors.matplotlib/r.colors.matplotlib.py	2016-06-22 03:38:15 UTC (rev 68724)
@@ -33,7 +33,7 @@
 #% key: color
 #% type: string
 #% label: Name of color table
-#% description: Available color tables depend on the Matplotlib version
+#% description: Available color tables depend on the Matplotlib version. Alternatively this can be file name of a file generated by Python viscm tool
 #% required: no
 #% guisection: Basic
 #%end
@@ -141,17 +141,32 @@
     if flags['n']:
         name += '_r'
 
-    # not sure if datad is part of the API but it is in one example
-    # datad might be potentially better way of getting the table
-    # it contains the raw data, but on the other hand it might not be
-    # clear if you can interpolate linearly in between (but likely yes)
-    if hasattr(cm, 'datad') and name not in cm.datad.keys():
-        import matplotlib as mpl
-        gscript.fatal(_("Matplotlib {v} does not contain color table"
-                        " <{n}>").format(v=mpl.__version__, n=name))
 
-    cmap = cm.get_cmap(name, lut=n_colors)
+    if os.path.isfile(name):
+        ns = {'__name__': '',
+              '__file__': os.path.basename(name),
+        }
 
+        with open(name) as f:
+            code = compile(f.read(),
+                           os.path.basename(name),
+                           'exec')
+            exec(code, globals(), ns)
+        cmap = ns.get("test_cm", None)
+        # we ignore user input since we need to use whatever the
+        # color map object is defined with
+        n_colors = cmap.N
+    else:
+        # not sure if datad is part of the API but it is in one example
+        # datad might be potentially better way of getting the table
+        # it contains the raw data, but on the other hand it might not be
+        # clear if you can interpolate linearly in between (but likely yes)
+        if hasattr(cm, 'datad') and name not in cm.datad.keys():
+            import matplotlib as mpl
+            gscript.fatal(_("Matplotlib {v} does not contain color table"
+                            " <{n}>").format(v=mpl.__version__, n=name))
+        cmap = cm.get_cmap(name, lut=n_colors)
+
     comments = []
     comments.append(
         "Generated from Matplotlib color table <{}>".format(name))

Added: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_viscm.png
===================================================================
(Binary files differ)


Property changes on: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_viscm.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_viscm_edit.png
===================================================================
(Binary files differ)


Property changes on: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_viscm_edit.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_viscm_properties.png
===================================================================
(Binary files differ)


Property changes on: grass-addons/grass7/raster/r.colors.matplotlib/r_colors_matplotlib_viscm_properties.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream



More information about the grass-commit mailing list