|
17 | 17 | import sqlite3
|
18 | 18 | import dicom
|
19 | 19 |
|
20 |
| -_study_instance_uid_tag = dicom.tag.Tag(0x0020, 0x000d) |
21 |
| -_study_date_tag = dicom.tag.Tag(0x0008, 0x0020) |
22 |
| -_study_time_tag = dicom.tag.Tag(0x0008, 0x0030) |
23 |
| -_study_comments_tag = dicom.tag.Tag(0x0032, 0x4000) |
24 |
| - |
25 |
| -_series_instance_uid_tag = dicom.tag.Tag(0x0020, 0x000e) |
26 |
| -_series_number_tag = dicom.tag.Tag(0x0020, 0x0011) |
27 |
| -_series_description_tag = dicom.tag.Tag(0x0008, 0x103e) |
28 |
| -_rows_tag = dicom.tag.Tag(0x0028, 0x0010) |
29 |
| -_columns_tag = dicom.tag.Tag(0x0028, 0x0011) |
30 |
| -_bits_allocated_tag = dicom.tag.Tag(0x0028, 0x0100) |
31 |
| -_bits_stored_tag = dicom.tag.Tag(0x0028, 0x0101) |
32 |
| - |
33 |
| -_patients_name_tag = dicom.tag.Tag(0x0010, 0x0010) |
34 |
| -_patient_id_tag = dicom.tag.Tag(0x0010, 0x0020) |
35 |
| -_patients_birth_date_tag = dicom.tag.Tag(0x0010, 0x0030) |
36 |
| -_patients_sex_tag = dicom.tag.Tag(0x0010, 0x0040) |
37 |
| - |
38 |
| -_sop_instance_uid_tag = dicom.tag.Tag(0x0008, 0x0018) |
39 |
| -_instance_number_tag = dicom.tag.Tag(0x0020, 0x0013) |
40 |
| - |
41 |
| -_pixel_spacing_tag = dicom.tag.Tag(0x0028, 0x0030) |
42 |
| -_image_position_tag = dicom.tag.Tag(0x0020, 0x0032) |
43 |
| -_image_orientation_tag = dicom.tag.Tag(0x0020, 0x0037) |
44 |
| -_spacing_between_slices_tag = dicom.tag.Tag(0x0018, 0x0088) |
45 |
| - |
46 |
| -_pixel_data_tag = dicom.tag.Tag(0x7fe0, 0x0010) |
47 |
| - |
48 | 20 | class DFTError(Exception):
|
49 | 21 | "base class for DFT exceptions"
|
50 | 22 |
|
@@ -177,21 +149,21 @@ def as_nifti(self):
|
177 | 149 | d1 = self.storage_instances[0].dicom()
|
178 | 150 | dn = self.storage_instances[-1].dicom()
|
179 | 151 |
|
180 |
| - pdi = d1[_pixel_spacing_tag].value[0] |
181 |
| - pdj = d1[_pixel_spacing_tag].value[1] |
182 |
| - pdk = d1[_spacing_between_slices_tag].value |
| 152 | + pdi = d1.PixelSpacing[0] |
| 153 | + pdj = d1.PixelSpacing[0] |
| 154 | + pdk = d1.SpacingBetweenSlices |
183 | 155 |
|
184 |
| - cosi = d1[_image_orientation_tag].value[0:3] |
| 156 | + cosi = d1.ImageOrientationPatient[0:3] |
185 | 157 | cosi[0] = -1 * cosi[0]
|
186 | 158 | cosi[1] = -1 * cosi[1]
|
187 |
| - cosj = d1[_image_orientation_tag].value[3:6] |
| 159 | + cosj = d1.ImageOrientationPatient[3:6] |
188 | 160 | cosj[0] = -1 * cosj[0]
|
189 | 161 | cosj[1] = -1 * cosj[1]
|
190 | 162 |
|
191 |
| - pos_1 = numpy.array(d1[_image_position_tag].value) |
| 163 | + pos_1 = numpy.array(d1.ImagePositionPatient) |
192 | 164 | pos_1[0] = -1 * pos_1[0]
|
193 | 165 | pos_1[1] = -1 * pos_1[1]
|
194 |
| - pos_n = numpy.array(dn[_image_position_tag].value) |
| 166 | + pos_n = numpy.array(dn.ImagePositionPatient) |
195 | 167 | pos_n[0] = -1 * pos_n[0]
|
196 | 168 | pos_n[1] = -1 * pos_n[1]
|
197 | 169 | cosk = pos_n - pos_1
|
@@ -378,90 +350,68 @@ def _update_dir(c, dir, files, studies, series, storage_instances):
|
378 | 350 | c.execute(query, (mtime, si_uid, dir, fname))
|
379 | 351 | return
|
380 | 352 |
|
381 |
| -def _update_file(c, dir, fname, studies, series, storage_instances): |
| 353 | +def _update_file(c, path, fname, studies, series, storage_instances): |
382 | 354 | try:
|
383 |
| - do = dicom.read_file('%s/%s' % (dir, fname)) |
384 |
| -# do = dicom.read_file('%s/%s' % (dir, fname), stop_before_pixels=True) |
385 |
| - study_uid = do[_study_instance_uid_tag].value |
386 |
| - study_date = do[_study_date_tag].value |
387 |
| - study_time = do[_study_time_tag].value |
388 |
| - try: |
389 |
| - study_comments = do[_study_comments_tag].value |
390 |
| - except KeyError: |
391 |
| - study_comments = '' |
392 |
| - patient_name = do[_patients_name_tag].value |
393 |
| - patient_id = do[_patient_id_tag].value |
394 |
| - patient_birth_date = do[_patients_birth_date_tag].value |
395 |
| - patient_sex = do[_patients_sex_tag].value |
396 |
| - series_uid = do[_series_instance_uid_tag].value |
397 |
| - series_number = do[_series_number_tag].value |
398 |
| - series_description = do[_series_description_tag].value |
399 |
| - series_rows = int(do[_rows_tag].value) |
400 |
| - series_columns = int(do[_columns_tag].value) |
401 |
| - series_bits_allocated = int(do[_bits_allocated_tag].value) |
402 |
| - series_bits_stored = int(do[_bits_stored_tag].value) |
403 |
| - instance_number = int(do[_instance_number_tag].value) |
404 |
| - storage_instance_uid = do[_sop_instance_uid_tag].value |
405 |
| -# except Exception, data: |
406 |
| -# print 'exc', type(data), data, str(data) |
407 |
| -# return None |
| 355 | + do = dicom.read_file('%s/%s' % (path, fname)) |
408 | 356 | except dicom.filereader.InvalidDicomError:
|
409 | 357 | print ' not a DICOM file'
|
410 | 358 | return None
|
411 |
| - except KeyError, data: |
412 |
| - print ' missing tag %s' % str(data) |
413 |
| - return None |
414 |
| - except Exception, data: |
415 |
| - print ' error: %s' % str(data) |
| 359 | + try: |
| 360 | + study_comments = do.StudyComments |
| 361 | + except AttributeError: |
| 362 | + study_comments = '' |
| 363 | + try: |
| 364 | + print ' storage instance %s' % str(do.SOPInstanceUID) |
| 365 | + if str(do.StudyInstanceUID) not in studies: |
| 366 | + query = """INSERT INTO study (uid, |
| 367 | + date, |
| 368 | + time, |
| 369 | + comments, |
| 370 | + patient_name, |
| 371 | + patient_id, |
| 372 | + patient_birth_date, |
| 373 | + patient_sex) |
| 374 | + VALUES (?, ?, ?, ?, ?, ?, ?, ?)""" |
| 375 | + params = (str(do.StudyInstanceUID), |
| 376 | + do.StudyDate, |
| 377 | + do.StudyTime, |
| 378 | + study_comments, |
| 379 | + do.PatientsName, |
| 380 | + do.PatientID, |
| 381 | + do.PatientsBirthDate, |
| 382 | + do.PatientsSex) |
| 383 | + c.execute(query, params) |
| 384 | + studies.append(str(do.StudyInstanceUID)) |
| 385 | + if str(do.SeriesInstanceUID) not in series: |
| 386 | + query = """INSERT INTO series (uid, |
| 387 | + study, |
| 388 | + number, |
| 389 | + description, |
| 390 | + rows, |
| 391 | + columns, |
| 392 | + bits_allocated, |
| 393 | + bits_stored) |
| 394 | + VALUES (?, ?, ?, ?, ?, ?, ?, ?)""" |
| 395 | + params = (str(do.SeriesInstanceUID), |
| 396 | + str(do.StudyInstanceUID), |
| 397 | + do.SeriesNumber, |
| 398 | + do.SeriesDescription, |
| 399 | + do.Rows, |
| 400 | + do.Columns, |
| 401 | + do.BitsAllocated, |
| 402 | + do.BitsStored) |
| 403 | + c.execute(query, params) |
| 404 | + series.append(str(do.SeriesInstanceUID)) |
| 405 | + if str(do.SOPInstanceUID) not in storage_instances: |
| 406 | + query = """INSERT INTO storage_instance (uid, instance_number, series) |
| 407 | + VALUES (?, ?, ?)""" |
| 408 | + params = (str(do.SOPInstanceUID), do.InstanceNumber, str(do.SeriesInstanceUID)) |
| 409 | + c.execute(query, params) |
| 410 | + storage_instances.append(str(do.SOPInstanceUID)) |
| 411 | + except AttributeError, data: |
| 412 | + print ' %s' % str(data) |
416 | 413 | return None
|
417 |
| - print ' storage instance %s' % storage_instance_uid |
418 |
| - if study_uid not in studies: |
419 |
| - query = """INSERT INTO study (uid, |
420 |
| - date, |
421 |
| - time, |
422 |
| - comments, |
423 |
| - patient_name, |
424 |
| - patient_id, |
425 |
| - patient_birth_date, |
426 |
| - patient_sex) |
427 |
| - VALUES (?, ?, ?, ?, ?, ?, ?, ?)""" |
428 |
| - params = (study_uid, |
429 |
| - study_date, |
430 |
| - study_time, |
431 |
| - study_comments, |
432 |
| - patient_name, |
433 |
| - patient_id, |
434 |
| - patient_birth_date, |
435 |
| - patient_sex) |
436 |
| - c.execute(query, params) |
437 |
| - studies.append(study_uid) |
438 |
| - if series_uid not in series: |
439 |
| - query = """INSERT INTO series (uid, |
440 |
| - study, |
441 |
| - number, |
442 |
| - description, |
443 |
| - rows, |
444 |
| - columns, |
445 |
| - bits_allocated, |
446 |
| - bits_stored) |
447 |
| - VALUES (?, ?, ?, ?, ?, ?, ?, ?)""" |
448 |
| - params = (series_uid, |
449 |
| - study_uid, |
450 |
| - series_number, |
451 |
| - series_description, |
452 |
| - series_rows, |
453 |
| - series_columns, |
454 |
| - series_bits_allocated, |
455 |
| - series_bits_stored) |
456 |
| - c.execute(query, params) |
457 |
| - series.append(series_uid) |
458 |
| - if storage_instance_uid not in storage_instances: |
459 |
| - query = """INSERT INTO storage_instance (uid, instance_number, series) |
460 |
| - VALUES (?, ?, ?)""" |
461 |
| - params = (storage_instance_uid, instance_number, series_uid) |
462 |
| - c.execute(query, params) |
463 |
| - storage_instances.append(storage_instance_uid) |
464 |
| - return storage_instance_uid |
| 414 | + return str(do.SOPInstanceUID) |
465 | 415 |
|
466 | 416 | def clear_cache():
|
467 | 417 | with _db_change() as c:
|
|
0 commit comments