Commit 42fb1066 authored by Christian Schneider's avatar Christian Schneider
Browse files

Xarray implementation for data_grid

parent 0f857872
......@@ -2,43 +2,70 @@
Author: Christian Schneider <>
Date: 17.03.2018
data_grid structure for our DataModule.
Powered by xarray (
from .base import data_module_base
import holoviews as hv
import holoviews.operation.datashader as hd
import numpy as np
import xarray as xr
class data_grid(data_module_base):
"""Class for grid like data with one or two independent variables and
one or more dependent variables
data_arrays :
Data to save. Format should be
[array_xaxis, array_yaxis, matrix(yaxis, xaxis)]
"""Class for grid like data in multiple dimensions. Powered by the
excellent library xarray for python.
Initialization should be [x1_coords, x2_coords, ...., xN_coords,
Which means that the matrix for data is the last element of the given array.
Supports also custom names for each dimension which can be given as an
array. The last element is the name for the values (not the dimesion).
Example: ['Probe Frequency (GHz)', 'Readout Frequency (GHz)', 'Mag (dB)']
def __init__(self, data_arrays, data_names=None):
def __init__(self, data_array, data_names=None):
# Create default names
df_names = ['x{}'.format(i) for i in range(len(data_arrays))]
df_names = ['x{}'.format(i) for i in range(len(data_array))]
df_names[-1] = 'Value'
# Replace default names by the names given
if data_names:
for idx in range(len(data_names)):
if data_names[idx]:
df_names[idx] = data_names[idx]
# Create holoviews object TODO: Adapt to xarray
# Assume the last element of the given arrays is the grid data
self.ds = hv.Dataset(tuple(i for i in data_arrays),
[i for i in df_names[:-1]], df_names[-1])
self.df = self.ds.dframe()
# Create xarray
self.df = xr.DataArray(data_array[-1],
dims=tuple(d_name for d_name in data_names[:-1]),
coords={d_name: d_vals for (d_name, d_vals) in
zip(data_names[:-1], data_array[:-1])},
def return_coord(self, coord_name):
return np.array(self.df.coords[coord_name])
def x(self):
return self.df
"""Default for two dim grid: Return first dimension"""
dim_name = self.df.dims[0]
return self.return_coord(dim_name)
def y(self):
"""Default for two dim grid: Return second dimension"""
dim_name = self.df.dims[1]
return self.return_coord(dim_name)
def z(self):
"""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.
......@@ -68,8 +95,7 @@ class data_grid(data_module_base):
self.df = self.df.append({key: value[idx]} for key, value
in zip(order_names, data_arrays))
def plot_hv(self, name_x=None, name_y=None, height=400, width=800,
def plot_hv(self, name_x=None, name_y=None, cmap='magma'):
"""Plot table with Holoviews
......@@ -107,5 +133,6 @@ class data_grid(data_module_base):
for i in range(len(y_vals) - len(x_vals)):
plot=dict(width=800, height=400), Image=dict(cmap=cmap))
ds = hv.Dataset(self.df)
plot=dict(width=800, height=400), Image=dict(cmap=cmap)))
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