Commit 778ec4eb authored by Somnath, Suhas's avatar Somnath, Suhas
Browse files

Fixed image cleaned results plotter to use new plot_map definition

parent 1278d9b2
......@@ -14,8 +14,8 @@ import numpy as np
from IPython.display import display
from matplotlib import pyplot as plt
from .plot_utils import plot_loops, plot_map_stack, get_cmap_object, plot_map, save_fig_filebox_button, \
set_tick_font_size, plot_complex_loop_stack, plot_complex_map_stack
from .plot_utils import plot_loops, plot_map_stack, get_cmap_object, plot_map, set_tick_font_size, plot_complex_loop_stack, plot_complex_map_stack
from pycroscopy.viz.jupyter_utils import save_fig_filebox_button
from ..analysis.utils.be_loop import loop_fit_function
from ..analysis.utils.be_sho import SHOfunc
from ..io.hdf_utils import reshape_to_Ndims, getAuxData, get_sort_order, get_dimensionality, get_attr, \
......
......@@ -2,6 +2,7 @@
Created on 11/11/16 10:08 AM
@author: Suhas Somnath, Chris Smith
"""
import os
import matplotlib.pyplot as plt
from IPython.display import display
......@@ -278,4 +279,169 @@ def simple_ndim_visualizer(data_mat, pos_dim_names, pos_dim_units_old, spec_dim_
slice_dict.update(kwargs)
display(fig)
widgets.interact(update_plots, **slice_dict);
\ No newline at end of file
widgets.interact(update_plots, **slice_dict);
def save_fig_filebox_button(fig, filename):
"""
Create ipython widgets to allow the user to save a figure to the
specified file.
Parameters
----------
fig : matplotlib.Figure
The figure to be saved.
filename : str
The filename the figure should be saved to
Returns
-------
widget_box : ipywidgets.HBox
Widget box holding the text entry and save button
"""
filename = os.path.abspath(filename)
file_dir, filename = os.path.split(filename)
name_box = widgets.Text(value=filename,
placeholder='Type something',
description='Output Filename:',
disabled=False,
layout={'width': '50%'})
save_button = widgets.Button(description='Save figure')
def _save_fig():
save_path = os.path.join(file_dir, filename)
fig.save_fig(save_path, dpi='figure')
print('Figure saved to "{}".'.format(save_path))
widget_box = widgets.HBox([name_box, save_button])
save_button.on_click(_save_fig)
return widget_box
def export_fig_data(fig, filename, include_images=False):
"""
Export the data of all plots in the figure `fig` to a plain text file.
Parameters
----------
fig : matplotlib.figure.Figure
The figure containing the data to be exported
filename : str
The filename of the output text file
include_images : bool
Should images in the figure also be exported
Returns
-------
"""
# Get the data from the figure
axes = fig.get_axes()
axes_dict = dict()
for ax in axes:
ax_dict = dict()
ims = ax.get_images()
if len(ims) != 0 and include_images:
im_dict = dict()
for im in ims:
# Image data
im_lab = im.get_label()
im_dict[im_lab] = im.get_array().data
# X-Axis
x_ax = ax.get_xaxis()
x_lab = x_ax.label.get_label()
if x_lab == '':
x_lab = 'X'
im_dict[im_lab + x_lab] = x_ax.get_data_interval()
# Y-Axis
y_ax = ax.get_yaxis()
y_lab = y_ax.label.get_label()
if y_lab == '':
y_lab = 'Y'
im_dict[im_lab + y_lab] = y_ax.get_data_interval()
ax_dict['Images'] = im_dict
lines = ax.get_lines()
if len(lines) != 0:
line_dict = dict()
xlab = ax.get_xlabel()
ylab = ax.get_ylabel()
if xlab == '':
xlab = 'X Data'
if ylab == '':
ylab = 'Y Data'
for line in lines:
line_dict[line.get_label()] = {xlab: line.get_xdata(),
ylab: line.get_ydata()}
ax_dict['Lines'] = line_dict
if ax_dict != dict():
axes_dict[ax.get_title()] = ax_dict
'''
Now that we have the data from the figure, we need to write it to file.
'''
filename = os.path.abspath(filename)
basename, ext = os.path.splitext(filename)
folder, _ = os.path.split(basename)
spacer = r'**********************************************\n'
data_file = open(filename, 'w')
data_file.write(fig.get_label() + '\n')
data_file.write('\n')
for ax_lab, ax in axes_dict.items():
data_file.write('Axis: {} \n'.format(ax_lab))
for im_lab, im in ax['Images'].items():
data_file.write('Image: {} \n'.format(im_lab))
data_file.write('\n')
im_data = im.pop('data')
for row in im_data:
row.tofile(data_file, sep='\t', format='%s')
data_file.write('\n')
data_file.write('\n')
for key, val in im.items():
data_file.write(key + '\n')
val.tofile(data_file, sep='\n', format='%s')
data_file.write('\n')
data_file.write(spacer)
for line_lab, line_dict in ax['Lines'].items():
data_file.write('Line: {} \n'.format(line_lab))
data_file.write('\n')
dim1, dim2 = line_dict.keys()
data_file.write('{} \t {} \n'.format(dim1, dim2))
for val1, val2 in zip(line_dict[dim1], line_dict[dim2]):
data_file.write('{} \t {} \n'.format(str(val1), str(val2)))
data_file.write(spacer)
data_file.write(spacer)
data_file.close()
return
\ No newline at end of file
......@@ -9,12 +9,10 @@ Created on Thu May 05 13:29:12 2016
from __future__ import division, print_function, absolute_import, unicode_literals
import inspect
import os
import sys
from warnings import warn
import h5py
import ipywidgets as widgets
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
......@@ -1443,166 +1441,3 @@ def plot_image_cleaning_results(raw_image, clean_image, stdevs=2, heading='Image
return fig_clean, axes_clean
def save_fig_filebox_button(fig, filename):
"""
Create ipython widgets to allow the user to save a figure to the
specified file.
Parameters
----------
fig : matplotlib.Figure
The figure to be saved.
filename : str
The filename the figure should be saved to
Returns
-------
widget_box : ipywidgets.HBox
Widget box holding the text entry and save button
"""
filename = os.path.abspath(filename)
file_dir, filename = os.path.split(filename)
name_box = widgets.Text(value=filename,
placeholder='Type something',
description='Output Filename:',
disabled=False,
layout={'width': '50%'})
save_button = widgets.Button(description='Save figure')
def _save_fig():
save_path = os.path.join(file_dir, filename)
fig.save_fig(save_path, dpi='figure')
print('Figure saved to "{}".'.format(save_path))
widget_box = widgets.HBox([name_box, save_button])
save_button.on_click(_save_fig)
return widget_box
def export_fig_data(fig, filename, include_images=False):
"""
Export the data of all plots in the figure `fig` to a plain text file.
Parameters
----------
fig : matplotlib.figure.Figure
The figure containing the data to be exported
filename : str
The filename of the output text file
include_images : bool
Should images in the figure also be exported
Returns
-------
"""
# Get the data from the figure
axes = fig.get_axes()
axes_dict = dict()
for ax in axes:
ax_dict = dict()
ims = ax.get_images()
if len(ims) != 0 and include_images:
im_dict = dict()
for im in ims:
# Image data
im_lab = im.get_label()
im_dict[im_lab] = im.get_array().data
# X-Axis
x_ax = ax.get_xaxis()
x_lab = x_ax.label.get_label()
if x_lab == '':
x_lab = 'X'
im_dict[im_lab + x_lab] = x_ax.get_data_interval()
# Y-Axis
y_ax = ax.get_yaxis()
y_lab = y_ax.label.get_label()
if y_lab == '':
y_lab = 'Y'
im_dict[im_lab + y_lab] = y_ax.get_data_interval()
ax_dict['Images'] = im_dict
lines = ax.get_lines()
if len(lines) != 0:
line_dict = dict()
xlab = ax.get_xlabel()
ylab = ax.get_ylabel()
if xlab == '':
xlab = 'X Data'
if ylab == '':
ylab = 'Y Data'
for line in lines:
line_dict[line.get_label()] = {xlab: line.get_xdata(),
ylab: line.get_ydata()}
ax_dict['Lines'] = line_dict
if ax_dict != dict():
axes_dict[ax.get_title()] = ax_dict
'''
Now that we have the data from the figure, we need to write it to file.
'''
filename = os.path.abspath(filename)
basename, ext = os.path.splitext(filename)
folder, _ = os.path.split(basename)
spacer = r'**********************************************\n'
data_file = open(filename, 'w')
data_file.write(fig.get_label() + '\n')
data_file.write('\n')
for ax_lab, ax in axes_dict.items():
data_file.write('Axis: {} \n'.format(ax_lab))
for im_lab, im in ax['Images'].items():
data_file.write('Image: {} \n'.format(im_lab))
data_file.write('\n')
im_data = im.pop('data')
for row in im_data:
row.tofile(data_file, sep='\t', format='%s')
data_file.write('\n')
data_file.write('\n')
for key, val in im.items():
data_file.write(key + '\n')
val.tofile(data_file, sep='\n', format='%s')
data_file.write('\n')
data_file.write(spacer)
for line_lab, line_dict in ax['Lines'].items():
data_file.write('Line: {} \n'.format(line_lab))
data_file.write('\n')
dim1, dim2 = line_dict.keys()
data_file.write('{} \t {} \n'.format(dim1, dim2))
for val1, val2 in zip(line_dict[dim1], line_dict[dim2]):
data_file.write('{} \t {} \n'.format(str(val1), str(val2)))
data_file.write(spacer)
data_file.write(spacer)
data_file.close()
return
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment