/
pipe_proc.py
3173 lines (2713 loc) · 89.4 KB
/
pipe_proc.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
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
"""
NMRPipe like processing functions for use with the
:py:mod:`nmrglue.fileio.pipe` module.
These functions attempt to mimic NMRPipe's processing functions but small
differences exist between to two implementations. In particular when using
this module:
* hdr=True overrides all values in the calling function.
* A di flag is not used, rather the :py:func:`di` function should be used
to delete the imaginary portion of a spectra.
* x1, xn and other limits must be expressed in points. A unit conversion
object function should be used before calling the processing function to
calculate these values.
* No functions implement the dmx or nodmx flags.
Additional differences from NMRPipe's functions are documented in the
individual processing functions.
The following functions have not been implemented and will raise a
NotImplemented exception:
* ann Fourier Analysis by Neural Net
* ebs EBS Reconstruction
* mac Macro Language Interpreter
* mem Maximum Entropy
* ml Maximum likelyhood frequency
* poly Polynomail baseline correction
* xyz2zyx 3D matrix transpose
* ztp 3D matrix transpose
"""
import numpy as np
# nmrglue modules
from ..fileio import pipe, fileiobase
from . import proc_base as p
from . import proc_bl
from . import proc_lp
pi = np.pi
###################
# Unit conversion #
###################
class unit_conversion(fileiobase.unit_conversion):
"""
Unit converter class that returns NMRPipe like index values. Useful
when calling pipe_proc functions
"""
# NMRPipe indexes from 1 to MAX instead on 0 to MAX-1
# we need to modify two method to account for this off by one problem
def __unit2pnt(self, val, units):
return fileiobase.unit_conversion.__unit2pnt(self, val, units) + 1
def __pnt2unit(self, val, units):
return fileiobase.unit_conversion.__pnt2unit(self, val - 1, units)
def make_uc(dic, data, dim=-1):
"""
Create a unit conversion object which accepts/returns NMRPipe indices.
Parameters
----------
dic : dict
Dictionary of NMRPipe parameters
data : ndarray
Array of NMR data.
dim : int, optional
Dimension number to create unit conversion object for. Default is for
last (direct) dimension.
Returns
-------
uc : unit conversion object
Unit conversion object for the given dimension which accepts and returns
NMRPipe indices (starting from 1).
"""
if dim == -1:
dim = data.ndim - 1 # last dimention
fn = "FDF" + str(int(dic["FDDIMORDER"][data.ndim - 1 - dim]))
size = float(data.shape[dim])
# check for quadrature in indirect dimentions
if (dic[fn + "QUADFLAG"] != 1) and (dim != data.ndim - 1):
size = size / 2.
cplx = True
else:
cplx = False
sw = dic[fn + "SW"]
if sw == 0.0:
sw = 1.0
obs = dic[fn + "OBS"]
if obs == 0.0:
obs = 1.0
car = dic[fn + "CAR"] * obs
return unit_conversion(size, cplx, sw, obs, car)
########################
# Dictionary functions #
########################
def recalc_orig(dic, data, fn, axis=-1):
"""
Recalculate the origin for given axis
"""
# ORIG calculation
s = float(data.shape[axis])
# This really should check that the axis is not the last...
if dic[fn + "QUADFLAG"] == 0 and axis != -1:
s = int(s / 2.)
# correct TPPI size in indirect dim when in time domain
if dic["FD2DPHASE"] == 1 and fn != "FDF2" and dic[fn + "FTFLAG"] != 1:
s = int(s / 2.)
sw = dic[fn + "SW"]
car = dic[fn + "CAR"]
obs = dic[fn + "OBS"]
s2 = float(dic[fn + "CENTER"])
# DEBUG
#print "Recalc of origin"
#print "s:",s
#print "axis:",axis
#print "sw:",sw
#print "car:",car
#print "obs:",obs
#print "s2:",s2
dic[fn + "ORIG"] = car * obs - sw * ((s - s2) / s)
return dic
def update_minmax(dic, data):
"""
Update the MAX/MIN dictionary keys.
"""
# maximum and minimum values
dic["FDMAX"] = float(data.max().real)
dic["FDDISPMAX"] = dic["FDMAX"]
dic["FDMIN"] = float(data.min().real)
dic["FDDISPMIN"] = dic["FDMIN"]
dic["FDSCALEFLAG"] = 1.0 # FDMIN/MAX are valid
return dic
def clean_minmax(dic):
"""
Clean (set to zero) the MAX/MIN dictionary keys.
"""
# maximum and minimum values
dic["FDMAX"] = 0.0
dic["FDDISPMAX"] = 0.0
dic["FDMIN"] = 0.0
dic["FDDISPMIN"] = 0.0
dic["FDSCALEFLAG"] = 0.0 # FDMIN/MAX not valid
return dic
#########################
# Apodization functions #
#########################
def apod(dic, data, qName=None, q1=1.0, q2=1.0, q3=1.0, c=1.0, start=1,
size='default', inv=False, one=False, hdr=False):
"""
Generic apodization.
Parameters
----------
dic : dict
Dictionary of NMRPipe parameters.
data : ndarray
Array of NMR data.
qName : {'SP', 'EM', 'GM', 'GMB', 'TM', 'TRI', 'JMOD'}
Abbreviation of apodization function the apply. See the specific
apodization function for a description.
q1 : float
First apodization function parameter. See specific apodization function
for details.
q2 : float
Second apodization function parameter. See specific apodization function
for details.
q3 : float
Third apodization function parameter. See specific apodization function
for details.
c : float
First point scale value.
start : int, optional
Starting location of apodization window. Default is the first point, 1.
size : int, optional
Size of the apodization window. Default ('default') is the full size of
the active dimension.
inv : bool, optional
True for inverse apodization, False for normal apodization.
one : bool, optional
True to set points outside of window to 1. False leaves points outside
the apodization window as is.
hdr : bool, optional
True to read apodization parameters from the the parameters in dic.
Returns
-------
ndic : dict
Dictionary of updated NMRPipe parameters.
ndata : ndarray
Array of NMR data with apodization applied.
See Also
--------
em : Exponential apodization.
gm : Lorentz-to-Gauss apodization.
gmb : Modified Gaussian apodization.
jmod : Exponentially damped J-modulation apodization.
sp : Sine bell apodization.
tm : Trapezoid apodization.
tri : Triangular apodization.
"""
if hdr:
fn = "FDF" + str(int(dic["FDDIMORDER"][0])) # F1, F2, etc
qnum = dic[fn + "APODCODE"]
qName = ["", "SP", "EM", "GM", "TM", "", "TRI", "GMB", "JMOD"][qnum]
# Set apod codes here so that all three parameter are set
fn = "FDF" + str(int(dic["FDDIMORDER"][0])) # F1, F2, etc
dic[fn + "APODQ1"] = q1
dic[fn + "APODQ2"] = q2
dic[fn + "APODQ3"] = q3
if qName == "EM":
return em(dic, data, q1, c, start, size, inv, one, hdr)
elif qName == "GM":
return gm(dic, data, q1, q2, q3, c, start, size, inv, one, hdr)
elif qName == "GMB":
return gmb(dic, data, q1, q2, c, start, size, inv, one, hdr)
elif qName == "JMOD":
return jmod(dic, data, q1, q2, q3, False, False, c, start, size, inv,
one, hdr)
elif qName == "SP":
return sp(dic, data, q1, q2, q3, c, start, size, inv, one, hdr)
elif qName == "TM":
return tm(dic, data, q1, q2, c, start, size, inv, one, hdr)
elif qName == "TRI":
return tri(dic, data, q1, q2, q3, c, start, size, inv, one, hdr)
else:
raise ValueError("qName must be SP, EM, GM, GMB, TM, TRI or JMOD")
def em(dic, data, lb=0.0, c=1.0, start=1, size='default', inv=False, one=False,
hdr=False):
"""
Exponential apodization.
Parameters
----------
dic : dict
Dictionary of NMRPipe parameters.
data : ndarray
Array of NMR data.
lb : float
Exponential line broadening in Hz.
c : float
First point scale value.
start : int, optional
Starting location of apodization window. Default is the first point, 1.
size : int, optional
Size of the apodization window. Default ('default') is the full size of
the active dimension.
inv : bool, optional
True for inverse apodization, False for normal apodization.
one : bool, optional
True to set points outside of window to 1. False leaves points outside
the apodization window as is.
hdr : bool, optional
True to read apodization parameters from the the parameters in dic.
Returns
-------
ndic : dict
Dictionary of updated NMRPipe parameters.
ndata : ndarray
Array of NMR data with exponential apodization applied.
"""
start = start - 1 # arrays should start at 0
# update dictionary
fn = "FDF" + str(int(dic["FDDIMORDER"][0])) # F1, F2, etc
if hdr: # read apod values from data headers
c = dic[fn + "C1"] + 1
lb = dic[fn + "APODQ1"]
dic[fn + "C1"] = c - 1.0
# set the apod flags
dic[fn + "APODCODE"] = 2.0
dic[fn + "APODQ1"] = lb
dic[fn + "APODQ2"] = 0.0
dic[fn + "APODQ3"] = 0.0
sw = dic[fn + "SW"]
flb = lb / sw
# apply apodization to data
if start == 0 and size == 'default':
data = p.em(data, lb=flb, inv=inv)
else: # only part of the data window is apodized
if size == 'default':
stop = data.shape[-1]
else:
stop = start+size
data[..., start:stop] = p.em(data[..., start:stop], lb=flb, inv=inv)
if one == False:
data[..., :start] = 0.0
data[..., stop:] = 0.0
# first point scaling
if inv:
data[..., 0] = data[..., 0] / c
else:
data[..., 0] = data[..., 0] * c
dic = update_minmax(dic, data)
return dic, data
def gm(dic, data, g1=0.0, g2=0.0, g3=0.0, c=1.0, start=1, size='default',
inv=False, one=False, hdr=False):
"""
Lorentz-to-Gauss apodization
Parameters
----------
dic : dict
Dictionary of NMRPipe parameters.
data : ndarray
Array of NMR data.
g1 : float
Inversion exponential width in Hz.
g2 : float
Gaussian broadening width in Hz
g3 : float
Location of Gaussian maximum, should be between 0.0 and 1.0.
c : float
First point scale value.
start : int, optional
Starting location of apodization window. Default is the first point, 1.
size : int, optional
Size of the apodization window. Default ('default') is the full size of
the active dimension.
inv : bool, optional
True for inverse apodization, False for normal apodization.
one : bool, optional
True to set points outside of window to 1. False leaves points outside
the apodization window as is.
hdr : bool, optional
True to read apodization parameters from the the parameters in dic.
Returns
-------
ndic : dict
Dictionary of updated NMRPipe parameters.
ndata : ndarray
Array of NMR data with lorentz-to-gauss apodization applied.
"""
start = start - 1 # arrays should start at 0
fn = "FDF" + str(int(dic["FDDIMORDER"][0])) # F1, F2, etc
if hdr: # read apod values from data header
g1 = dic[fn + "APODQ1"]
g2 = dic[fn + "APODQ2"]
g3 = dic[fn + "APODQ3"]
c = dic[fn + "C1"] + 1
# update the dictionary
dic[fn + "C1"] = c - 1.0
# set the apod flags
dic[fn + "APODCODE"] = 3.0
dic[fn + "APODQ1"] = g1
dic[fn + "APODQ2"] = g2
dic[fn + "APODQ3"] = g3
# calculate native parameters
sw = dic[fn + "SW"]
g1p = g1 / sw
g2p = g2 / sw
g3p = g3
# apply apodization to data
if start == 0 and size == 'default':
data = p.gm(data, g1p, g2p, g3p, inv=inv)
else: # only part of the data window is apodized
if size == 'default':
stop = data.shape[-1]
else:
stop = start + size
# pipe sets the maximum to the actual data maximum not
# the maximum of the windowed region, so adj. g3p as necessary
g3p = g3p * data.shape[-1] / (stop - start)
#print start,stop,g1p,g2p,g3p
data[..., start:stop] = p.gm(data[..., start:stop], g1p, g2p, g3p,
inv=inv)
if one == False:
data[..., :start] = 0.0
data[..., stop:] = 0.0
# first point scaling
if inv:
data[..., 0] = data[..., 0] / c
else:
data[..., 0] = data[..., 0] * c
dic = update_minmax(dic, data)
return dic, data
def gmb(dic, data, lb=0.0, gb=0.0, c=1.0, start=1, size='default', inv=False,
one=False, hdr=False):
"""
Modified Gaussian Apodization
Parameters
----------
dic : dict
Dictionary of NMRPipe parameters.
data : ndarray
Array of NMR data.
lb : float
Exponential apodization term in Hz.
gb : float
Gaussian apodization term in Hz.
c : float
First point scale value.
start : int, optional
Starting location of apodization window. Default is the first point, 1.
size : int, optional
Size of the apodization window. Default ('default') is the full size of
the active dimension.
inv : bool, optional
True for inverse apodization, False for normal apodization.
one : bool, optional
True to set points outside of window to 1. False leaves points outside
the apodization window as is.
hdr : bool, optional
True to read apodization parameters from the the parameters in dic.
Returns
-------
ndic : dict
Dictionary of updated NMRPipe parameters.
ndata : ndarray
Array of NMR data with a modified gaussian apodization applied.
"""
start = start - 1 # arrays should start at 0
fn = "FDF" + str(int(dic["FDDIMORDER"][0])) # F1, F2, etc
if hdr: # read apod values from data header
lb = dic[fn + "APODQ1"]
gb = dic[fn + "APODQ2"]
c = dic[fn + "C1"] + 1
# update the dictionary
dic[fn + "C1"] = c - 1.0
# set the apod flags
dic[fn + "APODCODE"] = 7.0
dic[fn + "APODQ1"] = lb
dic[fn + "APODQ2"] = gb
dic[fn + "APODQ3"] = 0.0
# calculate native parameters
sw = dic[fn + "SW"]
a = pi * lb / sw
b = -a / (2.0 * gb * data.shape[-1])
# apply apodization to data
if start == 0 and size == 'default':
data = p.gmb(data, a, b, inv=inv)
else: # only part of the data window is apodized
if size == 'default':
stop = data.shape[-1]
else:
stop = start + size
data[..., start:stop] = p.gmb(data[..., start:stop], a, b, inv=inv)
if one == False:
data[..., :start] = 0.0
data[..., stop:] = 0.0
# first point scaling
if inv:
data[..., 0] = data[..., 0] / c
else:
data[..., 0] = data[..., 0] * c
dic = update_minmax(dic, data)
return dic, data
def jmod(dic, data, off=0.0, j=0.0, lb=0.0, sin=False, cos=False, c=1.0,
start=1, size='default', inv=False, one=False, hdr=False):
"""
Exponentially Damped J-Modulation Apodization
Parameters
----------
dic : dict
Dictionary of NMRPipe parameters.
data : ndarray
Array of NMR data.
off : float
Starting location of J-modulation in a fractions of pi radians. This
parameter is ignored if sin or cos parameters are True.
j : float
J-modulation in Hz.
lb :
Expoentntial line broadening in Hz.
sin : bool
True for sine modulation, off parameter is ignored.
cos : bool
True for cosine modulation, off parameter is ignored.
c : float
First point scale value.
start : int, optional
Starting location of apodization window. Default is the first point, 1.
size : int, optional
Size of the apodization window. Default ('default') is the full size of
the active dimension.
inv : bool, optional
True for inverse apodization, False for normal apodization.
one : bool, optional
True to set points outside of window to 1. False leaves points outside
the apodization window as is.
hdr : bool, optional
True to read apodization parameters from the the parameters in dic.
Returns
-------
ndic : dict
Dictionary of updated NMRPipe parameters.
ndata : ndarray
Array of NMR data with a exponentially damped J-modulation apodization
applied.
"""
start = start - 1 # arrays should start at 0
fn = "FDF" + str(int(dic["FDDIMORDER"][0])) # F1, F2, etc
if sin:
off = 0.0
if cos:
off = 0.5
if hdr: # read apod values from data header
off = dic[fn + "APODQ1"]
j = dic[fn + "APODQ2"]
lb = dic[fn + "APODQ3"]
c = dic[fn + "C1"] + 1
# update the dictionary
dic[fn + "C1"] = c - 1.0
# set the apod flags
dic[fn + "APODCODE"] = 8.0
dic[fn + "APODQ1"] = off
dic[fn + "APODQ2"] = j
dic[fn + "APODQ3"] = lb
# calculate native parameters
sw = dic[fn + "SW"]
e = pi * lb / sw
end = off + j * (data.shape[-1] - 1) / sw
# apply apodization to data
if start == 0 and size == 'default':
data = p.jmod(data, e, off, end, inv=inv)
else: # only part of the data window is apodized
if size == 'default':
stop = data.shape[-1]
else:
stop = start + size
#print start,stop,e,off,end
end = off + j * (stop - start - 1) / sw
data[..., start:stop] = p.jmod(data[..., start:stop], e, off, end,
inv=inv)
if one == False:
data[..., :start] = 0.0
data[..., stop:] = 0.0
# first point scaling
if inv:
data[..., 0] = data[..., 0] / c
else:
data[..., 0] = data[..., 0] * c
dic = update_minmax(dic, data)
return dic, data
def sp(dic, data, off=0.0, end=1.0, pow=1.0, c=1.0, start=1, size='default',
inv=False, one=False, hdr=False):
"""
Sine bell apodization.
Parameters
----------
dic : dict
Dictionary of NMRPipe parameters.
data : ndarray
Array of NMR data.
off : float
Starting location of sine-bell as a fraction of pi radians.
end : float
Ending location of sine-bell as a fraction of pi radians.
pow : int
Sine-bell power.
c : float
First point scale value.
start : int, optional
Starting location of apodization window. Default is the first point, 1.
size : int, optional
Size of the apodization window. Default ('default') is the full size of
the active dimension.
inv : bool, optional
True for inverse apodization, False for normal apodization.
one : bool, optional
True to set points outside of window to 1. False leaves points outside
the apodization window as is.
hdr : bool, optional
True to read apodization parameters from the the parameters in dic.
Returns
-------
ndic : dict
Dictionary of updated NMRPipe parameters.
ndata : ndarray
Array of NMR data with a sine-bell apodization applied.
"""
start = start - 1 # arrays should start at 0
fn = "FDF" + str(int(dic["FDDIMORDER"][0])) # F1, F2, etc
if hdr: # read apod values from data header
off = dic[fn + "APODQ1"]
end = dic[fn + "APODQ2"]
pow = dic[fn + "APODQ3"]
c = dic[fn + "C1"] + 1
# update the dictionary
dic[fn+"C1"] = c - 1.0
# set the apod flags
dic[fn + "APODCODE"] = 1.0
dic[fn + "APODQ1"] = off
dic[fn + "APODQ2"] = end
dic[fn + "APODQ3"] = pow
# apply apodization to data
if start == 0 and size == 'default':
data = p.sp(data, off, end, pow, inv=inv)
else: # only part of the data window is apodized
if size == 'default':
stop = data.shape[-1]
else:
stop = start + size
data[..., start:stop] = p.sp(data[..., start:stop], off, end, pow,
inv=inv)
if one == False:
data[..., :start] = 0.0
data[..., stop:] = 0.0
# first point scaling
if inv:
data[..., 0] = data[..., 0] / c
else:
data[..., 0] = data[..., 0] * c
dic = update_minmax(dic, data)
return dic, data
sine = sp # wrapper for sine functions
def tm(dic, data, t1=0.0, t2=0.0, c=1.0, start=1, size='default', inv=False,
one=False, hdr=False):
"""
Trapezoid apodization.
Parameters
----------
dic : dict
Dictionary of NMRPipe parameters.
data : ndarray
Array of NMR data.
t1 : float
Length in points of left side of the trapezoid.
t2 : float
Length in points of right side of the trapezoid.
c : float
First point scale value.
start : int, optional
Starting location of apodization window. Default is the first point, 1.
size : int, optional
Size of the apodization window. Default ('default') is the full size of
the active dimension.
inv : bool, optional
True for inverse apodization, False for normal apodization.
one : bool, optional
True to set points outside of window to 1. False leaves points outside
the apodization window as is.
hdr : bool, optional
True to read apodization parameters from the the parameters in dic.
Returns
-------
ndic : dict
Dictionary of updated NMRPipe parameters.
ndata : ndarray
Array of NMR data with a trapezoid apodization applied.
"""
start = start - 1 # arrays should start at 0
fn = "FDF" + str(int(dic["FDDIMORDER"][0])) # F1, F2, etc
if hdr: # read apod values from data header
t1 = dic[fn + "APODQ1"]
t2 = dic[fn + "APODQ2"]
c = dic[fn + "C1"] + 1
# update the dictionary
dic[fn + "C1"] = c - 1.0
# set the apod flags
dic[fn + "APODCODE"] = 4.0
dic[fn + "APODQ1"] = t1
dic[fn + "APODQ2"] = t2
dic[fn + "APODQ3"] = 0.0
# apply apodization to data
if start == 0 and size == 'default':
data = p.tm(data, t1=t1, t2=t2, inv=inv)
else: # only part of the data window is apodized
if size == 'default':
stop = data.shape[-1]
else:
stop = start + size
data[..., start:stop] = p.tm(data[..., start:stop], t1=t1, t2=t2,
inv=inv)
if one == False:
data[..., :start] = 0.0
data[..., stop:] = 0.0
# first point scaling
if inv:
data[..., 0] = data[..., 0] / c
else:
data[..., 0] = data[..., 0] * c
# check for NaN in array (when div by 0)
if np.isnan(data).any():
data = np.array(np.nan_to_num(data), dtype=data.dtype)
dic = update_minmax(dic, data)
return dic, data
def tri(dic, data, loc="auto", lHi=0.0, rHi=0.0, c=1.0, start=1,
size='default', inv=False, one=False, hdr=False):
"""
Triangular apodization
Parameters
----------
dic : dict
Dictionary of NMRPipe parameters.
data : ndarray
Array of NMR data.
loc : int or "auto"
Location in points of triangle apex. The default ("auto") is to place
the apex in the middle.
lHi : float
Starting height of the left side of the triangle.
rHi : float
Starting height of the right side of the triangle.
c : float
First point scale value.
start : int, optional
Starting location of apodization window. Default is the first point, 1.
size : int, optional
Size of the apodization window. Default ('default') is the full size of
the active dimension.
inv : bool, optional
True for inverse apodization, False for normal apodization.
one : bool, optional
True to set points outside of window to 1. False leaves points outside
the apodization window as is.
hdr : bool, optional
True to read apodization parameters from the the parameters in dic.
Returns
-------
ndic : dict
Dictionary of updated NMRPipe parameters.
ndata : ndarray
Array of NMR data with a triangular apodization applied.
Notes
-----
The right side of the apodization is differs slightly from NMRPipe's tri
function.
"""
start = start - 1 # arrays should start at 0
if loc == "auto":
loc = data.shape[-1] / 2
fn = "FDF" + str(int(dic["FDDIMORDER"][0])) # F1, F2, etc
if hdr: # read apod values from data header
loc = dic[fn + "APODQ1"]
lHi = dic[fn + "APODQ2"]
rHi = dic[fn + "APODQ3"]
c = dic[fn + "C1"] + 1
# update the dictionary
dic[fn + "C1"] = c - 1.0
# set the apod flags
dic[fn + "APODCODE"] = 6.0
dic[fn + "APODQ1"] = loc
dic[fn + "APODQ2"] = lHi
dic[fn + "APODQ3"] = rHi
# apply apodization to data
if start == 0 and size == 'default':
data = p.tri(data, loc=loc, lHi=lHi, rHi=rHi, inv=inv)
else: # only part of the data window is apodized
if size == 'default':
stop = data.shape[-1]
else:
stop = start + size
data[..., start:stop] = p.tri(data[..., start:stop], loc, lHi, rHi,
inv=inv)
if one == False:
data[..., :start] = 0.0
data[..., stop:] = 0.0
# first point scaling
if inv:
data[..., 0] = data[..., 0] / c
else:
data[..., 0] = data[..., 0] * c
dic = update_minmax(dic, data)
return dic, data
###################
# Shift functions #
###################
def rs(dic, data, rs=0.0, sw=False):
"""
Right shift and zero pad.
Parameters
----------
dic : dict
Dictionary of NMRPipe parameters.
data : ndarray
Array of NMR data.
rs : float
Number of points to right shift. Negative values will left shift.
sw : bool
True to update chemical shift calibration parameters.
Returns
-------
ndic : dict
Dictionary of updated NMRPipe parameters.
ndata : ndarray
Array of NMR data which has been right shifted.
"""
if rs < 0: # negative right shifts are left shifts
return ls(dic, data, ls=-rs, sw=sw)
data = p.rs(data, pts=rs)
dic = update_minmax(dic, data)
fn = "FDF" + str(int(dic["FDDIMORDER"][0])) # F1, F2, etc
if sw and dic[fn + "FTFLAG"] == 1:
# we are in freq domain and must update NDORIG and NDCENTER
dic[fn + "CENTER"] = dic[fn + "CENTER"] + rs
dic = recalc_orig(dic, data, fn)
return dic, data
def ls(dic, data, ls=0.0, sw=False):
"""
Left Shift and Zero Pad
Parameters
----------
dic : dict
Dictionary of NMRPipe parameters.
data : ndarray
Array of NMR data.
ls : float
Number of points to left shift. Negative values will right shift.
sw : bool
True to update chemical shift calibration parameters.
Returns
-------
ndic : dict
Dictionary of updated NMRPipe parameters.
ndata : ndarray
Array of NMR data which has been left shifted.
"""
if ls < 0:
return rs(dic, data, rs=-ls, sw=sw)
data = p.ls(data, ls)
dic = update_minmax(dic, data)
if sw:
fn = "FDF" + str(int(dic["FDDIMORDER"][0])) # F1, F2, etc
if dic[fn + "FTFLAG"] == 1: # freq domain
# update NDORIG and NDCENTER
dic[fn + "CENTER"] = dic[fn + "CENTER"] - ls
dic = recalc_orig(dic, data, fn)
else: # time domain
dic[fn + "APOD"] = data.shape[-1] - ls
dic[fn + "TDSIZE"] = data.shape[-1] - ls
return dic, data
def cs(dic, data, dir, pts=0.0, neg=False, sw=False):
"""
Circular shift
The syntax of this function is different from NMRPipe's CS function.
Parameters
----------
dic : dict
Dictionary of NMRPipe parameters.
data : ndarray
Array of NMR data.
dir : {'rs' or 'ls'}
Direction to shift spectra, 'rs' for right shifting, 'ls' for left
shifting.
pts : float
Number of points to shift.
neg : bool
True to negative points which are shifted.
sw : bool
True to update chemical shift calibration parameters.
Returns
-------
ndic : dict
Dictionary of updated NMRPipe parameters.
ndata : ndarray
Array of NMR data which has been circular shifted.
"""
if dir == "ls":
pts = -pts
elif dir != "rs":
raise ValueError("dir must be ls or rs")
data = p.cs(data, pts, neg=neg)
dic = update_minmax(dic, data)
fn = "FDF" + str(int(dic["FDDIMORDER"][0])) # F1, F2, etc
if sw and dic[fn + "FTFLAG"] == 1:
# freq domain update NDORIG and NDCENTER
dic[fn + "CENTER"] = dic[fn + "CENTER"] + pts
dic = recalc_orig(dic, data, fn)
return dic, data
def fsh(dic, data, dir, pts, sw=True):
"""
Frequency shift.
Parameters
----------
dic : dict
Dictionary of NMRPipe parameters.
data : ndarray
Array of NMR data.
dir : {'rs' or 'ls'}
Direction to shift spectra, 'rs' for right shifting, 'ls' for left
shifting.
pts : float
Number of points to shift.
sw : bool