-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoNoteDiffs.py
More file actions
executable file
·984 lines (841 loc) · 34.5 KB
/
doNoteDiffs.py
File metadata and controls
executable file
·984 lines (841 loc) · 34.5 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
#!/usr/bin/env python
#*********************************************************************
# 24 Sep 2015
# Andrew J. Worth
# andy@neuromorphometrics.com
#
# Neuromorphometrics
# 22 Westminster Street
# Somerville, MA 02144-1630 USA
#
# http://neuromorphometrics.com
#
#*********************************************************************
#* *
#* (c) Copyright 2015 Neuromorphometrics All rights reserved *
#* *
#*********************************************************************
#-------------------------------------------------------------------------------
# Usage String:
"""
SYNOPSIS
doNoteDiffs.py [-h] [-v,--verbose] [--version]
Short for Run All commands over All scans
DESCRIPTION
Runs a bunch of stuff (or not) on the scans in the current project.
The possible scans are defined in ../Settings/ScanList.py.
Purpose: select scans, set variables and run commands on selected scans.
#############
# Commands
#############
#
# Commands (in bin/Cmds) can do things in 5 places:
# 1) Settings - (import) Set variables/defaults for this command
# 2) Before Loop - Initialize() create directories, etc. Build HeaderExtra
# In Loop - G.PID,G.SCN, etc. are set, do command for specific scan
# 3) Part 1 - SayIt() return string for InfoExtra to print on CSV line
# 4) Part 2 - DoIt() to current scan, print output lines, etc.
# 5) After loop - Finalize(): clean up, etc. after command
RUN WITH:
./bin/doNoteDiffs.py
EXIT STATUS
Exit codes: none
AUTHOR
Andrew J. Worth
andy@neuromorphometrics.com
Neuromorphometrics, Inc.
22 Westminster Street
Somerville, MA 02144-1630 USA
http://Neuromorphometrics.com
LICENSE
(c) Copyright 2015 Neuromorphometrics, Inc. All rights reserved.
VERSION
"""
Dash80 = "--------------------------------------------------------------------------------"
import sys
import os
import re
import glob
import fnmatch
import filecmp
import shutil
import traceback
import optparse
import time
from datetime import timedelta, datetime
import xml.etree.ElementTree as ET
import G # Globals!
# I'm just wondering who I really am
FullPathToMe = os.path.realpath(__file__)
#print "FullPathToMe ", FullPathToMe
SplitFullPathToMe = os.path.split(FullPathToMe)
PathToMe = SplitFullPathToMe[0]
ME = SplitFullPathToMe[1]
s = 'Starting '+ME+'\n pwd: '+G.WorkingDirectory+'\n'
if G.VerboseLevel >= 1: # set VerboseLevel in G.py to affect this
print 'I am "%s"' % ( ME )
print 'Some call me "%s"' % ( __name__ )
print 'I am at "%s"' % ( PathToMe )
print 'I was run from "%s"' % ( G.WorkingDirectory )
print s
#print ''
#print 'Exiting early!'
#sys.exit()
G.TimeLeftFileP.write(s)
#===============================================================================
def main ():
global options, args, AvgTime
AvgTime = 0
ThingsToDo = [ ] # a list of messages about things that need to be done
#---------------------------------------------------------------------------
# Get project-specific settings on what to List
# (symbolically linked to ../Settings/Conf.py)
import Conf
if options.ScanList == 'OnlyOne':
G.OnlyOneScan = 1
elif options.ScanList == 'Random':
G.DoRandomList = 1
elif options.ScanList == 'Flip':
G.DoFlipList = 1
elif options.ScanList == 'DontFlip':
G.DoDontFlipList = 1
elif options.ScanList == 'Orig':
G.DoOrigList = 1
elif options.ScanList != '':
print "Bad -ScanList option: ", options.ScanList
print " use: OnlyOne Random Flip DontFlip or Orig"
sys.exit()
#---------------------------------------------------------------------------
DoLoadLabeles = 1 # read all default label files (needed to get the
# proper label # to put in notations)
DoNoteDiffs = 1 # Create Notations based on Differences
DoTemplate = 0 # (Search for and copy this to add a new command)
DoNotations = 1 # Write Notations (after everything else)
#---------------------------------------------------------------------------
# Generally needed stuff (always needed in multiple places)
#---------------------------------------------------------------------------
# Possibly needed stuff (will be turned on below if needed)
DoGetInfoFileInfo = 0 # Gets slice res, etc. from Info File
DoGetNIFTIheaderInfo = 0 # Gets slice res, etc. from NIfTI header
DoGetVoxelCounts = 0 # generate NIfTIPath/voxelCounts.txt
DoGetMetaInfo = 0 # Sets Meta data global info: ID1&2, age, MF, hand
#===========================================================================
# 1) Settings - (import) Set variables/defaults for this command
#===========================================================================
#---------------------------------------------------------------------------
# Init Strings:
HeaderExtra = "" # after G.PID, G.SCN, add this to header
# NOTE: the order of the following must match the ordering of commands
# inside the loop (part 1) because the order stuff of HeaderExtra needs
# to match the order of corresponding stuff in InfoExtra
#...........................................................................
if Conf.DoShowGroup == 1:
HeaderExtra += ", Group"
#...........................................................................
if Conf.DoShowSource == 1:
HeaderExtra += ", Source"
#...........................................................................
if Conf.DoShowAge == 1:
DoGetMetaInfo = 1
HeaderExtra += ", Age"
#...........................................................................
if Conf.DoShowMF == 1:
DoGetMetaInfo = 1
HeaderExtra += ", M/F"
#...........................................................................
if Conf.DoShowHandedness == 1:
DoGetMetaInfo = 1
HeaderExtra += ", R/L"
#...........................................................................
if Conf.DoShowRepeat == 1:
DoGetMetaInfo = 1
HeaderExtra += ", Rpt?"
#...........................................................................
if Conf.DoShowMeta == 1:
HeaderExtra += ", Meta "
#...........................................................................
if Conf.DoShowLabelCount == 1:
HeaderExtra += ", #lbl"
#...........................................................................
if Conf.DoShowCheckedOut == 1:
HeaderExtra += ", checked out "
#...........................................................................
if Conf.DoShowSegDone == 1:
HeaderExtra += ", basic seg done? "
#...........................................................................
if Conf.DoShowAtlasDone == 1:
HeaderExtra += ", AtlasCheck run?, AtlasCheck done?"
#...........................................................................
if Conf.DoShowIDInfo == 1:
DoGetMetaInfo = 1
HeaderExtra += MetaInfo.IDInfoHeaderExtra
#...........................................................................
if Conf.DoShowOrigInfoFile == 1:
DoGetMetaInfo = 1
HeaderExtra += ", Original Info File "
#...........................................................................
if Conf.DoShowLandmarks == 1:
if G.VerboseLevel >= 1:
print ME + ": Running DoShowLandmarks (settings)"
import Landmarks
# Add each landmark to header
for LM in Landmarks.AllLand.split():
HeaderExtra += ', '+LM
# if DoShowLandmarks
#...........................................................................
# Count the number of a specific voxel values in result NIfTI files
if Conf.DoSeeIfLabelIsIn == 1:
DoGetNIFTIheaderInfo = 1 # Gets slice res, etc. from NIfTI header
DoGetVoxelCounts = 1 # generate NIfTIPath/voxelCounts.txt
HeaderExtra += ", label, found"
#...........................................................................
if Conf.DoShowNIfTIInfo == 1:
DoGetNIFTIheaderInfo = 1
HeaderExtra +=", Cols, Rows, Slis, ColRes, RowRes, SliRes, " + \
"Type, Offset"
#...........................................................................
# Count the number of all voxel values in result NIfTI files
if Conf.DoSeeWhatsIn == 1:
DoGetVoxelCounts = 1 # generate NIfTIPath/voxelCounts.txt
DoGetNIFTIheaderInfo = 1 # Gets slice res, etc. from NIfTI header
#...........................................................................
if DoLoadLabeles == 1:
import Labels
#...........................................................................
if DoNoteDiffs == 1:
import Cmds.NoteDiffs
#...........................................................................
if DoTemplate == 1:
import Cmds.Template
#...........................................................................
if Conf.TestNotations > 0 or \
Conf.DoShowNotationCount == 1 or \
DoNotations == 1:
import Notations
#---------------------------------------------------------------------------
# General Settings and Defaults
#
TooLongToWait = 5 # seconds
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
import ScanList
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
splitProject = ScanList.PROJECT.split('/')
#print "splitProject = ",splitProject
Project = splitProject[-1]
ProjectPath = splitProject[0]
ii = 1
while ii < len(splitProject)-1:
ProjectPath += '/' + splitProject[ii]
ii += 1
splitSandbox = ScanList.SANDBOX.split('/')
#print "splitSandbox = ",splitSandbox
Sandbox = splitSandbox[-1]
SandboxPath = splitSandbox[0]
ii = 1
while ii < len(splitSandbox)-1:
SandboxPath += '/' + splitSandbox[ii]
ii += 1
if G.VerboseLevel >= 1:
print ''
print '--------------------------------------------------------------------'
print 'Project = ', Project
print 'ProjectPath = ', ProjectPath
print 'Sandbox = ', Sandbox
print 'SandboxPath = ', SandboxPath
print 'DATAVERSION = ', ScanList.DATAVERSION
print 'ME = ', ME
print 'VERSION = ', G.Version
print 'WorkingDirectory = ', G.WorkingDirectory
print 'PathToMe = ', PathToMe
print 'YYYYMMDD_HHMMSS = ', G.YYYYMMDD_HHMMSS
if G.VerboseLevel >= 2:
print 'YYMMDDHHMM = ', G.YYMMDDHHMM
print 'TimeLeftFile = ', G.TimeLeftFile
print '--------------------------------------------------------------------'
print ''
print 'sys.version:'
print sys.version
print ''
#===========================================================================
# 2) Before Loop - Initialize() create directories, etc. Build HeaderExtra
#===========================================================================
#...........................................................................
if DoGetInfoFileInfo == 1:
import Info
#...........................................................................
if Conf.DoShowLandmarks == 1:
if G.VerboseLevel >= 1:
print ME + ": Running DoShowLandmarks (before loop)"
Landmarks.Initialize()
# if Conf.DoShowLandmarks
#...........................................................................
if DoLoadLabeles == 1:
Labels.Read(G.VerboseLevel)
#...........................................................................
if Conf.DoShowNotationCount == 1:
HeaderExtra += Notations.Initialize()
#...........................................................................
if DoNoteDiffs == 1:
HeaderExtra += Cmds.NoteDiffs.Initialize()
#...........................................................................
if DoTemplate == 1:
HeaderExtra += Cmds.Template.Initialize()
#---------------------------------------------------------------------------
if G.VerboseLevel >= 1:
print '\nHeader:'
# Main Header info for each scan
Header = " PID, SCN, SEG" + HeaderExtra
print Header
#sys.exit()
if G.VerboseLevel >= 1:
print ''
print '===================================================================='
print ' Main Loop '
print '===================================================================='
print ''
#for s in G.SCANLIST:
# print s
G.TotalNumScans = len(G.SCANLIST)/3
ListNum = 0
G.SBJ = 0
SumTime = 0.0
PrintingTimeLeft = False
while ListNum < len(G.SCANLIST):
if G.OnlyOneScan == 1: # then skip to the one and only
# ScanList.THEONLYSCAN = [ 1123, 3, "glm" ]
while G.SCANLIST[G.SBJ*3+0] != ScanList.THEONLYSCAN[0] or \
G.SCANLIST[G.SBJ*3+1] != ScanList.THEONLYSCAN[1] or \
G.SCANLIST[G.SBJ*3+2] != ScanList.THEONLYSCAN[2]:
if G.VerboseLevel >= 1:
print "Skipping ", G.SCANLIST[G.SBJ*3+0],"looking for "\
,ScanList.THEONLYSCAN[0]
G.SBJ += 1
ListNum += 3
#print G.SBJ*3 , len(G.SCANLIST)
if G.SBJ*3 >= len(G.SCANLIST):
print "ERROR: Could not find one and only scan, ", ScanList.THEONLYSCAN[0]
sys.exit(1)
loopStart = time.time()
G.PID = G.SCANLIST[G.SBJ*3+0] # NMM Patient/Subject ID
G.SCN = G.SCANLIST[G.SBJ*3+1] # NMM Scan number
G.SEG = G.SCANLIST[G.SBJ*3+2] # NMM Segmenter Prefix
G.OPTH = G.OPATHLIST[G.SBJ] # Original scan path (e.g. scans 1 & 2)
G.GRP = G.GROUPLIST[G.SBJ] # Group e.g. C16AD1 (deliverable 1)
G.SRC = G.METADLIST[G.SBJ*2+0] # Data source e.g. "OASIS" or "CANDI"
G.META = G.METADLIST[G.SBJ*2+1] # Meta data .csv file
if options.segPrefix != '':
G.SEG = options.segPrefix
if G.VerboseLevel > 0:
print "Overriding G.SEG with '" + G.SEG + "'"
G.SBJ += 1
ListNum += 3
# These string versions will be used everywhere:
G.PIDs = str(G.PID)
G.SCNs = str(G.SCN)
G.PIDSpaceSCNs = G.PIDs + " " + G.SCNs
G.PIDSlashSCNs = G.PIDs + "/" + G.SCNs
G.PID_SCNs = G.PIDs + "_" + G.SCNs
G.PID_SCN_SEGs = G.PIDs+"_"+G.SCNs+"_"+G.SEG
#===========================================================================
# 3) In Loop Part 1: SayIt() return string for InfoExtra to print CSV line
#===========================================================================
#---------------------------------------------------------------------------
# Strings to build for each scan:
InfoExtra = "" # after G.PID, G.SCN, print this
# Set path to the inside the scan directory:
G.ScanPath = "./Data/"+G.PIDSlashSCNs+"/"
# Set up path to info file:
G.InfoFile = G.PID_SCNs + ".xml"
G.InfoPath = G.ScanPath
G.FullInfoPath = G.InfoPath + G.InfoFile
# Set up path to NIfTI result file:
G.NIfTIFile = G.PID_SCN_SEGs + ".nii"
G.NIfTIPath = "Results/Data/"+G.PIDSlashSCNs+"/NIFTI/"
G.FullNIfTIPath = G.NIfTIPath + G.NIfTIFile
# Set up path to result tgz file:
G.TGZFile = G.PID_SCNs + ".tgz"
G.TGZPath = "Results/"
G.FullTGZPath = G.TGZPath + G.TGZFile
# Set up path to original scan's info file:
# (G.OPTH ends in '../Data')
G.OFile = G.PIDs + '_1.xml'
G.OPath = G.OPTH + '/'+ G.PIDs + '/1/'
G.FullOPath = G.OPath + G.OFile
# Set up path to Meta data .csv file
G.MetaPath = './MetaData/' + G.SRC + '/'
G.FullMetaPath = G.MetaPath + G.META
#.........................................................................
if DoGetMetaInfo == 1:
# set a bunch of Meta variables: ID, age, m/f, handedness, repeat?
MetaInfo.GetMetaInfo()
#.........................................................................
if DoGetNIFTIheaderInfo == 1:
if os.access(G.FullNIfTIPath,os.F_OK):
cmd = "nifti_tool -disp_hdr -infiles " + G.FullNIfTIPath
aFile = os.popen(cmd)
cmdOutput = aFile.read()
if G.VerboseLevel >= 2:
print cmdOutput
cmdLines = cmdOutput.splitlines()
aFile.close()
Cols = 0
Rows = 0
Slis = 0
ColRes = 0.0
RowRes = 0.0
SliRes = 0.0
DataType = -1
VoxOffset = -1
for line in cmdLines:
#print "line is ", line
#print "line.find is ", line.find('pixdim')
lineWords = line.split()
#print lineWords
if len(lineWords)>0:
#print lineWords[0]
#if lineWords[0] == 'dim' and lineWords[1] == '40':
if lineWords[1] == '40':
#print "found it in ", line
Cols = int(lineWords[4])
Rows = int(lineWords[5])
Slis = int(lineWords[6])
#if lineWords[0] == 'pixdim' and lineWords[1] == '76':
if lineWords[1] == '76':
#print "found it in ", line
ColRes = float(lineWords[4])
RowRes = float(lineWords[5])
SliRes = float(lineWords[6])
# datatype
if lineWords[1] == '70':
DataType = int(lineWords[3])
# vox_offset
if lineWords[1] == '108':
VoxOffset = int(float(lineWords[3]))
else: # G.FullNIfTIPath not available
print 'WARNING:',G.FullNIfTIPath,'is not available'
Cols = 0
Rows = 0
Slis = 0
ColRes = 0.0
RowRes = 0.0
SliRes = 0.0
DataType = 0
VoxOffset = 0
#.........................................................................
# Get scan information from 'info file' Data/PID/SCN/PID_SCN.xml
if DoGetInfoFileInfo == 1:
Info.DoIt()
#.........................................................................
if DoGetVoxelCounts == 1:
if G.VerboseLevel >= 1:
print ME + ": Running DoGetVoxelCounts"
# If count file already exists, see if it needs to be re-made
YesDo = False
FullCountPath = G.NIfTIPath+'voxelCounts.txt'
if os.access(FullCountPath,os.F_OK):
CountmodtimeSec = os.path.getmtime(FullCountPath)
CountmodtimeStr = time.ctime(CountmodtimeSec)
#print "CountmodtimeStr = ", CountmodtimeStr
NIfTImodtimeSec = os.path.getmtime(G.FullNIfTIPath)
NIfTImodtimeStr = time.ctime(NIfTImodtimeSec)
#print "NIfTImodtimeStr = ", NIfTImodtimeStr
if CountmodtimeSec < NIfTImodtimeSec: # then count is older, re-make it
YesDo = True
else:
YesDo = True
if YesDo:
if os.access(G.FullNIfTIPath,os.F_OK) == False:
YesDo = False
print 'ERROR could not find. Cannot do counts.', G.FullNIfTIPath
if YesDo:
cmd = "changeLabels show " + G.FullNIfTIPath + \
" " + str(Cols) + \
" " + str(Rows) + \
" " + str(Slis) + \
" " + str(VoxOffset) + " > " + FullCountPath
#print "cmd is ", cmd
os.system(cmd)
# if DoGetVoxelCounts
#.........................................................................
# Set Scan Info to print
#.........................................................................
FirstPart = "%d, %3d, %s" % (G.PID, G.SCN, G.SEG)
#.........................................................................
if Conf.DoShowGroup == 1:
InfoExtra += ", %11s" % (G.GRP)
#.........................................................................
if Conf.DoShowSource == 1:
InfoExtra += ",%7s" % (G.SRC)
#.........................................................................
if Conf.DoShowAge == 1:
InfoExtra += MetaInfo.AGEFormat % (MetaInfo.AGE)
#.........................................................................
if Conf.DoShowMF == 1:
InfoExtra += MetaInfo.MFFormat % (MetaInfo.MF)
#.........................................................................
if Conf.DoShowHandedness == 1:
InfoExtra += MetaInfo.HANDFormat % (MetaInfo.HAND)
#.........................................................................
if Conf.DoShowRepeat == 1:
InfoExtra += ", %4s" % (MetaInfo.RepeatPID)
#.........................................................................
if Conf.DoShowMeta == 1:
InfoExtra += ",%21s" % (G.META)
#.........................................................................
if Conf.DoShowLabelCount == 1:
fromName = 'alllabel_'+G.SEG
toName = G.ScanPath + 'status/'+fromName+'.txt'
if os.access(toName,os.F_OK) == True:
cmd = 'tail -1 '+toName
aFile = os.popen(cmd)
cmdOutput = aFile.read()
aFile.close()
cmdOutSplit = cmdOutput.split()
InfoExtra += ", %4s" % (cmdOutSplit[0])
else:
InfoExtra += ", "
#.........................................................................
if Conf.DoShowCheckedOut == 1:
isDoneTF,last14Lines = G.GetStatus('0','CheckScanOut')
if isDoneTF:
#print 'YES!'
cmdOutput = 'huh? '
findme = re.compile(' nvmG_User ')
for line in last14Lines.splitlines():
if findme.search(line):
cmdOutput = line.split()[2]
else:
if last14Lines == '':
#print 'no, and no status file either!'
cmdOutput = 'no way '
else:
#print 'no!'
cmdOutput = 'no '
InfoExtra += ",%18s " % (cmdOutput)
#.........................................................................
if Conf.DoShowSegDone == 1:
isDoneTF,last14Lines = G.GetStatus('2','Basic Segmentation')
if isDoneTF:
#print 'YES!'
cmdOutput = 'huh? '
findme = re.compile(' nvmG_Version ')
for line in last14Lines.splitlines():
if findme.search(line):
cmdOutput = line.split()[2:]
if cmdOutput[0] == 'Version':
vers = cmdOutput[1] # not used
usr = 'who?'
findme = re.compile(' nvmG_User ')
for line in last14Lines.splitlines():
if findme.search(line):
usr = line.split()[2]
cmdOutput = usr
elif cmdOutput[0] == 'doMakeLabelList.py':
cmdOutput = '#lbl==15 '
else:
cmdOutput = "error"
else:
if last14Lines == '':
#print 'no, and no status file either!'
cmdOutput = 'no way '
else:
#print 'no!'
cmdOutput = 'no '
InfoExtra += ",%18s " % (cmdOutput)
#.........................................................................
if Conf.DoShowAtlasDone == 1:
isDoneTF,last14Lines = G.GetStatus('4.6.e1','CheckWithAtlas')
if isDoneTF:
#print 'YES!'
cmdOutput = 'huh? '
findme = re.compile('Date/Time:')
for line in last14Lines.splitlines():
#print line
if findme.search(line):
cmdOutput = line.split()[1]
#print 'cmdOutput =',cmdOutput
else:
if last14Lines == '':
#print 'no, and no status file either!'
cmdOutput = 'no status file'
else:
#print 'no!'
cmdOutput = 'no '
InfoExtra += ",%16s" % (cmdOutput)
isDoneTF,last14Lines = G.GetStatus('4.6','CheckWithAtlas')
if isDoneTF:
#print 'YES!'
cmdOutput = 'huh? '
findme = re.compile('Date/Time:')
for line in last14Lines.splitlines():
#print line
if findme.search(line):
cmdOutput = line.split()[1]
#print 'cmdOutput =',cmdOutput
else:
if last14Lines == '':
#print 'no, and no status file either!'
cmdOutput = 'no status file'
else:
#print 'no!'
cmdOutput = 'no '
InfoExtra += ",%17s" % (cmdOutput)
#print 'Exiting early!'
#sys.exit()
#.........................................................................
if Conf.DoShowIDInfo == 1:
InfoExtra += MetaInfo.IDInfoHeaderFormat % (MetaInfo.ID1, MetaInfo.ID2)
#.........................................................................
if Conf.DoShowOrigInfoFile == 1:
InfoExtra += ",%s" % (G.OFile)
#.........................................................................
if Conf.DoShowLandmarks == 1:
if G.VerboseLevel >= 1:
print ME + ": Running DoShowLandmarks (in loop)"
Landmarks.DoIt()
InfoExtra += Landmarks.FinalMessage
# if Conf.DoShowLandmarks
#.........................................................................
if Conf.DoSeeIfLabelIsIn == 1:
cmd = "grep ' " + ROI + " ' " + G.NIfTIPath + "voxelCounts.txt "
#print "cmd is ", cmd
aFile = os.popen(cmd)
cmdOutput = aFile.read()
aFile.close()
cmdOutSplit = cmdOutput.split()
#print cmdOutSplit
#print len(cmdOutSplit)
if len(cmdOutSplit) >= 2:
InfoExtra += ",%5s ,%5s " % (cmdOutSplit[0], cmdOutSplit[1])
else:
InfoExtra += ", , "
#.........................................................................
if Conf.DoShowNIfTIInfo == 1:
InfoExtra += ", %d, %d, %d, %f, %f, %f, %d, %d" % \
(Cols, Rows, Slis, ColRes, RowRes, SliRes, DataType, VoxOffset)
#.........................................................................
if Conf.DoShowNotationCount == 1:
InfoExtra += Notations.SayIt()
#.........................................................................
if DoNoteDiffs == 1:
InfoExtra += Cmds.NoteDiffs.SayIt()
#.........................................................................
if DoTemplate == 1:
InfoExtra += Cmds.Template.SayIt()
#.........................................................................
# Print Scan Info - Strings and files are created above and will be
# shown here.
#.........................................................................
if G.VerboseLevel >= 1:
print ""
print Dash80
print Header
WholeLine = FirstPart + InfoExtra
# Here it is, the MAIN PRINT STATEMENT! Can you believe it? Wow.
print WholeLine
if G.VerboseLevel >= 1:
print Dash80
print ""
print "ScanPath ", G.ScanPath
print "FullInfoPath ", G.FullInfoPath
print "FullNIfTIPath ", G.FullNIfTIPath
print "FullTGZPath ", G.FullTGZPath
print "FullOrigPath ", G.FullOPath
print ""
#===========================================================================
# 4) In Loop Part 2 - DoIt() to current scan, print output lines, etc.
#===========================================================================
#...........................................................................
if Conf.DoSHowOrigInfoPath:
print "Original info file path =", G.FullOPath
#...........................................................................
if Conf.DoSeeWhatsIn == 1:
# Show the whole file
n = G.NIfTIPath + "voxelCounts.txt"
if os.access(n,os.F_OK):
print "What's in ", n
f = open(n, 'r')
o = f.read()
f.close()
print o
n = G.NIfTIPath + 'voxelCounts_simp.txt'
print "What's in ", n
f = open(n, 'r')
o = f.read()
f.close()
print o
else:
print 'Cannot see what is in. Cannot find',n
#.........................................................................
if DoNoteDiffs == 1:
Cmds.NoteDiffs.DoIt(options.force)
#.........................................................................
if DoTemplate == 1:
Cmds.Template.DoIt(options.force)
#.........................................................................
# This should be done after everything else (that might create Notations)
if Conf.TestNotations > 0 or \
DoNotations == 1:
Notations.DoIt(options.force)
#-------------------------------------------------------------------------
if G.OnlyOneScan == 1: # then don't do any more
ListNum = len(G.SCANLIST)
G.SBJ = 1
#-------------------------------------------------------------------------
# Calculate how long it has taken to get through this iteration
#time.sleep(0.125) # for testing time display
thisTime = (time.time() - loopStart)
SumTime += thisTime
AvgTime = SumTime / G.SBJ
NumToGo = G.TotalNumScans - G.SBJ
TimeLeft = AvgTime * NumToGo
if G.VerboseLevel >= 1:
print ''
print 'Time', timedelta(seconds=thisTime), \
' Avg.', timedelta(seconds=AvgTime), \
' Done in', timedelta(seconds=TimeLeft), '\n'
elif TimeLeft > TooLongToWait or PrintingTimeLeft:
t = str(timedelta(seconds=TimeLeft))
#print >> sys.stderr, '[32;40mDone in', t, '[0m'
s='%4d left to do' % ( NumToGo )
s=s+', this '+ str(timedelta(seconds=thisTime))
s=s+', avg '+ str(timedelta(seconds=AvgTime))
s=s+'\n'
G.TimeLeftFileP.write('Done in ' + t + s)
if PrintingTimeLeft == False: # Only executes once
cmd = 'xterm -r -title "Time Left" -e "sleep 2 ; tail -f ' + \
G.TimeLeftFile + '" &'
os.system(cmd)
else:
G.TimeLeftFileP.flush()
PrintingTimeLeft = True
StopFileName = 'bin/Stop'
if os.access(StopFileName,os.F_OK):
print ME + '.py found file, "'+StopFileName+\
'". Deleting that and quitting. Bye!'
os.remove(StopFileName)
sys.exit()
# End of while loop
#===========================================================================
# 5) After loop - Finalize(): clean up, etc. after command
#===========================================================================
#.........................................................................
if Conf.DoShowLandmarks == 1:
if G.VerboseLevel >= 1:
print ME + ": Running DoShowLandmarks (after loop)"
Landmarks.Finalize()
# if Conf.DoShowLandmarks
#.........................................................................
if DoNoteDiffs == 1:
Cmds.NoteDiffs.Finalize()
#.........................................................................
if DoTemplate == 1:
Cmds.Template.Finalize()
#.........................................................................
if DoNotations == 1:
Notations.Finalize()
#---------------------------------------------------------------------------
print ""
print "Total subjects ", G.SBJ
print ""
if len(G.SCANLIST) == 0:
ThingsToDo.append('Edit ScanList.py::SL1, SL2, etc. so there are scans'+\
' to run on')
if len(ThingsToDo) > 0:
print 'There are things that need to be done:'
for thing in ThingsToDo:
print ' '+thing
print ''
s = 'Finishing '+ME+' at '+time.asctime() + '\n'
G.TimeLeftFileP.write(s)
tt = 'Total time: %s (average: %s)\n' % \
(timedelta(seconds=(time.time() - G.StartTime)), \
timedelta(seconds=AvgTime))
G.TimeLeftFileP.write(tt)
G.TimeLeftFileP.close()
# End of main()
#---------------------------------------------------------------------------
# Call Main Program (above) with exceptions, etc.
#---------------------------------------------------------------------------
# Uncomment the following section if you want readline history support.
#import readline, atexit
#histfile = os.path.join(os.environ['HOME'], '.RunAll_history')
#try:
# readline.read_history_file(histfile)
#except IOError:
# pass
#atexit.register(readline.write_history_file, histfile)
if __name__ == '__main__':
try:
G.Version+='$Id: doNoteDiffs.py,v 1.4 2013/04/11 04:19:15 andy Exp $'
G.StartTime = time.time()
parser = optparse.OptionParser(
formatter=optparse.TitledHelpFormatter(),
usage=globals()['__doc__'],
version=G.Version)
parser.add_option ('-v', '--verbose', action='store_true',
default=False, help='verbose output')
parser.add_option ('-V', '--Verbose', action='store_true',
default=False, help='more verbose output')
parser.add_option ('-l', '--verboseLevel', action='store', type="int",
default=0, help='level of verbose output')
parser.add_option ('-s', '--segPrefix', action='store', type="string",
default='', help='override seg prefix with this')
parser.add_option ('-S', '--ScanList', action='store', type="string",
default='', help='OnlyOne, Random, Flip, DontFlip')
parser.add_option ('-f', '--force', action='store_true',
default=False, help='force recalculation of whatever')
(options, args) = parser.parse_args()
#if len(args) < 1:
# parser.error ('missing argument')
G.VerboseLevel = options.verboseLevel
if options.verbose:
if G.VerboseLevel < 1: G.VerboseLevel = 1
if options.Verbose:
if G.VerboseLevel < 2: G.VerboseLevel = 2
#---------------------------------------------------------------------------
# Install links if the files in bin are not present
fileList = ['MetaInfo.py',\
'ScanList.py',\
'Conf.py']
for theFile in fileList:
#print theFile
targFile = '../'+G.Settings+'/'+theFile
linkFile = 'bin/'+theFile
if os.access(linkFile,os.F_OK) == False:
cmd = 'cd bin ; ln -s '+targFile+' '+theFile
print 'cmd is',cmd
os.system(cmd)
else:
if G.VerboseLevel > 0:
print linkFile+' is already there, no need to link'
#---------------------------------------------------------------------------
import MetaInfo # Where to find ID, age etc in Meta Info File
if G.VerboseLevel > 0:
print 'Running: ', __file__
print time.ctime(G.StartTime)
print "VerboseLevel is", G.VerboseLevel
print "force is",options.force
print ''
#print 'Exiting early.'
#sys.exit()
exit_code = main() # DoItToIt!
if exit_code is None:
exit_code = 0
tt = 'Total time: %s (average: %s)' % \
(timedelta(seconds=(time.time() - G.StartTime)), \
timedelta(seconds=AvgTime))
if options.verbose >= 1:
print time.asctime()
print tt
sys.exit(exit_code)
except KeyboardInterrupt, e: # Ctrl-C
raise e
except SystemExit, e: # sys.exit()
raise e
except Exception, e:
print 'ERROR, UNEXPECTED EXCEPTION'
print str(e)
traceback.print_exc()
os._exit(1)
# vi:set autoindent ts=2 sw=2 expandtab : See Vim, :help 'modeline'