Commit 286158c9 authored by Christian Schneider's avatar Christian Schneider
Browse files

Implemented extract functions for data_grid

parent 21c7e5d6
......@@ -12,7 +12,8 @@ hv.extension('bokeh')
import holoviews.operation.datashader as hd
import numpy as np
import xarray as xr
from .data_xy import data_xy
from IPython.display import display
class data_grid(data_module_base):
"""Class for grid like data in multiple dimensions. Powered by the
......@@ -45,6 +46,7 @@ class data_grid(data_module_base):
zip(data_names[:-1], data_array[:-1])},
name=data_names[-1])
# Helpful data functions ###################################################
def return_coord(self, coord_name):
return np.array(self.df.coords[coord_name])
......@@ -65,35 +67,77 @@ class data_grid(data_module_base):
"""Default for two dim grid: Return values"""
return self.df.values
# TODO Implement This
def import_data(self, data_arrays, data_names=None):
"""Import data from new arrays. Naming highly recommencd!
Assumes first given array is the array of the independant variable.
def extract_x(self, x0, plot=True):
"""Extract z values along axis for specified x value x0.
This function will return the data at the line corresponding at
the specified value of the x-axis.
If the value is not exact, it will take the closest one above the
value.
Parameters
-----------
data_arrays : list, array, np.array
List of data arrays. Structure
[[x, x, x, x, ....], [y, y, y,....], ... ]
name_list : list, array, np.array, None
List of names for arrays:
['x1', 'y', 'Resistances', ... ]
x0 : float
y value for which the data should be extracted
plot : bool
Plot the extracted datamodule directly
Returns
--------
DataModule
data_line module
"""
if data_names:
order_names = data_names
else:
order_names = df.columns
for d, idx in zip(data_arrays[0],
list(self.df[order_names[0]]
.index.reindex(data_arrays[0])[1])):
if idx != -1:
for i in range(1, len(data_arrays)):
# Use mean
self.df[order_names[i]][idx] += data_arrays[i]
self.df[order_names[i]][idx] /= 2
else:
self.df = self.df.append({key: value[idx]} for key, value
in zip(order_names, data_arrays))
x, y = self.df.dims[:2]
ex = self.df.sel(x=x0, method='nearest')
data = data_xy(np.array(self.df.coords[y]), np.array(ex),
x, y)
data.par = self.par
data.comments = self.comments
data.temp_start = self.temp_start
data.temp_stop = self.temp_stop
data.time_start = self.time_start
data.time_stop = self.time_stop
if plot:
display(data.plot_hv(title='{} = {:.6e}'.format(x, x0)))
return data
def extract_y(self, y0, plot=True):
"""Extract z values along axis for specified y value y0.
This function will return the data at the line corresponding at
the specified value of the x-axis.
If the value is not exact, it will take the closest one above the
value.
Parameters
-----------
y0 : float
y value for which the data should be extracted
plot : bool
Plot the extracted datamodule directly
Returns
--------
DataModule
data_line module
"""
x, y = self.df.dims[:2]
ex = self.df.sel(x=x0, method='nearest')
data = data_xy(np.array(self.df.coords[y]), np.array(ex),
x, y)
data.par = self.par
data.comments = self.comments
data.temp_start = self.temp_start
data.temp_stop = self.temp_stop
data.time_start = self.time_start
data.time_stop = self.time_stop
if plot:
display(data.plot_hv(title='{} = {:.6e}'.format(x, x0)))
return data
def plot_hv(self, name_x=None, name_y=None, cmap='magma', height=400,
width=800):
......@@ -125,3 +169,4 @@ class data_grid(data_module_base):
'style': {'cmap': cmap}}})
ds = hv.Dataset(self.df)
return hd.regrid(ds.to(hv.Image, [x_vals, y_vals]))
......@@ -72,7 +72,7 @@ class data_table(data_module_base):
in zip(order_names, data_arrays))
def plot_hv(self, x=None, y=None, height=400, width=800,
line_width=3):
title='', **kwargs):
"""Plot table with Holoviews
Parameters
......@@ -111,7 +111,8 @@ class data_table(data_module_base):
x_vals.append(self.df.keys()[0])
# Plot FIRST element
s = hv.Scatter(self.df, x_vals[0], y_vals[0])
hv.opts({'Curve': {'style': kwargs}})
s = hv.Scatter(self.df, x_vals[0], y_vals[0], label=title)
scatter_plots = hd.dynspread(hd.datashade(hv.Curve(s),
cmap=[color_scheme[0]]))
# Plot the other elements
......
......@@ -73,9 +73,6 @@ class data_xy(data_table):
self.import_data([x, y], ['x', 'y'])
def return_ysel(self):
"""Return currently selected y values"""
return self.y
# Plotting ################################################################
def plot(self, style='b-o', color=None, xscale=1, yscale=1,
......@@ -612,6 +609,13 @@ class data_xy(data_table):
Same as fit_func for compatibility to previous versions
"""
return self.fit_func(x)
# Compatibility to lower versions
def return_xsel(self):
return self.x
def return_ysel(self):
"""Return currently selected y values"""
return self.y
###############################################################################
# Aliases (for compatibility to previous datamdoule versions) #################
data_2d = data_xy
......@@ -14,6 +14,8 @@ import pandas as pd
from .data_xy import data_xy
from .data_surface import data_surface
from .data_complex import data_complex
from .data_grid import data_grid
from .data_table import data_table
from .data_line import data_line
from .data_IQ import data_IQ
import matplotlib.pyplot as plt
......@@ -22,7 +24,7 @@ from .version import __version__
from bokeh.palettes import Category10_10
import bokeh.plotting as bp
current_types = (data_xy, data_grid, data_complex, data_table)
# Function Library ############################################################
def load_datamodule(filename, upgrade=True):
""" Load a datamodule
......@@ -544,7 +546,7 @@ def upgrade_dm(old_dm):
print('Loaded Datamodule newer than your installed version\n' +
'Please update python_repo (git pull)')
elif vers_f < new_vers:
elif vers_f < new_vers or not isinstance(old_dm, current_types):
# Check if old datamodule is below current version and upgrade if
# Initialization
if isinstance(old_dm, data_xy) or isinstance(old_dm, data_line):
......@@ -553,12 +555,10 @@ def upgrade_dm(old_dm):
try:
data_new.xmin = old_dm.xmin
data_new.xmax = old_dm.xmax # Selected
data_new.xerr = old_dm.xerr
data_new.yerr = old_dm.yerr
except AttributeError:
pass
# Sort data
# TODO sort data automatically
# TODO
# Fit
try:
tmp = old_dm._fit_function_code
......@@ -569,21 +569,13 @@ def upgrade_dm(old_dm):
data_new._fit_par_errors = old_dm._fit_par_errors
data_new._fit_data_error = old_dm._fit_data_error
data_new._fit_labels = old_dm._fit_labels
except:
pass
elif isinstance(old_dm, data_surface):
data_new = data_surface(old_dm.x, old_dm.y, old_dm.z)
try:
data_new.xmin = old_dm.xmin
data_new.xmax = old_dm.xmax
data_new.ymin = old_dm.ymin
data_new.ymax = old_dm.ymax
data_new.xsellen = old_dm.xsellen
data_new.ysellen = old_dm.ysellen
except AttributeError:
pass
elif isinstance(old_dm, (data_grid, data_surface)):
data_new = data_grid([old_dm.x, old_dm.y, old_dm.z.T],
['x', 'y', 'z'])
elif isinstance(old_dm, data_complex):
data_new = data_complex(old_dm.x, old_dm.value)
try:
......@@ -642,6 +634,7 @@ def upgrade_dm(old_dm):
return old_dm
# TODO
###############################################################################
# NEED RETHINKING #############################################################
def data_stack_y(*args):
......
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