Created
April 10, 2021 02:02
-
-
Save neon-ninja/173c0413531b9dca67b250af888d11df to your computer and use it in GitHub Desktop.
replacing values in an 2D matrix containing irregular arrays, ints to floats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import numpy as np | |
from scipy.io import loadmat, savemat | |
from tqdm.auto import tqdm | |
p_idx = loadmat("P_idx.mat",simplify_cells=True,mat_dtype=True)["P_idx"] # mat_dtype is necessary as we're replacing ints with floats | |
print(p_idx.shape) | |
replace = loadmat("Replacing_values.mat",simplify_cells=True)["network1"] | |
print(replace) | |
for pair in tqdm(replace): | |
FID = pair["FID_1"] | |
Volume = pair["Volume"] | |
#print(FID, Volume) | |
for x in range(p_idx.shape[0]): | |
for y in range(p_idx.shape[1]): | |
cell = p_idx[x,y] | |
if type(cell) == float and cell == FID: | |
#print(f"Replacing {x},{y}={p_idx[x,y]} with {Volume}") | |
p_idx[x,y] = Volume | |
elif type(cell) == np.ndarray: | |
for z in range(len(cell)): | |
if cell[z] == FID: | |
#print(f"Replacing {x},{y},{z}={p_idx[x,y][z]} with {Volume}") | |
cell[z] = Volume | |
savemat("replaced_P_idx_volumes.mat", {"replaced": p_idx}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment