Problems with converting all slices into nii.gz at once - python

I'm using the following code to convert all my 332 npy slices into the nii.gz format:
import numpy as np
import nibabel as nib
file_dir = "D:/Volumes convertidos LIDC/"
fileNPY1 = "slice200.npy"
img_array1 = np.load(file_dir + fileNPY1)
nifti_file = nib.Nifti1Image(img_array1 , np.eye(4))
nib.save(nifti_file, "D:/slices convertidos/slice200converted.nii.gz")
There are just too many slices for me (and tons of images) to keep doing it that way, is there a way to convert them all at once?

I don’t know nibabel, and I am not sure if I understand correctly what you are trying to do, but perhaps this will be helpful:
import numpy as np
import nibabel as nib
file_dir = "D:/Volumes convertidos LIDC/"
for i in range(332):
fileNPY1 = f"slice{i}.npy"
img_array1 = np.load(file_dir + fileNPY1)
nifti_file = nib.Nifti1Image(img_array1 , np.eye(4))
nib.save(nifti_file, f"D:/slicesconvertidos/slice{i}converted.nii.gz")

Related

Python "make_transforms" shows list index error while aligning fits images

I am trying to align 4 fits (V2.fits,V3.fits,V4.fits and, V5.fits) with respect to a reference fits file (V1.fits). I tried the following by employing the python fits_align package :
from fits_align.ident import make_transforms
from fits_align.align import affineremap
from glob import glob
from astropy.io import fits
from numpy import shape
import os
img_list = sorted(glob(os.path.join("*.fits")))
ref_image = img_list[0]
images_to_align = img_list[1:]
print(img_list) # List of all images
print(ref_image) # Reference image
print(images_to_align) # Images to align
identifications = make_transforms(ref_image, images_to_align)
aligned_images = [ref_image]
for id in identifications:
if id.ok:
alignedimg = affineremap(id.ukn.filepath, id.trans, outdir=tmpdir)
aligned_images.append(alignedimg)
I get the following index error:
What could be the reasons for this error? please suggest possible solutions

Python: how can I read data from files and assign it to an array? error":could not broadcast input array from shape"

I have the following MATLAB code that works fine using text data files, now I am trying to rewrite it using Python but running into errors. I have results that I am trying to apply some calculations on (perform data analysis). My results are in the format of binary files and I have a specific package I am using to help me import the data. For example, here ne is a 1024x256 array with 159 number of files printed per each iteration. So, in MATLAB I can simply do the following:
% Load data:
frame = 6; % how many number of output files
ne_bg = load([DirPath '/ne_unpert.txt']);
ne_p = load([DirPath '/ne_' num2str(frame) '.txt']);
% perform calculations on data:
ne = ne_bg + ne_p;
dn_over_n = ne_p ./ ne;
Since MATLAB deals easily with multi-dimensional arrays and matrices, I am struggling to interpret that to python.
My Python code:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.gridspec as gridspec
import matplotlib.colors as colors
import matplotlib.patches as patches
import scipy.optimize as opt
from scipy.special import erf, comb, gamma, gammainc
import scipy.constants as const
from scipy.integrate import odeint
import sys
from glob import glob
from mpl_toolkits.axes_grid1 import make_axes_locatable
import Package as pg
# Initialize sizes
ne = np.zeros((1024,256))
ne_p = np.zeros((1024,256))
# Data
data = pg.GData('ne_p.bp')
dg = pg.GInterpModal(data, 2, 'ms')
#dg.interpolate(overwrite=True)
ne_p = data.getValues()
data = pg.GData('ne0.gkyl')
dg = pg.GInterpModal(data, 2, 'ms')
#dg.interpolate(overwrite=True)
ne_bg = data.getValues()
for i in range(1,159): # would like to look at files start from 1 to 159 not 0
data = pg.GData('ne{:d}.gkyl'.format(i))
dg = pg.GInterpModal(data, 2, 'ms')
ne[i,:] = data.getValues() # ERROR HERE
dn_over_n = ne_p/ne # get
....
Error message:
ValueError Traceback (most recent call last)
<ipython-input-35-d6134fb807e8> in <module>
48 dg = pg.GInterpModal(data, 2, 'ms')
49 #dg.interpolate(overwrite=True)
---> 50 ne[i,:] = data.getValues()
ValueError: could not broadcast input array from shape (1024,256,1) into shape (256)
Can someone show me how to fix this and explain what it means?

how to load images into a python list and convert to a dataframe object

I am trying to read a bunch of png images in a directory into a python list. After getting the images in the list I would like to resize and do some reshaping. But it always throws an error. Below is the sample of my code.
Well it doesn't really throw an error but it always prints out an empty dataset.
import pandas as pd
from sklearn.manifold import Isomap
from scipy import misc
from os import listdir
import glob
dset = []
for image_path in glob.glob("/Documents/Python/DAT210x-master/Module4 /Datasets/ALOI/32/*.png"):
img = misc.imread(image_path)
img = img[::2, ::2]
X = (img / 255.0).reshape(-1)
dset.append(X)
dset = pd.DataFrame(dset)
print(dset)
If I'm not mistaken, DataFrame objects are typically initialized from dict objects. What happens if you add:
dset = {'Pictures': dset}
dset = pd.DataFrame(dset)

Importing images for manifold Isomap

There are 192 x 144 pixel images. They should be imported to a Python list so that the items in the list are NDArray instances. New dataframe should be created from the list and that dataframe should be given to Isomap. iso.fit(df) fails with the errors
array = array.astype(np.float64)
ValueError: setting an array element with a sequence.
I have spent more than one day trying to figure out how the NDArrays should be processed and the dataframe loaded with them. No luck. Any help would be appreciated.
import pandas as pd
from scipy import misc
import glob
from sklearn import manifold
samples = []
for filename in glob.glob('Datasets/ALOI/32/*.png'):
img = misc.imread(filename, mode='I')
samples.append(img)
df = pd.DataFrame.from_records(samples, coerce_float=True)
iso = manifold.Isomap(n_neighbors=6, n_components=3)
iso.fit(df)
If those are gray scale images from the ALOI, you probably want to treat each pixel's brightness as a feature. Therefore, you should flatten the img array with img.reshape(-1). The revised code follows:
import pandas as pd
from scipy import misc
import glob
from sklearn import manifold
samples = []
for filename in glob.glob('Datasets/ALOI/32/*.png'):
img = misc.imread(filename, mode='I')
# the following line changed
samples.append(img.reshape(-1))
df = pd.DataFrame.from_records(samples, coerce_float=True)
iso = manifold.Isomap(n_neighbors=6, n_components=3)
iso.fit(df)

ndimage script mis-behaving

I have a script that reads in image data, and then iterates over the images with the median filter in scipy.ndimage. From the iteration i create new arrays.
However when i attempt to run the script with
run filtering.py
The filtering does not seem to work. The new arrays (month_f) are the same as the old ones.
import matplotlib.pyplot as plt
import numpy as numpy
from scipy import ndimage
import Image as Image
# Get images
#Load images
jan1999 = Image.open('jan1999.tif')
mar1999 = Image.open('mar1999.tif')
may1999 = Image.open('may1999.tif')
sep1999 = Image.open('sep1999.tif')
dec1999 = Image.open('dec1999.tif')
jan2000 = Image.open('jan2000.tif')
feb2000 = Image.open('feb2000.tif')
#Compute numpy arrays
jan1999 = numpy.array(jan1999)
mar1999 = numpy.array(mar1999)
may1999 = numpy.array(may1999)
sep1999 = numpy.array(sep1999)
dec1999 = numpy.array(dec1999)
jan2000 = numpy.array(jan2000)
feb2000 = numpy.array(feb2000)
########### Put arrays into a list
months = [jan1999, mar1999, may1999, sep1999, dec1999, jan2000, feb2000]
############ Filtering = 3,3
months_f = []
for image in months:
image = scipy.ndimage.median_filter(image, size=(5,5))
months_f.append(image)
Any help would be much appreciated :)
This is rather a comment but due to reputation limits I'm not able to write one.
The way you import your modules is a bit strange. Especially "import .. as" with the idential name. I think a more pythonian way would be
import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage
from PIL import Image
and then call
image = ndimage.median_filter(image, size=(...))
When I run your steps with a RGB test image it seems to work.
What does jan1999.shape return?

Categories

Resources