I have two data sources (Subjects
) which are loaded via np.loadtxt
into Python.
The loop for ROI in ROIs
is nested in the loop for Subject in Subjects
.
My aim is to append the computational results of PLE = sp.stats.linregress(np.log(Ideal_frequency), np.log(Data))
of both subjects for each ROI into the dictionary. The code should append the result of PLE[0] of both subjects together into one list or array for each ROI.
My problem that I am unable to solve is that my code only appends the computational result PLE[0]
for Subject1
into PLE_dict
. Consequently, Subject2
is not included.
For example, printing the result via PLE_dict
yields:
{1: [-0.9924575013746104],
2: [-0.8905778114042852],
3: [-1.0143346946069933],
.... # Cut the data here
The numbers 1 to 3 represent the first three ROIs, and the values in the square brackets the result of PLE[0]
. Both is correct. However, I would also like to append the results of subject2 into the array or list for each ROI.
Here is my current code:
import numpy as np
import scipy as sp
import itertools as it
from scipy import stats
Ideal_frequency = np.loadtxt("/volumes/... .1D")
ROIs = it.chain(range(1,181), range(1001,1181)) # ROIs 1-180 and 1001-1180
Subjects = ["Subject1", "Subject2"]
PLE_dict = {}
for Subject in Subjects:
for ROI in ROIs:
Data = np.loadtxt(f"/volumes/seagate/{Subject}/G360_{ROI}_{Subject}.1D")
PLE = sp.stats.linregress(np.log(Ideal_frequency), np.log(Data))
if ROI not in PLE_dict:
PLE_dict[ROI] = [PLE[0]]
else:
PLE_dict[ROI].append(PLE[0])
print(PLE_dict)
This screenshot maybe describes the problem better. Here is the output of print(PLE_dict)
. It shows the results of PLE[0]
for the last ROIs (one ROI per row). But these results are only for subject1
. My aim is to add the results of subject2
into the same rows for the single ROIs.
CodePudding user response:
You can solve this by:
if ROI not in PLE_dict:
PLE_dict[ROI] = [PLE[0]]
else:
PLE_dict[ROI].append(PLE[0])
Running
PLE_dict = {}
for Subject in range(2):
for ROI in range(2):
if ROI not in PLE_dict:
PLE_dict[ROI] = [Subject*ROI]
else:
PLE_dict[ROI].append(Subject*ROI)
print(PLE_dict)
gives
{0: [0, 0], 1: [0, 1]}
Also
it.chain(range(1,181), range(1001,1181)) # ROIs 1
is an iterator and once iterated, it is empty and will not run a second time. So either turn it into a list of put it directly into the loop.