Created
July 10, 2023 17:19
-
-
Save FloWuenne/3b28db88e6d0a86b49d13fb0da10ec22 to your computer and use it in GitHub Desktop.
Perform sliding window analysis on count data in a 2d array
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
## This code was entirely generated using OpenAIs Code Interpreter and prompting for the type of analysis that was desired | |
import pandas as pd | |
# Load the data | |
data = pd.read_csv('/mnt/data/example_rs_fish.csv') | |
# Calculate various quantiles | |
quantiles = data['intensity'].quantile([0.10, 0.25, 0.50, 0.75]) | |
quantiles | |
# Filter the data based on the 10th percentile threshold | |
threshold = quantiles[0.10] | |
filtered_data = data[data['intensity'] > threshold] | |
# Check the shape of the filtered data | |
filtered_data.shape | |
# Binning the data into 10x10 windows | |
filtered_data['x_bin'] = (filtered_data['x'] // window_size).astype(int) | |
filtered_data['y_bin'] = (filtered_data['y'] // window_size).astype(int) | |
# Group by the binned x and y, and calculate the spot count and total intensity | |
grouped_data = filtered_data.groupby(['x_bin', 'y_bin']).agg( | |
spot_count=('intensity', 'count'), | |
total_intensity=('intensity', 'sum') | |
).reset_index() | |
# Adjust the bin numbers back to pixel numbers | |
grouped_data['window_x_start'] = grouped_data['x_bin'] * window_size | |
grouped_data['window_y_start'] = grouped_data['y_bin'] * window_size | |
# Keep only the columns we need and sort them for readability | |
grouped_data = grouped_data[['window_x_start', 'window_y_start', 'spot_count', 'total_intensity']] | |
grouped_data | |
# Set up the figure and axes for the heatmaps | |
fig, axs = plt.subplots(2, 1, figsize=(10, 20)) | |
# Create a heatmap for the spot count | |
spot_count_pivot = grouped_data.pivot('window_y_start', 'window_x_start', 'spot_count') | |
sns.heatmap(spot_count_pivot, ax=axs[0], cmap='viridis', cbar_kws={'label': 'Spot Count'}) | |
axs[0].set_title('Heatmap of Spot Count') | |
# Create a heatmap for the total intensity | |
intensity_pivot = grouped_data.pivot('window_y_start', 'window_x_start', 'total_intensity') | |
sns.heatmap(intensity_pivot, ax=axs[1], cmap='viridis', cbar_kws={'label': 'Total Intensity'}) | |
axs[1].set_title('Heatmap of Total Intensity') | |
# Reverse the y-axis to match the image orientation | |
axs[0].invert_yaxis() | |
axs[1].invert_yaxis() | |
plt.tight_layout() | |
plt.show() | |
# Create a scatter plot to check for correlation between spot count and total intensity | |
sns.jointplot(data=grouped_data, x='spot_count', y='total_intensity', kind='reg', color='b') | |
plt.xlabel('Spot Count') | |
plt.ylabel('Total Intensity') | |
plt.title('Correlation between Spot Count and Total Intensity', pad=90) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment