Commit 31c6b611 authored by Unknown's avatar Unknown
Browse files

Updated the tutorial to also show the PycroDataset object.

parent 9c11a8f1
Examples using ``pycroscopy.PycroDataset``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. raw:: html
<div class="sphx-glr-thumbcontainer" tooltip="**Suhas Somnath** 8/8/2017">
.. only:: html
.. figure:: /auto_examples/images/thumb/sphx_glr_tutorial_03_multidimensional_data_thumb.png
:ref:`sphx_glr_auto_examples_tutorial_03_multidimensional_data.py`
.. raw:: html
</div>
.. only:: not html
* :ref:`sphx_glr_auto_examples_tutorial_03_multidimensional_data.py`
{
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"name": "python3",
"language": "python"
},
"language_info": {
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"mimetype": "text/x-python",
"name": "python",
"file_extension": ".py",
"version": "3.5.2",
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python"
}
},
"cells": [
{
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"%matplotlib inline"
]
......@@ -23,32 +42,13 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"# Code source: pycroscopy\n# Liscense: MIT\n\nfrom __future__ import division, print_function, absolute_import, unicode_literals\nimport h5py\ntry:\n # This package is not part of anaconda and may need to be installed.\n import wget\nexcept ImportError:\n import pip\n pip.main(['install', 'wget'])\n import wget\n\nfrom os import remove\nimport pycroscopy as px\n\n# Downloading the file from the pycroscopy Github project\nurl = 'https://raw.githubusercontent.com/pycroscopy/pycroscopy/master/data/BELine_0004.h5'\nh5_path = 'temp.h5'\n_ = wget.download(url, h5_path)\n\n# h5_path = px.io_utils.uiGetFile(caption='Select .h5 file', filter='HDF5 file (*.h5)')\n\n# Read the file using using h5py:\nh5_file1 = h5py.File(h5_path, 'r')\n\n# Look at the contents of the file:\npx.hdf_utils.print_tree(h5_file1)\n\n# Access the \"Raw_Data\" dataset from its absolute path\nh5_raw1 = h5_file1['Measurement_000/Channel_000/Raw_Data']\nprint('h5_raw1: ', h5_raw1)\n\n# We can get to the same dataset through relative paths:\n\n# Access the Measurement_000 group first\nh5_meas_grp = h5_file1['Measurement_000']\nprint('h5_meas_grp:', h5_meas_grp)\n\n# Now we can access the \"Channel_000\" group via the h5_meas_grp object\nh5_chan_grp = h5_meas_grp['Channel_000']\n\n# And finally, the same raw dataset can be accessed as:\nh5_raw1_alias_1 = h5_chan_grp['Raw_Data']\nprint('h5_raw1_alias_1:', h5_raw1_alias_1)\n\n# Another way to get this dataset is via functions written in pycroscopy:\nh5_dsets = px.hdf_utils.getDataSet(h5_file1, 'Raw_Data')\nprint('h5_dsets:', h5_dsets)\n\n# In this case, there is only a single Raw_Data, so we an access it simply as:\nh5_raw1_alias_2 = h5_dsets[0]\nprint('h5_raw1_alias_2:', h5_raw1_alias_2)\n\n# Let's just check to see if these are indeed aliases of the same dataset:\nprint('All aliases of the same dataset?', h5_raw1 == h5_raw1_alias_1 and h5_raw1 == h5_raw1_alias_2)\n\n# Let's close this file\nh5_file1.close()\n\n# Load the dataset with pycroscopy\nhdf = px.ioHDF5(h5_path)\n\n# Getting the same h5py handle to the file:\nh5_file2 = hdf.file\n\nh5_raw2 = h5_file2['Measurement_000/Channel_000/Raw_Data']\nprint('h5_raw2:', h5_raw2)\n\nh5_file2.close()\n\n# Delete the temporarily downloaded h5 file:\nremove(h5_path)"
]
}
],
"nbformat_minor": 0,
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"name": "python3",
"language": "python"
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"file_extension": ".py",
"pygments_lexer": "ipython3",
"version": "3.5.2",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"name": "python"
}
},
"nbformat": 4
"nbformat": 4,
"nbformat_minor": 0
}
\ No newline at end of file
{
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"name": "python3",
"language": "python"
},
"language_info": {
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"mimetype": "text/x-python",
"name": "python",
"file_extension": ".py",
"version": "3.5.2",
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python"
}
},
"cells": [
{
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"%matplotlib inline"
]
......@@ -23,8 +42,8 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"# Code source: Chris Smith -- cq6@ornl.gov\n# Liscense: MIT\n\nimport numpy as np\nimport pycroscopy as px"
]
......@@ -41,8 +60,8 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"# First create some data\ndata1 = np.random.rand(5, 7)"
]
......@@ -59,8 +78,8 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"ds_main = px.MicroDataset('Main_Data', data=data1, parent='/')"
]
......@@ -77,8 +96,8 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"ds_empty = px.MicroDataset('Empty_Data', data=[], dtype=np.float32, maxshape=[7, 5, 3])"
]
......@@ -95,8 +114,8 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"data_group = px.MicroDataGroup('Data_Group', parent='/')\n\nroot_group = px.MicroDataGroup('/')\n\n# After creating the group, we then add an existing object as its child.\ndata_group.addChildren([ds_empty])\nroot_group.addChildren([ds_main, data_group])"
]
......@@ -113,8 +132,8 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"root_group.showTree()"
]
......@@ -131,8 +150,8 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"# First we specify the path to the file\nh5_path = 'microdata_test.h5'\n\n# Then we use the ioHDF5 class to build the file from our objects.\nhdf = px.ioHDF5(h5_path)"
]
......@@ -149,8 +168,8 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"h5_refs = hdf.writeData(root_group, print_log=True)\n\n# We can use these references to get the h5py dataset and group objects\nh5_main = px.io.hdf_utils.getH5DsetRefs(['Main_Data'], h5_refs)[0]\nh5_empty = px.io.hdf_utils.getH5DsetRefs(['Empty_Data'], h5_refs)[0]"
]
......@@ -167,8 +186,8 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"print(np.allclose(h5_main[()], data1))"
]
......@@ -185,8 +204,8 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"data2 = np.random.rand(*h5_empty.shape)\nh5_empty[:] = data2[:]"
]
......@@ -203,8 +222,8 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"h5_file = hdf.file\nh5_file.flush()"
]
......@@ -221,32 +240,13 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"h5_file.close()"
]
}
],
"nbformat_minor": 0,
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"name": "python3",
"language": "python"
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"file_extension": ".py",
"pygments_lexer": "ipython3",
"version": "3.5.2",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"name": "python"
}
},
"nbformat": 4
"nbformat": 4,
"nbformat_minor": 0
}
\ No newline at end of file
{
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"name": "python3",
"language": "python"
},
"language_info": {
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"mimetype": "text/x-python",
"name": "python",
"file_extension": ".py",
"version": "3.5.2",
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python"
}
},
"cells": [
{
"metadata": {
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"%matplotlib inline"
]
......@@ -23,8 +42,8 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"#Import packages\n\n# Ensure that this code works on both python 2 and python 3\nfrom __future__ import division, print_function, absolute_import, unicode_literals\n\n# basic numeric computation:\nimport numpy as np\n\n# The package used for creating and manipulating HDF5 files:\nimport h5py\n\n# Plotting and visualization:\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.axes_grid1 import make_axes_locatable\n\n# for downloading files:\nimport wget\nimport os\n\n# multivariate analysis:\nfrom sklearn.cluster import KMeans\nfrom sklearn.decomposition import NMF\nfrom pysptools import eea\nimport pysptools.abundance_maps as amp\nfrom pysptools.eea import nfindr\n\n# finally import pycroscopy:\nimport pycroscopy as px\n\n\"\"\"\n \n\"\"\""
]
......@@ -41,8 +60,8 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"data_file_path = 'temp_um.h5'\n# download the data file from Github:\nurl = 'https://raw.githubusercontent.com/pycroscopy/pycroscopy/master/data/BELine_0004.h5'\n_ = wget.download(url, data_file_path, bar=None)\n\nhdf = px.ioHDF5(data_file_path)\nh5_file = hdf.file\n\nprint('Contents of data file:')\nprint('----------------------')\npx.hdf_utils.print_tree(h5_file)\nprint('----------------------')\n\nh5_meas_grp = h5_file['Measurement_000']\n\n# Extracting some basic parameters:\nnum_rows = px.hdf_utils.get_attr(h5_meas_grp,'grid_num_rows')\nnum_cols = px.hdf_utils.get_attr(h5_meas_grp,'grid_num_cols')\n\n# Getting a reference to the main dataset:\nh5_main = h5_meas_grp['Channel_000/Raw_Data']\n\n# Extracting the X axis - vector of frequencies\nh5_spec_vals = px.hdf_utils.getAuxData(h5_main,'Spectroscopic_Values')[-1]\nfreq_vec = np.squeeze(h5_spec_vals.value) * 1E-3\n\nprint('Data currently of shape:', h5_main.shape)\n\nx_label = 'Frequency (kHz)'\ny_label = 'Amplitude (a.u.)'"
]
......@@ -59,8 +78,8 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"px.viz.be_viz_utils.jupyter_visualize_be_spectrograms(h5_main)"
]
......@@ -77,8 +96,8 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"h5_svd_group = px.doSVD(h5_main, num_comps=256)\n\nh5_u = h5_svd_group['U']\nh5_v = h5_svd_group['V']\nh5_s = h5_svd_group['S']\n\n# Since the two spatial dimensions (x, y) have been collapsed to one, we need to reshape the abundance maps:\nabun_maps = np.reshape(h5_u[:,:25], (num_rows, num_cols, -1))\n\n# Visualize the variance / statistical importance of each component:\npx.plot_utils.plotScree(h5_s, title='Note the exponential drop of variance with number of components')\n\n# Visualize the eigenvectors:\nfirst_evecs = h5_v[:9, :]\n\npx.plot_utils.plot_loops(freq_vec, np.abs(first_evecs), x_label=x_label, y_label=y_label, plots_on_side=3,\n subtitles='Component', title='SVD Eigenvectors (Amplitude)', evenly_spaced=False)\npx.plot_utils.plot_loops(freq_vec, np.angle(first_evecs), x_label=x_label, y_label='Phase (rad)', plots_on_side=3,\n subtitles='Component', title='SVD Eigenvectors (Phase)', evenly_spaced=False)\n\n# Visualize the abundance maps:\npx.plot_utils.plot_map_stack(abun_maps, num_comps=9, heading='SVD Abundance Maps',\n color_bar_mode='single', cmap='inferno')"
]
......@@ -95,8 +114,8 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"num_clusters = 4\n\nestimators = px.Cluster(h5_main, 'KMeans', n_clusters=num_clusters)\nh5_kmeans_grp = estimators.do_cluster(h5_main)\nh5_kmeans_labels = h5_kmeans_grp['Labels']\nh5_kmeans_mean_resp = h5_kmeans_grp['Mean_Response']\n\npx.plot_utils.plot_cluster_h5_group(h5_kmeans_grp)"
]
......@@ -113,8 +132,8 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"num_comps = 4\n\n# get the non-negative portion of the dataset\ndata_mat = np.abs(h5_main)\n\nmodel = NMF(n_components=num_comps, init='random', random_state=0)\nmodel.fit(data_mat)\n\nfig, axis = plt.subplots(figsize=(5.5, 5))\npx.plot_utils.plot_line_family(axis, freq_vec, model.components_, label_prefix='NMF Component #')\naxis.set_xlabel(x_label, fontsize=12)\naxis.set_ylabel(y_label, fontsize=12)\naxis.set_title('NMF Components', fontsize=14)\naxis.legend(bbox_to_anchor=[1.0, 1.0], fontsize=12)"
]
......@@ -131,8 +150,8 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"num_comps = 4\n\n# get the amplitude component of the dataset\ndata_mat = np.abs(h5_main)\n\nnfindr_results = eea.nfindr.NFINDR(data_mat, num_comps) #Find endmembers\nend_members = nfindr_results[0]\n\nfig, axis = plt.subplots(figsize=(5.5, 5))\npx.plot_utils.plot_line_family(axis, freq_vec, end_members, label_prefix='NFINDR endmember #')\naxis.set_title('NFINDR Endmembers', fontsize=14)\naxis.set_xlabel(x_label, fontsize=12)\naxis.set_ylabel(y_label, fontsize=12)\naxis.legend(bbox_to_anchor=[1.0,1.0], fontsize=12)\n\n# fully constrained least squares model:\nfcls = amp.FCLS()\n# Find abundances:\namap = fcls.map(data_mat[np.newaxis, :, :], end_members)\n\n# Reshaping amap\namap = np.reshape(np.squeeze(amap), (num_rows, num_cols, -1))\n\npx.plot_utils.plot_map_stack(amap, heading='NFINDR Abundance maps', cmap=plt.cm.inferno,\n color_bar_mode='single');"
]
......@@ -142,32 +161,13 @@
"collapsed": false
},
"outputs": [],
"execution_count": null,
"cell_type": "code",
"execution_count": null,
"source": [
"# Close and delete the h5_file\nh5_file.close()\nos.remove(data_file_path)"
]
}
],
"nbformat_minor": 0,
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"name": "python3",
"language": "python"
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"file_extension": ".py",
"pygments_lexer": "ipython3",
"version": "3.5.2",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"name": "python"
}
},
"nbformat": 4
"nbformat": 4,
"nbformat_minor": 0
}
\ No newline at end of file
......@@ -218,45 +218,29 @@ print('Positions:', pos_dim_sizes, '\nSpectroscopic:', spec_dim_sizes)
# Let's assume that we are interested in visualizing the spectrograms at the first field of the second cycle at
# position - row:3 and column 2. There are two ways of accessing the data:
#
# 1. The easier method - reshape the data to N dimensions and slice the dataset
# 1. The hard method - find the spectroscopic and position indices of interest and slice the 2D dataset
#
# * This approach, while trivial, may not be suitable for large datasets which may or may not fit in memory
# 2. The easier method - reshape the data to N dimensions and slice the dataset
#
# 2. The harder method - find the spectroscopic and position indices of interest and slice the 2D dataset
# * This approach, while easy, may not be suitable for large datasets which may or may not fit in memory
#
# 3. The easiest method - use the PycroDataset class to slice the data
#
# * This method will only work for ``main`` datasets. We recommend using method 2 for slicing all others.
#
# Approach 1 - N-dimensional form
# -------------------------------
# We will use convenient pycroscopy function that safely reshapes the data to its N dimensional form with a single
# line. Note that while this approach appears simple on the surface, there are a fair number of lines of code that
# make up this function.
ds_nd, success, labels = px.hdf_utils.reshape_to_Ndims(h5_main, get_labels=True)
print('Shape of the N-dimensional dataset:', ds_nd.shape)
print(labels)
#########################################################################
# Now that we have the data in its original N dimensional form, we can easily slice the dataset:
spectrogram = ds_nd[2, 3, :, 0, :, 1]
# Now the spectrogram is of order (frequency x DC_Offset).
spectrogram = spectrogram.T
# Now the spectrogram is of order (DC_Offset x frequency)
fig, axis = plt. subplots()
axis.imshow(np.abs(spectrogram), origin='lower')
axis.set_xlabel('Frequency Index')
axis.set_ylabel('DC Offset Index')
axis.set_title('Spectrogram Amplitude');
#########################################################################
# Approach 2 - slicing the 2D matrix
# Approach 1 - slicing the 2D matrix
# ----------------------------------
#
# This approach is more hands-on and requires that we be very careful with the indexing and slicing. Nonetheless,
# This approach is hands-on and requires that we be very careful with the indexing and slicing. Nonetheless,
# the process is actually fairly intuitive. We rely entirely upon the spectroscopic and position ancillary datasets
# to find the indices for slicing the dataset. Unlike the main dataset, the ancillary datasets are very small and
# can be stored easily in memory. Once the slicing indices are calculated, we __only read the desired portion of
# `main` data to memory__. Thus the amount of data loaded into memory is only the amount that we absolutely need.
# __This is the only approach that can be applied to slice very large datasets without ovwhelming memory overheads__.
# The comments for each line explain the entire process comprehensively
#
# Get only the spectroscopic dimension names:
spec_dim_names = px.hdf_utils.get_attr(h5_spec_ind, 'labels')
......@@ -338,12 +322,35 @@ else:
print('We need to reshape the vector by the tuple:', spectrogram_shape)
# Reshaping from 1D to 2D:
spectrogram2 = np.reshape(np.squeeze(data_vec), spectrogram_shape)
spectrogram = np.reshape(np.squeeze(data_vec), spectrogram_shape)
#########################################################################
# Now that the spectrogram is indeed two dimensional, we can visualize it. This plot should match the one from the first
# approach.
# Now the spectrogram is of order (DC_Offset x frequency)
fig, axis = plt. subplots()
axis.imshow(np.abs(spectrogram), origin='lower')
axis.set_xlabel('Frequency Index')
axis.set_ylabel('DC Offset Index')
axis.set_title('Spectrogram Amplitude')
#########################################################################
# Approach 2 - N-dimensional form
# -------------------------------
# We will use convenient pycroscopy function that safely reshapes the data to its N dimensional form with a single
# line. Note that while this approach appears simple on the surface, there are a fair number of lines of code that
# make up this function.
ds_nd, success, labels = px.hdf_utils.reshape_to_Ndims(h5_main, get_labels=True)
print('Shape of the N-dimensional dataset:', ds_nd.shape)
print(labels)
#########################################################################
# Now that we have the data in its original N dimensional form, we can easily slice the dataset:
spectrogram2 = ds_nd[2, 3, :, :, 0, 1]
# Now the spectrogram is of order (frequency x DC_Offset).
spectrogram2 = spectrogram2.T
# Now the spectrogram is of order (DC_Offset x frequency)
fig, axis = plt. subplots()
axis.imshow(np.abs(spectrogram2), origin='lower')
......@@ -352,6 +359,46 @@ axis.set_ylabel('DC Offset Index')
axis.set_title('Spectrogram Amplitude');
#########################################################################
# Approach 3 - Using the PycroDataset
# -----------------------------------
# We will use the new PycroDataset class to create an N dimensional slice directly from the two dimensional
# data in the file.
#
# First we convert from an HDF5 Dataset to a PycroDataset
pd_main = px.PycroDataset(h5_main)
print(pd_main.shape)
#########################################################################
# As you can see, the data is still two dimensional. The PycroDataset has several attributes that will help with
# the slicing.
#
# Let's check the names and sizes of each dimension
print(pd_main.n_dim_labels)
print(pd_main.n_dim_sizes)
#########################################################################
# With this information, we can now get our data slice.
#
slice_dict = dict(X=[2], Y=[3], Field=[0], Cycle=[1])
nd_spec, success = pd_main.slice(**slice_dict)
print(success)
print(nd_spec.shape)
#########################################################################
# The slice is returned already in the N dimensional form. We just need to remove all the
# dimensions with length one, transpose it like in method 2, and plot.
#
spectrogram3 = nd_spec.squeeze().T
# Now the spectrogram is of order (DC_Offset x frequency)
fig, axis = plt. subplots()
axis.imshow(np.abs(spectrogram3), origin='lower')
axis.set_xlabel('Frequency Index')
axis.set_ylabel('DC Offset Index')
axis.set_title('Spectrogram Amplitude');
# Close and delete the h5_file
h5_file.close()
......
5bdff36c42774593e0b5e5bedbcee2cf
\ No newline at end of file
7b9e608c44816efc3e84fb6ee3b4bece
\ No newline at end of file
......@@ -396,68 +396,22 @@ Slicing the Main dataset
Let's assume that we are interested in visualizing the spectrograms at the first field of the second cycle at
position - row:3 and column 2. There are two ways of accessing the data:
1. The easier method - reshape the data to N dimensions and slice the dataset
1. The hard method - find the spectroscopic and position indices of interest and slice the 2D dataset
* This approach, while trivial, may not be suitable for large datasets which may or may not fit in memory
2. The easier method - reshape the data to N dimensions and slice the dataset
2. The harder method - find the spectroscopic and position indices of interest and slice the 2D dataset
Approach 1 - N-dimensional form
-------------------------------
We will use convenient pycroscopy function that safely reshapes the data to its N dimensional form with a single
line. Note that while this approach appears simple on the surface, there are a fair number of lines of code that
make up this function.
.. code-block:: python
ds_nd, success, labels = px.hdf_utils.reshape_to_Ndims(h5_main, get_labels=True)
print('Shape of the N-dimensional dataset:', ds_nd.shape)
print(labels)
.. rst-class:: sphx-glr-script-out
Out::
Shape of the N-dimensional dataset: (5, 5, 87, 64, 2, 2)
['X' 'Y' 'Frequency' 'DC_Offset' 'Field' 'Cycle']
Now that we have the data in its original N dimensional form, we can easily slice the dataset:
.. code-block:: python
spectrogram = ds_nd[2, 3, :, 0, :, 1]
# Now the spectrogram is of order (frequency x DC_Offset).
spectrogram = spectrogram.T
# Now the spectrogram is of order (DC_Offset x frequency)
fig, axis = plt. subplots()
axis.imshow(np.abs(spectrogram), origin='lower')
axis.set_xlabel('Frequency Index')
axis.set_ylabel('DC Offset Index')
axis.set_title('Spectrogram Amplitude');
* This approach, while easy, may not be suitable for large datasets which may or may not fit in memory
3. The easiest method - use the PycroDataset class to slice the data