-
Notifications
You must be signed in to change notification settings - Fork 3
/
GeneralMappingWindowController.m
495 lines (417 loc) · 22 KB
/
GeneralMappingWindowController.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
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
//
// GeneralMappingWindowController.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 "GeneralMappingWindowController.h"
//#import <OsiriXAPI/DicomStudy.h>
//#import <OsiriXAPI/DicomSeries.h>
#import <OsiriXAPI/BrowserController.h>
#import "OBOCollectedData.h"
#import "OBOSeries.h"
#import "MappingSummaryWindowController.h"
#import "DCM Framework/DCMObject.h"
#import "DCM Framework/DCMAttribute.h"
#import "DCM Framework/DCMAttributeTag.h"
@implementation GeneralMappingWindowController
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
OBOCollectedData *sharedData = [OBOCollectedData sharedManager];
return [sharedData.seriesDescription count];
}
-(NSView* )tableView:(NSTableView*)tableView viewForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row{
OBOCollectedData *sharedData = [OBOCollectedData sharedManager];
NSArray *sequenceNames = [sharedData.seriesDescription allKeys];
NSString *currentName = [sequenceNames objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
if ([identifier isEqualToString:@"NameCol"]) {
NSTableCellView *cellView = [tableView makeViewWithIdentifier:@"NameCell" owner:self]; // will give us back the view that we created in the gui editor; identifier refers to TableCellView
[cellView.textField setStringValue:currentName];
return cellView;
}
// suffix - T1w, bold, ... - will also determine anat, func, fmap
else if ([identifier isEqualToString:@"SuffixCol"]){
NSTableCellView *cellView = [tableView makeViewWithIdentifier:@"SuffixCell" owner:self];
[cellView.textField setStringValue:[[sharedData.seriesDescription valueForKey:currentName] valueForKey:@"suffix"]]; // in case it was changed outside UI - eg by loading or in another function
return cellView;
}
// run
else if ([identifier isEqualToString:@"RunCol"]){
NSTableCellView *cellView = [tableView makeViewWithIdentifier:@"RunCell" owner:self];
[cellView.textField setEditable:TRUE];
[cellView.textField setStringValue:[[sharedData.seriesDescription valueForKey:currentName] valueForKey:@"run"]];
return cellView;
}
// task
else if ([identifier isEqualToString:@"TaskCol"]){
NSTableCellView *cellView = [tableView makeViewWithIdentifier:@"TaskCell" owner:self];
[cellView.textField setStringValue:[[sharedData.seriesDescription valueForKey:currentName] valueForKey:@"task"]];
NSString *currentSuffix = [[sharedData.seriesDescription valueForKey:currentName] valueForKey:@"suffix"];
if ([currentSuffix isEqualToString:@"bold"]) {
[cellView.textField setEditable:TRUE];
} else {
[cellView.textField setEditable:FALSE];
// [cellView.textField setStringValue:@""]; // but this does not affect the underlying data source (old value still stays)
}
return cellView;
}
// acq
else if ([identifier isEqualToString:@"AcqCol"]) {
NSTableCellView *cellView = [tableView makeViewWithIdentifier:@"AcqCell" owner:self];
[cellView.textField setEditable:TRUE];
[cellView.textField setStringValue:[[sharedData.seriesDescription valueForKey:currentName] valueForKey:@"acq"]];
return cellView;
}
return nil;
}
-(IBAction)itemTextFieldUpdated:(id)sender{
NSInteger selectedRow = [self.tableView rowForView:sender];
NSString *newValue = [sender stringValue];
NSInteger columnIndex = [self.tableView columnForView:sender];
OBOCollectedData *sharedData = [OBOCollectedData sharedManager];
NSArray *sequenceNames = [sharedData.seriesDescription allKeys]; // this prob should be class variable
NSString *currentName = [sequenceNames objectAtIndex:selectedRow];
if (columnIndex == 1){
// suffix
[[sharedData.seriesDescription objectForKey:currentName] setValue:newValue forKey:@"suffix"];
if (![newValue isEqualToString:@"bold"]) {
// only bold sequences can have task name
[[sharedData.seriesDescription objectForKey:currentName] setValue:@"" forKey:@"task"];
}
[self.tableView abortEditing]; // in case editing of a text field was not finished
//[self.tableView reloadData]; // so that task name turns editable or not
[self.tableView reloadDataForRowIndexes:[NSIndexSet indexSetWithIndex:selectedRow] columnIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 5)]];
}
if (columnIndex == 2){
// task
[[sharedData.seriesDescription objectForKey:currentName] setValue:newValue forKey:@"task"];
}
else if (columnIndex == 3){
// run
[[sharedData.seriesDescription objectForKey:currentName] setValue:newValue forKey:@"run"];
}
else if (columnIndex == 4){
// acq
[[sharedData.seriesDescription objectForKey:currentName] setValue:newValue forKey:@"acq"];
}
}
-(IBAction)saveMapping:(id)sender{
OBOCollectedData *sharedData = [OBOCollectedData sharedManager];
[sharedData.listOfSeries removeAllObjects];
[self annotateAllSeries];
[self createDatasetDescription];
_SummaryWindow = [[MappingSummaryWindowController alloc] initWithWindowNibName:@"MappingSummaryWindow"];
[_SummaryWindow showWindow:self];
}
- (IBAction)showSessionPopover:(id)sender {
[_sessionPopover showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxYEdge];
}
- (IBAction)showDescriptionPopover:(id)sender {
[_descriptionPopover showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMinYEdge];
}
- (IBAction)storeMappingForLater:(id)sender {
OBOCollectedData *sharedData = [OBOCollectedData sharedManager];
NSData *mappingJson = [NSJSONSerialization dataWithJSONObject:sharedData.seriesDescription options:NSJSONWritingPrettyPrinted error:nil];
NSSavePanel *saveDlg = [NSSavePanel savePanel];
[saveDlg setTitle:@"Store mapping for later"];
[saveDlg setAllowedFileTypes:@[@"json"]];
if ( [saveDlg runModal] == NSModalResponseOK ) {
[mappingJson writeToURL:[saveDlg URL] atomically:YES];
}
}
- (IBAction)useStoredMapping:(id)sender {
OBOCollectedData *sharedData = [OBOCollectedData sharedManager];
NSOpenPanel *openDlg = [NSOpenPanel openPanel];
[openDlg setTitle:@"Select mapping to be used"];
[openDlg setAllowedFileTypes:@[@"json"]];
if ( [openDlg runModal] == NSModalResponseOK ) {
NSData *mappingData = [NSData dataWithContentsOfURL:[openDlg URL]];
NSMutableDictionary *storedDict = [NSJSONSerialization JSONObjectWithData:mappingData
options:NSJSONReadingMutableContainers error:nil];
// annotate the series we already have with values that were loaded
// can't just replace the entire dictionary because the loaded one may be incomplete
for (NSString *seriesName in [sharedData.seriesDescription allKeys]) {
if ([storedDict objectForKey:seriesName]) {
[[sharedData.seriesDescription objectForKey:seriesName] addEntriesFromDictionary:[storedDict objectForKey:seriesName]];
}
}
// do the same things as save mapping would do
[sharedData.listOfSeries removeAllObjects];
[_tableView reloadData];
[self annotateAllSeries];
[self createDatasetDescription];
_SummaryWindow = [[MappingSummaryWindowController alloc] initWithWindowNibName:@"MappingSummaryWindow"];
[_SummaryWindow showWindow:self];
}
}
-(void) annotateAllSeries {
OBOCollectedData *sharedData = [OBOCollectedData sharedManager];
NSPredicate *takeOnlyMR = [NSPredicate predicateWithFormat:@"modality = %@", @"MR"];
NSMutableArray *decoratedFromCurrentStudy = [[NSMutableArray alloc] init];
NSCountedSet *namesFromCurrentStudy = [[NSCountedSet alloc] init]; // to keep track of repetitions
NSString *sessionLabel = [[NSString alloc] init];
NSString *subjectName = [[NSString alloc] init];
for (DicomStudy *currentStudy in sharedData.listOfStudies) {
[decoratedFromCurrentStudy removeAllObjects];
[namesFromCurrentStudy removeAllObjects];
sessionLabel = [self createSessionLabelForStudy:currentStudy];
subjectName = [self createSubjectNameForStudy:currentStudy];
for (DicomSeries *currentSeries in [[currentStudy imageSeries] filteredArrayUsingPredicate:takeOnlyMR]) {
OBOSeries *decoratedSeries = [[OBOSeries alloc] initWithSeries:currentSeries params:[sharedData.seriesDescription objectForKey:currentSeries.name]];
[decoratedSeries setValue:subjectName forKey:@"participant"];
[decoratedSeries setValue:sessionLabel forKey:@"session"];
if ([[decoratedSeries suffix] hasSuffix:@"_epi"]){
// our GUI allows dir-AP_epi and dir-PA_epi "suffixes", translate them into dir & suffix
[decoratedSeries setValue:[self getDirFromSuffix:[decoratedSeries suffix]] forKey:@"dir"];
[decoratedSeries setValue:@"epi" forKey:@"suffix"];
}
[decoratedFromCurrentStudy addObject:decoratedSeries];
[namesFromCurrentStudy addObject:currentSeries.name];
}
// discard BOLD series from current study if they have less volumes than requested minimum
NSNumber *minimum = [NSNumber numberWithInteger:[[self minimumBoldField] integerValue]];
for (OBOSeries *decoratedSeries in decoratedFromCurrentStudy){
if ([[decoratedSeries suffix] isEqualToString:@"bold"]){
if ([[[decoratedSeries series] numberOfImages] isLessThan:minimum]){
[decoratedSeries setDiscard:YES];
[decoratedSeries setValue:@"TooShort" forKey:@"comment"];
}
}
}
// handle series which had the same series name: add run numbers (if not given) or discard all but the last
NSNumber *runNumber;
for (NSString *seriesName in [namesFromCurrentStudy objectEnumerator]) {
if ([namesFromCurrentStudy countForObject:seriesName] > 1) { // series with this name has been repeated
// get (decorated) series matching this name
NSArray *repeated = [decoratedFromCurrentStudy filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"originalName=%@", seriesName]];
// sort them by acquisition date
repeated = [repeated sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
NSDate *first = [[(OBOSeries*)obj1 series] date];
NSDate *second = [[(OBOSeries*)obj2 series] date];
return [first compare:second];
}];
if ([[[sharedData.seriesDescription objectForKey:seriesName] objectForKey:@"suffix"] isEqualToString:@"(fmap)"]) {
// give field maps special treatment (they come in threes, at least from our Siemens)
if ([[[sharedData.seriesDescription objectForKey:seriesName] objectForKey:@"run"] length] > 0) {
// if run number was specified, assume it's a repeat
// discard all but the last three, leave last three for further work
for(int i = 0; i < [repeated count] - 3; i++){
[[repeated objectAtIndex:i] setDiscard:YES];
[[repeated objectAtIndex:i] setValue:@"Discard - repeated" forKey:@"comment"];
}
repeated = [repeated subarrayWithRange:NSMakeRange([repeated count]-3, 3)];
}
// handle them by threes
for (int i = 0; i < [repeated count]; i=i+3) {
NSArray *triplet = [repeated subarrayWithRange:NSMakeRange(i, 3)];
[self assignFieldMapSuffixes:triplet];
for (OBOSeries *series in triplet){
if ([repeated count] == 3) {
// run number was given or there was only one run
[series setValue:[[sharedData.seriesDescription objectForKey:seriesName] objectForKey:@"run"] forKey:@"run"];
}
else {
[series setValue:[@(i/3 + 1) stringValue] forKey:@"run"];
}
}
}
}
else if ([[[sharedData.seriesDescription objectForKey:seriesName] objectForKey:@"run"] length] > 0) {
// run number was specified for this study name, assume it's a repeat and discard all but latest
for (int i = 0; i < [repeated count] - 1; i++){
[[repeated objectAtIndex:i] setDiscard:YES]; // turns out this is the way to set bool flag :/
[[repeated objectAtIndex:i] setValue:@"Discard - repeated" forKey:@"comment"];
}
}
else {
// create and insert run numbers
runNumber = [NSNumber numberWithInt:1]; // keep it separate from iterator because some runs may have been discarded for different reasons
for (int i = 0; i < [repeated count]; i++){
if (! [[repeated objectAtIndex:i] discard]) {
[[repeated objectAtIndex:i] setValue:[runNumber stringValue] forKey:@"run"];
runNumber = [NSNumber numberWithInt:[runNumber intValue]+1];
}
}
}
}
}
[sharedData.listOfSeries addObjectsFromArray:decoratedFromCurrentStudy];
}
}
-(void)assignFieldMapSuffixes:(NSArray*)fieldMapTriplet{
DCMAttributeTag *echoTimeTag = [DCMAttributeTag tagWithTagString:@"0018, 0081"]; // EchoTime
DCMAttributeTag *imageTypeTag = [DCMAttributeTag tagWithTagString:@"0008, 0008"]; // ImageType
// ORIGINAL, PRIMARY, M, ND - Magnitude
// ORIGINAL, PRIMARY, P, ND - Phasediff
double shorterTE = 0;
double longerTE = 0;
double echoTime;
NSString *imageType;
DCMAttribute *attr;
// first run - get shorter and longer TE
for (OBOSeries *fieldMapSeries in fieldMapTriplet){
DicomSeries *series = [fieldMapSeries series];
NSArray *imagesArray = [[[series paths] allObjects] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSString *firstImagePath = [imagesArray objectAtIndex:0];
DCMObject *dcmObj = [DCMObject objectWithContentsOfFile:firstImagePath decodingPixelData:NO];
attr = [dcmObj attributeForTag:imageTypeTag];
imageType = [[attr values] objectAtIndex:2];
attr = [dcmObj attributeForTag:echoTimeTag];
echoTime = [[attr value] doubleValue];
if ([imageType isEqualToString:@"M"]){
if (echoTime > shorterTE){
shorterTE = longerTE;
longerTE = echoTime;
}
else {
shorterTE = echoTime;
}
}
}
// second run - assign suffix
for (OBOSeries *fieldMapSeries in fieldMapTriplet){
DicomSeries *series = [fieldMapSeries series];
NSArray *imagesArray = [[[series paths] allObjects] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSString *firstImagePath = [imagesArray objectAtIndex:0];
DCMObject *dcmObj = [DCMObject objectWithContentsOfFile:firstImagePath decodingPixelData:NO];
attr = [dcmObj attributeForTag:imageTypeTag];
imageType = [[attr values] objectAtIndex:2];
attr = [dcmObj attributeForTag:echoTimeTag];
echoTime = [[attr value] doubleValue];
if ([imageType isEqualToString:@"P"]){
[fieldMapSeries setValue:@"phasediff" forKey:@"suffix"];
// also store TE in seconds (DICOM values are in ms) to be written in json sidecar
[[fieldMapSeries fieldmapParams] setObject:[NSNumber numberWithDouble:shorterTE*0.001] forKey:@"EchoTime1"];
[[fieldMapSeries fieldmapParams] setValue:[NSNumber numberWithDouble:longerTE*0.001] forKey:@"EchoTime2"];
}
else if ([imageType isEqualToString:@"M"]){
if (echoTime == shorterTE){
[fieldMapSeries setValue:@"magnitude1" forKey:@"suffix"];
}
else {
[fieldMapSeries setValue:@"magnitude2" forKey:@"suffix"];
}
}
}
}
-(NSString*) createSessionLabelForStudy:(DicomStudy *)study {
NSString *pattern = [_studyNameRegexpField stringValue];
NSString *template = [_sessionLabelTemplateField stringValue];
NSString *result;
if (!_seriesCheckBoxIsEnabled) {
// derive from subject name
NSString *subjectName = study.name;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
NSTextCheckingResult *match = [regex firstMatchInString:subjectName options:0 range:NSMakeRange(0, [subjectName length])];
if (match) {
result = [regex replacementStringForResult:match inString:subjectName offset:0 template:template];
} else {
result = template;
}
} else {
// derive from series name
NSTextCheckingResult *match;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"_ses-([a-zA-Z0-9]*)" options:0 error:nil];
for (DicomSeries *currentSeries in [study imageSeries]) {
match = [regex firstMatchInString:[currentSeries name] options:0 range:NSMakeRange(0, [[currentSeries name] length])];
if (match) {
result = [[currentSeries name] substringWithRange:[match rangeAtIndex:1]];
break;
}
}
if ([result length] == 0) {
result = @"unknown";
}
}
return result;
}
-(NSString*) createSubjectNameForStudy:(DicomStudy *)study {
NSString *result;
NSString *subjectName = study.name;
NSString *pattern = [_studyNameRegexpField stringValue];
NSString *template = [_subjectLabelTemplateField stringValue];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
NSTextCheckingResult *match = [regex firstMatchInString:subjectName options:0 range:NSMakeRange(0, [subjectName length])];
if (match) {
result = [regex replacementStringForResult:match inString:subjectName offset:0 template:template];
} else {
result = subjectName;
}
return result;
}
-(NSString*) getDirFromSuffix:(NSString *)suffix {
// little helper for getting dir label from "dir-(AP|PA)_epi"
NSString *result = @"";
if ([suffix hasPrefix:@"dir-AP"]) {
result = @"AP";
}
else if ([suffix hasPrefix:@"dir-PA"]) {
result = @"PA";
}
return result;
}
-(void) createDatasetDescription {
OBOCollectedData *sharedData = [OBOCollectedData sharedManager];
[sharedData.datasetDescription removeAllObjects];
// Name
if ([[_datasetNameField stringValue] length] > 0) {
[sharedData.datasetDescription setValue:[_datasetNameField stringValue] forKey:@"Name"];
} else {
[sharedData.datasetDescription setValue:@"Unnamed experiment" forKey:@"Name"];
}
// BIDSVersion
if ([[_bidsVersionField stringValue] length] > 0) {
[sharedData.datasetDescription setValue:[_bidsVersionField stringValue] forKey:@"BIDSVersion"];
} else {
[sharedData.datasetDescription setValue:@"1.0.2" forKey:@"BIDSVersion"];
}
// License
if ([[_licenseField stringValue] length] > 0) {
[sharedData.datasetDescription setValue:[_licenseField stringValue] forKey:@"License"];
}
// Authors
if ([[_authorsField stringValue] length] > 0) {
[sharedData.datasetDescription setValue:[[_authorsField stringValue] componentsSeparatedByString:@";"] forKey:@"Authors"];
}
// Acknowledgements
if ([[_acknowledgementsField stringValue] length] > 0) {
[sharedData.datasetDescription setValue:[_acknowledgementsField stringValue] forKey:@"Acknowledgements"];
}
// HowToAcknowledge
if ([[_howToAcknowledgeField stringValue] length] > 0) {
[sharedData.datasetDescription setValue:[_howToAcknowledgeField stringValue] forKey:@"HowToAcknowledge"];
}
// Funding
if ([[_fundingField stringValue] length] > 0) {
[sharedData.datasetDescription setValue:[_fundingField stringValue] forKey:@"Funding"];
}
// ReferencesAndLinks
if ([[_referencesAndLinksField stringValue] length] > 0) {
[sharedData.datasetDescription setValue:[[_referencesAndLinksField stringValue] componentsSeparatedByString:@";"] forKey:@"ReferencesAndLinks"];
}
// DatasetDOI
if ([[_datasetDoiField stringValue] length] > 0) {
[sharedData.datasetDescription setValue:[_datasetDoiField stringValue] forKey:@"DatasetDOI"];
}
// write or not
if ([_noDescriptionCheckBox state] == NSOnState) {
[sharedData setWriteDatasetDescription:NO];
} else {
[sharedData setWriteDatasetDescription:YES];
}
}
@end