Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Error bars 1D plots #247

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions vcs/Canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1995,6 +1995,14 @@ def drawline(self, name=None, ltype='solid', width=1, color=241, # noqa
drawline.__doc__ = drawline.__doc__ % (xmldocs.name, xmldocs.color, xmldocs.priority, xmldocs.viewport,
xmldocs.worldcoordinate, xmldocs.x_y_coords, xmldocs.projection)

def createerrorbars(self, name=None, source='default', etype=None, color=None):
return vcs.createerrorbars(name, source, etype, color)
createerrorbars.__doc__ = vcs.manageElements.createerrorbars.__doc__

def geterrorbars(self, name='default', etype=None, color=None):
return vcs.geterrorbars(name, etype, color)
geterrorbars.__doc__ = vcs.manageElements.geterrorbars.__doc__

def createmarker(self, name=None, source='default', mtype=None, # noqa
size=None, color=None, priority=1,
viewport=None, worldcoordinate=None,
Expand Down
49 changes: 48 additions & 1 deletion vcs/VCS_validation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,9 @@ def checkMarker(self, name, value):
'triangle_left_fill',
'triangle_right_fill',
'square_fill',
'hurricane']
'hurricane',
'error_x',
'error_y']
for i in range(103):
oks.append('w%.2i' % i)
for i in range(19):
Expand Down Expand Up @@ -346,6 +348,10 @@ def checkMarker(self, name, value):
value = 'square_fill'
elif value in ('hurricane', 18):
value = 'hurricane'
elif value in ('error_x', 19):
value = 'error_x'
elif value in ('error_y', 20):
value = 'error_y'
elif isinstance(value, str) and value[0] == "w" and int(value[1:]) in range(103):
value = value
elif value in range(100, 203):
Expand Down Expand Up @@ -376,6 +382,47 @@ def checkMarkersList(self, name, value):
return hvalue


def checkErrorBars(self, name, value):
import queries
checkName(self, name, value)
if (queries.iserrorbars(value) != 1):
checkedRaise(
self,
value,
ValueError,
'The ' +
name +
' value must be of type \'vcs.errorbars.Te\'.' +
' Instead it is %s' %
type(value))
return value


def checkErrorBarsType(self, name, value):
checkName(self, name, value)
oks = [
'x',
'y',
'xy']
if ((value in oks) or (value in range(0, 3))):
if value in ('x', 0):
value = 'x'
elif value in ('y', 0):
value = 'y'
elif value in ('xy', 0):
value = 'xy'
else:
checkedRaise(
self,
value,
ValueError,
'The ' +
name +
' value must be in : %s. Instead, it is %s' %
(oks, value))
return value


def checkListElements(self, name, value, function):
checkName(self, name, value)
if not isinstance(value, (list, tuple)):
Expand Down
7 changes: 6 additions & 1 deletion vcs/VTKPlots.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def __init__(self, canvas, renWin=None, debug=False, bg=None, geometry=None):
'vtk_backend_grid',
# vtkGeoTransform used for geographic transformation
'vtk_backend_geo',
'error'
]
self.numberOfPlotCalls = 0
self.renderWindowSize = None
Expand Down Expand Up @@ -722,9 +723,13 @@ def plot(self, data1, data2, template, gtype, gname, bg, *args, **kargs):
create_renderer=create_renderer)
create_renderer = False
elif gtype == "marker":
error = None
if kargs.get("error", None) is not None:
error = kargs["error"]
if gm.priority != 0:
actors = vcs2vtk.prepMarker(self.renWin, gm,
cmap=self.canvas.colormap)
cmap=self.canvas.colormap,
error=error)
returned["vtk_backend_marker_actors"] = actors
create_renderer = True
for g, gs, pd, act, geo in actors:
Expand Down
2 changes: 2 additions & 0 deletions vcs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class VCSDeprecationWarning(DeprecationWarning):
elements["textcombined"] = {}
elements["line"] = {}
elements["marker"] = {}
elements["errorbars"] = {}
elements["fillarea"] = {}
elements["font"] = {}
elements["fontNumber"] = {}
Expand Down Expand Up @@ -240,6 +241,7 @@ class VCSDeprecationWarning(DeprecationWarning):
vector.Gv("default")
streamline.Gs("default")
marker.Tm("default")
errorbars.Te("default")
meshfill.Gfm("default")
colormap.Cp("default")
displayplot.Dp("default")
Expand Down
248 changes: 248 additions & 0 deletions vcs/errorbars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
#
# Errorbars (Te) module
###############################################################################
# #
# Module: errorbars (Te) module #
# #
# Copyright: 2000, Regents of the University of California #
# This software may not be distributed to others without #
# permission of the author. #
# #
# Author: PCMDI Software Team #
# Lawrence Livermore NationalLaboratory: #
# support@pcmdi.llnl.gov #
# #
# Description: Python command/1d wrapper for VCS's errorbars object #
# #
# Version: 1.0 #
# #
###############################################################################
#
#
#
import VCS_validation_functions
import vcs
import genutil
from xmldocs import scriptdocs, listdoc


def process_src(nm, code):

# Takes VCS script code (string) as input and generates oneD gm from it
try:
gm = Te(nm)
except:
gm = vcs.elements["errorbars"][nm]
# process attributes with = as assignment
for att in ["type", "color"]:
i = code.find(" %s(" % att)
if i == -1:
i = code.find(",%s(" % att)
if i > -1:
v = genutil.get_parenthesis_content(code[i:])
setattr(gm, att, eval(v))


###############################################################################
# #
# Errorbars (Te) Class. #
# #
###############################################################################
class Te(vcs.bestMatch):

"""
The Errorbars object allows the manipulation of errorbar type, size and
color index.

This class is used to define an error bars for 1D plots in VCS, or it can be
used to change some or all of the error bar attributes in an existing plot.

.. describe:: Useful functions:

.. code-block:: python

# VCS canvas Constructor
x = vcs.init()
# mock data
data = "-1 -2 -1.34 -2 -1.1 -12.2".split()
data = numpy.array(data, dtype=numpy.float)
data = MV2.array(data)
# mock errors
error = "0.2 0.1 0.1 0.3 0.4 0.1".split()
error = numpy.array(error, dtype=numpy.float)
# Create default error bars
errorbars = vcs.createerrorbars('eb')
# Create a yx 1D plot
yx = vcs.createyxvsx()
yx.errorbars = errorbars
# plot
x.plot(data, yx, error)

.. describe:: Create a new instance of errorbars:

.. code-block:: python

# Copies content of 'old' to 'new'
eb = a.createerrorbars('new', 'old')
# Copies content of 'default' to 'new'
eb = a.createerrorbars('new')

.. describe:: Modify an existing errorbars:

.. code-block:: python

eb = a.geterrorbars('eb')

.. describe:: Overview of errorbars attributes:

* List all the errorbars attribute values:

.. code-block:: python

eb.list()
# Range from 1 to 256
eb.color = 100
# One of "x", "y" or "xy"
eb.type = "y"

* Specify the errorbars type:

.. code-block:: python

eb.type = 'x'
eb.type = 'y'

.. pragma: skip-doctest
"""
__slots__ = [
'name',
's_name',
'color',
'type',
'_name',
'_color',
'_type'
]

def _getname(self):
return self._name

def _setname(self, value):
value = VCS_validation_functions.checkname(self, 'name', value)
if value is not None:
self._name = value
name = property(_getname, _setname)

def _getcolor(self):
return self._color

def _setcolor(self, value):
if isinstance(value, int):
value = [value, ]
if value is not None:
value = VCS_validation_functions.checkColorList(
self,
'color',
value)
self._color = value
color = property(_getcolor, _setcolor)

def _gettype(self):
return self._type

def _settype(self, value):
if value is not None:
value = VCS_validation_functions.checkErrorBarsType(
self,
'type',
value)
self._type = value
type = property(_gettype, _settype)

def __init__(self, Te_name, Te_name_src='default'):
if (Te_name is None):
raise ValueError('Must provide an errorbar name.')
self._name = Te_name
self.s_name = 'Te'
if Te_name == "default":
self._type = "y"
self._color = [0, 0, 0, 100]
else:
if isinstance(Te_name_src, Te):
Te_name_src = Te_name_src.name
if Te_name_src not in vcs.elements["errorbars"]:
raise ValueError(
"The errorbars object '%s' does not exist" %
Te_name_src)
src = vcs.elements["errorbars"][Te_name_src]
for att in ['type', 'color']:
setattr(self, att, getattr(src, att))
# Ok now we need to stick it in the elements
vcs.elements["errorbars"][Te_name] = self

def list(self):
if (self.name == '__removed_from_VCS__'):
raise ValueError('This instance has been removed from VCS.')
print "---------- ErrorBars (Te) member (attribute) listings ----------"
print "secondary method =", self.s_name
print "name =", self.name
print "type =", self.type
print "color =", self.color
list.__doc__ = listdoc.format(name="errorbars", parent="")

def script(self, script_filename=None, mode=None):
if (script_filename is None):
raise ValueError(
'Error - Must provide an output script file name.')

if (mode is None):
mode = 'a'
elif (mode not in ('w', 'a')):
raise ValueError(
'Error - Mode can only be "w" for replace or "a" for append.')

# By default, save file in json
scr_type = script_filename.split(".")
if len(scr_type) == 1 or len(scr_type[-1]) > 5:
scr_type = "json"
if script_filename != "initial.attributes":
script_filename += ".json"
else:
scr_type = scr_type[-1]
if scr_type == '.scr':
raise vcs.VCSDeprecationWarning("scr script are no longer generated")
elif scr_type == "py":
mode = mode + '+'
py_type = script_filename[
len(script_filename) -
3:len(script_filename)]
if (py_type != '.py'):
script_filename = script_filename + '.py'

# Write to file
fp = open(script_filename, mode)
if (fp.tell() == 0): # Must be a new file, so include below
fp.write("#####################################\n")
fp.write("# #\n")
fp.write("# Import and Initialize VCS #\n")
fp.write("# #\n")
fp.write("#############################\n")
fp.write("import vcs\n")
fp.write("v=vcs.init()\n\n")

unique_name = '__Te__' + self.name
fp.write("#----------Errorbars (Te) member (attribute) listings ----------\n")
fp.write("tm_list=v.listelements('errorbars')\n")
fp.write("if ('%s' in tm_list):\n" % self.name)
fp.write(" %s = v.geterrorbars('%s')\n" % (unique_name, self.name))
fp.write("else:\n")
fp.write(" %s = v.createerrorbars('%s')\n" % (unique_name, self.name))
fp.write("%s.type = %s\n" % (unique_name, self.type))
fp.write("%s.color = %s\n\n" % (unique_name, self.color))
else:
# Json type
mode += "+"
f = open(script_filename, mode)
vcs.utils.dumpToJson(self, f)
f.close()
script.__doc__ = scriptdocs['errorbars']
Loading