@@ -190,7 +190,68 @@ def dicom_to_nifti_multiframe(ds, nii_path):
190190 return nii_path
191191
192192
193- def _convert_to_nifti (dicom_out_path , nii_path ):
193+ def _export_pdf_from_dicom (dcm_path , out_pdf ):
194+ assert len (dcm_path ) == 1 , dcm_path
195+ ds = dcm_path [0 ]
196+
197+ # verify modality / SOP class
198+ if ds .Modality .upper () != "PDF" :
199+ raise ValueError ("Not a PDF DICOM" )
200+
201+ if "EncapsulatedDocument" not in ds :
202+ raise ValueError ("No embedded PDF found" )
203+
204+ pdf_bytes = ds .EncapsulatedDocument
205+
206+ out_pdf = Path (out_pdf )
207+ out_pdf .write_bytes (pdf_bytes )
208+
209+
210+ def _collect_text (ds , txt_lines : list [str ] | None = None ):
211+ if txt_lines is None :
212+ txt_lines = []
213+
214+ def _help_collect_text (content_sequence , level : int = 0 ):
215+ for item in content_sequence :
216+ prefix = " " * level
217+
218+ concept = ""
219+
220+ if hasattr (item , "ConceptNameCodeSequence" ):
221+ try :
222+ concept = item .ConceptNameCodeSequence [0 ].CodeMeaning
223+ except Exception :
224+ pass
225+
226+ value = None
227+
228+ for attr in ["TextValue" , "CodeMeaning" , "NumericValue" ]:
229+ if hasattr (item , attr ):
230+ value = getattr (item , attr )
231+ break
232+
233+ if concept or value is not None :
234+ txt_lines .append (f"{ prefix } { concept } : { value } " )
235+
236+ if hasattr (item , "ContentSequence" ):
237+ _help_collect_text (
238+ item .ContentSequence ,
239+ level + 1 ,
240+ )
241+
242+ if hasattr (ds , "ContentSequence" ):
243+ _help_collect_text (ds .ContentSequence )
244+ return txt_lines
245+
246+
247+ def _extract_txt_from_dicom (dcm_path , out_txt ):
248+ lines = []
249+ for p in dcm_path :
250+ lines = _collect_text (p , lines )
251+ Path (out_txt ).write_text ("\n " .join (lines ))
252+
253+
254+ def _extract_nii_from_dicom (dicom_out_path , nii_path ):
194255 """
195256 Convert DICOM files to NIfTI format and handle common conversion errors.
196257
@@ -206,6 +267,7 @@ def _convert_to_nifti(dicom_out_path, nii_path):
206267 FunctionTimedOut: Raised if the DICOM-to-NIfTI conversion times out.
207268 ValueError: Raised for generic validation failures.
208269 """
270+
209271 try :
210272 if isinstance (dicom_out_path , list ):
211273 try :
@@ -217,6 +279,7 @@ def _convert_to_nifti(dicom_out_path, nii_path):
217279 return True
218280 except Exception as e :
219281 logger .on_debug ("Multi-Frame DICOM did not work:" , e )
282+ ## The PDF dicom lands here
220283 convert_dicom .dicom_array_to_nifti (dicom_out_path , nii_path , True )
221284 else :
222285 # func_timeout(10, dicom2nifti.dicom_series_to_nifti, (dicom_out_path, nii_path, True))
@@ -246,6 +309,9 @@ def _convert_to_nifti(dicom_out_path, nii_path):
246309 logger .print_error ()
247310
248311 return False
312+ except Exception :
313+ print (nii_path )
314+
249315 return True
250316
251317
@@ -264,7 +330,7 @@ def _get_paths(
264330):
265331 if keys is None :
266332 keys = {}
267- (mri_format , keys ) = extract_keys_from_json (
333+ (mri_format , keys , ending ) = extract_keys_from_json (
268334 simp_json ,
269335 dcm_data_l ,
270336 use_session ,
@@ -277,7 +343,7 @@ def _get_paths(
277343 json_file_name , json_bids_name = _generate_bids_path (
278344 dataset_nifti_dir , keys , mri_format , simp_json , make_subject_chunks = make_subject_chunks , parent = parent
279345 )
280- nii_path = str (json_file_name ).replace (".json" , "" ) + ".nii.gz"
346+ nii_path = str (json_file_name ).replace (".json" , "" ) + ending
281347 return json_file_name , json_bids_name , nii_path
282348
283349
@@ -364,11 +430,17 @@ def _from_dicom_to_nii(
364430 if exist and Path (nii_path ).exists ():
365431 logger .print ("already exists:" , json_file_name , ltype = Log_Type .STRANGE , verbose = verbose )
366432 return nii_path
367- suc = _convert_to_nifti (dcm_data_l , nii_path )
433+ add_grid = False
434+ if nii_path .endswith (".pdf" ):
435+ _export_pdf_from_dicom (dcm_data_l , nii_path )
436+ elif nii_path .endswith (".txt" ):
437+ _extract_txt_from_dicom (dcm_data_l , nii_path )
438+ else :
439+ add_grid = _extract_nii_from_dicom (dcm_data_l , nii_path )
368440
369- if suc :
441+ if add_grid :
370442 _add_grid_info_to_json (nii_path , json_file_name )
371- return nii_path if suc else None
443+ return nii_path if add_grid else None
372444
373445
374446def _add_grid_info_to_json (nii_path : Path | str , simp_json : Path | str , force_update = False , add = True ):
@@ -390,7 +462,7 @@ def _add_grid_info_to_json(nii_path: Path | str, simp_json: Path | str, force_up
390462 return json_dict
391463
392464
393- def _find_all_files (dcm_dirs : Path | list [Path ]):
465+ def _find_all_files (dcm_dirs : Path | list [Path ], verbose = False ):
394466 """
395467 Recursively find all DICOM directories or files in the given paths.
396468
@@ -400,6 +472,9 @@ def _find_all_files(dcm_dirs: Path | list[Path]):
400472 Yields:
401473 Path: Paths to directories or individual DICOM files found during the search.
402474 """
475+ if verbose :
476+ logger .on_neutral ("Start file searching" )
477+ i = 0
403478 yield dcm_dirs
404479 dcm_dirs = dcm_dirs if isinstance (dcm_dirs , list ) else [dcm_dirs ]
405480 for dcm_dir in dcm_dirs :
@@ -408,9 +483,15 @@ def _find_all_files(dcm_dirs: Path | list[Path]):
408483 file = ""
409484 for file in files :
410485 if Path (file ).is_file (): # str(file).endswith(".dcm") or str(file).endswith(".ima")
486+ if verbose :
487+ logger .on_neutral ("File " ,i ,end = "\r " )
488+ i += 1
411489 yield Path (root , file ).absolute ().parent
412490 break
413491 else :
492+ if verbose :
493+ logger .on_neutral ("File " ,i ,end = "\r " )
494+ i += 1
414495 yield Path (root , file )
415496 # if "." not in str(file):
416497 # yield Path(root, file).absolute().parent
@@ -606,7 +687,7 @@ def extract_dicom_folder(
606687 convert_dicom .settings .disable_validate_slice_increment ()
607688 outs = {}
608689
609- for p in _find_all_files (dicom_folder ):
690+ for p in _find_all_files (dicom_folder , verbose = verbose ):
610691 dicom_path = p
611692
612693 if str (dicom_path ).endswith (".pkl" ):
@@ -672,6 +753,7 @@ def process_series(key, files, parts):
672753 p , Path ("/media/data/robert/datasets" , "dataset-Durchleuchtung222" ), False , False , validate_slice_increment = False
673754 )
674755
756+
675757 sys .exit ()
676758 # s = "/home/robert/Downloads/bein/dataset-oberschenkel/rawdata/sub-1-3-46-670589-11-2889201787-2305829596-303261238-2367429497/mr/sub-1-3-46-670589-11-2889201787-2305829596-303261238-2367429497_sequ-406_mr.nii.gz"
677759 # nii2 = NII.load(s, False)
0 commit comments