Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
tool_chooser = Enum(values="tool_list")
ts_list = List()
ts1_chooser = Enum(values="ts_list")
ts2_chooser = Enum(values="ts_list")
# Moving average window size (in number of observations)
ma_window_size = Int(0)
# Analysis details
ts_analysis_details = Str("No details available")
# Data
ts_data = Dict()
arr_plot_data = Instance(ArrayPlotData, ())
index_is_dates = Bool()
# Plots
ts_plot = Instance(ToolbarPlot)
ts_analysis_plot = Instance(ToolbarPlot)
def trait_view(self, view):
""" Build the view. The local namespace is
"""
return View(
VGroup(Item('data_file', style='simple', label="HDF file to load"),
HSplit(Item('ts_plot', editor=ComponentEditor(size=(400, 600)),
show_label=False),
VGroup(Item('tool_chooser', show_label = True, label="Choose tool"),
Item('ts1_chooser', label="TS 1"),
Item('ts2_chooser', label="TS 2",
visible_when="tool_chooser in ['%s']" % CORRELATION),
Item('ma_window_size', label="MA window size",
visible_when="tool_chooser in ['%s']" % MA),
Item('ts_analysis_plot', editor=ComponentEditor(size=(400, 600)),
def update_main_plot(self):
""" Build main plot
"""
self.ts_plot = ToolbarPlot(self.arr_plot_data)
for i, k in enumerate([k for k in self.ts_data.keys() if k != "index"]):
renderer = self.ts_plot.plot(("index", k), name = k, color = colors[i % len(colors)])[0]
if self.index_is_dates:
# Index was an array of datetime: overwrite the x axis
self.ts_plot.x_axis = None
x_axis = PlotAxis(self.ts_plot, orientation="bottom",
tick_generator=ScalesTickGenerator(scale=CalendarScaleSystem()))
self.ts_plot.overlays.append(x_axis)
self.ts_plot.x_grid.tick_generator = x_axis.tick_generator
if self.data_file:
self.ts_plot.title = ("Time series visualization from %s"
% (os.path.split(self.data_file)[1]))
else:
self.ts_plot.title = "Time series visualization"
attach_tools(self.ts_plot)
ts_list = List()
ts1_chooser = Enum(values="ts_list")
ts2_chooser = Enum(values="ts_list")
# Moving average window size (in number of observations)
ma_window_size = Int(0)
# Analysis details
ts_analysis_details = Str("No details available")
# Data
ts_data = Dict()
arr_plot_data = Instance(ArrayPlotData, ())
index_is_dates = Bool()
# Plots
ts_plot = Instance(ToolbarPlot)
ts_analysis_plot = Instance(ToolbarPlot)
def trait_view(self, view):
""" Build the view. The local namespace is
"""
return View(
VGroup(Item('data_file', style='simple', label="HDF file to load"),
HSplit(Item('ts_plot', editor=ComponentEditor(size=(400, 600)),
show_label=False),
VGroup(Item('tool_chooser', show_label = True, label="Choose tool"),
Item('ts1_chooser', label="TS 1"),
Item('ts2_chooser', label="TS 2",
visible_when="tool_chooser in ['%s']" % CORRELATION),
Item('ma_window_size', label="MA window size",
visible_when="tool_chooser in ['%s']" % MA),
Item('ts_analysis_plot', editor=ComponentEditor(size=(400, 600)),
show_label=False),
def _plot_default(self, toolbar=True, **pltkwds):
''' Draw bare plot, including main plotting area, toolbar, etc...
either at initialization or global redo'''
if toolbar:
self.plot=ToolbarPlot(self.plotdata, **pltkwds)
else:
self.plot=Plot(self.plotdata, **pltkwds)
self.plot.title = self.title
self.plot.padding = 50
self.plot.legend.visible=False
self.plot.tools.append(PanTool(self.plot))
zoom=BetterSelectingZoom(component=self.plot, tool_mode="box", always_on=False)
self.plot.overlays.append(zoom)
def update_main_plot(self):
""" Build main plot
"""
self.ts_plot = ToolbarPlot(self.arr_plot_data)
for i, k in enumerate([k for k in self.ts_data.keys() if k != "index"]):
self.ts_plot.plot(("index", k), name = k, color = colors[i % len(colors)])
if self.index_is_dates:
# Index was an array of datetime: overwrite the x axis
self.ts_plot.x_axis = None
x_axis = PlotAxis(self.ts_plot, orientation="bottom",
tick_generator=ScalesTickGenerator(scale=CalendarScaleSystem()))
self.ts_plot.overlays.append(x_axis)
self.ts_plot.x_grid.tick_generator = x_axis.tick_generator
if self.data_file:
self.ts_plot.title = "Time series visualization from %s" % self.data_file
else:
self.ts_plot.title = "Time series visualization"
attach_tools(self.ts_plot)
# Show legend
plot.legend.visible = True
plot.legend.align = "lr"
# Legend Highlighter: allows to click on the line in the legend to show that one
highlight_tool = LegendHighlighter(plot.legend)
plot.tools.append(highlight_tool)
class GSODDataPlotterView(HasTraits):
""" Application of the zoom tool to the GSOD plotting tool.
Load a HDF file containing one or more timeseries and plot the entire data inside.
The zoom tool allows to explore a subset of it. The legend allows to (de)select some
timeseries.
"""
data_file = File()
ts_data = Dict()
ts_plot = Instance(ToolbarPlot)
traits_view = View(
VGroup(Item('data_file', style = 'simple', label="HDF file to load"),
Item('ts_plot', editor=ComponentEditor(size=(800, 600)),
show_label=False),),
title='Chaco Plot with file loader and legend highlighter',
width=900, height=800, resizable=True)
def __init__(self, pandas_list = [], array_dict = {}, *args, **kw):
""" If a (list of) pandas or a dict of arrays is passed, load them up.
"""
ts_data = {}
super(GSODDataPlotterView, self).__init__(*args, **kw)
if not isinstance(pandas_list, list):
pandas_list = [pandas_list]
if pandas_list:
def __init__(self, depth, data_series, **kw):
super(MyPlot, self).__init__(**kw)
plot_data = ArrayPlotData(index=depth)
plot_data.set_data('data_series', data_series)
self.plot = ToolbarPlot(plot_data, orientation='v', origin='top left')
line = self.plot.plot(('index', 'data_series'))[0]
line_inspector = LineInspector(component=line, write_metadata=True)
line.tools.append(line_inspector)
line.overlays.append(line_inspector)
import numpy
from chaco.api import ToolbarPlot, ArrayPlotData
from chaco.tools.api import LineInspector
from enable.component_editor import ComponentEditor
from traits.api import HasTraits, Instance
from traitsui.api import UItem, View
class MyPlot(HasTraits):
""" Plot where depth is the index such that the plot is vertical
and the origin is the upper left
"""
plot = Instance(ToolbarPlot)
traits_view = View(UItem('plot', editor=ComponentEditor()),
width=600, height=600, resizable=True
)
def __init__(self, depth, data_series, **kw):
super(MyPlot, self).__init__(**kw)
plot_data = ArrayPlotData(index=depth)
plot_data.set_data('data_series', data_series)
self.plot = ToolbarPlot(plot_data, orientation='v', origin='top left')
line = self.plot.plot(('index', 'data_series'))[0]
line_inspector = LineInspector(component=line, write_metadata=True)
line.tools.append(line_inspector)
line.overlays.append(line_inspector)
def update_analysis_plot(self):
""" Build analysis plot
"""
self.ts_analysis_plot = ToolbarPlot(self.arr_plot_data)
if self.tool_chooser == CORRELATION:
self.corr_renderer = self.ts_analysis_plot.plot((self.ts1_chooser,
self.ts2_chooser), type = "scatter", color = "blue")[0]
self.ts_analysis_plot.title = "%s plotted against %s" % (self.ts1_chooser, self.ts2_chooser)
self.ts_analysis_plot.index_axis.title = self.ts1_chooser
self.ts_analysis_plot.value_axis.title = self.ts2_chooser
elif self.tool_chooser == MA and self.ma_window_size > 0:
ts1_ma = pandas.rolling_mean(self.arr_plot_data.get_data(self.ts1_chooser),
self.ma_window_size)
self.arr_plot_data.set_data("ts1_ma", ts1_ma)
self.ts_analysis_plot.plot(("index", self.ts1_chooser), type = "scatter", color = "blue")
self.ts_analysis_plot.plot(("index", "ts1_ma"), type = "line", color = "blue")
def _ts_data_changed(self):
""" Dataset has changed: update the plot.
ENH: add the possibility to pass a dict to ArrayPlotData.
"""
print "data changed: updating the plot..."
arr_data = ArrayPlotData()
for k,v in self.ts_data.items():
arr_data.set_data(k,v)
self.ts_plot = ToolbarPlot(arr_data)
for i, k in enumerate([k for k in self.ts_data.keys() if k != "index"]):
self.ts_plot.plot(("index", k), name = k, color = colors[i % len(colors)])
if self.index_is_dates:
# Index was an array of datetime: overwrite the x axis
self.ts_plot.x_axis = None
x_axis = PlotAxis(self.ts_plot, orientation="bottom",
tick_generator=ScalesTickGenerator(scale=CalendarScaleSystem()))
self.ts_plot.overlays.append(x_axis)
self.ts_plot.x_grid.tick_generator = x_axis.tick_generator
if self.data_file:
self.ts_plot.title = "Time series visualization from %s" % self.data_file
else:
self.ts_plot.title = "Time series visualization"
attach_tools(self.ts_plot)