Skip to content

Instantly share code, notes, and snippets.

@paulklemm
Created January 16, 2024 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulklemm/7378873bc50474609e0d97ed6d974f32 to your computer and use it in GitHub Desktop.
Save paulklemm/7378873bc50474609e0d97ed6d974f32 to your computer and use it in GitHub Desktop.
Revise Theodora's Data Normalisation
import uMAIA
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import warnings
import os
# Define the path to save the data
workdir = '/beegfs/scratch/bruening_scratch/pklemm/2024-01-theodora/uMAIA/data_maldi'
# List of acquisitions
acquisitions = [
'20231120_Hypothalamus_Brain3_slide10_421x352_Att35_7um',
'20231210_Hypothalamus_Brain10_slide4_417x375_7um_Att35',
'20231211_Hypothalamus_Brain8_slide8_506x272_7um_Att35',
'20231212_Hypothalamus_Brain5_slide3_460x364_7um_Att35'
]
# Define the path to save the data
PATH_SAVE = os.path.join(workdir, 'theodora_maldi.zarr')
# Define the list of columns and rows
cols_list = [421, 417, 506, 460]
rows_list = [352, 375, 272, 364]
# Create a list of NumPy arrays filled with boolean True values
masks = [np.full((rows_list[i], cols_list[i]), True, dtype=bool) for i, m in enumerate(acquisitions)]
# Loop over the acquisitions
for i, m in enumerate(acquisitions):
# Construct the full path for the current mask's .npy file
full_path = os.path.join(workdir, m, 'mask.npy')
# Ensure the subdirectory exists, create if necessary
os.makedirs(os.path.dirname(full_path), exist_ok=True)
# Save the mask array to the .npy file
print(full_path)
np.save(full_path, masks[i])
# Load known masks into a list
mask_list = [np.load(f'data/{m}/mask.npy') for m in acquisitions]
small_num = 0.0002
# Read images and masks
x, masks, masks_list = uMAIA.ut.tools.read_images_masks(acquisitions,
path_images=PATH_SAVE, mask_list=mask_list,
gaussian_smoothing=True, gaussian_sigma=0.4,
log_transform=True, epsilon=small_num
)
# Define the path to save the parameters
PATH_SAVE = os.path.join(workdir, 'parameters_maldi')
# Initialize the state
init_state = uMAIA.norm.initialize(x, masks, subsample=True)
# Normalize the data
svi_result = uMAIA.norm.normalize(x,
masks,
init_state=init_state,
subsample=True,
optimizer=None,
num_steps=5000,
seed=42)
# Check if the directory exists, if not, create it
if not os.path.isdir(PATH_SAVE):
os.mkdir(PATH_SAVE)
# Save the result
uMAIA.ut.tools.save_svi(svi_result, PATH_SAVE)
# Transform the data
x_MAIA = uMAIA.norm.transform(x, masks, PATH_SAVE)
# Save the normalized data to a zarr file
path_theodora_maldi = os.path.join(workdir, 'theodora_maldi.zarr')
path_theodora_maldi_nprmalised = os.path.join(workdir, 'theodora_maldi_normalised.zarr')
uMAIA.ut.tools.to_zarr_normalised(path_theodora_maldi, path_theodora_maldi_nprmalised, acquisitions, x_MAIA, mask_list, small_num)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment