Skip to content

Commit

Permalink
Merge branch '30_new_export_option_.csv'
Browse files Browse the repository at this point in the history
  • Loading branch information
RukiyyeE committed Jul 24, 2024
2 parents 4b6fcf0 + 1db0d4c commit 9936b92
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
23 changes: 22 additions & 1 deletion cellpose/gui/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import logging
import fastremap

from ..io import imread, imsave, outlines_to_text, add_model, remove_model, save_rois, save_settings
from ..io import imread, imsave, outlines_to_text, add_model, remove_model, save_rois, save_settings, save_features_csv
from ..models import normalize_default, MODEL_DIR, MODEL_LIST_PATH, get_user_models
from ..utils import masks_to_outlines, outlines_list
from . import guiparts, gui
Expand Down Expand Up @@ -570,6 +570,27 @@ def _masks_to_gui(parent, masks, outlines=None, colors=None):
else:
parent.ViewDropDown.setCurrentIndex(0)

def _save_features_csv(parent):
"""
Saves features to CSV if dataset is 2D.
Args:
parent: GUI object with filename and NZ attributes.
"""

# check if a file is loaded, if not, print an error message and return
if not parent.filename:
print("ERROR: No file loaded. Please load a file before attempting to save features.")
return

filename = parent.filename
base = os.path.splitext(filename)[0] + "_features.csv"
# check if the dataset is 2D (NZ == 1 implies a single z-layer)
if parent.NZ == 1:
print("GUI_INFO: saving features to CSV file")
save_features_csv(parent.filename)
else:
print("ERROR: cannot save features")

def _save_png(parent):
""" save masks to png or tiff (if 3D) """
Expand Down
11 changes: 11 additions & 0 deletions cellpose/gui/menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ def mainmenu(parent):
file_menu.addAction(parent.saveSettings)
parent.saveSettings.setEnabled(True)

"""
This adds a new menu item for saving features as a .csv file.
The user can activate this function to export specific data directly from the GUI.
The function `_save_features_as_csv` from the `io` module is called when the user clicks on the menu item.
"""
parent.saveFeaturesCsv = QAction("Save Features as .&csv", parent)
parent.saveFeaturesCsv.setShortcut("Ctrl+Shift+C")
parent.saveFeaturesCsv.triggered.connect(lambda: io._save_features_csv(parent))
file_menu.addAction(parent.saveFeaturesCsv)
parent.saveFeaturesCsv.setEnabled(True)

"""
This creates a new menu item for the minimap that the user can activate.
It is deactivated by default and has to be checked.
Expand Down
17 changes: 17 additions & 0 deletions cellpose/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import re
from . import version_str
from roifile import ImagejRoi, roiwrite
import csv

try:
from qtpy import QtGui, QtCore, Qt, QtWidgets
Expand Down Expand Up @@ -560,6 +561,22 @@ def masks_flows_to_seg(images, masks, flows, file_names, diams=30., channels=Non

np.save(base + "_seg.npy", dat)

def save_features_csv(file_name):
"""
Save features to .csv file and remove if it already exists
Args:
file_name (str): Target CSV file name
Returns:
None
"""
file_name = os.path.splitext(file_name)[0] + "_cp_features.csv"
if os.path.exists(file_name):
os.remove(file_name)
with open(file_name, mode='w', newline='') as f:
# creating an empty csv file or clearing the existing one
pass

def save_to_png(images, masks, flows, file_names):
""" deprecated (runs io.save_masks with png=True)
Expand Down

0 comments on commit 9936b92

Please sign in to comment.