-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot_objects.py
More file actions
1631 lines (1239 loc) · 54 KB
/
plot_objects.py
File metadata and controls
1631 lines (1239 loc) · 54 KB
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -------------------------------------------------------------------------
# Copyright (C) 2005-2013 Martin Strohalm <www.mmass.org>
# 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.
# Complete text of GNU GPL can be found in the file LICENSE.TXT in the
# main directory of the program.
# -------------------------------------------------------------------------
# load libs
import wx
import numpy
import copy
# load modules
import mod_signal
import calculations
# MAIN PLOT OBJECTS
# -----------------
class container:
"""Container to hold plot objects."""
def __init__(self, objects):
self.objects = objects
# ----
def __additem__(self, obj):
self.objects.append(obj)
# ----
def __delitem__(self, index):
del self.objects[index]
# ----
def __setitem__(self, index, obj):
self.objects[index] = obj
# ----
def __getitem__(self, index):
return self.objects[index]
# ----
def __len__(self):
return len(self.objects)
# ----
def getBoundingBox(self, minX=None, maxX=None, absolute=False):
"""Get bounding box coverring all visible objects."""
# init values if no data in objects
rect = [numpy.array([0, 0]), numpy.array([1, 1])]
# get bouding boxes from objects
have = False
for obj in self.objects:
if obj.properties['visible']:
oRect = obj.getBoundingBox(minX, maxX, absolute)
if not oRect or not numpy.all(numpy.isfinite(oRect)):
continue
elif have and oRect:
rect[0] = numpy.minimum(rect[0], oRect[0])
rect[1] = numpy.maximum(rect[1], oRect[1])
elif oRect:
rect = oRect
have = True
# check scale
if rect[0][0] == rect[1][0]:
rect[0][0] -= 0.5
rect[1][0] += 0.5
if rect[0][1] == rect[1][1]:
rect[1][1] += 0.5
return rect
# ----
def getLegend(self):
"""Get a list of legend names."""
# get names
names = []
for obj in self.objects:
if obj.properties['visible']:
legend = obj.getLegend()
if legend and legend[0] != '':
names.append(obj.getLegend())
return names
# ----
def getPoint(self, obj, xPos, coord='screen'):
"""Get interpolated Y position for given X."""
return self.objects[obj].getPoint(xPos, coord)
# ----
def countGels(self):
"""Get number of visible gels."""
count = 0
for obj in self.objects:
if obj.properties['visible'] and obj.properties['showInGel']:
count += 1
return max(count,1)
# ----
def cropPoints(self, minX, maxX):
"""Crop points in all visible objects to selected X range."""
for obj in self.objects:
if obj.properties['visible']:
obj.cropPoints(minX, maxX)
# ----
def scaleAndShift(self, scale, shift):
"""Scale and shift all visible objects."""
for obj in self.objects:
if obj.properties['visible']:
obj.scaleAndShift(scale, shift)
# ----
def filterPoints(self, filterSize):
"""Filter points in all visible objects."""
for obj in self.objects:
if obj.properties['visible']:
obj.filterPoints(filterSize)
# ----
def draw(self, dc, printerScale, overlapLabels, reverse):
"""Draw all visible objects."""
# draw in reverse order
if reverse:
self.objects.reverse()
# draw objects
for obj in self.objects:
if obj.properties['visible']:
obj.draw(dc, printerScale)
# draw object's labels
self.drawLabels(dc, printerScale, overlapLabels)
# reverse back order
if reverse:
self.objects.reverse()
# ----
def drawLabels(self, dc, printerScale, overlapLabels):
"""Draw labels for all visible objects."""
# get labels from objects
annots = []
labels = []
for obj in self.objects:
if obj.properties['visible'] and isinstance(obj, annotations):
annots += obj.makeLabels(dc, printerScale)
elif obj.properties['visible']:
labels += obj.makeLabels(dc, printerScale)
# check labels
if not annots and not labels:
return
# sort labels
annots.sort()
annots.reverse()
labels.sort()
labels.reverse()
labels = annots + labels
# preset font by first label
font = labels[0][3]['labelFont']
colour = labels[0][3]['labelColour']
bgr = labels[0][3]['labelBgr']
bgrColour = labels[0][3]['labelBgrColour']
dc.SetFont(_scaleFont(font, printerScale['fonts']))
dc.SetTextForeground(colour)
dc.SetTextBackground(bgrColour)
if bgr:
dc.SetBackgroundMode(wx.SOLID)
# draw labels
occupied = []
for label in labels:
text = label[1]
textCoords = label[2]
properties = label[3]
# check limits
if abs(textCoords[1]) > 10000000:
continue
# check free space and draw label
if overlapLabels or self._checkFreeSpace(textCoords, occupied):
# check pen
if properties['labelFont'] != font:
font = properties['labelFont']
dc.SetFont(_scaleFont(font, printerScale['fonts']))
if properties['labelColour'] != colour:
colour = properties['labelColour']
dc.SetTextForeground(colour)
#if properties['labelBgrColour'] != bgrColour:
# bgrColour = properties['labelBgrColour']
# dc.SetTextBackground(bgrColour)
if properties['labelBgr'] != bgr:
bgr = properties['labelBgr']
if bgr:
dc.SetBackgroundMode(wx.SOLID)
else:
dc.SetBackgroundMode(wx.TRANSPARENT)
# set angle
angle = properties['labelAngle']
if angle == 90 and properties['flipped']:
angle = -90
# draw label
dc.DrawRotatedText(text, textCoords[0], textCoords[1], angle)
occupied.append(textCoords)
dc.SetBackgroundMode(wx.TRANSPARENT)
# ----
def drawGel(self, dc, gelCoords, gelHeight, printerScale):
"""Draw gel for all allowed objects."""
# draw objects
for obj in self.objects:
if obj.properties['visible'] and obj.properties['showInGel']:
obj.drawGel(dc, gelCoords, gelHeight, printerScale)
gelCoords[0] += gelHeight
# ----
def append(self, obj):
self.objects.append(obj)
# ----
def empty(self):
del self.objects[:]
# ----
def _checkFreeSpace(self, coords, occupied):
"""Check free space for label."""
curX1, curY1, curX2, curY2 = coords
# check occupied space
for occX1, occY1, occX2, occY2 in occupied:
if (curX1 < curX2) and ((occX1 <= curX1 <= occX2) or (occX1 <= curX2 <= occX2) or (curX1 <= occX1 and curX2 >= occX2)):
if (occY2 <= curY1 <= occY1) or (occY2 <= curY2 <= occY1) or (curY1 >= occY1 and curY2 <= occY2):
return False
elif (curX1 > curX2) and ((occX2 <= curX1 <= occX1) or (occX2 <= curX2 <= occX1) or (curX1 <= occX2 and curX2 >= occX1)):
if (occY1 <= curY1 <= occY2) or (occY1 <= curY2 <= occY2) or (curY1 >= occY2 and curY2 <= occY1):
return False
return True
# ----
class annotations:
"""Base class for annotations drawing."""
def __init__(self, points, **attr):
# set default params
self.properties = {
'visible': True,
'flipped': False,
'xOffset': 0,
'yOffset': 0,
'normalized': False,
'showInGel': False,
'exactFit': False,
'showPoints': True,
'showLabels': True,
'showXPos': True,
'pointColour': (0, 0, 255),
'pointSize': 3,
'labelAngle': 90,
'labelBgr': True,
'labelColour': (0, 0, 0),
'labelBgrColour': (255, 255, 255),
'labelFont': wx.Font(10, wx.SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0),
'labelMaxLength': 20,
'xPosDigits': 2,
}
self.currentScale = (1., 1.)
self.currentShift = (0., 0.)
self.normalization = 1.0
# get new attributes
for name, value in attr.items():
self.properties[name] = value
# convert points to array
self.points = numpy.array([[p[0], p[1]] for p in points])
self.pointsCropped = self.points
self.pointsScaled = self.pointsCropped
if len(self.points):
self.pointsBox = (numpy.minimum.reduce(self.points), numpy.maximum.reduce(self.points))
# get labels
self.labels = ['']*len(points)
for x, point in enumerate(points):
if len(point) > 2:
self.labels[x] = point[2]
self.labelsCropped = self.labels
# calculate normalization
self._normalization()
# ----
def setProperties(self, **attr):
"""Set object properties."""
for name, value in attr.items():
self.properties[name] = value
# ----
def setNormalization(self, value):
"""Force specified normalization to be used insted of calculated one."""
value = float(value)
if value == 0.0:
value = 1.0
self.normalization = value
# ----
def getBoundingBox(self, minX=None, maxX=None, absolute=False):
"""Get bounding box for whole data or X selection"""
# use relevant data
if minX != None and maxX != None:
self.cropPoints(minX, maxX)
data = self.pointsCropped
else:
data = self.points
# check data
if not len(data):
return False
# get range
if minX != None and maxX != None:
minXY = numpy.minimum.reduce(data)
maxXY = numpy.maximum.reduce(data)
else:
minXY = [self.pointsBox[0][0], self.pointsBox[0][1]]
maxXY = [self.pointsBox[1][0], self.pointsBox[1][1]]
# extend values slightly to fit data
if not absolute and not self.properties['exactFit']:
xExtend = (maxXY[0] - minXY[0]) * 0.05
yExtend = (maxXY[1] - minXY[1]) * 0.05
minXY[0] -= xExtend
maxXY[0] += xExtend
minXY[1] -= yExtend
maxXY[1] += yExtend
# extend values to fit labels
elif not absolute:
if self.properties['showLabels'] and self.properties['labelAngle']==0:
maxXY[1] += (maxXY[1] - minXY[1]) * 0.2
elif self.properties['showLabels'] and self.properties['labelAngle']==90:
maxXY[1] += (maxXY[1] - minXY[1]) * 0.4
else:
maxXY[1] += (maxXY[1] - minXY[1]) * 0.05
# apply normalization
if self.properties['normalized']:
minXY[1] = minXY[1] / self.normalization
maxXY[1] = maxXY[1] / self.normalization
# apply offset
minXY[0] += self.properties['xOffset']
minXY[1] += self.properties['yOffset']
maxXY[0] += self.properties['xOffset']
maxXY[1] += self.properties['yOffset']
# apply flipping
if self.properties['flipped']:
minY = -1 * maxXY[1]
maxY = -1 * minXY[1]
minXY[1] = minY
maxXY[1] = maxY
return [minXY, maxXY]
# ----
def getLegend(self):
"""Get legend."""
return None
# ----
def cropPoints(self, minX, maxX):
"""Crop points to selected X range."""
# apply offset
minX -= self.properties['xOffset']
maxX -= self.properties['xOffset']
# get indexes of points in selection
i1 = mod_signal.locate(self.points, minX)
i2 = mod_signal.locate(self.points, maxX)
# crop data
self.pointsCropped = self.points[i1:i2]
self.labelsCropped = self.labels[i1:i2]
# ----
def scaleAndShift(self, scale, shift):
"""Scale and shift points to screen coordinations."""
self.pointsScaled = self.pointsCropped
xScale = scale[0]
yScale = scale[1]
xShift = shift[0]
yShift = shift[1]
# apply flipping
if self.properties['flipped']:
yScale *= -1
# apply normalization
if self.properties['normalized']:
yScale /= self.normalization
# apply offset
xShift += self.properties['xOffset'] * xScale
yShift += self.properties['yOffset'] * yScale
# recalculate data
self.pointsScaled = _scaleAndShift(self.pointsCropped, xScale, yScale, xShift, yShift)
self.currentScale = scale
self.currentShift = shift
# ----
def filterPoints(self, filterSize):
"""Filter points for printing and exporting"""
pass
# ----
def draw(self, dc, printerScale):
"""Draw object."""
# check data
if not len(self.pointsScaled):
return
# draw points
if self.properties['showPoints']:
pencolour = [max(x-70,0) for x in self.properties['pointColour']]
pen = wx.Pen(pencolour, 1*printerScale['drawings'], wx.SOLID)
brush = wx.Brush(self.properties['pointColour'], wx.SOLID)
dc.SetPen(pen)
dc.SetBrush(brush)
for point in self.pointsScaled:
dc.DrawCircle(point[0], point[1], self.properties['pointSize']*printerScale['drawings'])
# ----
def drawGel(self, dc, gelCoords, gelHeight, printerScale):
"""Draw gel."""
pass
# ----
def makeLabels(self, dc, printerScale):
"""Get object labels."""
# check labels
if not self.properties['showLabels'] or not self.labelsCropped:
return []
# set font
dc.SetFont(_scaleFont(self.properties['labelFont'], printerScale['fonts']))
# prepare labels
labels = []
format = '%0.'+`self.properties['xPosDigits']`+'f - '
for x, label in enumerate(self.labelsCropped):
# check max length
if len(label) > self.properties['labelMaxLength']:
label = label[:self.properties['labelMaxLength']] + '...'
# add X position
if self.properties['showXPos']:
label = (format % self.pointsCropped[x][0]) + label
# get position
xPos = self.pointsScaled[x][0]
yPos = self.pointsScaled[x][1]
# get text position
textSize = dc.GetTextExtent(label)
if self.properties['labelAngle'] == 90:
if self.properties['flipped']:
textXPos = xPos + textSize[1]*0.5
textYPos = yPos + 5*printerScale['drawings']
textCoords = (textXPos, textYPos, textXPos-textSize[1], textYPos+textSize[0])
else:
textXPos = xPos - textSize[1]*0.5
textYPos = yPos - 5*printerScale['drawings']
textCoords = (textXPos, textYPos, textXPos+textSize[1], textYPos-textSize[0])
elif self.properties['labelAngle'] == 0:
if self.properties['flipped']:
textXPos = xPos - textSize[0]*0.5
textYPos = yPos + 4*printerScale['drawings']
textCoords = (textXPos, textYPos, textXPos+textSize[0], textYPos-textSize[1])
else:
textXPos = xPos - textSize[0]*0.5
textYPos = yPos - textSize[1] - 4*printerScale['drawings']
textCoords = (textXPos, textYPos, textXPos+textSize[0], textYPos-textSize[1])
# add label and sort by intensity
labels.append((self.pointsCropped[x][1], label, textCoords, self.properties))
return labels
# ----
def _normalization(self):
"""Calculate normalization constants."""
normalization = 1.0
# calc normalization
if len(self.points):
normalization = self.pointsBox[1][1] / 100.
# check value
if normalization == 0.0:
normalization = 1.0
# set value
self.normalization = normalization
# ----
class points:
"""Base class for polypoints and polylines drawing."""
def __init__(self, points, **attr):
# set default params
self.properties = {
'legend': '',
'visible': True,
'flipped': False,
'xOffset': 0,
'yOffset': 0,
'normalized': False,
'showInGel': False,
'exactFit': False,
'showPoints': True,
'pointColour': (0, 0, 255),
'pointSize': 3,
'fillPoints': True,
'showLines': True,
'lineColour': (0, 0, 255),
'lineWidth': 1,
'lineStyle': wx.SOLID,
'xOffsetDigits': 2,
'yOffsetDigits': 0,
}
self.currentScale = (1., 1.)
self.currentShift = (0., 0.)
self.normalization = 1.0
# get new attributes
for name, value in attr.items():
self.properties[name] = value
# convert points to array
self.points = numpy.array(points)
self.cropped = self.points
self.scaled = self.cropped
if len(self.points):
self.pointsBox = (numpy.minimum.reduce(self.points), numpy.maximum.reduce(self.points))
# calculate normalization
self._normalization()
# ----
def setProperties(self, **attr):
"""Set object properties."""
for name, value in attr.items():
self.properties[name] = value
# ----
def setNormalization(self, value):
"""Force specified normalization to be used insted of calculated one."""
value = float(value)
if value == 0.0:
value = 1.0
self.normalization = value
# ----
def getBoundingBox(self, minX=None, maxX=None, absolute=False):
"""Get bounding box for whole data or X selection"""
# use relevant data
if minX != None and maxX != None:
self.cropPoints(minX, maxX)
data = self.cropped
else:
data = self.points
# check data
if not len(data):
return False
# get range
if minX != None and maxX != None:
minXY = numpy.minimum.reduce(data)
maxXY = numpy.maximum.reduce(data)
else:
minXY = [self.pointsBox[0][0], self.pointsBox[0][1]]
maxXY = [self.pointsBox[1][0], self.pointsBox[1][1]]
# extend values slightly to fit data
if not absolute and not self.properties['exactFit']:
xExtend = (maxXY[0] - minXY[0]) * 0.05
yExtend = (maxXY[1] - minXY[1]) * 0.05
minXY[0] -= xExtend
maxXY[0] += xExtend
minXY[1] -= yExtend
maxXY[1] += yExtend
# apply normalization
if self.properties['normalized']:
minXY[1] = minXY[1] / self.normalization
maxXY[1] = maxXY[1] / self.normalization
# apply offset
minXY[0] += self.properties['xOffset']
minXY[1] += self.properties['yOffset']
maxXY[0] += self.properties['xOffset']
maxXY[1] += self.properties['yOffset']
# apply flipping
if self.properties['flipped']:
minY = -1 * maxXY[1]
maxY = -1 * minXY[1]
minXY[1] = minY
maxXY[1] = maxY
return [minXY, maxXY]
# ----
def getLegend(self):
"""Get legend."""
# get legend
legend = self.properties['legend']
offset = ''
# add current offset
if not self.properties['normalized']:
if self.properties['xOffset']:
format = ' X%0.'+`self.properties['xOffsetDigits']`+'f'
offset += format % self.properties['xOffset']
if self.properties['yOffset']:
format = ' Y%0.'+`self.properties['yOffsetDigits']`+'f'
offset += format % self.properties['yOffset']
if legend and offset:
legend += ' (Offset%s)' % offset
# set colour
if self.properties['showPoints']:
return (legend, self.properties['pointColour'])
else:
return (legend, self.properties['lineColour'])
# ----
def cropPoints(self, minX, maxX):
"""Crop points to selected X range."""
# apply offset
minX -= self.properties['xOffset']
maxX -= self.properties['xOffset']
# crop line
if self.properties['showLines']:
self.cropped = mod_signal.crop(self.points, minX, maxX)
# crop points
else:
i1 = mod_signal.locate(self.points, minX)
i2 = mod_signal.locate(self.points, maxX)
self.cropped = self.points[i1:i2]
# ----
def scaleAndShift(self, scale, shift):
"""Scale and shift points to screen coordinations."""
self.scaled = self.cropped
xScale = scale[0]
yScale = scale[1]
xShift = shift[0]
yShift = shift[1]
# apply flipping
if self.properties['flipped']:
yScale *= -1
# apply normalization
if self.properties['normalized']:
yScale /= self.normalization
# apply offset
xShift += self.properties['xOffset'] * xScale
yShift += self.properties['yOffset'] * yScale
# recalculate data
if len(self.cropped):
self.scaled = _scaleAndShift(self.cropped, xScale, yScale, xShift, yShift)
self.currentScale = scale
self.currentShift = shift
# ----
def filterPoints(self, filterSize):
"""Filter points for printing and exporting"""
# filter data
if len(self.scaled) and self.properties['showLines']:
self.scaled = _filterPoints(self.scaled, filterSize)
# ----
def draw(self, dc, printerScale):
"""Draw object."""
# check data
if not len(self.scaled):
return
# draw lines
if self.properties['showLines'] and len(self.scaled) > 1:
pen = wx.Pen(self.properties['lineColour'], self.properties['lineWidth']*printerScale['drawings'], self.properties['lineStyle'])
brush = wx.Brush(self.properties['lineColour'], wx.SOLID)
dc.SetPen(pen)
dc.SetBrush(brush)
dc.DrawLines(self.scaled)
# draw points
if self.properties['showPoints']:
if self.properties['fillPoints']:
pencolour = [max(x-70,0) for x in self.properties['pointColour']]
pen = wx.Pen(pencolour, self.properties['lineWidth']*printerScale['drawings'], wx.SOLID)
brush = wx.Brush(self.properties['pointColour'], wx.SOLID)
else:
pencolour = self.properties['pointColour']
pen = wx.Pen(pencolour, self.properties['lineWidth']*printerScale['drawings'], wx.SOLID)
brush = wx.TRANSPARENT_BRUSH
dc.SetPen(pen)
dc.SetBrush(brush)
for point in self.scaled:
dc.DrawCircle(point[0], point[1], self.properties['pointSize']*printerScale['drawings'])
# ----
def drawGel(self, dc, gelCoords, gelHeight, printerScale):
"""Draw gel."""
pass
# ----
def makeLabels(self, dc, printerScale):
"""Get object labels."""
return []
# ----
def _normalization(self):
"""Calculate normalization constants."""
normalization = 1.0
# calc normalization
if len(self.points):
normalization = self.pointsBox[1][1] / 100.
# check value
if normalization == 0.0:
normalization = 1.0
# set value
self.normalization = normalization
# ----
class spectrum:
"""Base class for spectrum drawing."""
def __init__(self, scan, **attr):
# set default params
self.properties = {
'legend': '',
'visible': True,
'flipped': False,
'xOffset': 0,
'yOffset': 0,
'normalized': False,
'showInGel': True,
'showSpectrum': True,
'showPoints': True,
'showLabels': True,
'showIsotopicLabels': True,
'showTicks': True,
'showGelLegend': True,
'spectrumColour': (0, 0, 255),
'spectrumWidth': 1,
'spectrumStyle': wx.SOLID,
'labelAngle': 90,
'labelDigits': 2,
'labelCharge': False,
'labelGroup': False,
'labelBgr': True,
'labelColour': (0, 0, 0),
'labelBgrColour': (255, 255, 255),
'labelFont': wx.Font(10, wx.SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0),
'tickColour': (200, 200, 200),
'isotopeColour': None,
'msmsColour': None,
'tickWidth': 1,
'tickStyle': wx.SOLID,
'xOffsetDigits': 2,
'yOffsetDigits': 0,
}
self.currentScale = (1., 1.)
self.currentShift = (0., 0.)
self.normalization = 1.0
# get new attributes
for name, value in attr.items():
self.properties[name] = value
# convert spectrum points to array
self.spectrumPoints = numpy.array(scan.profile)
self.spectrumCropped = self.spectrumPoints
self.spectrumScaled = self.spectrumCropped
if len(self.spectrumPoints):
self.spectrumBox = (numpy.minimum.reduce(self.spectrumPoints), numpy.maximum.reduce(self.spectrumPoints))
# convert peaklist points to array
self.peaklist = copy.deepcopy(scan.peaklist)
self.peaklistPoints = numpy.array([[peak.mz, peak.ai, peak.base] for peak in scan.peaklist])
self.peaklistCropped = self.peaklistPoints
self.peaklistScaled = self.peaklistCropped
self.peaklistCroppedPeaks = self.peaklist[:]
if len(self.peaklistPoints):
self.peaklistBox = (numpy.minimum.reduce(self.peaklistPoints), numpy.maximum.reduce(self.peaklistPoints))
# calculate normalization
self._normalization()
# ----
def setProperties(self, **attr):
"""Set object properties."""
for name, value in attr.items():
self.properties[name] = value
# ----
def setNormalization(self, value):
"""Force specified normalization to be used insted of calculated one."""
value = float(value)
if value == 0.0:
value = 1.0
self.normalization = value
# ----
def getBoundingBox(self, minX=None, maxX=None, absolute=False):
"""Get bounding box for whole data or X selection."""
spectrumBox = None
peaklistBox = None
# use relevant data
if minX != None and maxX != None:
self.cropPoints(minX, maxX)
spectrumData = self.spectrumCropped
peaklistData = self.peaklistCropped
else:
spectrumData = self.spectrumPoints
peaklistData = self.peaklistPoints
# calculate bounding box for spectrum
if len(spectrumData) and self.properties['showSpectrum']:
if minX != None and maxX != None:
minXY = numpy.minimum.reduce(spectrumData)
maxXY = numpy.maximum.reduce(spectrumData)
else:
minXY = [self.spectrumBox[0][0], self.spectrumBox[0][1]]
maxXY = [self.spectrumBox[1][0], self.spectrumBox[1][1]]
if not absolute:
maxXY[1] += (maxXY[1] - minXY[1]) * 0.05
spectrumBox = [minXY, maxXY]
# calculate bounding box for peaklist
if len(peaklistData) and (self.properties['showSpectrum'] or self.properties['showLabels'] or self.properties['showTicks']):
if minX != None and maxX != None:
minXY = numpy.minimum.reduce(peaklistData)
maxXY = numpy.maximum.reduce(peaklistData)
else:
minXY = [self.peaklistBox[0][0], self.peaklistBox[0][1], self.peaklistBox[0][2]]
maxXY = [self.peaklistBox[1][0], self.peaklistBox[1][1], self.peaklistBox[1][2]]
minXY = [minXY[0], min(minXY[1:])]
maxXY = [maxXY[0], max(maxXY[1:])]
# extend values to fit labels
if not absolute:
xExtend = (maxXY[0] - minXY[0]) * 0.02
minXY[0] -= xExtend
maxXY[0] += xExtend