ioFuncs.py 13.9 KB
Newer Older
Unknown's avatar
Unknown committed
1
"""
2
3
4
Created on Apr 20, 2016

@author: Chris Smith -- csmith55@utk.edu
Unknown's avatar
Unknown committed
5
"""
6
7
8
9

import sys
import numpy as np
import h5py
Unknown's avatar
Unknown committed
10

11
12
13
14
15
sys.path.append('../')
from scipy.io import loadmat
from pycroscopy.io.io_hdf5 import ioHDF5
from pycroscopy.io.hdf_utils import reshape_to_Ndims

Unknown's avatar
Unknown committed
16

17
def loadDataFunc(filePath, **kwargs):
Unknown's avatar
Unknown committed
18
    """
19
20
21
    Function to load the N-D data from a .mat file
    Output:  N-D numpy data array, Nx2 x-vector array
            array indices are (Step, #rows, #cols, cycle#)
Unknown's avatar
Unknown committed
22
    """
23
24
    data = loadmat(filePath)
    data_mat = data['loop_mat']
Unknown's avatar
Unknown committed
25
26
27
28
    data_mat = data_mat[:, :, :, :]
    data_mat[np.where(data_mat > 1E-1)] = 0  # Presumably, this will not be requried.
    data_mat[np.where(data_mat < -1E-1)] = 0  # Otherwise we will have to write smoothing/cleaning functions
    data_mat = np.transpose(data_mat, (3, 0, 1, 2))
29
    xvec = data['VV'].ravel()
Unknown's avatar
Unknown committed
30
31
32

    return data_mat, xvec

33
34

def readData(h5_path, dset_name='SHO_Fit_Guess'):
Unknown's avatar
Unknown committed
35
    """
36
37
38
39
    Reads the hdf5 data file and calls appropriate reader based on data type
    Input:
        h5_path -- the absolute file path to the hdf5 file to be read in.
        dset_name -- the name of the main dataset
Unknown's avatar
Unknown committed
40

41
42
43
44
    Outputs:
        data_mat -- the transformed data read to be plotted
        xvec -- numpy array containing the possible plot data of the slice viewer
        xvec_labs -- numpy array of labels and units for the xvec array
Unknown's avatar
Unknown committed
45
46

    """
Unknown's avatar
Unknown committed
47

48
49
50
    hdf = ioHDF5(h5_path)

    h5_file = hdf.file
Unknown's avatar
Unknown committed
51

52
53
54
    exp_type = h5_file.attrs['data_type']

    h5_group = h5_file['Measurement_000']
Unknown's avatar
Unknown committed
55

56
57
58
59
60
    data_type = h5_group.attrs['VS_mode']
    if data_type == 'DC modulation mode':
        return readDCData(h5_group)
    elif data_type == 'AC modulation mode with time reversal':
        return readACData(h5_group)
Unknown's avatar
Unknown committed
61
62
63
64
65


# return readDataGen(h5_group['SHO_Fit_Results'])


66
def readDCData(h5_group):
Unknown's avatar
Unknown committed
67
    """
68
    Reads the data for DC modulation experiments
Unknown's avatar
Unknown committed
69

70
    Inputs:
Unknown's avatar
Unknown committed
71
        h5_group -- hdf5 group holding the SHO_Fit Data for the chosen
72
                    Measurement group
Unknown's avatar
Unknown committed
73

74
75
76
77
    Outputs:
        data_guess -- the transformed data to be plotted
        xvec -- numpy array containing the possible plot data of the slice viewer
        xvec_labs -- numpy array of labels and units for the xvec array
Unknown's avatar
Unknown committed
78
    """
79
80
81
82
83
84
85
86
    h5_chan = h5_group['Channel_000']
    h5_main = h5_chan['Raw_Data']
    h5_file = h5_main.file
    h5_specv = h5_file[h5_main.attrs['Spectroscopic_Values']]
    h5_bins = h5_file[h5_main.attrs['Bin_Frequencies']]

    h5_shogroup = h5_chan['Raw_Data-SHO_Fit_000']
    h5_guess = h5_shogroup['Guess']
Unknown's avatar
Unknown committed
87

88
89
90
91
    h5_data_labels = h5_specv.attrs['labels']
    h5_sho_specv = h5_file[h5_guess.attrs['Spectroscopic_Values']]
    h5_indices = h5_file[h5_guess.attrs['Spectroscopic_Indices']]
    h5_pos = h5_file[h5_main.attrs['Position_Indices']]
Unknown's avatar
Unknown committed
92
93
94

    num_rows, num_cols = __getPos(h5_pos)
    num_pos = num_rows * num_cols
95
96

    ndims = len(np.where(np.array(h5_pos.shape) > 1)[0])
Unknown's avatar
Unknown committed
97

98
    num_cycles = h5_group.attrs['VS_number_of_cycles']
Unknown's avatar
Unknown committed
99
    num_steps = len(np.unique(h5_indices[:, 1]))
100
    num_bins = len(np.unique(h5_bins))
Unknown's avatar
Unknown committed
101

102
103
    data_xvec = h5_sho_specv[h5_sho_specv.attrs['DC_Offset']].flatten()
    field_type = h5_group.attrs['VS_measure_in_field_loops']
Unknown's avatar
Unknown committed
104

105
106
107
    '''
    Separate data into cycles
    '''
Unknown's avatar
Unknown committed
108
109
110
111
112
113
114
115
    data_guess = np.reshape(h5_guess, [num_pos, num_cycles, -1])
    data_guess = np.transpose(data_guess, [1, 2, 0])
    data_xvec = np.reshape(data_xvec, [num_cycles, -1])
    data_main = np.reshape(h5_main, [num_pos, num_cycles, -1])
    data_main = np.transpose(data_main, [1, 2, 0])

    pt_xvec = np.tile(np.arange(data_xvec.shape[1]), [num_cycles, 1])

116
117
118
119
    if field_type == 'in and out-of-field':
        '''
    Seperate out in-field and out-of-field values
        '''
Unknown's avatar
Unknown committed
120
121
122
123
124
125
126
127
128
        data_guess = np.array([data_guess[:, slice(0, None, 2), :], data_guess[:, slice(1, None, 2), :]])
        data_guess = np.reshape(data_guess, (2, num_cycles, -1, num_rows, num_cols))
        data_xvec = np.array([data_xvec[:, slice(0, None, 2)], data_xvec[:, slice(1, None, 2)]])
        pt_xvec = np.array([pt_xvec[:, slice(0, None, 2)], pt_xvec[:, slice(1, None, 2)]])

        data_main = np.reshape(data_main, (num_cycles, -1, num_bins, num_rows, num_cols))
        data_main = np.array([data_main[:, slice(0, None, 2), :, :, :], data_main[:, slice(1, None, 2), :, :, :]])

        data_parts = ['In-field', 'Out-of-field']
129
    elif field_type == 'in-field':
Unknown's avatar
Unknown committed
130
131
132
133
134
135
        data_guess = np.reshape(data_guess, (1, num_cycles, -1, num_rows, num_cols))
        data_xvec = data_xvec.reshape(1, num_cycles, -1)
        pt_xvec = pt_xvec.reshape(1, num_cycles, -1)

        data_main = np.reshape(data_main, (1, num_cycles, -1, num_bins, num_rows, num_cols))

136
        data_parts = ['In-field']
Unknown's avatar
Unknown committed
137

138
    elif field_type == 'out-of-field':
Unknown's avatar
Unknown committed
139
140
141
142
143
144
        data_guess = np.reshape(data_guess, (1, num_cycles, -1, num_rows, num_cols))
        data_xvec = data_xvec.reshape(1, num_cycles, -1)
        pt_xvec = pt_xvec.reshape(1, num_cycles, -1)

        data_main = np.reshape(data_main, (1, num_cycles, -1, num_bins, num_rows, num_cols))

145
146
147
        data_parts = ['Out-of-field']
    else:
        raise ValueError('Unknown field type in data.  Cannot parse')
Unknown's avatar
Unknown committed
148

149
150
151
    '''
    Repeat steps for reading guess on the results if present
    '''
Unknown's avatar
Unknown committed
152

153
154
    try:
        h5_results = h5_shogroup['Fit']
Unknown's avatar
Unknown committed
155
156
157
        data_results = np.reshape(h5_results, [num_pos, num_cycles, -1])
        data_results = np.transpose(data_results, [1, 2, 0])

158
        if field_type == 'in and out-of-field':
Unknown's avatar
Unknown committed
159
160
161
            data_results = np.array([data_results[:, slice(0, None, 2), :],
                                     data_results[:, slice(1, None, 2), :]])
            data_results = np.reshape(data_results, (2, num_cycles, -1, num_rows, num_cols))
162
        elif field_type == 'in-field':
Unknown's avatar
Unknown committed
163
            data_results = np.reshape(data_results, (1, num_cycles, -1, num_rows, num_cols))
164
        elif field_type == 'out-of-field':
Unknown's avatar
Unknown committed
165
166
            data_results = np.reshape(data_results, (1, num_cycles, -1, num_rows, num_cols))

167
168
    except KeyError:
        data_results = None
Unknown's avatar
Unknown committed
169
170
171

    xvec = np.array([data_xvec, pt_xvec])
    xvec_labs = np.array([['Voltage', 'V'], ['UDVS Step', '']])
172
173
174
175
176
177
178
179
180
181
182
    data_labs = h5_data_labels
    data_units = list()
    for label in data_labs:
        if label == 'Amplitude':
            data_units.append('V')
        elif label == 'Frequency':
            data_units.append('Hz')
        elif label == 'Quality Factor':
            data_units.append('')
        elif label == 'Phase':
            data_units.append('rad')
Unknown's avatar
Unknown committed
183
184


Unknown's avatar
Unknown committed
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
            #     xaxis = {'XData':data_xvec,
            #              'XData Name':'Voltage',
            #              'XData Unit':'V',
            #              'XStep':pt_xvec,
            #              'XStep Name':'UDVS Step',
            #              'XStep Unit':''}

            #     yaxis = dict([(data_labs[i],data_units[i]) for i in xrange(4)])
            #     elements = {'Field':data_parts,
            #                 'Parameters':yaxis,
            #                 'Cycles':num_cycles,
            #                 'Steps':num_steps,
            #                 'X positions':num_rows,
            #                 'Y positions':num_cols}
            #
            #     data_pack = {'Guess':data_guess,
            #                  'Fit':data_results,
            #                  'Raw':data_main,
            #                  'Data Elements':elements,
            #                  'X-axis':xaxis,
            #                  'Y-axis':yaxis,
            #                  'Num Cycles':num_cycles,
            #                  'Groups':data_parts,
            #                  'Dims':ndims,
            #                  'Bins':h5_bins.value}
Unknown's avatar
Unknown committed
210

211
212
    return data_guess, data_results, xvec, xvec_labs, data_parts, ndims, data_main, np.unique(h5_bins.value)

Unknown's avatar
Unknown committed
213

214
def readACData(h5_group):
Unknown's avatar
Unknown committed
215
    """
216
    Reads the data for AC modulation experiments
Unknown's avatar
Unknown committed
217

218
    Inputs:
Unknown's avatar
Unknown committed
219
        h5_group -- hdf5 group holding the SHO_Fit Data for the chosen
220
                    Measurement group
Unknown's avatar
Unknown committed
221

222
223
224
225
    Outputs:
        data_guess -- the transformed data to be plotted
        xvec -- numpy array containing the possible plot data of the slice viewer
        xvec_labs -- numpy array of labels and units for the xvec array
Unknown's avatar
Unknown committed
226
    """
227
228
229
230
231
232
233
    h5_chan = h5_group['Channel_000']
    h5_main = h5_chan['Raw_Data']
    h5_specv = h5_chan['Spectroscopic_Values']
    h5_bins = h5_chan['Bin_Frequencies']

    h5_shogroup = h5_chan['Raw_Data-SHO_Fit_000']
    h5_guess = h5_shogroup['Guess']
Unknown's avatar
Unknown committed
234

235
236
237
238
    h5_data_labels = h5_specv.attrs['labels']
    h5_sho_specv = h5_shogroup['Spectroscopic_Values']
    h5_indices = h5_shogroup['Spectroscopic_Indices']
    h5_pos = h5_chan['Position_Indices']
Unknown's avatar
Unknown committed
239
240
241

    num_rows, num_cols = __getPos(h5_pos)
    num_pos = num_rows * num_cols
242
243

    ndims = len(np.where(np.array(h5_pos.shape) > 1)[0])
Unknown's avatar
Unknown committed
244

245
    num_cycles = h5_group.attrs['VS_number_of_cycles']
Unknown's avatar
Unknown committed
246
    num_steps = len(np.unique(h5_indices[:, 1]))
247
    num_bins = len(np.unique(h5_bins))
Unknown's avatar
Unknown committed
248

249
250
251
252
253
254
255
    data_xvec = h5_sho_specv[h5_sho_specv.attrs['AC_Amplitude']].flatten()
    direction = h5_specv[h5_specv.attrs['Direction']].flatten()
    direction = h5_sho_specv[h5_sho_specv.attrs['Direction']].flatten()

    '''
    Separate data into cycles
    '''
Unknown's avatar
Unknown committed
256
257
258
259
260
261
262
    data_guess = np.reshape(h5_guess, [num_pos, num_cycles, -1])
    data_guess = np.transpose(data_guess, [1, 2, 0])
    data_xvec = np.reshape(data_xvec, [num_cycles, -1])
    data_main = np.reshape(h5_main, [num_pos, num_cycles, -1])
    data_main = np.transpose(data_main, [1, 2, 0])

    pt_xvec = np.tile(np.arange(data_xvec.shape[1]), [num_cycles, 1])
263
264
265
266
267
268
269

    '''
    Seperate out forward and reverse values
    '''
    for_dir = np.where(direction == 1.0)[0]
    rev_dir = np.where(direction == -1.0)[0]

Unknown's avatar
Unknown committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
    data_guess = np.array([data_guess[:, for_dir, :], data_guess[:, rev_dir, :]])
    data_guess = np.reshape(data_guess, (2, num_cycles, -1, num_rows, num_cols))

    data_main = np.reshape(data_main, (num_cycles, -1, num_bins, num_rows, num_cols))
    data_main = np.array([data_main[:, for_dir, :, :, :], data_main[:, rev_dir, :, :, :]])

    data_xvec = np.array([data_xvec[:, for_dir], data_xvec[:, rev_dir]])
    pt_xvec = np.array([pt_xvec[:, for_dir], pt_xvec[:, rev_dir]])
    data_parts = ['Forward', 'Reverse']

    #     '''
    #     Get the mean and standard deviation of each variable
    #     Then use these to set upper and lower bounds
    #     '''
    #     for var in xrange(data_guess.shape[0]):
    #         for cycle in xrange(data_guess.shape[1]):
    #             mean_data, std_data = getGoodLims(data_guess[var,cycle,:,:])
    #             max_data = mean_data+3*std_data
    #             min_data = mean_data-3*std_data
    #             np.clip(data_guess[var,cycle,:,:],min_data,max_data,data_guess[var,cycle,:,:])

    xvec = np.array([data_xvec, pt_xvec])
    xvec_labs = np.array([['AC Current', 'A'], ['UDVS Step', '']])
293
294
295
296

    '''
    Repeat steps for reading guess on the results if present
    '''
Unknown's avatar
Unknown committed
297

298
299
    try:
        h5_results = h5_shogroup['Fit']
Unknown's avatar
Unknown committed
300
301
302
303
304
        data_results = np.reshape(h5_results, [num_pos, num_cycles, -1])
        data_results = np.transpose(data_results, [1, 2, 0])

        data_results = np.array([data_results[:, for_dir, :], data_results[:, rev_dir, :]])
        data_results = np.reshape(data_results, (2, num_cycles, -1, num_rows, num_cols))
305
306
307
308
309

    except KeyError:
        data_results = None
    except:
        raise
Unknown's avatar
Unknown committed
310

311
312
313
314
315
316
317
318
319
320
321
    data_labs = h5_data_labels
    data_units = list()
    for label in data_labs:
        if label == 'Amplitude':
            data_units.append('V')
        elif label == 'Frequency':
            data_units.append('Hz')
        elif label == 'Quality Factor':
            data_units.append('')
        elif label == 'Phase':
            data_units.append('rad')
Unknown's avatar
Unknown committed
322
323


Unknown's avatar
Unknown committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
            #     xaxis = {'XData':data_xvec,
            #              'XData Name':'Voltage',
            #              'XData Unit':'V',
            #              'XStep':pt_xvec,
            #              'XStep Name':'UDVS Step',
            #              'XStep Unit':''}

            #     yaxis = dict([(data_labs[i],data_units[i]) for i in xrange(4)])
            #     elements = {'Field':data_parts,
            #                 'Parameters':yaxis,
            #                 'Cycles':num_cycles,
            #                 'Steps':num_steps,
            #                 'X positions':num_rows,
            #                 'Y positions':num_cols}
            #
            #     data_pack = {'Guess':data_guess,
            #                  'Fit':data_results,
            #                  'Raw':data_main,
            #                  'Data Elements':elements,
            #                  'X-axis':xaxis,
            #                  'Y-axis':yaxis,
            #                  'Num Cycles':num_cycles,
            #                  'Groups':data_parts,
            #                  'Dims':ndims,
            #                  'Bins':h5_bins.value}
Unknown's avatar
Unknown committed
349
350
351

    return data_guess, data_results, xvec, xvec_labs, data_parts, ndims, data_main, np.unique(h5_bins.value)

352
353
354
355
356
357
358
359
360
361
362
363
364

def getSpectralData(point, data_mat):
    """
    This function accepts a tuple (x,y) and extracts the spectra
    from matrix (data) at that point, and returns an Nx2 vector
    
    Inputs:
        point -- (x,y) position to be retrieved from data_mat
        data_mat -- matrix to retrieve data from
        
    Outputs:
        
    """
Unknown's avatar
Unknown committed
365
    spec_ydata = data_mat[:, int(np.round(point[0])), int(np.round(point[1]))]
366
367
    return spec_ydata.ravel()

Unknown's avatar
Unknown committed
368

369
def __getPos(h5_pos):
Unknown's avatar
Unknown committed
370
    """
371
    Return the number of rows and columns in the dataset
Unknown's avatar
Unknown committed
372
    """
Unknown's avatar
Unknown committed
373
    num_rows = len(np.unique(h5_pos[:, 0]))
374
    try:
Unknown's avatar
Unknown committed
375
        num_cols = len(np.unique(h5_pos[:, 1]))
376
377
378
    except ValueError:
        num_cols = 1

Unknown's avatar
Unknown committed
379
380
381
382
    return num_rows, num_cols


def __findDataset(h5_file, ds_name):
Unknown's avatar
Unknown committed
383
    """
384
    Uses visit() to find all datasets with the desired name
Unknown's avatar
Unknown committed
385
    """
Unknown's avatar
Unknown committed
386
    print('Finding all instances of', ds_name)
387
    ds = []
Unknown's avatar
Unknown committed
388
389
390
391

    def __findName(name, obj):
        if name.split('/')[-1] == ds_name and isinstance(obj, h5py.Dataset):
            ds.append([name, obj])
392
        return
Unknown's avatar
Unknown committed
393

394
    h5_file.visititems(__findName)
Unknown's avatar
Unknown committed
395

Unknown's avatar
Unknown committed
396
    return ds