Skip to content

Commit

Permalink
Some fixes and modifications to gnuplot
Browse files Browse the repository at this point in the history
  • Loading branch information
István Bozsó committed Jul 30, 2019
1 parent 23e2737 commit add7b8d
Showing 1 changed file with 94 additions and 55 deletions.
149 changes: 94 additions & 55 deletions gnuplot/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from time import sleep
from shlex import split
from sys import maxsize
from functools import partial

try:
from IPython.display import Image, display
Expand All @@ -39,7 +40,7 @@

__all__ = (
"arrow", "call", "colorbar", "histo", "label", "line", "linedef",
"margins", "multiplot", "obj", "output", "palette", "plot", "data",
"margins", "multiplot", "output", "palette", "plot", "data",
"file", "grid", "refresh", "replot", "reset", "save", "set", "silent",
"splot", "style", "term", "title", "unset_multi", "colors",
"x", "y", "z", "sym", "col", "update", "points", "lines",
Expand Down Expand Up @@ -112,7 +113,8 @@ def __init__(self):
else:
self.cmd = config["exe"]

self.sets, self.commands, self.ext = {}, [], None
self.global_sets, self.sets, self.commands, self.ext, self.palette = \
{}, {}, [], None, None


def __del__(self):
Expand All @@ -128,8 +130,13 @@ def __call__(self, *commands):


def __hash__(self):
return phash((tuple(self.set_commands), tuple(self.commands)))
return phash((self.sets, tuple(self.commands)))

def __setitem__(self, item, val):
self.sets[item] = val

def __getitem__(self, item):
return self.sets[item]

def path(self, data):
return pth.join(tmp_path, "%s" % (phash(data)))
Expand All @@ -138,8 +145,18 @@ def path(self, data):
def refresh(self, plot_cmd, *items, **kwargs):
term(**kwargs)

txt = "%s\n%s %s\n" % ("\n".join(parse_set(key, val)
for key, val in self.sets.items()),
txt = "%s\n%s\n%s\n" % (
"\n".join(parse_set(key, val)
for key, val in self.global_sets.items()),
"\n".join(parse_set(key, val)
for key, val in self.sets.items()),
"\n".join(self.commands)
)

if self.palette is not None:
txt = "%s\n%s" % (txt, self.palette)

txt = "%s\n%s %s\n" % (txt,
plot_cmd,
", ".join(plot.command for plot in items))

Expand All @@ -149,8 +166,8 @@ def refresh(self, plot_cmd, *items, **kwargs):
if not pth.isfile(path):
txt = "set output '%s'\n%s" % (path, txt)

assert len({item.ptype for item in items}) == 1
ptype = items[0].ptype
# assert len({item.ptype for item in items}) == 1
# ptype = items[0].ptype

with open(gp_path, "w") as f:
f.write(txt)
Expand All @@ -162,19 +179,25 @@ def refresh(self, plot_cmd, *items, **kwargs):
raise e


self.commands = []
self.sets, self.commands = {}, []

return path


def gen_property(name, set_fmt=None):
session = Gnuplot()


def make_property(name, set_fmt=None, prefix=None):
mangled = "_%s" % name

if prefix is not None:
name = prefix + name

if set_fmt is None:
set_fmt = name

def setter(self, val):
set(**{set_fmt: val})
session.sets[name]
setattr(self, mangled, val)

def getter(self):
Expand All @@ -183,40 +206,58 @@ def getter(self):
return property(getter, setter)


def properties(Cls, **kwargs):
props = {
key: gen_property(key, val)
for key, val in Cls.__properties__.items()
}
def properties(Cls, prefix=None):
props = Cls.__properties__

Cls.prefix = prefix
# Cls.parse_set = staticmethod(partial(parse_set, prefix=prefix))

def init(self, *args, **kwargs):
for key in props:
setattr(self, "_%s" % key, kwargs.get(key, None))


return type(type(Cls).__name__, (Cls,), props)
def parse(self):
return "\n".join(parse_set(key, getattr(self, key), Cls.prefix)
for key in Cls.__properties__
if getattr(self, key) is not None)


for key, val in props.items():
setattr(Cls, key, make_property(key, val))

Cls.__init__ = init
Cls.parse = parse

return Cls


@properties
class Datafile(object):
__properties__ = {
"separator": None
"separator": None,
"binary": None,
"comment": "commentschars",
"missing": None,
"fortran": None
}



datafile = Datafile()
datafile = properties(Datafile, prefix="datafile ")()


class Axis(object):
def __init__(self, call, name="x"):
self.call, self.name, self._range, self._label = \
call, name, [], None
def __init__(self, name="x"):
self.name, self._range, self._label = \
name, [], None

def __getitem__(self, k):
if isinstance(k, slice):
key = "%stics" % self.name
set(key="%f,%f,%f" % (k.start, k.step, k.stop))
session["%stics" % self.name] = \
"%f,%f,%f" % (k.start, k.step, k.stop)

def set_range(self, _range):
self._range = _range
key = "%srange" % self.name
set(key="[%f:%f]" % (_range[0], _range[1]))
session["%srange" % self.name] = "[%f:%f]" % (_range[0], _range[1])

def get_range(self):
return self._range
Expand All @@ -226,8 +267,7 @@ def get_range(self):

def set_label(self, label):
self._label = label
key = "%slabel" % self.name
set(key="'%s'" % label)
session["%slabel" % self.name] = "'%s'" % label


def get_label(self):
Expand All @@ -241,14 +281,11 @@ def get_format(self):

def set_format(self, format):
self._format = format
key = "format %s" % self.name
set(key="'%s'" % format)
session["format %s" % self.name] = "'%s'" % format

format = property(get_format, set_format)


session = Gnuplot()

call, refresh = session.__call__, session.refresh
silent = config["silent"]

Expand All @@ -260,9 +297,9 @@ def set(**kwargs):
# TODO: properly provide access to debug
# debug = property(session.get_debug, session.set_debug)

x = Axis(call, "x")
y = Axis(call, "y")
z = Axis(call, "z")
x = Axis("x")
y = Axis("y")
z = Axis("z")


def data(*arrays, ltype="points", **kwargs):
Expand Down Expand Up @@ -461,7 +498,7 @@ def size(scale, square=False, ratio=None):


def palette(pal):
call(color_palettes[pal])
session.palette = color_palettes[pal]


def margins(screen=False, **kwargs):
Expand Down Expand Up @@ -510,7 +547,6 @@ def title(title):


def term(term, size=None, **kwargs):
size = kwargs.get("size")
enhanced = bool(kwargs.get("enhanced", False))

if "font" in kwargs or "fontsize" in kwargs:
Expand All @@ -530,14 +566,14 @@ def term(term, size=None, **kwargs):


def font(font="Verdena", fontsize=12.0):
set(font="'%s, %f'" % (font, fontsize))
set(font="'%s, %d'" % (font, fontsize))


def output(outfile, **kwargs):
if kwargs:
term(**kwargs)

session.set_commands.append("set output '%s'" % (outfile))
set(output=outfile)


def arrow(From, to, style=None, tag=""):
Expand All @@ -549,10 +585,6 @@ def arrow(From, to, style=None, tag=""):
set(arrow=temp)


def obj(kind):
pass


def label(definition, position, **kwargs):
font = str(kwargs.get("font", "Verdena"))
fontsize = int(kwargs.get("fontsize", 8))
Expand Down Expand Up @@ -737,7 +769,9 @@ def parse_range(args):
"separator",
"locale",
"rgb",
"rgbcolor"
"rgbcolor",
"commentschars",
"output"
})


Expand Down Expand Up @@ -767,13 +801,18 @@ def proc_using(txt):
return txt


def parse_set(key, value):
def parse_set(key, value, prefix=None):
if prefix is None:
tpl = "set %s"
else:
tpl = "set %s %%s" % prefix

if value is True:
return "set %s" % (key)
return tpl % (key)
elif value is False:
return "unset %s" % (key)
return ("un" + tpl) % (key)
else:
return "set %s" % parse_option(key, value)
return tpl % parse_option(key, value)


color_codes = {
Expand Down Expand Up @@ -1545,11 +1584,11 @@ def parse_set(key, value):
# palette
set palette defined ( 0 '#FFFFD9',\
1 '#EDF8B1',\
2 '#C7E9B4',\
3 '#7FCDBB',\
4 '#41B6C4',\
5 '#1D91C0',\
6 '#225EA8',\
7 '#0C2C84' )
2 '#C7E9B4',\
3 '#7FCDBB',\
4 '#41B6C4',\
5 '#1D91C0',\
6 '#225EA8',\
7 '#0C2C84' )
"""
}

0 comments on commit add7b8d

Please sign in to comment.