BE_Processing.ipynb 23 KB
Newer Older
1
{
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "# Band Excitation data procesing\n",
    "### Suhas Somnath, Chris R. Smith, Stephen Jesse\n",
    "The Center for Nanophase Materials Science and The Institute for Functional Imaging for Materials <br>\n",
    "Oak Ridge National Laboratory<br>\n",
    "4/6/2020\n",
    "\n",
    "### Reference:\n",
    "This Jupyter notebook uses [pycroscopy](https://pycroscopy.github.io/pycroscopy/about.html) to analyze Band Excitation data. We request you to reference the [Arxiv paper](https://arxiv.org/abs/1903.09515) titled \"*USID and Pycroscopy - Open frameworks for storing and analyzing spectroscopic and imaging data*\" in your publications.  \n",
    "\n",
    "#### Jupyter Notebooks:\n",
    "This is a Jupyter Notebook - it contains text and executable code `cells`. To learn more about how to use it, please see [this video](https://www.youtube.com/watch?v=jZ952vChhuI). Please see the image below for some basic tips on using this notebook.\n",
    "\n",
    "If you have any questions or need help running this notebook, please get in touch with your host if you are a users at the Center for Nanophase Materials Science (CNMS) or our [google group](https://groups.google.com/forum/#!forum/pycroscopy).\n",
    "\n",
    "![notebook_rules.png](https://raw.githubusercontent.com/pycroscopy/pycroscopy/master/jupyter_notebooks/notebook_rules.png)\n",
    "\n",
    "Image courtesy of Jean Bilheux from the [neutron imaging](https://github.com/neutronimaging/python_notebooks) GitHub repository."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "## Configure the notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "# Make sure needed packages are installed and up-to-date\n",
    "import sys\n",
    "!conda install --yes --prefix {sys.prefix} numpy scipy matplotlib scikit-learn Ipython ipywidgets h5py\n",
48
    "!{sys.executable} -m pip install -U --no-deps pycroscopy  # this will automatically install sidpy and pyUSID as well"
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "# Ensure python 3 compatibility\n",
    "from __future__ import division, print_function, absolute_import\n",
    "\n",
    "# Import necessary libraries:\n",
    "# General utilities:\n",
    "import os\n",
    "\n",
    "# Computation:\n",
    "import numpy as np\n",
    "import h5py\n",
    "\n",
    "# Visualization:\n",
    "import matplotlib.pyplot as plt\n",
    "from IPython.display import display, HTML\n",
    "\n",
    "# The engineering components supporting pycroscopy:\n",
ramav87's avatar
ramav87 committed
75
    "import sidpy\n",
76
77
78
    "import pyUSID as usid\n",
    "# Finally, pycroscopy itself\n",
    "import pycroscopy as px\n",
ramav87's avatar
ramav87 committed
79
   
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
    "\n",
    "# Make Notebook take up most of page width\n",
    "display(HTML(data=\"\"\"\n",
    "<style>\n",
    "    div#notebook-container    { width: 95%; }\n",
    "    div#menubar-container     { width: 65%; }\n",
    "    div#maintoolbar-container { width: 99%; }\n",
    "</style>\n",
    "\"\"\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "# set up notebook to show plots within the notebook\n",
    "%matplotlib notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "## Set some basic preferences\n",
    "This notebook performs some functional fitting whose duration can be substantially decreased by using more memory and CPU cores. We have provided default values below but you may choose to change them if necessary. Setting `max_cores` to `None` will allow usage of all but one CPU core for the computations. \n",
    "\n",
    "By default, results of the functional fitting will be written back to the same HDF5 file. However, if you prefer to write results into different HDF5 files, please set the `results_to_new_file` parameter to `True` instead. Users of the [DataFed](https://datafed.ornl.gov) Scientific Data Management System may want to set this parameter to `True`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "max_mem         = 1024*8  # Maximum memory to use, in Mbs. Default = 1024\n",
    "max_cores       = None    # Number of logical cores to use in fitting.  None uses all but 2 available cores.\n",
125
    "results_to_new_file = False" #Note that if you set this to True, visualization is unlikely to work!
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "## Make the data pycroscopy compatible\n",
    "Converting the raw data into a pycroscopy compatible hierarchical data format (HDF or .h5) file gives you access to the fast fitting algorithms and powerful analysis functions within pycroscopy\n",
    "\n",
    "#### H5 files:\n",
    "* are like smart containers that can store matrices with data, folders to organize these datasets, images, metadata like experimental parameters, links or shortcuts to datasets, etc.\n",
    "* are readily compatible with high-performance computing facilities\n",
    "* scale very efficiently from few kilobytes to several terabytes\n",
    "* can be read and modified using any language including Python, Matlab, C/C++, Java, Fortran, Igor Pro, etc.\n",
    "\n",
    "#### You can load either of the following:\n",
    "* Any .mat or .txt parameter file from the original experiment\n",
    "* A .h5 file generated from the raw data using pycroscopy - skips translation\n",
    "\n",
    "You can select desired file type by choosing the second option in the pull down menu on the bottom right of the file window"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {},
    "scrolled": false
   },
   "outputs": [],
   "source": [
159
    "input_file_path = 'my_file.h5' #put the path to your file here,
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    "\n",
    "(data_dir, filename) = os.path.split(input_file_path)\n",
    "\n",
    "if input_file_path.endswith('.h5'):\n",
    "    # No translation here\n",
    "    h5_path = input_file_path\n",
    "    force = True # Set this to true to force patching of the datafile.\n",
    "    tl = px.io.translators.LabViewH5Patcher()\n",
    "    tl.translate(h5_path, force_patch=force)\n",
    "else:\n",
    "    # Set the data to be translated\n",
    "    data_path = input_file_path\n",
    "\n",
    "    (junk, base_name) = os.path.split(data_dir)\n",
    "\n",
    "    # Check if the data is in the new or old format.  Initialize the correct translator for the format.\n",
    "    if base_name == 'newdataformat':\n",
    "        (junk, base_name) = os.path.split(junk)\n",
    "        translator = px.io.translators.BEPSndfTranslator(max_mem_mb=max_mem)\n",
    "    else:\n",
    "        translator = px.io.translators.BEodfTranslator(max_mem_mb=max_mem)\n",
    "    if base_name.endswith('_d'):\n",
    "        base_name = base_name[:-2]\n",
    "    # Translate the data\n",
    "    h5_path = translator.translate(data_path, show_plots=True, save_plots=False)\n",
    "\n",
    "folder_path, h5_raw_file_name = os.path.split(h5_path)\n",
    "h5_file = h5py.File(h5_path, 'r+')\n",
    "print('Working on:\\n' + h5_path)\n",
    "\n",
    "h5_main = usid.hdf_utils.find_dataset(h5_file, 'Raw_Data')[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "##### Inspect the contents of this h5 data file\n",
    "The file contents are stored in a tree structure, just like files on a conventional computer.\n",
    "The data is stored as a 2D matrix (position, spectroscopic value) regardless of the dimensionality of the data. Thus, the positions will be arranged as row0-col0, row0-col1.... row0-colN, row1-col0.... and the data for each position is stored as it was chronologically collected  \n",
    "\n",
    "The main dataset is always accompanied by four ancillary datasets that explain the position and spectroscopic value of any given element in the dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {},
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "print('Datasets and datagroups within the file:\\n------------------------------------')\n",
216
    "sidpy.hdf.hdf_utils.print_tree(h5_file)\n",
217
218
219
220
    " \n",
    "print('\\nThe main dataset:\\n------------------------------------')\n",
    "print(h5_main)\n",
    "\n",
221
    "print('\\nMetadata or attributes in the measurement datagroup\\n------------------------------------')\n",
222
    "for key, val in sidpy.hdf.hdf_utils.get_attributes(h5_main.parent.parent).items():\n",
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
    "    print('{} : {}'.format(key, val))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "## Get some basic parameters from the H5 file\n",
    "This information will be vital for futher analysis and visualization of the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "h5_pos_inds = h5_main.h5_pos_inds\n",
    "pos_dims = h5_main.pos_dim_sizes\n",
    "pos_labels = h5_main.pos_dim_labels\n",
    "print(pos_labels, pos_dims)\n",
    "\n",
249
250
    "h5_meas_grp = h5_main.parent.parent\n",
    "\n",
251
    "parm_dict = sidpy.hdf.hdf_utils.get_attributes(h5_meas_grp)\n",
252
    "\n",
253
    "expt_type = sidpy.hdf.hdf_utils.get_attr(h5_file, 'data_type')\n",
254
255
    "\n",
    "is_ckpfm = expt_type == 'cKPFMData'\n",
256
257
258
    "if is_ckpfm:\n",
    "    num_write_steps = parm_dict['VS_num_DC_write_steps']\n",
    "    num_read_steps = parm_dict['VS_num_read_steps']\n",
259
260
261
    "    num_fields = 2\n",
    "    \n",
    "if expt_type != 'BELineData':\n",
262
    "    vs_mode = sidpy.hdf.hdf_utils.get_attr(h5_meas_grp, 'VS_mode')\n",
263
    "    try:\n",
264
    "        field_mode = sidpy.hdf.hdf_utils.get_attr(h5_meas_grp, 'VS_measure_in_field_loops')\n",
265
266
267
268
    "    except KeyError:\n",
    "        print('field mode could not be found. Setting to default value')\n",
    "        field_mode = 'out-of-field'\n",
    "    try:\n",
269
    "        vs_cycle_frac = sidpy.hdf.hdf_utils.get_attr(h5_meas_grp, 'VS_cycle_fraction')\n",
270
271
272
    "    except KeyError:\n",
    "        print('VS cycle fraction could not be found. Setting to default value')\n",
    "        vs_cycle_frac = 'full'"
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "## Visualize the raw data\n",
    "Use the sliders below to visualize spatial maps (2D only for now), and spectrograms.\n",
    "For simplicity, all the spectroscopic dimensions such as frequency, excitation bias, cycle, field, etc. have been collapsed to a single slider."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {},
291
    "scrolled": true
292
293
294
295
296
297
   },
   "outputs": [],
   "source": [
    "fig = px.viz.be_viz_utils.jupyter_visualize_be_spectrograms(h5_main)"
   ]
  },
ssomnath's avatar
ssomnath committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fit the Band Excitation (BE) spectra\n",
    "Fit each of the acquired spectra to a simple harmonic oscillator (SHO) model to extract the following information regarding the response:\n",
    "* Oscillation amplitude\n",
    "* Phase\n",
    "* Resonance frequency\n",
    "* Quality factor\n",
    "\n",
    "By default, the cell below will take any previous result instead of re-computing the SHO fit"
   ]
  },
312
313
314
315
316
317
318
319
320
321
322
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "sho_fit_points = 5  # The number of data points at each step to use when fitting\n",
    "sho_override = False  # Force recompute if True\n",
    "\n",
323
    "h5_sho_targ_grp = None\n",
324
325
326
327
328
329
330
331
    "if results_to_new_file:\n",
    "    h5_sho_file_path = os.path.join(folder_path, \n",
    "                                h5_raw_file_name.replace('.h5', '_sho_fit.h5'))\n",
    "    print('\\n\\nSHO Fits will be written to:\\n' + h5_sho_file_path + '\\n\\n')\n",
    "    f_open_mode = 'w'\n",
    "    if os.path.exists(h5_sho_file_path):\n",
    "        f_open_mode = 'r+'\n",
    "    h5_sho_file = h5py.File(h5_sho_file_path, mode=f_open_mode)\n",
332
    "    h5_sho_targ_grp = h5_sho_file\n",
333
    "    \n",
334
    "sho_fitter = px.analysis.BESHOfitter(h5_main, cores=max_cores, verbose=False, h5_target_group=h5_sho_targ_grp)\n",
335
336
337
338
    "sho_fitter.set_up_guess(guess_func=px.analysis.be_sho_fitter.SHOGuessFunc.complex_gaussian,\n",
    "                        num_points=sho_fit_points)\n",
    "h5_sho_guess = sho_fitter.do_guess(override=sho_override)\n",
    "sho_fitter.set_up_fit()\n",
339
340
    "h5_sho_fit = sho_fitter.do_fit(override=sho_override)\n",
    "h5_sho_grp = h5_sho_fit.parent"
341
   ]
342
  },
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "## Visualize the SHO results\n",
    "Here, we visualize the parameters for the SHO fits. BE-line (3D) data is visualized via simple spatial maps of the SHO parameters while more complex BEPS datasets (4+ dimensions) can be visualized using a simple interactive visualizer below. \n",
    "\n",
    "You can choose to visualize the guesses for SHO function or the final fit values from the first line of the cell below.\n",
    "\n",
    "Use the sliders below to inspect the BE response at any given location. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "h5_sho_spec_inds = h5_sho_fit.h5_spec_inds\n",
    "sho_spec_labels = h5_sho_fit.spec_dim_labels\n",
    "\n",
    "if is_ckpfm:\n",
    "    # It turns out that the read voltage index starts from 1 instead of 0\n",
    "    # Also the VDC indices are NOT repeating. They are just rising monotonically\n",
    "    write_volt_index = np.argwhere(sho_spec_labels == 'write_bias')[0][0]\n",
    "    read_volt_index = np.argwhere(sho_spec_labels == 'read_bias')[0][0]\n",
    "    h5_sho_spec_inds[read_volt_index, :] -= 1\n",
    "    h5_sho_spec_inds[write_volt_index, :] = np.tile(np.repeat(np.arange(num_write_steps), num_fields), num_read_steps)\n",
    "\n",
    "(Nd_mat, success, nd_labels) = usid.hdf_utils.reshape_to_n_dims(h5_sho_fit, get_labels=True)\n",
    "print('Reshape Success: ' + str(success))\n",
    "\n",
    "print(nd_labels)\n",
    "print(Nd_mat.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {},
388
    "scrolled": true
389
390
391
392
   },
   "outputs": [],
   "source": [
    "use_sho_guess = False\n",
393
    "use_static_viz_func = True\n",
394
395
396
397
398
    "\n",
    "if use_sho_guess:\n",
    "    sho_dset = h5_sho_guess\n",
    "else:\n",
    "    sho_dset = h5_sho_fit\n",
399
400
    "  \n",
    "if expt_type == 'BELineData' or len(pos_dims) != 2:\n",
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
    "    use_static_viz_func = True\n",
    "    step_chan = None\n",
    "else:\n",
    "    if vs_mode not in ['AC modulation mode with time reversal', \n",
    "                                                      'DC modulation mode']:\n",
    "        use_static_viz_func = True\n",
    "    else:\n",
    "        if vs_mode == 'DC modulation mode':\n",
    "            step_chan = 'DC_Offset'\n",
    "        else:\n",
    "            step_chan = 'AC_Amplitude'\n",
    "if not use_static_viz_func:\n",
    "    try:\n",
    "        # use interactive visualization\n",
    "        px.viz.be_viz_utils.jupyter_visualize_beps_sho(sho_dset, step_chan)\n",
    "    except:\n",
    "        raise\n",
    "        print('There was a problem with the interactive visualizer')\n",
    "        use_static_viz_func = True\n",
    "else:\n",
421
422
    "    chan_grp = h5_main.parent\n",
    "    meas_grp = chan_grp.parent\n",
423
    "    # show plots of SHO results vs. applied bias\n",
424
425
426
    "    figs = px.viz.be_viz_utils.visualize_sho_results(sho_dset, show_plots=True, save_plots=False, \n",
    "                                                     expt_type=expt_type, meas_type=vs_mode, \n",
    "                                                     field_mode=field_mode)"
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "## Fit loops to a function\n",
    "This is applicable only to DC voltage spectroscopy datasets from BEPS. The PFM hysteresis loops in this dataset will be projected to maximize the loop area and then fitted to a function.\n",
    "\n",
    "Note: This computation generally takes a while for reasonably sized datasets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {},
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# Do the Loop Fitting on the SHO Fit dataset\n",
    "loop_success = False\n",
    "\n",
    "h5_loop_group = None\n",
    "if results_to_new_file:\n",
    "    h5_loop_file_path = os.path.join(folder_path, \n",
    "                                     h5_raw_file_name.replace('.h5', '_loop_fit.h5'))\n",
    "    print('\\n\\nLoop Fits will be written to:\\n' + h5_loop_file_path + '\\n\\n')\n",
    "    f_open_mode = 'w'\n",
    "    if os.path.exists(h5_loop_file_path):\n",
    "        f_open_mode = 'r+'\n",
    "    h5_loop_file = h5py.File(h5_loop_file_path, mode=f_open_mode)\n",
    "    h5_loop_group = h5_loop_file\n",
463
464
465
466
    "        \n",
    "loop_fitter = px.analysis.BELoopFitter(h5_sho_fit, expt_type, vs_mode, vs_cycle_frac,\n",
    "                                       cores=max_cores, h5_target_group=h5_loop_group, \n",
    "                                       verbose=False)\n",
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
    "loop_fitter.set_up_guess()\n",
    "h5_loop_guess = loop_fitter.do_guess(override=False)\n",
    "# Calling explicitely here since Fitter won't do it automatically\n",
    "h5_guess_loop_parms = loop_fitter.extract_loop_parameters(h5_loop_guess)\n",
    "\n",
    "loop_fitter.set_up_fit()\n",
    "h5_loop_fit = loop_fitter.do_fit(override=False)\n",
    "h5_loop_group = h5_loop_fit.parent\n",
    "loop_success = True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "## Prepare datasets for visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "# Prepare some variables for plotting loops fits and guesses\n",
    "# Plot the Loop Guess and Fit Results\n",
    "if loop_success:\n",
    "    h5_projected_loops = usid.USIDataset(h5_loop_guess.parent['Projected_Loops'])\n",
    "    h5_proj_spec_inds = h5_projected_loops.h5_spec_inds\n",
    "    h5_proj_spec_vals = h5_projected_loops.h5_spec_vals\n",
    "\n",
    "    # reshape the vdc_vec into DC_step by Loop\n",
    "    sort_order = usid.hdf_utils.get_sort_order(h5_proj_spec_inds)\n",
    "    dims = usid.hdf_utils.get_dimensionality(h5_proj_spec_inds[()], \n",
    "                                           sort_order[::-1])\n",
    "    vdc_vec = np.reshape(h5_proj_spec_vals[h5_proj_spec_vals.attrs['DC_Offset']], dims).T\n",
    "\n",
    "    #Also reshape the projected loops to Positions-DC_Step-Loop\n",
    "    # Also reshape the projected loops to Positions-DC_Step-Loop\n",
    "    proj_nd = h5_projected_loops.get_n_dim_form()\n",
    "    proj_3d = np.reshape(proj_nd, [h5_projected_loops.shape[0], \n",
    "                                   proj_nd.shape[2], -1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "## Visualize Loop fits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {},
529
    "scrolled": false
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
   },
   "outputs": [],
   "source": [
    "use_static_plots = True\n",
    "if loop_success:\n",
    "    if not use_static_plots:\n",
    "        try:\n",
    "            fig = px.viz.be_viz_utils.jupyter_visualize_beps_loops(h5_projected_loops, h5_loop_guess, h5_loop_fit)\n",
    "        except:\n",
    "            print('There was a problem with the interactive visualizer')\n",
    "            use_static_plots = True\n",
    "    if use_static_plots:\n",
    "        for iloop in range(h5_loop_guess.shape[1]):\n",
    "            fig, ax = px.viz.be_viz_utils.plot_loop_guess_fit(vdc_vec[:, iloop], proj_3d[:, :, iloop], \n",
    "                                          h5_loop_guess[:, iloop], h5_loop_fit[:, iloop],\n",
    "                                          title='Loop {} - All Positions'.format(iloop))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "## Loop Parameters\n",
    "We will now load the loop parameters caluculated from the fit and plot them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {},
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "h5_loop_parameters = h5_loop_group['Fit_Loop_Parameters']\n",
    "fig = px.viz.be_viz_utils.jupyter_visualize_parameter_maps(h5_loop_parameters)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {},
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "map_parm = 'Work of Switching'\n",
    "plot_cycle = 0\n",
    "plot_position = (int(pos_dims[0]/2), int(pos_dims[1]/2))\n",
    "plot_bias_step = 0\n",
584
585
586
587
588
589
    "\n",
    "fig = px.viz.be_viz_utils.plot_loop_sho_raw_comparison(h5_loop_parameters, h5_sho_grp, h5_main,\n",
    "                                                       selected_loop_parm=map_parm, \n",
    "                                                       selected_loop_cycle=plot_cycle, \n",
    "                                                       selected_loop_pos=plot_position, \n",
    "                                                       selected_step=plot_bias_step)\n"
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "## Save and close\n",
    "* Save the .h5 file that we are working on by closing it. <br>\n",
    "* Also, consider exporting this notebook as a notebook or an html file. <br> To do this, go to File >> Download as >> HTML\n",
    "* Finally consider saving this notebook if necessary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
612
613
614
615
    "h5_file.close()\n",
    "if results_to_new_file:\n",
    "    h5_sho_fit.file.close()\n",
    "    h5_loop_fit.file.close()"
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.5"
  },
  "widgets": {
   "state": {
    "626c09f4ed724d658d702180fe718a7f": {
     "views": [
      {
       "cell_index": 12
      }
     ]
    }
   },
   "version": "1.2.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}