forked from pyosirix/pyosirix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyDicomStudy.m
592 lines (517 loc) · 17.9 KB
/
pyDicomStudy.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
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
//
// pyDicomStudy.m
// pyOsiriX
//
/*
Copyright (c) 2016, The Institute of Cancer Research and The Royal Marsden.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "pyDicomStudy.h"
#import "pyDicomImage.h"
#import "pyDicomSeries.h"
#import <OsiriXAPI/DicomImage.h>
#import <OsiriXAPI/Dicomseries.h>
#import <OsiriXAPI/DicomStudy.h>
#include <Python/datetime.h>
# pragma mark -
# pragma mark pyDicomStudy initialization/deallocation
static void pyDicomStudy_dealloc(pyDicomStudyObject *self)
{
[self->obj release];
self->ob_type->tp_free(self);
}
# pragma mark -
# pragma mark pyDicomStudyObject str/repr
static PyObject *DicomStudy_str(pyDicomStudyObject *self)
{
NSString *str = [NSString stringWithFormat:@"DicomStudy object\nName: %@\nDate: %@\nModality:%@\nNumber of images:%@\n", [self->obj name], [self->obj date], [self->obj modality], [self->obj numberOfImages]];
PyObject *ostr = PyString_FromString([str UTF8String]);
if (ostr == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
return ostr;
}
# pragma mark -
# pragma mark pyDicomStudyObject getters/setters
PyDoc_STRVAR(DicomStudyNumberOfImagesAttr_doc,
"The integer number of images contained by the DicomStudy. This property cannot be set.\n"
);
static PyObject *pyDicomStudy_getNumberOfImages(pyDicomStudyObject *self, void *closure)
{
NSNumber *num = [self->obj numberOfImages];
return PyInt_FromLong([num longValue]);
}
PyDoc_STRVAR(DicomStudySeriesAttr_doc,
"A tuple containing all DicomSeries within the DicomStudy. This property cannot be set.\n"
);
static PyObject *pyDicomStudy_getSeries(pyDicomStudyObject *self, void *closure)
{
NSSet *series = [self->obj series];
if ([series count] == 0) {
Py_INCREF(Py_None);
return Py_None;
}
PyObject *oArr = PyTuple_New([series count]);
int idx = 0;
for (DicomSeries *serie in series) {
PyTuple_SetItem(oArr, idx, [pyDicomSeries pythonObjectWithInstance:serie]);
idx++;
}
return oArr;
}
PyDoc_STRVAR(DicomStudyNameAttr_doc,
"The patient name for DicomStudy as a string. This property cannot be set.\n"
);
static PyObject *pyDicomStudy_getName(pyDicomStudyObject *self, void *closure)
{
NSString *name = [self->obj name];
if (name == nil) {
Py_INCREF(Py_None);
return Py_None;
}
return PyString_FromString([name UTF8String]);
}
PyDoc_STRVAR(DicomStudyDateAttr_doc,
"The date of the DicomStudy as a datetime object. This property cannot be set.\n"
);
static PyObject *pyDicomStudy_getDate(pyDicomStudyObject *self, void *closure)
{
NSDate *date = [self->obj date];
if (date == nil) {
Py_INCREF(Py_None);
return Py_None;
}
NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSCalendarUnitNanosecond | NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
PyObject * oDate = PyDateTime_FromDateAndTime((int)[comps year], (int)[comps month], (int)[comps day], (int)[comps hour], (int)[comps minute], (int)[comps second], (int)([comps nanosecond]*1000));
return oDate;
}
PyDoc_STRVAR(DicomStudyDateAddedAttr_doc,
"The date the DicomStudy was added to OsiriX as a datetime object. This property cannot be set.\n"
);
static PyObject *pyDicomStudy_getDateAdded(pyDicomStudyObject *self, void *closure)
{
NSDate *date = [self->obj dateAdded];
if (date == nil) {
Py_INCREF(Py_None);
return Py_None;
}
NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSCalendarUnitNanosecond | NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
PyObject * oDate = PyDateTime_FromDateAndTime((int)[comps year], (int)[comps month], (int)[comps day], (int)[comps hour], (int)[comps minute], (int)[comps second], (int)([comps nanosecond]*1000));
return oDate;
}
PyDoc_STRVAR(DicomStudyDateOfBirthAttr_doc,
"The patient DOB for the DicomStudy as a datetime object. This property cannot be set.\n"
);
static PyObject *pyDicomStudy_getDateOfBirth(pyDicomStudyObject *self, void *closure)
{
NSDate *date = [self->obj dateOfBirth];
if (date == nil) {
Py_INCREF(Py_None);
return Py_None;
}
NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSCalendarUnitNanosecond | NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
PyObject * oDate = PyDateTime_FromDateAndTime((int)[comps year], (int)[comps month], (int)[comps day], (int)[comps hour], (int)[comps minute], (int)[comps second], (int)([comps nanosecond]*1000));
return oDate;
}
PyDoc_STRVAR(DicomStudyInstitutionNameAttr_doc,
"The institution name of the DicomStudy as a string. This property cannot be set.\n"
);
static PyObject *pyDicomStudy_getInstitutionName(pyDicomStudyObject *self, void *closure)
{
NSString *inst = [self->obj institutionName];
if (inst == nil) {
Py_INCREF(Py_None);
return Py_None;
}
return PyString_FromString([inst UTF8String]);
}
PyDoc_STRVAR(DicomStudyModalityAttr_doc,
"The modality of the DicomStudy as a string. This property cannot be set.\n"
);
static PyObject *pyDicomStudy_getModality(pyDicomStudyObject *self, void *closure)
{
NSString *mod = [self->obj modality];
if (mod == nil) {
Py_INCREF(Py_None);
return Py_None;
}
return PyString_FromString([mod UTF8String]);
}
PyDoc_STRVAR(DicomStudyPatientIDAttr_doc,
"The patient ID of the DicomStudy as a string. This property cannot be set.\n"
);
static PyObject *pyDicomStudy_getPatientID(pyDicomStudyObject *self, void *closure)
{
NSString *ID = [self->obj patientID];
if (ID == nil) {
Py_INCREF(Py_None);
return Py_None;
}
return PyString_FromString([ID UTF8String]);
}
PyDoc_STRVAR(DicomStudyPatientUIDAttr_doc,
"The patient UID of the DicomStudy as a string. This property cannot be set.\n"
);
static PyObject *pyDicomStudy_getPatientUID(pyDicomStudyObject *self, void *closure)
{
NSString *UID = [self->obj patientUID];
if (UID == nil) {
Py_INCREF(Py_None);
return Py_None;
}
return PyString_FromString([UID UTF8String]);
}
PyDoc_STRVAR(DicomStudyPatientSexAttr_doc,
"The patient sex of the DicomStudy as a string. This property cannot be set.\n"
);
static PyObject *pyDicomStudy_getPatientSex(pyDicomStudyObject *self, void *closure)
{
NSString *sex = [self->obj patientSex];
if (sex == nil) {
Py_INCREF(Py_None);
return Py_None;
}
return PyString_FromString([sex UTF8String]);
}
PyDoc_STRVAR(DicomStudyPerformingPhysicianAttr_doc,
"The name of the performing physician for the DicomStudy as a string. This property cannot be set.\n"
);
static PyObject *pyDicomStudy_getPerformingPhysician(pyDicomStudyObject *self, void *closure)
{
NSString *pp = [self->obj performingPhysician];
if (pp == nil) {
Py_INCREF(Py_None);
return Py_None;
}
return PyString_FromString([pp UTF8String]);
}
PyDoc_STRVAR(DicomStudyReferringPhysicianAttr_doc,
"The name of the referring physician for the DicomStudy as a string. This property cannot be set.\n"
);
static PyObject *pyDicomStudy_getReferringPhysician(pyDicomStudyObject *self, void *closure)
{
NSString *rp = [self->obj referringPhysician];
if (rp == nil) {
Py_INCREF(Py_None);
return Py_None;
}
return PyString_FromString([rp UTF8String]);
}
PyDoc_STRVAR(DicomStudyUIDAttr_doc,
"The DicomStudy UID as a string. This property cannot be set.\n"
);
static PyObject *pyDicomStudy_getStudyInstanceUID(pyDicomStudyObject *self, void *closure)
{
NSString *UID = [self->obj studyInstanceUID];
if (UID == nil) {
Py_INCREF(Py_None);
return Py_None;
}
return PyString_FromString([UID UTF8String]);
}
PyDoc_STRVAR(DicomStudyStudyNameAttr_doc,
"The DicomStudy name as a string. This property cannot be set.\n"
);
static PyObject *pyDicomStudy_getStudyName(pyDicomStudyObject *self, void *closure)
{
NSString *name = [self->obj studyName];
if (name == nil) {
Py_INCREF(Py_None);
return Py_None;
}
return PyString_FromString([name UTF8String]);
}
static PyGetSetDef pyDicomStudy_getsetters[] =
{
{"numberOfImages", (getter)pyDicomStudy_getNumberOfImages, NULL, DicomStudyNumberOfImagesAttr_doc, NULL},
{"series", (getter)pyDicomStudy_getSeries, NULL, DicomStudySeriesAttr_doc, NULL},
{"name", (getter)pyDicomStudy_getName, NULL, DicomStudyNameAttr_doc, NULL},
{"date", (getter)pyDicomStudy_getDate, NULL, DicomStudyDateAttr_doc, NULL},
{"dateAdded", (getter)pyDicomStudy_getDateAdded, NULL, DicomStudyDateAddedAttr_doc, NULL},
{"dateOfBirth", (getter)pyDicomStudy_getDateOfBirth, NULL, DicomStudyDateOfBirthAttr_doc, NULL},
{"institutionName", (getter)pyDicomStudy_getInstitutionName, NULL, DicomStudyInstitutionNameAttr_doc, NULL},
{"modality", (getter)pyDicomStudy_getModality, NULL, DicomStudyModalityAttr_doc, NULL},
{"patientID", (getter)pyDicomStudy_getPatientID, NULL, DicomStudyPatientIDAttr_doc, NULL},
{"patientUID", (getter)pyDicomStudy_getPatientUID, NULL, DicomStudyPatientUIDAttr_doc, NULL},
{"patientSex", (getter)pyDicomStudy_getPatientSex, NULL, DicomStudyPatientSexAttr_doc, NULL},
{"performingPhysician", (getter)pyDicomStudy_getPerformingPhysician, NULL, DicomStudyPerformingPhysicianAttr_doc, NULL},
{"referringPhysician", (getter)pyDicomStudy_getReferringPhysician, NULL, DicomStudyReferringPhysicianAttr_doc, NULL},
{"studyInstanceUID", (getter)pyDicomStudy_getStudyInstanceUID, NULL, DicomStudyUIDAttr_doc, NULL},
{"studyName", (getter)pyDicomStudy_getStudyName, NULL, DicomStudyStudyNameAttr_doc, NULL},
{NULL}
};
# pragma mark -
# pragma mark pyDicomStudyObject methods
PyDoc_STRVAR(DicomStudyPaths_doc,
"\n"
"Returns a tuple containing the paths of all associated dicom files for the study.\n"
"\n"
"Args:\n"
" None.\n"
"\n"
"Returns:\n"
" tuple: The filepaths to all associated dicom SOP instances.\n"
"\n"
);
static PyObject *pyDicomStudy_paths(pyDicomStudyObject *self)
{
NSSet *paths = [self->obj paths];
if ([paths count] == 0) {
Py_INCREF(Py_None);
return Py_None;
}
PyObject *tup = PyTuple_New([paths count]);
int idx = 0;
for (NSString *path in paths) {
PyObject *oPath = PyString_FromString([path UTF8String]);
PyTuple_SetItem(tup, idx, oPath);
idx++;
}
return tup;
}
PyDoc_STRVAR(DicomStudyImages_doc,
"\n"
"Returns a tuple containing the DicomImages contained within the study.\n"
"\n"
"Args:\n"
" None.\n"
"\n"
"Returns:\n"
" tuple: The DicomImage instances contained within the study.\n"
"\n"
);
static PyObject *pyDicomStudy_images(pyDicomStudyObject *self)
{
NSSet *images = [self->obj images];
if ([images count] == 0) {
Py_INCREF(Py_None);
return Py_None;
}
PyObject *tup = PyTuple_New([images count]);
int idx = 0;
for (id im in images) {
if ([im isKindOfClass:[DicomImage class]]) {
PyObject *o = [pyDicomImage pythonObjectWithInstance:(DicomImage *)im];
PyTuple_SetItem(tup, idx, o);
}
else
{
NSString *str = [NSString stringWithFormat:@"Invalid selection %@ encountered", [im className]];
PyErr_SetString(PyExc_TypeError, [str UTF8String]);
Py_DECREF(tup);
return NULL;
}
idx++;
}
return tup;
}
PyDoc_STRVAR(DicomStudyModalities_doc,
"\n"
"Returns a string with all modelities in the study.\n"
"\n"
"Args:\n"
" None.\n"
"\n"
"Returns:\n"
" str: See above.\n"
"\n"
);
static PyObject *pyDicomStudy_modalities(pyDicomStudyObject *self)
{
NSString *str = [self->obj modalities];
if (str == nil) {
Py_INCREF(Py_None);
return Py_None;
}
return PyString_FromString([str UTF8String]);
}
PyDoc_STRVAR(DicomStudyImageSeries_doc,
"\n"
"Returns a tuple containing the DicomSeries contained within the study.\n"
"\n"
"Args:\n"
" None.\n"
"\n"
"Returns:\n"
" tuple: The DicomSeries instances contained within the study.\n"
"\n"
);
static PyObject *pyDicomStudy_imageSeries(pyDicomStudyObject *self)
{
NSArray *series = [self->obj imageSeries];
if ([series count] == 0) {
Py_INCREF(Py_None);
return Py_None;
}
PyObject *oArr = PyTuple_New([series count]);
int idx = 0;
for (DicomSeries *serie in series) {
PyTuple_SetItem(oArr, idx, [pyDicomSeries pythonObjectWithInstance:serie]);
idx++;
}
return oArr;
}
PyDoc_STRVAR(DicomStudyNoFiles_doc,
"\n"
"Returns the number of dicom files associated with the study.\n"
"\n"
"Args:\n"
" None.\n"
"\n"
"Returns:\n"
" int: See above.\n"
"\n"
);
static PyObject *pyDicomStudy_noFiles(pyDicomStudyObject *self)
{
NSNumber *no = [self->obj noFiles];
if (no == nil) {
Py_INCREF(Py_None);
return Py_None;
}
return PyInt_FromLong([no longValue]);
}
PyDoc_STRVAR(DicomStudyRawNoFiles_doc,
"\n"
"Returns the raw number of dicom files associated with the study.\n"
"\n"
"Args:\n"
" None.\n"
"\n"
"Returns:\n"
" int: See above.\n"
"\n"
);
static PyObject *pyDicomStudy_rawNoFiles(pyDicomStudyObject *self)
{
NSNumber *no = [self->obj rawNoFiles];
if (no == nil) {
Py_INCREF(Py_None);
return Py_None;
}
return PyInt_FromLong([no longValue]);
}
PyDoc_STRVAR(DicomStudyNoFilesExcludingMultiframe_doc,
"\n"
"Returns the number of dicom files associated with the study, excluding any multiframe instances.\n"
"\n"
"Args:\n"
" None.\n"
"\n"
"Returns:\n"
" int: See above.\n"
"\n"
);
static PyObject *pyDicomStudy_noFilesExcludingMultiFrames(pyDicomStudyObject *self)
{
NSNumber *no = [self->obj noFilesExcludingMultiFrames];
if (no == nil) {
Py_INCREF(Py_None);
return Py_None;
}
return PyInt_FromLong([no longValue]);
}
static PyMethodDef pyDicomStudyMethods[] =
{
{"paths", (PyCFunction)pyDicomStudy_paths, METH_NOARGS, DicomStudyPaths_doc},
{"images", (PyCFunction)pyDicomStudy_images, METH_NOARGS, DicomStudyImages_doc},
{"modalities", (PyCFunction)pyDicomStudy_modalities, METH_NOARGS, DicomStudyModalities_doc},
{"imageSeries", (PyCFunction)pyDicomStudy_imageSeries, METH_NOARGS, DicomStudyImageSeries_doc},
{"noFiles", (PyCFunction)pyDicomStudy_noFiles, METH_NOARGS, DicomStudyNoFiles_doc},
{"rawNoFiles", (PyCFunction)pyDicomStudy_rawNoFiles, METH_NOARGS, DicomStudyRawNoFiles_doc},
{"noFilesExcludingMultiFrames", (PyCFunction)pyDicomStudy_noFilesExcludingMultiFrames, METH_NOARGS, DicomStudyNoFilesExcludingMultiframe_doc},
{NULL}
};
# pragma mark -
# pragma mark pyDicomStudyType definition
PyDoc_STRVAR(DicomStudy_doc,
"A python implementation of the OsiriX 'DicomStudy' class.\n"
"DicomStudy is a convienience class used by the main browser within OsiriX to organise and group dicom instances within the same study.\n"
"Instances of this class may not be created. Instead instances are accessed\n"
"via functions defined in the BrowserController class\n"
);
PyTypeObject pyDicomStudyType =
{
PyObject_HEAD_INIT(NULL)
0,
"osirix.DicomStudy",
sizeof(pyDicomStudyObject),
0,
(destructor)pyDicomStudy_dealloc,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
(reprfunc)DicomStudy_str,
0,
0,
0,
Py_TPFLAGS_DEFAULT,
DicomStudy_doc,
0,
0,
0,
0,
0,
0,
pyDicomStudyMethods,
0,
pyDicomStudy_getsetters,
0,
0,
0,
0,
0,
0,
0,
0,
};
# pragma mark -
# pragma mark pyDicomStudy implementation
@implementation pyDicomStudy
+ (void)initTypeInModule:(PyObject *)module
{
if (PyType_Ready(&pyDicomStudyType) < 0) {
return;
}
PyDateTime_IMPORT;
Py_INCREF(&pyDicomStudyType);
PyModule_AddObject(module, "DicomStudy", (PyObject*)&pyDicomStudyType);
}
+ (PyObject *)pythonObjectWithInstance:(id)obj
{
if ([obj class] != [DicomStudy class]) {
return NULL;
}
pyDicomStudyObject *o = PyObject_New(pyDicomStudyObject, &pyDicomStudyType);
o->obj = [obj retain];
return (PyObject *)o;
}
@end