script/read_dcm.pl untested

filter: Statementstmt 0.0% 0 / 140 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 13 Podpod n/a totaltotal 0.0% 0 / 220 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2 #
3  
4 use PDL::IO::Dcm qw/printStruct load_dcm_dir parse_dcms/;
5 use strict "vars";
6 use PDL::NiceSlice;
7 use PDL;
8 use PDL::IO::Sereal qw/wsereal/;
9 use Getopt::Tabular;
10 use Data::Dumper;
11 my %opt;
12 my $plugin="Primitive";
13 my ($d,$nifti,$sereal,$ps,$usage,$t,$del_raw,$sp,$use_dims,$flex,$video);
14 $del_raw=1;
15 my @opts=(
16 #['-d','boolean',0,\$d, 'split not into lProtID but dicom series number'],
17 ['-u', 'string' ,1,\$plugin,'specify the plugin to process data'],
18 ['-h', 'boolean',1,\$usage, 'print this help'],
19 ['-p', 'boolean',0,\$sp, 'split slice groups'],
20 ['-s','boolean',1,\$sereal, 'Create sereal '],
21 ['-t','boolean',0,\$t, 'serialise cardiac phases and time'],
22 ['-i','boolean',0,\$nifti, 'Create .nii and .txt files'],
23 ['-r','boolean',0,\$del_raw, 'delete raw dicom fields, default'],
24 ['-d','boolean',0,\$use_dims, 'include PDL::Dimd support, implies -s, otherwise useless.'],
25 ['-f','boolean',0,\$flex, 'save using writeflex (PDL::IO::FlexRaw'],
26 ['-v','boolean',0,\$video, 'save as video.'],
27 );
28 &GetOptions (\@opts, \@ARGV) || exit 1;
29  
30 if ($usage or $#ARGV ==0 ) {
31 print "read_dcm.pl [options] <directory> <output_prefix>\n";
32 print "Reads and sorts Siemens dicom files and converts them into piddles. If nothing else is specified, FlexRaw is used to write the data. A text header is written.\n";
33 for my $arg (@opts) {
34 print "$$arg[0] [ $$arg[1] ] : -- $$arg[-1]\n";
35 }
36 exit;
37 }
38  
39 my $dir=shift;
40 my $pre=shift;
41 $opt{plugin}=$plugin;
42 $plugin = "PDL/IO/Dcm/Plugins/$opt{plugin}"; # set plugin
43  
44 require ($plugin.'.pm') || die "Plugin $plugin could not be loaded!\n";
45 print "Plugin: PDL::IO::Dcm::Plugins::$opt{plugin}\n";
46 eval("PDL::IO::Dcm::Plugins::$opt{plugin}")->import( qw/setup_dcm/);
47  
48  
49 $opt{split}=$sp;
50 # clump flags
51 $opt{c_phase_t}=$t;
52 $ps=$nifti or $ps;
53 $opt{c_phase_set}=$ps;
54 $opt{Nifti}=$nifti;
55 $opt{use_dims}=$use_dims;
56 #eval("PDL::IO::Dcm::Plugins::$opt{plugin}")->import( qw/init_dims/) if $opt{use_dims};
57 $sereal |= $use_dims; # use sereal to save data, otherwise useless!
58 setup_dcm(\%opt);
59 $opt{delete_raw}=$del_raw;
60 # loads all dicom files in this directory
61 my $dcms=load_dcm_dir($dir,\%opt);
62 die "no data!" unless (keys %$dcms);
63 print "Read data; IDs: ",join ', ',keys %$dcms,"\n";
64 # sort all individual dicoms into a hash of piddles.
65 my $data=parse_dcms($dcms,\%opt);
66  
67 # save all data to disk
68 print "Nifti? $nifti Sereal? $sereal write? ",((! $sereal) or $nifti),"\n";
69 for my $pid (keys %$data) {
70 no PDL::NiceSlice;
71 print "Processing $pid.\n";
72 $opt{$$data{$pid}->hdr->{plugin}}->{init_dims}->($$data{$pid},\%opt) if ($opt{use_dims});
73 use PDL::NiceSlice;
74 my $name =sprintf("$pre\_%04d",$pid);
75 if ($pid =~/\d*[a-z]$/) {
76 $name =sprintf("$pre\_%05s",$pid);
77 }
78 my $hdr = $$data{$pid}->hdr;
79 if ($video && $opt{use_dims}) {
80 require (PDL::IO::Pic) || die "Make sure PDL::IO::Pic is installed!\n";
81 next if $$data{$pid}->hdr->{plugin} eq "MRSSiemens";
82 for my $dim ($opt{$$data{$pid}->hdr->{plugin}}->{dim_list} ) {
83
84 }
85 }
86 if ($nifti) {
87 require (PDL::IO::Nifti) || die "Make sure PDL::IO::Nifti is installed!\n";
88 print "Creating Nifti $pid ",$$data{$pid}->info,"\n";
89 my $ni=PDL::IO::Nifti->new;
90 $ni->img($$data{$pid}->double->(,-1:0,)); #->reorder(0,1,9,4,2,3,5,6,7,8,10));
91 $ni->write_nii("$name.nii",$pid);
92 }
93 if ($nifti ) {
94 open F,">","$name.txt";
95 print F "## Generated using PDL::IO::Dcm\n\n";
96 print F "dimensions: ",join ' ',(join ' ',@{$$data{$pid}->hdr->{dimnames}})."\n\n";
97 print F printStruct ($hdr->{rot},'rot');
98 for my $dname (@{$hdr->{dimnames}}) {
99 my $dim =$hdr->{$dname};
100 print F printStruct( $dim,$dname ) ;
101 print F "\n";
102 }
103 if ($plugin =~/Siemens/) {
104 print F "### ASCCONV BEGIN ###\n";
105 for my $k (sort keys %{$$data{$pid}->hdr->{ascconv}} )
106 { print F "$k = ",$$data{$pid}->hdr->{ascconv}->{$k},"\n" ;}
107 print F "### ASCCONV END ###\n\n";
108 }
109 print F "*** Parameters extracted from dicom fields \n";
110 for my $k (sort keys %{$$data{$pid}->hdr->{dicom}} ) {
111 print F printStruct( $$data{$pid}->hdr->{dicom}->{$k},$k,) ;
112 }
113 print F "*** Parameters extracted from dicom CSA fields \n";
114 for my $k (sort keys %{$$data{$pid}->hdr->{csa}} ) {
115 print F printStruct( $$data{$pid}->hdr->{csa}->{$k},$k,) ;
116 }
117 print F "\n\n*** Parameters that vary between images; from CSA diff fields \n";
118 for my $k (sort keys %{$$data{$pid}->hdr->{csa_diff}} ) {
119 print F printStruct( $$data{$pid}->hdr->{csa_diff}->{$k},$k,) ;
120 }
121 print F "\n\n*** Parameters that vary between images; from diff fields \n";
122 for my $k (sort keys %{$$data{$pid}->hdr->{diff}} ) {
123 print F printStruct( $$data{$pid}->hdr->{diff}->{$k},$k,) ;
124 }
125 print F "*** End of Parameters \n\n";
126 close F;
127 }
128 if ($sereal) {
129 print "Parsed data. IDs: ",join (', ',keys %$data),"\n";
130 #print "Pos: ",$$data{$pid}->hdr->{dicom}->{'Image Position (Patient)'};
131 print "Writing file $name.srl\n", $$data{$pid}->info,"\n";
132 $$data{$pid}->wsereal("$name.srl");
133 }
134 if ($flex || !($sereal or $nifti) ) {
135 require PDL::IO::FlexRaw ;
136 $PDL::IO::FlexRaw::writeflexhdr=1;
137 PDL::IO::FlexRaw::writeflex($name,$$data{$pid}) ||
138 die "Could not write data to $pre\_$pid";
139 }
140 }