|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 2, |
| 6 | + "id": "d9e1079f", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "import os\n", |
| 11 | + "import numpy as np\n", |
| 12 | + "import matplotlib.pyplot as plt\n", |
| 13 | + "import sidpy as sid\n", |
| 14 | + "import pyNSID as nsid\n", |
| 15 | + "import h5py" |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": null, |
| 21 | + "id": "4c562c94", |
| 22 | + "metadata": {}, |
| 23 | + "outputs": [], |
| 24 | + "source": [ |
| 25 | + "def axis_generator(start_letter='i', prefix='axis_'):\n", |
| 26 | + " \"\"\"\n", |
| 27 | + " Generates strings with prefix + letters from start_letter to 'z'.\n", |
| 28 | + " \n", |
| 29 | + " Args:\n", |
| 30 | + " start_letter (str): starting letter (default 'i')\n", |
| 31 | + " prefix (str): string prefix (default 'axis_')\n", |
| 32 | + " \n", |
| 33 | + " Yields:\n", |
| 34 | + " str: e.g. \"axis_i\", \"axis_j\", ...\n", |
| 35 | + " \"\"\"\n", |
| 36 | + " start_ord = ord(start_letter.lower())\n", |
| 37 | + " end_ord = ord('z')\n", |
| 38 | + " \n", |
| 39 | + " for c in range(start_ord, end_ord + 1):\n", |
| 40 | + " yield prefix + chr(c)" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": 6, |
| 46 | + "id": "f51806e7", |
| 47 | + "metadata": {}, |
| 48 | + "outputs": [ |
| 49 | + { |
| 50 | + "name": "stdout", |
| 51 | + "output_type": "stream", |
| 52 | + "text": [ |
| 53 | + "(50, 2, 80, 128)\n", |
| 54 | + "(50, 80, 128)\n" |
| 55 | + ] |
| 56 | + } |
| 57 | + ], |
| 58 | + "source": [ |
| 59 | + "all_results = np.load('all_results.npy')\n", |
| 60 | + "\n", |
| 61 | + "amp_mat = all_results[:,0,:,:]\n", |
| 62 | + "phase_mat = all_results[:,1,:,:]\n", |
| 63 | + "pr_mat = amp_mat*np.cos(phase_mat)\n", |
| 64 | + "\n", |
| 65 | + "print(pr_mat.shape) #line_number, time step, x\n", |
| 66 | + "#Make sidpy dataset from the amplitude, phase and PR mats\n", |
| 67 | + "\n", |
| 68 | + "pr_mat_sid = sid.Dataset.from_array(pr_mat, name='pr_mat')\n", |
| 69 | + "amp_mat_sid = sid.Dataset.from_array(amp_mat, name='amp_mat')\n", |
| 70 | + "phase_mat_sid = sid.Dataset.from_array(phase_mat, name='phase_mat')\n", |
| 71 | + "\n", |
| 72 | + "#make the dimension vectors\n", |
| 73 | + "line_vector = 2.0E-6*np.linspace(-1,1,pr_mat.shape[0]) #position in y for the line\n", |
| 74 | + "time_step_vector = np.arange(pr_mat.shape[1])\n", |
| 75 | + "x_vector = np.linspace(0, 2.0E-6, pr_mat.shape[2])\n", |
| 76 | + "\n", |
| 77 | + "for sid_dset in [pr_mat_sid, amp_mat_sid, phase_mat_sid]:\n", |
| 78 | + " sid_dset.data_type = 'spectral_image' # supported\n", |
| 79 | + " sid_dset.units = 'a.u.'\n", |
| 80 | + " sid_dset.quantity = 'Piezoresponse'\n", |
| 81 | + " sid_dset.set_dimension(0, sid.Dimension(line_vector,\n", |
| 82 | + " name='y', units='um', quantity='Width',\n", |
| 83 | + " dimension_type='spatial'))\n", |
| 84 | + " sid_dset.set_dimension(1, sid.Dimension(time_step_vector,\n", |
| 85 | + " 'time', units='Time Step', quantity='Time',\n", |
| 86 | + " dimension_type='spectral'))\n", |
| 87 | + " sid_dset.set_dimension(2, sid.Dimension(x_vector,\n", |
| 88 | + " 'x', units='um', quantity = 'Length', dimension_type='spatial' ))" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": null, |
| 94 | + "id": "8aa44d48", |
| 95 | + "metadata": {}, |
| 96 | + "outputs": [], |
| 97 | + "source": [ |
| 98 | + "data = {'piezoresponse': pr_mat_sid}\n", |
| 99 | + "h5_name = 'test_h5_file2.h5'\n", |
| 100 | + "\n", |
| 101 | + "with h5py.File(h5_name, 'w') as h5_f:\n", |
| 102 | + " h5_group = h5_f.create_group('Measurement_Nexus')\n", |
| 103 | + " my_str = \"NXsidpy\"\n", |
| 104 | + " def_dset = h5_group.create_dataset('definition', data=my_str)\n", |
| 105 | + " \n", |
| 106 | + " for key in list(data.keys()):\n", |
| 107 | + " h5_g = h5_group.create_group(key)\n", |
| 108 | + " nsid.hdf_io.write_nsid_dataset(data[key], h5_g)\n", |
| 109 | + " " |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "code", |
| 114 | + "execution_count": null, |
| 115 | + "id": "5ac219bb", |
| 116 | + "metadata": {}, |
| 117 | + "outputs": [], |
| 118 | + "source": [ |
| 119 | + "h5_g = h5_f['Measurement_Nexus/piezoresponse/generic']\n", |
| 120 | + "dset_dims = []\n", |
| 121 | + "[dset_dims.append(dim) for dim in h5_g if dim !='generic']\n", |
| 122 | + "print(dset_dims)\n", |
| 123 | + "\n", |
| 124 | + "# Example usage:\n", |
| 125 | + "gen = axis_generator()\n", |
| 126 | + "axis_ij_names = []\n", |
| 127 | + "for ind,dset in enumerate(dset_dims):\n", |
| 128 | + " axis_name = next(gen)\n", |
| 129 | + " axis_ij_names.append(axis_name)\n", |
| 130 | + " h5_g.copy(dset, axis_name)\n", |
| 131 | + "\n", |
| 132 | + "axes_refs = np.array(\n", |
| 133 | + " axis_ij_names,\n", |
| 134 | + " dtype='object'\n", |
| 135 | + " )\n", |
| 136 | + "h5_g.attrs[\"NX_class\"] = \"NXdata\"\n", |
| 137 | + "h5_g.attrs[\"axes\"] = axes_refs\n", |
| 138 | + "\n", |
| 139 | + "for ind in range(len(axis_ij_names)):\n", |
| 140 | + " lab = axis_ij_names[ind]+\"_indices\"\n", |
| 141 | + " h5_g.attrs[lab]=ind\n", |
| 142 | + "\n", |
| 143 | + "for ind,axis in enumerate(axis_ij_names):\n", |
| 144 | + " h5_g[axis].attrs[\"units\"] = h5_g[dset_dims[ind]].attrs['units']\n", |
| 145 | + " h5_g[axis].attrs[\"long_name\"] = h5_g[dset_dims[ind]].attrs['name']\n", |
| 146 | + "\n", |
| 147 | + "h5_g.attrs[\"default\"] = \"generic\"\n", |
| 148 | + "h5_f.attrs[\"default\"] = key\n", |
| 149 | + " \n", |
| 150 | + "del h5_f['Measurement_Nexus'][key]['generic']['generic'].attrs[\"DIMENSION_LIST\"] #delete dimension list attribute\n" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "markdown", |
| 155 | + "id": "000e0ce6", |
| 156 | + "metadata": {}, |
| 157 | + "source": [ |
| 158 | + "#Reminder:\n", |
| 159 | + "1. pyNSID shoudl only write to a Nexus compatible file format\n", |
| 160 | + "2. When we read the file back, we shoudl read it back as a sidpy dataset format. We need to make sure to be able to read back files that are NSID-NExus compatible and the older (standard) NSID format." |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": null, |
| 166 | + "id": "c44123ae", |
| 167 | + "metadata": {}, |
| 168 | + "outputs": [], |
| 169 | + "source": [] |
| 170 | + } |
| 171 | + ], |
| 172 | + "metadata": { |
| 173 | + "kernelspec": { |
| 174 | + "display_name": "Python 3 (ipykernel)", |
| 175 | + "language": "python", |
| 176 | + "name": "python3" |
| 177 | + }, |
| 178 | + "language_info": { |
| 179 | + "codemirror_mode": { |
| 180 | + "name": "ipython", |
| 181 | + "version": 3 |
| 182 | + }, |
| 183 | + "file_extension": ".py", |
| 184 | + "mimetype": "text/x-python", |
| 185 | + "name": "python", |
| 186 | + "nbconvert_exporter": "python", |
| 187 | + "pygments_lexer": "ipython3", |
| 188 | + "version": "3.11.4" |
| 189 | + } |
| 190 | + }, |
| 191 | + "nbformat": 4, |
| 192 | + "nbformat_minor": 5 |
| 193 | +} |
0 commit comments