-
Notifications
You must be signed in to change notification settings - Fork 3
/
MappingSummaryWindowController.m
254 lines (210 loc) · 10.1 KB
/
MappingSummaryWindowController.m
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
//
// MappingSummaryWindowController.m
// OsirixBidsOutput
//
// Copyright (c) 2018 Michał Szczepanik.
//
// This file is part of Osirix / Horos BIDS Output Extension.
//
// BIDS Output Extension 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.
//
// BIDS Output Extension is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with BIDS Output Extension. If not, see <http://www.gnu.org/licenses/>.
#import "MappingSummaryWindowController.h"
#import "OBOCollectedData.h"
#import "OBOSeries.h"
#import "OBOExporter.h"
@interface MappingSummaryWindowController ()
@end
@implementation MappingSummaryWindowController
- (void)windowDidLoad {
[super windowDidLoad];
NSString* defaultConverterPath = [NSHomeDirectory() stringByAppendingPathComponent:@"dcm2niix"];
if ( [[NSFileManager defaultManager] isExecutableFileAtPath:defaultConverterPath] )
{
[_converterTextField setStringValue:defaultConverterPath];
}
}
-(IBAction)changeConverterPath:(id)sender{
NSString *converterPath;
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseFiles:YES];
[openDlg setCanChooseDirectories:NO];
[openDlg setAllowsMultipleSelection:NO];
if ( [openDlg runModal] == NSModalResponseOK ) {
converterPath = [[openDlg URL] relativeString];
converterPath = [converterPath stringByReplacingOccurrencesOfString:@"file://" withString:@""];
converterPath = [converterPath stringByReplacingOccurrencesOfString:@"%20" withString:@" "];
[_converterTextField setStringValue:converterPath];
}
}
-(IBAction)changeBidsRoot:(id)sender{
NSString *bidsRootPath;
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseFiles:NO];
[openDlg setCanChooseDirectories:YES];
[openDlg setCanCreateDirectories:YES];
[openDlg setAllowsMultipleSelection:NO];
if ( [openDlg runModal] == NSModalResponseOK ) {
bidsRootPath = [[openDlg URL] relativeString];
bidsRootPath = [bidsRootPath stringByReplacingOccurrencesOfString:@"file://" withString:@""];
bidsRootPath = [bidsRootPath stringByReplacingOccurrencesOfString:@"%20" withString:@" "];
[_bidsRootTextField setStringValue:bidsRootPath];
}
}
-(IBAction)exportToBids:(id)sender{
// get parameters from the UI
BOOL compress = ([[self gzCheckBox] state] == NSOnState);
BOOL createScans = ([[self scansCheckBox] state] == NSOnState);
NSString *converterPath = [_converterTextField stringValue];
NSString *bidsRootPath = [_bidsRootTextField stringValue];
// check if paths seem ok
if ([bidsRootPath length] == 0 || ![[NSFileManager defaultManager] isExecutableFileAtPath:converterPath]){
NSAlert *warningAlert = [[NSAlert alloc] init];
[warningAlert addButtonWithTitle:@"OK"];
[warningAlert setAlertStyle:NSAlertStyleWarning];
if ([bidsRootPath length] == 0){
[warningAlert setMessageText:@"Empty BIDS root path"];
[warningAlert setInformativeText:@"Specify the path where BIDS root should be."];
} else {
[warningAlert setMessageText:@"Invalid dcm2niix executable"];
[warningAlert setInformativeText:@"Either path is incorrect or the chosen file can't be executed by the user."];
}
[warningAlert runModal];
return;
}
// create a temporary directory for dicoms, do not run conversion if directory already exists
BOOL createdDicomDir;
createdDicomDir = [OBOExporter createTemporaryDicomDirectory];
if (!createdDicomDir) {
// run an NSAlert asking for permission to delete temporary dicom folder and create it again
NSAlert *warningAlert = [[NSAlert alloc] init];
[warningAlert setAlertStyle:NSAlertStyleWarning];
[warningAlert addButtonWithTitle:@"Clear dicom and proceed"];
[warningAlert addButtonWithTitle:@"Cancel"];
[warningAlert setMessageText:@"Error when creating temporary dicom directory"];
[warningAlert setInformativeText:@"It appears that temporary dicom directory in ~/HorosBidsOutput folder already exists and may cause export conflicts. Make sure to remove the dicom folder before converting.\n This error may also occur if the directory didn't exist, but could not be created."];
if ([warningAlert runModal] == NSAlertFirstButtonReturn) {
// user chose to remove dicomdir, try removing & creating again
[OBOExporter removeTemporaryDicomDirectory];
createdDicomDir = [OBOExporter createTemporaryDicomDirectory];
if (!createdDicomDir) {
// still unable to create directory, possibly because of access rights
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"];
[alert setMessageText:@"Could not proceed"];
[alert runModal];
return;
}
} else {
// user chose not to proceed, do nothing
return;
}
}
[[self spinner] startAnimation:self];
// prepare for error handling
BOOL convertSuccessful;
NSError *conversionError;
NSMutableArray *errorList = [[NSMutableArray alloc] init];
// run the conversion for each file
OBOCollectedData *sharedData = [OBOCollectedData sharedManager];
for (OBOSeries *currentSeries in [sharedData listOfSeries]) {
conversionError = nil;
if ( ![currentSeries discard] && [[currentSeries getBidsPath] length] > 0) {
convertSuccessful = [OBOExporter exportSeries:currentSeries
usingConverterAt:converterPath
toFolder:bidsRootPath
withCompression:compress
withScansFile:createScans
error:&conversionError ];
if (!convertSuccessful) {
// if an error occurs, just store file name and error description (but we could also break out of the loop here)
[errorList addObject:@[currentSeries.getBidsPath, conversionError.localizedDescription]];
}
}
}
[OBOExporter removeTemporaryDicomDirectory];
// save summary table to a csv file
[self saveSummary];
// log errors if found
if ([errorList count] > 0) {
[self saveErrors:errorList];
}
// write dataset_description.json
NSMutableDictionary *datasetDescription = [sharedData datasetDescription];
if ([sharedData writeDatasetDescription] && [NSJSONSerialization isValidJSONObject:datasetDescription]){
NSString *jsonPath = [NSString pathWithComponents:@[bidsRootPath, @"dataset_description.json"]];
NSData *datasetDescriptionData = [NSJSONSerialization dataWithJSONObject:datasetDescription options:NSJSONWritingPrettyPrinted error:nil];
[datasetDescriptionData writeToFile:jsonPath atomically:NO];
}
// report the export as finished
[[self spinner] stopAnimation:self];
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"];
[alert setMessageText:@"Finished exporting files."];
if ([errorList count] > 0) {
[alert setAlertStyle:NSAlertStyleWarning];
[alert setInformativeText:@"Export finished with errors.\nCheck errors_(datetime).txt file in ~/HorosBidsOutput folder."];
}
[alert runModal];
}
-(void)saveSummary {
OBOCollectedData *sharedData = [OBOCollectedData sharedManager];
// create file name containing date
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd_HHmmss"];
NSString *fileName = [NSString stringWithFormat:@"export_%@.csv", [dateFormatter stringFromDate:now]];
NSString *filePath = [NSString pathWithComponents:@[NSHomeDirectory(), @"HorosBidsOutput", fileName]];
// prepare strings for csv content and row
NSMutableString * csvRep = [NSMutableString new];
NSString *row;
// add header
row = [NSString stringWithFormat:@"%@;%@;%@;%@;%@\n",
@"Subject Name",
@"Series Name",
@"Series ID",
@"Comment",
@"BIDS path"];
[csvRep appendString:row];
// add contents
for (OBOSeries *currentSeries in [sharedData listOfSeries])
{
row = [NSString stringWithFormat:@"%@;%@;%@;%@;%@\n",
[currentSeries originalSubjectName],
[[currentSeries series] valueForKey:@"name"],
[[currentSeries series] valueForKey:@"id"],
[currentSeries comment],
[currentSeries getBidsPath]];
[csvRep appendString:row];
}
// write the file
[csvRep writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
-(void) saveErrors: (NSMutableArray*) errorList {
// create file name containing date
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd_HHmmss"];
NSString *fileName = [NSString stringWithFormat:@"errors_%@.txt", [dateFormatter stringFromDate:now]];
NSString *filePath = [NSString pathWithComponents:@[NSHomeDirectory(), @"HorosBidsOutput", fileName]];
// prepare strings for csv content and row
NSMutableString * tsvRep = [NSMutableString new];
NSString *row;
// add contents
for (NSArray *entry in errorList) {
row = [NSString stringWithFormat:@"%@\t%@\n", [entry objectAtIndex:0], [entry objectAtIndex:1]];
[tsvRep appendString:row];
}
// write the file
[tsvRep writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
@end