<jupyter_start><jupyter_text>Załadowujemy trójwymiarową maskę z pliku .nii<jupyter_code>#example_filename = os.path.join(data_path, 'example4d.nii.gz')
#img = nib.load('../../data/001_25_mm.nii')
#img_data = img.get_fdata()[:50,90:100,:50]
#del img
img_mask = nib.load('../../data/001_25_mask.nii')
affine = img_mask.get_affine()
img_mask_data = img_mask.get_fdata()#[:50,90:100,:50]
del img_mask

import copy
img_mask_data_broken_1 = copy.deepcopy(img_mask_data)
np.isfinite(img_mask_data_broken_1[:,92,:]).any()
#zepsuc maske
img_mask_data_broken_1.shape
for y in tqdm(range(0,img_mask_data_broken_1.shape[1],5)):
    for i in range(1,4+1):
        img_mask_data_broken_1[:,y+i,:] = np.nan

        
fa = nib.Nifti1Image(img_mask_data_broken_1, affine)
nib.save(fa, 'img_mask_data_broken_1.nii.gz')
x, y, z = np.where(np.isfinite(img_mask_data_broken_1))
np.unique(y)
img_mask_data = img_mask_data_broken_1
direction = 1 
filled = np.array([np.isfinite(img_mask_data[:,x,:]).any() \
         for x in range(img_mask_data.shape[direction])])
## zaloadowac maske i obraz
# obejrzec w 3D
# sprobowac 'wymazac' z maski wszystkie maski oprocz co piatej
# sprobowac interpolowac maske na 'wewnetrzne slice'y
filled[0]
x = 1
filled[:x]
np.where(filled[:x])[0].max()<jupyter_output><empty_output><jupyter_text>read image | denoised image- | - {{x = show(img_data[:,9,:]) }}| {{y = show(img_mask_data[:,9,:])}}<jupyter_code>def prawidlowa_interpolacja_3D(img_mask_data, step=2):
    x_max, y_max, z_max = img_mask_data.shape
    grid_x, grid_y, grid_z = np.mgrid[0:x_max:1, 0:y_max:1,0:z_max:1]
    #points = np.random.rand(1000, 2)
    point_list = []
    for i in tqdm(range(x_max)):
        for j in range(0,y_max,2):
            for k in range(img_mask_data.shape[2]):
                point_list.append([i,j,k])
    points = np.array(point_list)
    values = img_mask_data[points[:,0],points[:,1],points[:,2]]
    grid_z2 = griddata(points, values, (grid_x, grid_y, grid_z), method='linear')
    #values = img_mask_data[points]
    #img_mask_data
    #del point_list
    grid_num = np.nan_to_num(grid_z2)
    show(grid_num[:,1,:])
    return grid_num
def trying_4D(img):
    data = img.get_fdata()
    hdr = img.header
    hdr.get_xyzt_units()
    raw = hdr.structarr
    raw['xyzt_units']
    img = nib.Nifti1Image(data, np.eye(4))
    img.to_filename(os.path.join('build','test4d.nii.gz'))
    return<jupyter_output><empty_output><jupyter_text>Przystepujemy do mojej uproszczonej liniowej interpolacji<jupyter_code>def simple_interpolation(img_mask_data):
    new_imgs = []
    for i in tqdm(range(5, img_mask_data.shape[1],5)):
        base_img1 = img_mask_data[:,i,:]
        base_img0 = img_mask_data[:,i-5,:]
        diff = (base_img1 - base_img0)
        diff_atomic = diff/5.0
        new_imgs.append(base_img0)
        for j in range(1, 5):
            interp = (base_img0 + (diff_atomic*j)).astype(int)
            new_imgs.append(interp)
    for k in range(i,img_mask_data.shape[1]):
        interp = (base_img1 + (diff_atomic*(img_mask_data.shape[1]-i))).astype(int)
        new_imgs.append(np.array(interp))
        
    return np.transpose(np.dstack(new_imgs),(0,2,1))<jupyter_output><empty_output><jupyter_text>Teraz nalezy ocenic blad takiej interpolacjiAle to zrobimy pozniej.(img_mask - interp_mask) **2 ( element wise )  Teraz interpolacja z konturow.Dla kazdego skrawka w plaszczyznie koronalnej.img_mask_data[:,y,:]wizualizujemy to i bierzemy<jupyter_code>x = show(img_mask_data[:,93,:])
slice_i = img_mask_data[:,93,:]
np.unique(slice_i)
show(slice_i==2)
import pandas as pd
y2, x2 = np.where(slice_i==2)
df = pd.DataFrame()
df['y'] = y2
df['x'] = x2
df['y'].min()
gorny_kontur = df.groupby('x').min().reset_index()
gorny_kontur.plot(kind='scatter', x='x', y='y')
dolny_kontur = df.groupby('x').max().reset_index()
dolny_kontur.plot(kind='scatter', x='x', y='y')
kontur = pd.concat([gorny_kontur, dolny_kontur])
kontur.plot(kind='scatter', x='x', y='y')<jupyter_output><empty_output><jupyter_text>powiedzmy ze wyglada to prawie ok;oprocz tego ze kontur sie nie zamyka ale to wezmiemy dla kazdego x wypelnimy y pomiedzykonturami dwojka(nalozymy taka maske albo cus)  to teraz wybierzmy sobie dolny kontur i znajdzmy dolny kontur dla kazdego skrawka w plaszczyznie koronalnej<jupyter_code>def prawidlowa_interpolacja_2D(img_mask_data, step=2):
    x_max, y_max, z_max = img_mask_data.shape
    grid_x, grid_y, grid_z = np.mgrid[0:x_max:1, 0:y_max:1,0:z_max:1]
    #points = np.random.rand(1000, 2)
    point_list = []
    for i in tqdm(range(x_max)):
        for j in range(0,y_max,2):
            for k in range(img_mask_data.shape[2]):
                point_list.append([i,j,k])
    points = np.array(point_list)
    values = img_mask_data[points[:,0],points[:,1],points[:,2]]
    grid_z2 = griddata(points, values, (grid_x, grid_y, grid_z), method='linear')
    #values = img_mask_data[points]
    #img_mask_data
    #del point_list
    grid_num = np.nan_to_num(grid_z2)
    show(grid_num[:,1,:])
    return grid_num
def func(x, z):
    return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*z**2)**2
def prawidlowa_interpolacja_2D():
    dolne_kontury = []
for z in tqdm(range(img_mask_data.shape[1])):
    coronal_slice = img_mask_data[:,z,:]
    y2, x2 = np.where(coronal_slice==2)
    df = pd.DataFrame()
    df['y'] = y2
    df['x'] = x2
    dolny_kontur = df.groupby('x').max().reset_index()
    gorny_kontur = df.groupby('x').min().reset_index()
    dolny_kontur['z'] = z
    dolne_kontury.append(dolny_kontur)
# teraz tak; dolny kontur:
#    dla dolnego kontura y jest funkcja x i zeta (ktory jest druga wspolrzedna niepostrzezenie)
# no i teraz z jest co piaty tylko
# i musimy znalezc dla reszty zetow wartosci
dolne_kontury = pd.concat(dolne_kontury)
x_max = dolny_kontur['x'].max()
z_max = img_mask_data.shape[1]
grid_x, grid_z = np.mgrid[50:x_max:1, 0:z_max:1]
points = dolne_kontury[['x','z']].values[::2]
values = dolne_kontury['y'].values[::2]
grid_z2 = griddata(points, values, (grid_x, grid_z), method='linear')
pd.DataFrame(grid_z2[:,93],columns=['y']).reset_index().plot()
    return
grid_x.max()
points = np.random.rand(1000, 2)
point_list = []
for i in tqdm(range(x_max)):
    for j in range(0, z_max, 2):
        point_list.append([i,j])
points = np.array(point_list)
values = img_mask_data[points[:,0],points[:,1],points[:,2]]<jupyter_output><empty_output><jupyter_text>trzeba sie zastanowic nad inputemNajwygodniej zeby inputem byl array w ktorym po prostu w nieokreslonychkomorkach bylo None<jupyter_code>#trzeba zmienic grida zeby sie zaczynal tak od 25
pd.DataFrame(points, columns=['x','z'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1528882220555139, 'fraction_numerical': 0.025356339084771192, 'mean_word_length': 4.821834061135371, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '455588', 'license': 'MIT', 'n_tokens_mistral': 2908, 'n_tokens_neox': 2868, 'n_words': 480, 'path': 'Interpolation.ipynb', 'repo_name': 'michalstepniewski/symmetry', 'size': '6665'}