plotter.py 2.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# -*- coding: utf-8 -*-

import abc
import os

import plotly
from plotly import tools
import plotly.offline as ply


from keepers import Loggable

from nifty.spaces.space import Space
from nifty.field_types.field_type import FieldType
import nifty.nifty_utilities as utilities

plotly.offline.init_notebook_mode()


class Plotter(Loggable, object):
    __metaclass__ = abc.ABCMeta

23
    def __init__(self, interactive=False, path='.', title=""):
24
25
        self.interactive = interactive
        self.path = path
26
        self.title = str(title)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

    @abc.abstractproperty
    def domain(self):
        return (Space,)

    @property
    def interactive(self):
        return self._interactive

    @interactive.setter
    def interactive(self, interactive):
        self._interactive = bool(interactive)

    @property
    def path(self):
        return self._path

    @path.setter
    def path(self, new_path):
        self._path = os.path.normpath(new_path)

48
49
50
51
    def plot(self, fields, spaces=None,  slice=None):
        # `fields` can contain one or more fields
        # if multiple fields are supplied -> stack the plots
        data = self._get_data_from_field(fields, spaces, slice)
Mihai Baltac's avatar
Mihai Baltac committed
52
53
54
55
        figures = self._create_individual_plot(data)
        self._finalize_figure(figures)

    @abc.abstractmethod
56
    def _get_data_from_field(self, field, spaces=None, slice=None):
57
58
59
60
61
62

        spaces = utilities.cast_axis_to_tuple(spaces, len(field.domain))
        if field.domain[spaces] != self.domain:
            raise AttributeError("Given space(s) of input field-domain do not "
                                 "match the plotters domain.")

Mihai Baltac's avatar
Mihai Baltac committed
63
64
65
66
            # iterate over the individual slices in order to compose the figure
            # -> make a d2o.get_full_data() (for rank==0 only?)
            # add
        return [1,2,3]
67
68
69
70
71

    def _create_individual_plot(self, data):
        pass

    def _finalize_figure(self, figure):
Mihai Baltac's avatar
Mihai Baltac committed
72
73
74
75
76
77
        pass
        # is there a use for ply.plot when one has no interest in
        # saving a file?

        # -> check for different file types
        # -> store the file to disk (MPI awareness?)
78
79
80
81