Conversion not working properly using dicom2nifti - python

I have a series of dicom files (.dcm) for each patient that I want to convert to nii.gz files, but nothing happens (even error info) with the following conversion...
import dicom2nifti
dicom2nifti.settings.set_gdcmconv_path('C:/Program Files/GDCM 3.0/bin/gdcmconv.exe')
dicom2nifti.convert_directory('M0_1', 'M0_1.nii.gz')
The error occurs when trying other methods....
import dicom2nifti
dicom2nifti.settings.set_gdcmconv_path('C:/Program Files/GDCM 3.0/bin/gdcmconv.exe')
dicom2nifti.dicom_series_to_nifti('M0_1', 'M0_1.nii.gz')
Error Log:
Traceback (most recent call last):
File ".\test.py", line 5, in <module>
dicom2nifti.dicom_series_to_nifti('M0_1', 'M0_1.nii.gz')
File "D:\Anaconda3\envs\pydicom\lib\site-packages\dicom2nifti\convert_dicom.py", line 78, in dicom_series_to_nifti
return dicom_array_to_nifti(dicom_input, output_file, reorient_nifti)
File "D:\Anaconda3\envs\pydicom\lib\site-packages\dicom2nifti\convert_dicom.py", line 112, in dicom_array_to_nifti
if not are_imaging_dicoms(dicom_list):
File "D:\Anaconda3\envs\pydicom\lib\site-packages\dicom2nifti\convert_dicom.py", line 151, in are_imaging_dicoms
if common.is_philips(dicom_input):
File "D:\Anaconda3\envs\pydicom\lib\site-packages\dicom2nifti\common.py", line 102, in is_philips
header = dicom_input[0]
IndexError: list index out of range
File structure:
M0_1
|----IM-0001-0001.dcm
|----IM-0001-0002.dcm
........
|----IM-0001-000299.dcm
|----IM-0001-000300.dcm
Environment:
windows10, python: 3.6, dicom2nifti2.3.0
Where am I doing wrong?
Or is there any other way to convert it?
Thanks in advance!

I haven't used dicom2nifti, but you can convert DICOM to Nifti using SimpleITK.
Here's the code that ought to do the job:
import SimpleITK as sitk
reader = sitk.ImageSeriesReader()
dicom_names = reader.GetGDCMSeriesFileNames('M0_1')
reader.SetFileNames(dicom_names)
image = reader.Execute()
# Added a call to PermuteAxes to change the axes of the data
image = sitk.PermuteAxes(image, [2, 1, 0])
sitk.WriteImage(image, 'M0_1.nii.gz')
You can learn more about SimpleITK here: https://simpleitk.readthedocs.io/
UPDATE: I added a call to SimpleITK's PermuteAxes to change the axis order from X-Y-Z to Z-Y-X.

Related

How to Display .raw Dataset?

I'm trying to write a script to display the images in the file burned_wood_with_tape_1664x512x256_12bit.raw from this website: https://figshare.com/articles/SSOCT_test_dataset_for_OCTproZ/12356705
for a research project. However, I can't find a way to display the images in this .raw dataset.
This is the software I have, using other questions on StackOverflow:
import rawpy
import imageio
path = "Datasets/burned_wood_with_tape_1664x512x256_12bit.raw"
for item in path:
item_path = path + item
raw = rawpy.imread(item_path)
rgb = raw.postprocess()
rawpy.imshow(rgb)
But I get this error:
Traceback (most recent call last):
File "[ENTER PATH]", line 7, in <module>
raw = rawpy.imread(item_path)
File "[ENTER PATH]\lib\site-packages\rawpy\__init__.py", line 20, in imread
d.open_file(pathOrFile)
File "rawpy\_rawpy.pyx", line 404, in rawpy._rawpy.RawPy.open_file
File "rawpy\_rawpy.pyx", line 914, in rawpy._rawpy.RawPy.handle_error
rawpy._rawpy.LibRawIOError: b'Input/output error'
The data that you have is not a ".raw" file. It is a dataset that can be used with the "Virtual OCT System" of OCTproZ (https://github.com/spectralcode/OCTproZ/). The "rawpy" library is not useful in this case. That library works for ".raw" photos.

Tuple index out of range : Audio to spectrogram

I am trying to run an audio to spectrogram script, namely:
https://github.com/grrrr/nsgt/tree/master/examples
When I try python spectrogram.py myaudio.wav
I get the error:
Traceback (most recent call last):
File "spectrogram.py", line 111, in <module>
coefs = assemble_coeffs(c, ncoefs)
File "spectrogram.py", line 27, in assemble_coeffs
out = np.empty((ncoefs,cq0.shape[1],cq0.shape[2]), dtype=cq0.dtype)
IndexError: tuple index out of range
Am I doing something wrong?
Please advise! :)
you can run it with just the argument --matrixform added.
I suggest you to see this issue! from the github project it can help you

How to append keywords to IPTC data in a JPG image?

I'm trying to add keywords to the IPTC data in a JPG file and failing miserably. I'm able to read in the keywords using the iptcinfo3 library and, seemingly, append the keyword to the list of current keywords but I'm failing when trying to write those keywords back to the JPG file, if not sooner. The error message is a bit unclear to me and may actually reference the appending of the new keyword (although a print statement seems to indicate it took).
I've tried three different metadata libraries (there doesn't seem to be one standard) and this is the furthest I've gotten with any of them (failing to even install one and not being able to get a second one to run). This seems so basic but I can't figure it out and haven't been able to adapt the few other code examples I've seen online to work, including iptcinfo3's example code fragment.
The current Error message is:
| => pipenv run python editMetadata.py
WARNING: problems with charset recognition (b'\x1b')
[b'Gus']
[b'Gus', b'frog']
Traceback (most recent call last):
File "editMetadata.py", line 22, in <module>
info.save_as('Gus2.jpg')
File "/Users/Scott/.local/share/virtualenvs/editPhotoMetadata-tx0JAOmI/lib/python3.7/site-packages/iptcinfo3.py", line 635, in save_as
jpeg_parts = jpeg_collect_file_parts(fh)
File "/Users/Scott/.local/share/virtualenvs/editPhotoMetadata-tx0JAOmI/lib/python3.7/site-packages/iptcinfo3.py", line 324, in jpeg_collect_file_parts
adobeParts = collect_adobe_parts(partdata)
File "/Users/Scott/.local/share/virtualenvs/editPhotoMetadata-tx0JAOmI/lib/python3.7/site-packages/iptcinfo3.py", line 433, in collect_adobe_parts
out = [''.join(out)]
TypeError: sequence item 0: expected str instance, bytes found
Code:
from iptcinfo3 import IPTCInfo
import os
# Create new info object
info = IPTCInfo('Gus.jpg')
# Print list of keywords
print(info['keywords'])
# Append the keyword I want to add
info['keywords'].append(b'frog')
# Print to test keyword has been added
print(info['keywords'])
# Save new info to file
info.save()
info.save_as('Gus2.jpg')
Instead of appending use equal "="
from iptcinfo3 import IPTCInfo
info = IPTCInfo('Gus.jpg')
print(info['keywords'])
# add keyword
info['keywords'] = ['new keyword']
info.save()
info.save_as('Gus_2.jpg')
I have the same error. It seems to be an issue with the save depending on the file.
from iptcinfo3 import IPTCInfo
info = IPTCInfo('image.jpg', force=True)
info.save()
Which gives me the same error.
WARNING: problems with charset recognition (b'\x1b')
WARNING: problems with charset recognition (b'\x1b')
Traceback (most recent call last):
File "./searchimages.py", line 123, in <module>
main(sys.argv[1:])
File "./searchimages.py", line 119, in main
find_photos(str(sys.argv[1]))
File "./searchimages.py", line 46, in find_photos
write_keywords(image, current_keywords, new_keywords)
File "./searchimages.py", line 109, in write_keywords
info.save_as('out.jpg')
File "/usr/local/lib/python3.7/site-packages/iptcinfo3.py", line 635, in save_as
jpeg_parts = jpeg_collect_file_parts(fh)
File "/usr/local/lib/python3.7/site-packages/iptcinfo3.py", line 324, in jpeg_collect_file_parts
adobeParts = collect_adobe_parts(partdata)
File "/usr/local/lib/python3.7/site-packages/iptcinfo3.py", line 433, in collect_adobe_parts
out = [''.join(out)]
TypeError: sequence item 0: expected str instance, bytes found

Reading TDMS File with python nptdms, cannot open tdms file

I am having issues with getting basic function of the nptdms module working.
First, I am just trying to open a TDMS file and print the contents of specific channels within specific groups.
Using python 2.7 and the nptdms quick start here
Following this, I will be writing these specific pieces of data into a new TDMS file. Then, my ultimate goal is to be able to take a set of source files, open each, and write (append) to a new file. The source data files contain far more information that is needed, so I am breaking out the specifics into their own file.
The problem I have is that I cannot get past a basic error.
When running this code, I get:
Traceback (most recent call last):
File "PullTDMSdataIntoNewFile.py", line 27, in <module>
tdms_file = TdmsFile(r"C:\\Users\daniel.worts\Desktop\this_is_my_tdms_file.tdms","r")
File "C:\Anaconda2\lib\site-packages\nptdms\tdms.py", line 94, in __init__
self._read_segments(f)
File "C:\Anaconda2\lib\site-packages\nptdms\tdms.py", line 119, in _read_segments
object._initialise_data(memmap_dir=self.memmap_dir)
File "C:\Anaconda2\lib\site-packages\nptdms\tdms.py", line 709, in _initialise_data
mode='w+b', prefix="nptdms_", dir=memmap_dir)
File "C:\Anaconda2\lib\tempfile.py", line 475, in NamedTemporaryFile
(fd, name) = _mkstemp_inner(dir, prefix, suffix, flags)
File "C:\Anaconda2\lib\tempfile.py", line 244, in _mkstemp_inner
fd = _os.open(file, flags, 0600)
OSError: [Errno 2] No such file or directory: 'r\\nptdms_yjfyam'
Here is my code:
from nptdms import TdmsFile
import numpy as np
import pandas as pd
#set Tdms file path
tdms_file = TdmsFile(r"C:\\Users\daniel.worts\Desktop\this_is_my_tdms_file.tdms","r")
# set variable for TDMS groups
group_nameone = '101'
group_nametwo = '752'
# set objects for TDMS channels
channel_dataone = tdms_file.object(group_nameone 'Payload_1')
channel_datatwo = tdms_file.object(group_nametwo, 'Payload_2')
# set data from channels
data_dataone = channel_dataone.data
data_datatwo = channel_datatwo.data
print data_dataone
print data_datatwo
Big thanks to anyone who may have encountered this before and can help point to what I am missing.
Best,
- Dan
edit:
Solved the read data issue by removing the 'r' argument from the file path.
Now I am having another error I can't trace when trying to write.
from nptdms import TdmsFile, TdmsWriter, RootObject, GroupObject, ChannelObject
import numpy as np
import pandas as pd
newfilepath = r"C:\\Users\daniel.worts\Desktop\Mined.tdms"
datetimegroup101_channel_object = ChannelObject('101', DateTime, data_datetimegroup101)
with TdmsWriter(newfilepath) as tdms_writer:
tdms_writer.write_segment([datetimegroup101_channel_object])
Returns error:
Traceback (most recent call last):
File "PullTDMSdataIntoNewFile.py", line 82, in <module>
tdms_writer.write_segment([datetimegroup101_channel_object])
File "C:\Anaconda2\lib\site-packages\nptdms\writer.py", line 68, in write_segment
segment = TdmsSegment(objects)
File "C:\Anaconda2\lib\site-packages\nptdms\writer.py", line 88, in __init__
paths = set(obj.path for obj in objects)
File "C:\Anaconda2\lib\site-packages\nptdms\writer.py", line 88, in <genexpr>
paths = set(obj.path for obj in objects)
File "C:\Anaconda2\lib\site-packages\nptdms\writer.py", line 254, in path
self.channel.replace("'", "''"))
AttributeError: 'TdmsObject' object has no attribute 'replace'

Cannot determine type of file

Hi i have just started learning image processing using python.
When i tried to open an image that i downloaded from the net, I keep getting this error and I have no idea about how to resolve it. Can anyone please help me with this?
>>> dna=mahotas.imread('dna.jpeg')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\mahotas\io\freeimage.py", line 773, in imread
img = read(filename)
File "C:\Python27\lib\site-packages\mahotas\io\freeimage.py", line 444, in read
bitmap = _read_bitmap(filename, flags)
File "C:\Python27\lib\site-packages\mahotas\io\freeimage.py", line 490, in _read_bitmap
'mahotas.freeimage: cannot determine type of file %s' % filename)
ValueError: mahotas.freeimage: cannot determine type of file dna.jpeg
Hello this looks like a pretty old thread but I found it recently because I had the same problem.
I think that the error message is misleading because it implies that the type of file is incorrect.
I fixed the problem by including the full path to the image file. For example, it could look something like:
dna = mahotas.imread('C:\Documents\dna.jpeg')

Categories