<jupyter_start><jupyter_text>Initiation<jupyter_code># To manipulate arrays
import numpy as np 
# To handle track DataFrames
import pandas as pd
# For working with x-arrays
import xarray as xr
# To import images
from skimage import io 
from skimage.io import imread
# For TrackPy
# To make plots
import matplotlib as mpl 
import matplotlib.pyplot as plt 
import seaborn as sns; sns.set()  
# For directories 
import os
import pathlib  # for working with windows paths
# For reloading a library when testing
# For deleting/reloading modules in another file for code testing
# Import trackArrayTools


current_dir = pathlib.Path().absolute()
croparray_dir = current_dir.parents[0].joinpath('croparray')
# Importing fish_analyses module
sys.path.append(str(croparray_dir))
import crop_array_tools as ca


# To work inline; change to %matplotlib notebook for interactive plotting
%matplotlib inline 
plt.style.use('dark_background')<jupyter_output><empty_output><jupyter_text>Directories<jupyter_code># Main directory
images_directory = current_dir.parents[0].joinpath('dataBase').joinpath('FISH_Analysis','GAPDH_Exon_Cy5_short') 

# Directories for images and database
image_2_dir = images_directory.joinpath('ROI001_XY1620755243_Z00_T0_merged.tif')
image_1_dir = images_directory.joinpath('ROI002_XY1620755646_Z00_T0_merged.tif')
database_dir =  current_dir.parents[0].joinpath('dataBase').joinpath('FISH_Analysis','dataframe_GAPDH_Exon_Cy5_short.csv')<jupyter_output><empty_output><jupyter_text>Loading images<jupyter_code># Reading  images as numpy arrays
image_1 = imread(str(image_1_dir))  # [Z,Y,X,C]
image_2 = imread(str(image_2_dir))  # [Z,Y,X,C]
image_2.shape
# Creating an array for croparray with dimensions (fov, f = frame, z, y, x, ch)
image_concatenated = np.concatenate((image_1[...,np.newaxis],image_2[...,np.newaxis]),axis=4) # concatenating the two arrays
image_new_order= np.moveaxis(image_concatenated,4,0) # moving axis to change dim from [Z,Y,X,C,fov] to [fov,Z,Y,X,C]
image_for_croparray = np.expand_dims(image_new_order,axis=1) # adding a dimension for f =frame
image_for_croparray.shape   # The final array with format: (fov, f = frame, z, y, x, ch)<jupyter_output><empty_output><jupyter_text>Loading dataframe<jupyter_code># Loading original dataframe
spots = pd.read_csv(database_dir) 
spots
# Dataframe for croparray
spots['f']= 0   # Adding a column with zeros for frame
# renaming some fields
spots.rename(columns={'x': 'xc','y': 'yc', 'z': 'zc','spot_id':'id', 'image_id': 'fov'}, inplace=True, errors='raise')
spots = spots[['fov','id','f','zc','yc','xc']]  
spots.head()<jupyter_output><empty_output><jupyter_text>Creating a croparray<jupyter_code>my_ca = ca.create_crop_array(image_for_croparray,spots,xy_pad=10)
my_ca
my_ca.attrs

#fov: 2, n: 3792, t: 1, z: 23, y: 21, x: 21, ch: 3
my_ca.sel(fov=1,ch=1,n=slice(20,30),t=0).int

sp= 20
zval = my_ca.sel(fov=0,n=sp,t=0,ch=1).zc.values
plt.imshow(my_ca.sel(fov=0,ch=1,n=sp,t=0,z=zval).int)
my_ca.sel(fov=1,ch=[0,1,2],n=slice(1,10)).int.shape

crop_array = ca.montage(my_ca, row = 'z', col = 'n').int.sel(ch=1)
crop_array.shape
# Plot one of the crops over time to test
best_z = ca.best_z_proj(my_ca, ref_ch=1, disk_r=3, roll_n=3)
best_z.where(my_ca.id==0).max('n').sel(fov=0).plot.imshow(col='t',rgb='ch',col_wrap=10,robust=True,xticks=[],yticks=[],size=1.5)
# Plotting
f = plt.figure(figsize=(20,15))
ax = plt.gca()
plt.imshow(crop_array[1,0,:,0:4000])
plt.grid(False)
plt.axis('off')
plt.show()
# Plotting
f = plt.figure(figsize=(20,15))
ax = plt.gca()
plt.imshow(crop_array[0,0,:,0:4000])
plt.grid(False)
plt.axis('off')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1482294811968158, 'fraction_numerical': 0.032939884710403516, 'mean_word_length': 7.244343891402715, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '41333', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1446, 'n_words': 286, 'path': 'notebooks/dev/ToolDevelopment_FISH.ipynb', 'repo_name': 'Colorado-State-University-Stasevich-Lab/croparray', 'size': '3643'}