Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding widget for adding points layer #20

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .Rhistory
Empty file.
3 changes: 3 additions & 0 deletions src/napari_brainbow_diagnose/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
)
from ._tooltip_pointer import tooltip_pointer_widget
from ._widget import DiagnoseWidget
from ._read_csv_widget import read_csv_widget, add_features_widget

__all__ = (
"tooltip_pointer_widget",
"make_rgb_cube_data",
"fetch_chroms_data",
"load_chroms_data_sample",
"DiagnoseWidget",
"read_csv_widget",
"add_features_widget"
)
71 changes: 71 additions & 0 deletions src/napari_brainbow_diagnose/_read_csv_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import napari
from napari.layers import Points

import pandas as pd
import numpy as np

from magicgui import magicgui
from magicgui import magic_factory
from ._utils_channel_space import rgb_to_maxwell_triangle, calculate_brightness
import pathlib

@magic_factory(call_button="(test) Create a Points layer", auto_call=False)
def read_csv_widget(filename = pathlib.Path('/path/to/csv/file.csv')):
# Getting the current viewer
viewer = napari.current_viewer()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know this existed! Cool!

# Import data
df = pd.read_csv(filename, usecols=range(6), names=['axis-0', 'axis-1', 'axis-2', 'red', 'green', 'blue'])
# Create points layer
coordinates = df[['axis-0', 'axis-1', 'axis-2']].values
colors = df[['red', 'green', 'blue']].values
points_layer = viewer.add_points(coordinates, name='points', face_color=colors, edge_color=colors, size=20, out_of_slice_display=True)

# Add Maxwell coordinates
df['maxwell_x'], df['maxwell_y'] = rgb_to_maxwell_triangle(df['red'], df['green'], df['blue'])
df['brightness'] = calculate_brightness(df['red'], df['green'], df['blue'])

df = df.sort_values(by='brightness').reset_index(drop=True)

features_table = {
'red': df['red'].values, 'green': df['green'].values, 'blue': df['blue'].values,
'maxwell_x': df['maxwell_x'].values, 'maxwell_y': df['maxwell_y'].values,
'brightness_index': df.index, 'brightness': df['brightness']
}
points_layer.features = features_table


# Add color features to an existing points layer which has features 'red', 'green', and 'blue'
@magic_factory(call_button="Add Features to Points Layer", auto_call=False, points_layer={"label": "Select Points Layer"}, points_selection={"label": "Add selected points feature"})
def add_features_widget(points_layer: Points, points_selection: bool):
# Extract existing features
if not all(col in points_layer.features for col in ['red', 'green', 'blue']):
raise ValueError("Selected points layer does not have 'red', 'green', and 'blue' features.")

red = points_layer.features['red']
green = points_layer.features['green']
blue = points_layer.features['blue']

# Add Maxwell coordinates
maxwell_x, maxwell_y = rgb_to_maxwell_triangle(red, green, blue)
brightness = calculate_brightness(red, green, blue)

features_table = {
'red': red.values,
'green': green.values,
'blue': blue.values,
'maxwell_x': maxwell_x.values,
'maxwell_y': maxwell_y.values,
'brightness': brightness.values,
'range': np.arange(len(brightness))
}

for key, value in features_table.items():
points_layer.features[key] = value

if points_selection:
selected = points_layer.selected_data
clustering = np.zeros(len(points_layer.data))
for point_index in selected:
clustering[point_index] = 1

points_layer.features['CLUSTER'] = clustering.astype(int)
18 changes: 18 additions & 0 deletions src/napari_brainbow_diagnose/_utils_channel_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ def rgb_to_hsv(rgb: np.ndarray, channel_axis: int = -1) -> np.ndarray:
return skc.rgb2hsv(rgb, channel_axis=channel_axis)


def rgb_to_maxwell_triangle(r, g, b):
"""
Given coordinates in rgb color space, returns x and y coordinates of the Maxwell triangle.
"""
s = r+g+b
r = r/s
g = g/s
b = b/s
x = (r-b)/np.sqrt(3)
y = g
return x, y

def calculate_brightness(r, g, b):
"""
Given standardized values (from 0 to 1) of rgb return brightness
"""
return (1 / 2)*(np.maximum(np.maximum(r, g), b) + np.minimum(np.minimum(r, g), b))

# TODO ajouter un paramètre pour choisir l'axe des canaux
def get_channels_ranges(a: np.ndarray) -> np.ndarray:
"""
Expand Down
12 changes: 12 additions & 0 deletions src/napari_brainbow_diagnose/napari.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ contributions:
- id: napari-brainbow-diagnose.DiagnoseWidget
python_name: napari_brainbow_diagnose._widget:DiagnoseWidget
title: Diagnose Brainbow Image
- id: napari-brainbow-diagnose.read_csv_widget
python_name: napari_brainbow_diagnose._read_csv_widget:read_csv_widget
title: Load a Points layer
- id: napari-brainbow-diagnose.add_features_widget
python_name: napari_brainbow_diagnose._read_csv_widget:add_features_widget
title: Add color features to existing Points layer
sample_data:
- command: napari-brainbow-diagnose.make_rgb_cube_data
display_name: RGB Cube
Expand All @@ -22,7 +28,13 @@ contributions:
display_name: Chroms Cortex Sample
key: unique_id.2
widgets:
- command: napari-brainbow-diagnose.read_csv_widget
display_name: Load a Points layer
- command: napari-brainbow-diagnose.add_features_widget
display_name: Add color features to existing Points layer
- command: napari-brainbow-diagnose.DiagnoseWidget
display_name: Diagnose Brainbow Image
- command: napari-brainbow-diagnose.tooltip_pointer_widget
display_name: Brainbow Tooltip