1 """\
2 Gnuplot-py visualization plugin for FuzzPy.
3
4 @author: Aaron Mavrinac
5 @organization: University of Windsor
6 @contact: mavrin1@uwindsor.ca
7 @license: LGPL-3
8 """
9
10 import os
11 import tempfile
12 import warnings
13 import inspect
14 from time import sleep
15
16 from ..fnumber import PolygonalFuzzyNumber, TriangularFuzzyNumber, \
17 TrapezoidalFuzzyNumber, GaussianFuzzyNumber
18 from abc_plugin import AbstractPlugin
19
20 VIS_PLUGIN = 'FuzzPyGnuplot'
21 VIS_TYPES = [PolygonalFuzzyNumber, TriangularFuzzyNumber,
22 TrapezoidalFuzzyNumber, GaussianFuzzyNumber]
23 VIS_FORMATS = {'png': 'png', 'jpg': 'jpeg', 'gif': 'gif', 'pbm': 'pbm',
24 'eps': 'postscript eps enhanced'}
28 """\
29 Gnuplot-py visualization plugin for FuzzPy.
30 """
31 - def __init__(self, obj=None, *args, **kwargs):
32 """\
33 Plugin constructor.
34
35 @param obj: The fuzzy number to visualize.
36 @type obj: L{PolygonalFuzzyNumber}
37 """
38 from Gnuplot import Gnuplot
39 self.Gnuplot = Gnuplot
40 self._N = obj
41
42 if hasattr(self._N, 'to_polygonal'):
43 if 'np' in inspect.getargspec(self._N.to_polygonal).args:
44 self._N = self._N.to_polygonal(np=50)
45 else:
46 self._N = self._N.to_polygonal()
47
48 @staticmethod
50 """\
51 Return whether this plugin is supported.
52
53 @return: True if the plugin can run in this environment.
54 @rtype: C{bool}
55 """
56 try:
57 import Gnuplot
58 except ImportError:
59 warnings.warn(("Gnuplot plugin will not run on this system. "
60 "You must install Gnuplot-py first."))
61 return False
62 return True
63
65 """\
66 Gnuplot-py visualization mehod.
67
68 @return: Postscript visualization string.
69 @rtype: C{str}
70 """
71 if kwargs.has_key('format') and kwargs['format'] in VIS_FORMATS:
72 output_format = kwargs['format']
73 else:
74 output_format = 'png'
75 plot = self.Gnuplot()
76 if output_format in ['eps']:
77
78 plot('set terminal %s' % VIS_FORMATS[output_format])
79 else:
80
81 if kwargs.has_key('size') and len(kwargs['size']) == 2:
82 w, h = kwargs['size']
83 else:
84 w, h = (640, 480)
85 plot('set terminal %s size %d,%d' % (VIS_FORMATS[output_format], w, h))
86 plot('set style data lines')
87 tmpdir = tempfile.mkdtemp()
88 filename = os.path.join(tmpdir, 'gnuplot-output' + output_format)
89 plot('set output \"%s\"' % filename)
90 plot.plot([[p[0], p[1]] for p in self._N.points])
91
92 sleep(0.5)
93 tmpfile = open(filename, 'r')
94 output_data = tmpfile.read()
95 tmpfile.close()
96 os.remove(filename)
97 os.rmdir(tmpdir)
98 return (output_format, output_data)
99
100 AbstractPlugin.register(FuzzPyGnuplot)
101