-
Notifications
You must be signed in to change notification settings - Fork 0
/
NortekDataArrays.py
176 lines (160 loc) · 6.47 KB
/
NortekDataArrays.py
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
48
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
75
76
77
78
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
125
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import numpy
import numpy.random
import scipy.stats
import scipy.signal
import scipy
import UserDict
import pdb
#import AuxiliaryFunctions as AF
# implement logging in this module
class Spectrum( object ):
pass
class GenericDataArray( object ):
# base class for single sample volume data (e.g. Vectrino, Vector, current meters)
# sample rate is a scalar
# shape is an tuple
# ( components or beams, sample number, cell number )
def __init__( self,
sampleRate = 1,
shape = ( 1, 1 ) ):
self.sampleRate = sampleRate
self.data = 0.0 * numpy.empty( shape )
if len( shape ) == 1:
self.numberOfSamples = shape[ 0 ]
else:
self.numberOfSamples = shape[ 1 ]
try:
self.calculateStatistics()
except IndexError as currentError:
# probably a 1D array
self.calculateStatistics( onAxis = 0 )
def calculateStatistics( self, onAxis = 1, useScreenedData = False ):
if useScreenedData:
try:
self.mean = scipy.stats.nanmean( self.data[ self.goodIndices ], onAxis )
self.median = scipy.stats.nanmedian( self.data[ self.goodIndices ], onAxis )
self.var = scipy.stats.nanstd( self.data[ self.goodIndices ], onAxis )**2
except IndexError:
for currentChannel in range( 0, self.data.shape[ 0 ] ):
try:
self.mean[ currentChannel ] = scipy.stats.nanmean(
self.data[ currentChannel, self.goodIndices ] )
self.median[ currentChannel ] = scipy.stats.nanmedian(
self.data[ currentChannel, self.goodIndices ] )
self.var[ currentChannel ] = scipy.stats.nanstd(
self.data[ currentChannel, self.goodIndices ] )**2
except IndexError:
self.mean[ currentChannel ] = scipy.stats.nanmean(
self.data[ currentChannel, self.goodIndices[ currentChannel, : ] ] )
self.median[ currentChannel ] = scipy.stats.nanmedian(
self.data[ currentChannel, self.goodIndices[ currentChannel, : ] ] )
self.var[ currentChannel ] = scipy.stats.nanstd(
self.data[ currentChannel, self.goodIndices[ currentChannel, : ] ] )**2
except Exception as currentError:
pdb.set_trace()
else:
self.mean = scipy.stats.nanmean( self.data, onAxis )
self.median = scipy.stats.nanmedian( self.data, onAxis )
self.var = scipy.stats.nanstd( self.data, onAxis )**2
def calculateScreenedStatistics( self ):
self.calculateStatistics( useScreenedData = True )
def calculateHistograms( self, bins = None ):
self.histograms = Histogram( self.data, bins )
def orGoodIndices( self ):
try:
self.goodIndicesByBeam = self.goodIndices
self.goodIndices = self.goodIndices.all( axis = 0 ).flatten()
except AttributeError as currentError:
pass
def applyDataCutoff( self, hardLimit = None ):
try:
self.goodIndices[ abs( self.data ) >= hardLimit ] = False
self.goodIndices[ abs( self.data ) <= hardLimit ] = True
except AttributeError as currentError:
self.goodIndices = numpy.isfinite( self.data )
self.applyDataCutoff( hardLimit )
except Exception as currentError:
pdb.set_trace()
class Histogram ( object ):
def __init__( self, dataArray, bins = None ):
self.binEdges = []
self.binCenters = []
self.densityInBin = []
if bins is 'correlation':
bins = numpy.linspace( 0, 100, 101 )
elif bins is 'vectrinoSNR':
bins = numpy.linspace( 0, 35, 35 )
elif bins is 'vectorSNR':
bins = numpy.linspace( 0, 45, 45 )
elif bins is 'vProSNR':
bins = numpy.linspace( 1, 60, 60 )
elif bins is 'amplitude':
bins = numpy.linspace( 0, 255, 256 )
for cellNumber in range( 0, dataArray.shape[ 0 ], 1 ):
self.binEdges.append( [] )
self.binCenters.append( [] )
self.densityInBin.append( [] )
for channelNumber in range( 0, dataArray.shape[ -1 ], 1 ):
if bins == None:
binEdges, binCenters = self.optimalHistogramBins( dataArray[ cellNumber, :, channelNumber ] )
densityInBin, otherBinEdges = numpy.histogram(
dataArray[ cellNumber, :, channelNumber ],
binEdges,
density = True )
elif isinstance( bins, ( int, numpy.ndarray ) ): # number of bins or binEdges specified
densityInBin, binEdges = numpy.histogram(
dataArray[ cellNumber, :, channelNumber ],
bins,
density = True )
binWidth = ( binEdges[ 1 ] - binEdges[ 0 ] ) / 2.
binCenters = numpy.linspace( binEdges[ 0 ] + binWidth,
binEdges[ -1 ] - binWidth,
densityInBin.shape[ 0 ] )
self.binEdges[ cellNumber ].append( binEdges )
self.binCenters[ cellNumber ].append( binCenters )
self.densityInBin[ cellNumber ].append( densityInBin )
def optimalHistogramBins( self, data ):
################################################################################
# optimal histogram bin width as shown by
# http://www.fmrib.ox.ac.uk/analysis/techrep/tr00mj2/tr00mj2/node24.html
# Summary reference is:
# Izenman, 1991
# Izenman, A. J. 1991.
# Recent developments in nonparametric density estimation.
# Journal of the American Statistical Association, 86(413):205-224.
################################################################################
data = data.flatten()
n = max(data.shape) - sum( numpy.isnan( data ) )
# need to estimate the IQR
interQuartileRange = AF.iqr( data )
binwidth = 2.0 * interQuartileRange * n ** (-1.0 / 3.0 )
# have one bin centered at the median and extend to either end of the data as
# appropriate
medianValue = numpy.median( data )
dataMinimumValue = min( data )
bins = int( ( medianValue - dataMinimumValue - binwidth / 2.0 ) / binwidth )
binCenters = medianValue - numpy.arange( bins ) * binwidth
dataMaximumValue = max( data )
bins = int( ( medianValue + dataMaximumValue - binwidth / 2.0 ) / binwidth )
binCenters2ndHalf = medianValue + numpy.arange( 1, bins + 1 ) * binwidth
binCenters = numpy.append( binCenters, binCenters2ndHalf )
binCenters.sort( )
binEdges = binCenters - binwidth / 2
# add one last bin edge so we get the right values to plot against binCenters
binEdges = numpy.append( binEdges, binEdges[-1] + binwidth/2 )
return binEdges, binCenters
class ScalarDataArray( GenericDataArray ):
def __init__( self,
sampleRate = 1,
arrayLength = 1 ):
super( ScalarDataArray, self ).__init__( sampleRate, shape = ( arrayLength, ) )
class VelocityDataArray( GenericDataArray ):
def __init__( self,
sampleRate = 1,
shape = ( 1, 1 ),
coordinateSystem = None,
units = 'mm/s' ):
super( VelocityDataArray, self ).__init__( shape = shape )
self.isInCoordinateSystem = coordinateSystem
self.units = units
self.calculateStatistics()