|
3 | 3 |
|
4 | 4 | import napari |
5 | 5 | import numpy as np |
| 6 | +import pandas as pd |
6 | 7 |
|
7 | 8 | # Qt |
8 | 9 | from qtpy.QtWidgets import QSizePolicy |
|
11 | 12 | from napari_cellseg3d import interface as ui |
12 | 13 | from napari_cellseg3d import utils |
13 | 14 | from napari_cellseg3d.model_framework import ModelFramework |
| 15 | +from napari_cellseg3d.model_instance_seg import volume_stats |
14 | 16 | from napari_cellseg3d.model_workers import InferenceWorker |
15 | 17 |
|
16 | 18 |
|
@@ -70,6 +72,7 @@ def __init__(self, viewer: "napari.viewer.Viewer"): |
70 | 72 | self.zoom = [1, 1, 1] |
71 | 73 |
|
72 | 74 | self.instance_params = None |
| 75 | + self.stats_to_csv = False |
73 | 76 |
|
74 | 77 | self.keep_on_cpu = False |
75 | 78 | self.use_window_inference = False |
@@ -211,6 +214,9 @@ def __init__(self, viewer: "napari.viewer.Viewer"): |
211 | 214 | left_or_above=self.instance_small_object_thresh_lbl, |
212 | 215 | horizontal=False, |
213 | 216 | ) |
| 217 | + self.save_stats_to_csv_box = ui.make_checkbox( |
| 218 | + "Save stats to csv", parent=self |
| 219 | + ) |
214 | 220 |
|
215 | 221 | ( |
216 | 222 | self.instance_param_container, |
@@ -315,6 +321,7 @@ def build(self): |
315 | 321 | self.instance_method_choice, |
316 | 322 | self.instance_prob_t_container, |
317 | 323 | self.instance_small_object_t_container, |
| 324 | + self.save_stats_to_csv_box, |
318 | 325 | ], |
319 | 326 | ) |
320 | 327 |
|
@@ -557,6 +564,7 @@ def start(self): # TODO update |
557 | 564 | "threshold": self.instance_prob_thresh.value(), |
558 | 565 | "size_small": self.instance_small_object_thresh.value(), |
559 | 566 | } |
| 567 | + self.stats_to_csv = self.save_stats_to_csv_box.isChecked() |
560 | 568 | # print(f"METHOD : {self.instance_method_choice.currentText()}") |
561 | 569 |
|
562 | 570 | self.show_res_nbr = self.display_number_choice.value() |
@@ -687,15 +695,35 @@ def on_yield(data, widget): |
687 | 695 |
|
688 | 696 | if data["instance_labels"] is not None: |
689 | 697 |
|
690 | | - number_cells = np.amax(data["instance_labels"]) |
691 | | - |
692 | | - widget.log.print_and_log( |
693 | | - f"\nNUMBER OF CELLS : {number_cells}\n" |
694 | | - ) |
695 | | - |
| 698 | + labels = data["instance_labels"] |
696 | 699 | method = widget.instance_params["method"] |
| 700 | + number_cells = np.amax(labels) |
| 701 | + |
697 | 702 | name = f"({number_cells})_{method}_instance_labels_{image_id}" |
698 | 703 |
|
699 | | - instance_layer = viewer.add_labels( |
700 | | - data[f"instance_labels"], name=name |
701 | | - ) |
| 704 | + instance_layer = viewer.add_labels(labels, name=name) |
| 705 | + |
| 706 | + if widget.stats_to_csv: # TODO move to worker |
| 707 | + |
| 708 | + cell_data = volume_stats( |
| 709 | + labels |
| 710 | + ) # TODO test with area mesh function |
| 711 | + # count = np.tile("", len(cell_data["Volume"])-1) |
| 712 | + # cell_data["Cell count"] = np.insert(count, 0, number_cells) |
| 713 | + # cell_data["Total cell volume"] = np.insert( |
| 714 | + # count, 0, tot_cell_volume |
| 715 | + # ) |
| 716 | + # cell_data["Cell volume ratio"] = np.insert( |
| 717 | + # count, 0, tot_cell_volume / len(labels.flatten()) |
| 718 | + # ) |
| 719 | + |
| 720 | + numeric_data = pd.DataFrame(cell_data) |
| 721 | + |
| 722 | + csv_name = f"/{method}_seg_results_{image_id}_{utils.get_date_time()}.csv" |
| 723 | + numeric_data.to_csv( |
| 724 | + widget.results_path + csv_name, index=False |
| 725 | + ) |
| 726 | + |
| 727 | + # widget.log.print_and_log( |
| 728 | + # f"\nNUMBER OF CELLS : {number_cells}\n" |
| 729 | + # ) |
0 commit comments