Nufa Nufa - 1 year ago 42
Python Question

Giving name to element in the list

I want to give names to call and concatenate the process data later.

from glob import glob
import nibabel as nib
import numpy as np

emot= glob('path/emotion_***.nii')

em_1 = nib.load(emot[0]).get_data()
em_2 = nib.load(emot[1]).get_data()

em_1 = np.reshape(em_1[:,36], (90,104))
em_2 = np.reshape(em_2[:,36], (90,104))

data_emot = np.concatenate([em_1,em_2])

How could I do it the faster way with more than 100 elements in the glob list?

Additional info, the
are the nibabel process. Thus, if they load together with numpy reshape, str is called, rather the loaded process.

Answer Source

What about simply using a for-loop.

from glob import glob
import nibabel as nib
import numpy as np

emot = glob('path/emotion_***.nii')
to_concat = []

for el_i in emot:
    em_i = np.reshape(nib.load(el_i).get_data()[:,36], (90,104))

So that you can concatenate the process data later.

data_emot = np.concatenate(to_concat)

To go further

Note that the for-loop above can be rewritten more compactly. As follows

to_concat = [np.reshape(nib.load(el_i).get_data()[:,36], (90,104))\
             for el_i in emot]

Or using the python-native map function

to_concat = map(lambda el_i:np.reshape(nib.load(el_i).get_data()[:,36], (90,104)),\
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download