/
janus_widget_fm_moments.py
632 lines (442 loc) · 19.1 KB
/
janus_widget_fm_moments.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
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
216
217
218
219
220
221
222
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
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
463
464
465
466
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
529
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
################################################################################
##
## Janus -- GUI Software for Processing Thermal-Ion Measurements from the
## Wind Spacecraft's Faraday Cups
##
## Copyright (C) 2016 Bennett A. Maruca (bmaruca@udel.edu)
##
## This program is free software: you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation, either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
## FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program. If not, see http://www.gnu.org/licenses/.
##
################################################################################
################################################################################
## LOAD THE NECESSARY MODULES.
################################################################################
# Load the modules necessary for the graphical interface.
from PyQt4.QtCore import QPointF, Qt, SIGNAL
from PyQt4.QtGui import QGridLayout, QWidget
# Load the modules necessary for plotting.
from pyqtgraph import AxisItem, GraphicsLayoutWidget, LabelItem, mkBrush, \
mkPen, PlotDataItem, TextItem
from janus_event_ViewBox import event_ViewBox
# Load the module necessary handling step functions.
from janus_step import step
# Load the necessary "numpy" array modules and numeric-function modules.
from numpy import amax, amin, array, ceil, floor, log10, sqrt, tile, where,\
mean, std, shape
# Load the necessary threading modules.
from threading import Thread
from janus_thread import n_thread, thread_chng_opt
from scipy.signal import medfilt
################################################################################
## DEFINE THE "widget_fm_moments" TO CUSTOMIZE "QWidget" FOR Wind/FC PLOTS.
################################################################################
class widget_fm_moments( QWidget ) :
#-----------------------------------------------------------------------
# DEFINE THE INITIALIZATION FUNCTION.
#-----------------------------------------------------------------------
def __init__( self, core, cup,
n_plt_x=None, n_plt_y=None, n_plt=None ) :
# Inherit all attributes of an instance of "QWidget".
super( widget_fm_moments, self ).__init__( )
# Initialize the counter of repaint events for this widget as
# well as a maximum value for this counter.
# Note:For some reason, adjusting the individual plots to have
# uniform sizes is difficult to achieve before the widget
# is rendered. Thus, once a paint event occurs, the
# "self.paintEvent( )" function picks it up and makes a
# call to "self.ajst_grd( )". This counter and its
# maximum value are to used ensure that "self.paintEvent( )
# makes such a call only in response to the intial few
# painting (so as to prevent an infinite loop).
# Note. The first paint seems to be a "dummy" of some sort.
# Whatever the case, "self.n_paint_max = 1" seems to
# generally be insufficient.
self.n_painted = 0
self.n_painted_max = 3
# Disable the mouse click for this widget.
self.setDisabled( True )
# Store the Janus core.
self.core = core
# Prepare to respond to signals received from the Janus core.
self.connect( self.core, SIGNAL('janus_rset'), self.resp_rset )
self.connect( self.core, SIGNAL('janus_chng_mfi'),
self.resp_chng_mfi )
self.connect( self.core, SIGNAL('janus_chng_opt'),
self.resp_chng_opt )
self.connect( self.core, SIGNAL('janus_rstr_opt'),
self.resp_rstr_opt )
# Assign (if not done so already) and store the shape of the
# plot-grid array.
self.n_plt_x = 1 if ( n_plt_x is None ) else n_plt_x
self.n_plt_y = 3 if ( n_plt_y is None ) else n_plt_y
if ( n_plt is None ) :
self.n_plt = self.n_plt_x * self.n_plt_y
# Initizalize the pens, brushes, and fonts used by this widget.
self.pen_plt = mkPen( color='k' )
self.pen_plt = mkPen( color='k' )
self.pen_pnt_c = mkPen( color='k' )
self.pen_pnt_y = mkPen( color='k' )
self.pen_pnt_r = mkPen( color='k' )
self.pen_crv_c = mkPen( color='c' )
self.pen_crv_r = mkPen( color='r' )
self.pen_crv_m = mkPen( color='m' )
self.pen_crv_g = mkPen( color='g' )
self.pen_crv_b = mkPen( color='b' )
self.pen_crv_gr = mkPen( color='#d7d1cf' )
self.pen_crv_br = mkPen( color='#4D2619' )
# self.pen_crv_gr = mkPen( color='#342C2B' )
# self.pen_crv_gr = mkPen( color='#342C2B' )
self.pen_crv = [ self.pen_crv_r, self.pen_crv_m, self.pen_crv_g,
self.pen_crv_b, self.pen_crv_gr, self.pen_crv_br]
self.bsh_pnt_c = mkBrush( color='c' )
self.bsh_pnt_y = mkBrush( color='y' )
self.bsh_pnt_r = mkBrush( color='r' )
self.fnt = self.core.app.font( )
# Initialize the widget and it's plot's.
self.init_plt( )
# Populate the plots with the histograms (and labels), the
# selection points, and the fit curves.
self.make_crv( )
#-----------------------------------------------------------------------
# DEFINE THE FUNCTION FOR INITIALIZING THE WIDGET AND ITS PLOTS.
#-----------------------------------------------------------------------
def init_plt( self ) :
# Initialize the "GraphicsLayoutWidget" for this widget. This
# will allow a grid of "GraphicsItem" objects, which will
# include the plots themselves, the axes, and the axis labels.
# Note. The "QGridLayout" object given to this widget as its
# layout is essentially a dummy. I tried to just having
# this widget be an extention of "GraphicsLayoutWidget"
# (i.e., having it inheret that type), but I couldn't get
# it to display anything at all.
self.setLayout( QGridLayout( ) )
self.grd = GraphicsLayoutWidget( )
self.grd.setBackground( 'w' )
self.layout( ).addWidget( self.grd )
self.layout().setContentsMargins( 0, 0, 0, 0 )
# Initialize the text for the x- and y-axis labels. Then,
# create the labels themselves and add them to the grid.
self.txt_axs_x = 'Time [s]'
self.txt_axs_y = 'Magnetic Field [nT]'
if ( self.core.app.res_lo ) :
size = '8pt'
else :
size = '10pt'
self.lab_axs_x = LabelItem( self.txt_axs_x, angle=0 ,
color='b', size=size )
self.lab_axs_y = LabelItem( self.txt_axs_y, angle=270,
color='b', size=size )
self.grd.addItem( self.lab_axs_x, self.n_plt_y + 1, 2,
1, self.n_plt_x )
self.grd.addItem( self.lab_axs_y, 0, 0,
self.n_plt_y, 1 )
# Initialize the arrays that will contain the individual axes,
# plots, and plot elements (i.e., the histograms, fit curves,
# labels, and selection points).
self.plt = tile( None, [ self.n_plt_y, self.n_plt_x ] )
self.axs_x = tile( None, self.n_plt_x )
self.axs_y = tile( None, self.n_plt_y )
self.tsp = tile( None, [ self.n_plt_y, self.n_plt_x ] )
self.lbl = tile( None, [ self.n_plt_y, self.n_plt_x ] )
self.crv_raw = tile( None, [ self.n_plt_y, self.n_plt_x ] )
self.crv_smt = tile( None, [ self.n_plt_y, self.n_plt_x ] )
self.crv_bgd = tile( None, [ self.n_plt_y, self.n_plt_x ] )
self.crv_fit = tile( None, [ self.n_plt_y, self.n_plt_x ] )
# Initialize the scale-type for each axis, then generate the
# (default) axis-limits and adjusted axis-limits.
self.log_x = False
self.log_y = False
self.make_lim( None )
# Create, store, and add to the grid the individual axes: first
# the horizontal and then the vertical.
for i in range( self.n_plt_x ) :
self.axs_x[i] = AxisItem( 'bottom', maxTickLength=5 )
self.axs_x[i].setLogMode( self.log_x )
self.axs_x[i].setRange( self.lim_x[0], self.lim_x[1] )
self.axs_x[i].setTickFont( self.fnt )
if ( self.core.app.res_lo ) :
self.axs_x[i].setHeight( 10 )
else :
self.axs_x[i].setHeight( 20 )
self.grd.addItem( self.axs_x[i], self.n_plt_y, i + 2 )
for j in range( self.n_plt_y ) :
self.axs_y[j] = AxisItem( 'left', maxTickLength=5 )
self.axs_y[j].setLogMode( self.log_y )
self.axs_y[j].setRange( self.lim_y[0], self.lim_y[1] )
self.axs_y[j].setTickFont( self.fnt )
if ( self.core.app.res_lo ) :
self.axs_y[j].setWidth( 32 )
else :
self.axs_y[j].setWidth( 40 )
self.grd.addItem( self.axs_y[j], j, 1 )
# Create, store, and add to the grid the individual plots.
# Likewise, create, store, and add to each plot a label.
for j in range( self.n_plt_y ) :
for i in range( self.n_plt_x ) :
# Compute the plot number of this plot.
d = self.calc_ind_d( j, i )
# If creating this plot would exceed the
# specified number of plots, don't create it.
if ( d >= self.n_plt ) :
continue
# Create and store this plot, adjust its limits,
# and add it to the grid.
self.plt[j,i] = event_ViewBox( self,
border=self.pen_plt,
enableMouse=False,
enableMenu=False )
self.plt[j,i].setRange( xRange=self.lim_x,
yRange=self.lim_y,
padding=0. )
self.grd.addItem( self.plt[j,i], j, i + 2 )
# Create and store an (empty) label and add it
# to this plot.
self.lbl[j,i] = TextItem( anchor=(1,0) )
self.lbl[j,i].setFont( self.fnt )
self.plt[j,i].addItem( self.lbl[j,i] )
#-----------------------------------------------------------------------
# DEFINE THE FUNCTION FOR GENERATING AXIS-LIMITS (AND ADJUSTED LIMITS).
#-----------------------------------------------------------------------
def make_lim( self, d ) :
# If no spectrum has been loaded, use the default limits;
# otherwise, use the spectral data to compute axis limits.
if ( d == None ) :
self.lim_x = [ 0., 90. ]
self.lim_y = [ -3., 3. ]
else :
data1 = self.core.mfi_vec_raw[d]
data2 = self.core.b0_fields[self.key][d]
data3 = self.core.b0_db_fields[self.key][d]
if( d == 0) :
self.lim_y = [
min(
mean( data1 ) - 2*std( data1 )*2.**0.5,
mean( data2 ) - 1.5*std( data2 )*2.**0.5 ),
max(
mean( data1 ) + 2*std( data1 )*2.**0.5,
mean( data2 ) + 1.5*std( data2 )*2.**0.5 ) ]
print 'Works'
else :
self.lim_y = [
min(
mean( data2 ) - 2*std( data1 )*2.**0.5,
mean( data3 ) - 1.5*std( data3 )*2.**0.5 ),
max(
mean( data2 ) + 2*std( data1 )*2.**0.5,
mean( data3 ) + 1.5*std( data3 )*2.**0.5 ) ]
#
self.lim_x = [ -5, 95 ]
#-----------------------------------------------------------------------
# DEFINE THE FUNCTION FOR CREATING THE PLOTS' FIT CURVES.
#-----------------------------------------------------------------------
def make_crv( self, d_lst=None ) :
# If no "list" of "p" index-values has been provided by the
# user, assume that the curves in all plots should be
# (re-)rendered.
if ( self.core.fc_spec is None ) :
return
if ( d_lst is None ) :
d_lst = range( self.n_plt )
text = [ 'X-component', 'Y-component', 'Z-component' ]
self.key = self.core.mfi_set_key
N = int( self.core.opt['mom_run_win']/2 )
NN = -int ( self.core.opt['mom_run_win']/2-1 )
x = self.core.mfi_s[N:NN]
try :
y_raw = [ self.core.b0_fields[self.key][0][N:NN],
self.core.b0_fields[self.key][1][N:NN],
self.core.b0_fields[self.key][2][N:NN] ]
y_smt = [ self.core.b0_smt_fields[self.key][0][N:NN],
self.core.b0_smt_fields[self.key][1][N:NN],
self.core.b0_smt_fields[self.key][2][N:NN] ]
if( self.key == 'mfi_set_rng_avg' ) :
y_bgd = self.core.b0_avg_fields[self.key]
y_fit = self.core.b0_db_fields[self.key]
else :
y_bgd = [
self.core.b0_avg_fields[self.key][0][N:NN],
self.core.b0_avg_fields[self.key][1][N:NN],
self.core.b0_avg_fields[self.key][2][N:NN] ]
y_fit = [
self.core.b0_db_fields[self.key][0][N:NN],
self.core.b0_db_fields[self.key][1][N:NN],
self.core.b0_db_fields[self.key][2][N:NN] ]
except :
return
# For each plot in the grid, generate and display a fit curve
# based on the results of the analysis.
for d in d_lst :
# Determine the location of this plot within the grid
# layout.
j = self.calc_ind_j( d )
i = self.calc_ind_i( d )
# If this plot does not exist, move onto the next grid
# element.
if ( self.plt[j,i] is None ) :
continue
# If any curves already exist for this plot, remove and
# delete them.
if ( self.crv_raw[j,i] is not None ) :
self.plt[j,i].removeItem( self.crv_raw[j,i] )
self.crv_raw[j,i] = None
if ( self.crv_smt[j,i] is not None ) :
self.plt[j,i].removeItem( self.crv_smt[j,i] )
self.crv_smt[j,i] = None
if ( self.crv_bgd[j,i] is not None ) :
self.plt[j,i].removeItem( self.crv_bgd[j,i] )
self.crv_bgd[j,i] = None
if ( self.crv_fit[j,i] is not None ) :
self.plt[j,i].removeItem( self.crv_fit[j,i] )
self.crv_fit[j,i] = None
# Clear this plot's label of text.
self.lbl[j,i].setText( '' )
# Update this plot's label with appropriate text
# indicating the pointing direction.
txt = text[d]
self.lbl[j,i].setText( txt, color=(1,1,1) )
self.lbl[j,i].setFont( self.fnt )
# Create and add the curve of the individual
# contributions to the modeled current to the plot.
# Adjust this plot's limits and then move it's label in
# response.
try :
# Adjust the individual axes to the new limits.
self.make_lim( d )
self.plt[j,i].setRange( xRange=self.lim_x,
yRange=self.lim_y,
padding=0. )
self.axs_x[i].setRange( self.lim_x[0],
self.lim_x[1] )
self.axs_y[j].setRange( self.lim_y[0],
self.lim_y[1] )
# self.axs_y[j].set_yticks( range( self.lim_y[0],
# self.lim_y[1]),
# ( self.lim_y[1] - self.lim_y[0] )/6 )
self.lbl[j,i].setPos( self.lim_x[1],
self.lim_y[1] )
self.crv_raw[j,i] = PlotDataItem( x,
y_raw[d], pen = self.pen_crv[4] )
self.plt[j,i].addItem( self.crv_raw[j][i] )
self.crv_smt[j,i] = PlotDataItem( x, y_smt[d],
pen = self.pen_crv[5] )
self.plt[j,i].addItem( self.crv_smt[j][i] )
try:
self.crv_bgd[j,i] = PlotDataItem( x,
y_bgd[d], pen = self.pen_crv[0] )
except:
pass
try :
self.plt[j,i].addItem(
self.crv_bgd[j][i] )
except:
pass
# except :
# raise TypeError('Median filter length must be odd')
# pass
try:
self.crv_fit[j,i] = PlotDataItem( x,
y_fit[d], pen = self.pen_crv[1] )
except:
pass
try :
self.plt[j,i].addItem(
self.crv_fit[j][i] )
except:
pass
except :
# raise TypeError('Median filter length must be odd')
pass
#-----------------------------------------------------------------------
# DEFINE THE FUNCTION FOR RESETTING THE PLOTS' FIT CURVES.
#-----------------------------------------------------------------------
def rset_crv( self, rset_lbl=False ) :
# For each plot that exists in the grid, remove and delete its
# fit curves.
for j in range( self.n_plt_y ) :
for i in range( self.n_plt_x ) :
# If the plot does not exist, move onto the the
# next one.
if ( self.plt[j,i] is None ) :
continue
# Remove and delete this plot's fit curve.
if ( self.crv_raw[j,i] is not None ) :
self.plt[j,i].removeItem(
self.crv_raw[j,i] )
self.crv_raw[j,i] = None
if ( self.crv_smt[j,i] is not None ) :
self.plt[j,i].removeItem(
self.crv_smt[j,i] )
self.crv_smt[j,i] = None
if ( self.crv_bgd[j,i] is not None ) :
self.plt[j,i].removeItem(
self.crv_bgd[j,i] )
self.crv_bgd[j,i] = None
if ( self.crv_fit[j,i] is not None ) :
self.plt[j,i].removeItem(
self.crv_fit[j,i] )
self.crv_fit[j,i] = None
# If requested, reset this plot's label text to
# the empty string.
if ( rset_lbl ) :
self.lbl[j,i].setText( '',
color=(0,0,0) )
#-----------------------------------------------------------------------
# DEFINE THE FUNCTION CALCULATING THE INDEX "i" FROM THE INDEX "d".
#-----------------------------------------------------------------------
def calc_ind_i( self, d ) :
# Return the index "i" (i.e., column in the grid of plots)
# corresponding to the index "d" (i.e., look direction value)
# passed by the user.
return d % self.n_plt_x
#-----------------------------------------------------------------------
# DEFINE THE FUNCTION CALCULATING THE INDEX "j" FROM THE INDEX "d".
#-----------------------------------------------------------------------
def calc_ind_j( self, d ) :
# Return the index "j" (i.e., row in the grid of plots)
# corresponding to the index "d" (i.e., look direction value)
# passed by the user.
return int( floor( d / ( 1. * self.n_plt_x ) ) )
#-----------------------------------------------------------------------
# DEFINE THE FUNCTION CALCULATING INDEX "d" FROM INDICES "j" AND "i".
#-----------------------------------------------------------------------
def calc_ind_d( self, j, i ) :
# Return the index "d" (i.e., look direction value)
# corresponding to the indices "j" and "i" (i.e., location in
# the grid of plots) passed by the user.
return i + ( j * self.n_plt_x )
#-----------------------------------------------------------------------
# DEFINE THE FUNCTION FOR RESPONDING TO THE "rset" SIGNAL.
#-----------------------------------------------------------------------
def resp_rset( self ) :
# Clear the plots of all their elements.
self.rset_crv( )
#-----------------------------------------------------------------------
# DEFINE THE FUNCTION FOR RESPONDING TO THE "chng_mfi" SIGNAL.
#-----------------------------------------------------------------------
def resp_chng_mfi( self ) :
# Clear the plots of all their elements and regenerate them.
self.rset_crv( )
self.make_crv( )
#-----------------------------------------------------------------------
# DEFINE THE FUNCTION FOR RESPONDING TO A CHANGE OF AN OPTION.
#-----------------------------------------------------------------------
def resp_chng_opt( self ) :
# Regenerate the menu.
self.make_crv( )
#-----------------------------------------------------------------------
# DEFINE THE FUNCTION FOR RESPONDING TO RESTORING DEFAULT OPTIONS.
#-----------------------------------------------------------------------
def resp_rstr_opt( self ) :
# Regenerate the menu.
self.make_crv( )