|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | +import napari |
| 4 | +import numpy as np |
| 5 | +from qtpy.QtWidgets import ( |
| 6 | + QWidget, |
| 7 | + QVBoxLayout, |
| 8 | + QPushButton, |
| 9 | + QSizePolicy, |
| 10 | + QLabel, |
| 11 | + QFileDialog, |
| 12 | + QLineEdit, |
| 13 | + QCheckBox, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +from skimage import io |
| 18 | +from napari_cellseg_annotator import utils |
| 19 | +from napari_cellseg_annotator.napari_view_simple import launch_viewers |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +def format_Warning(message, category, filename, lineno, line=""): |
| 24 | + return ( |
| 25 | + str(filename) |
| 26 | + + ":" |
| 27 | + + str(lineno) |
| 28 | + + ": " |
| 29 | + + category.__name__ |
| 30 | + + ": " |
| 31 | + + str(message) |
| 32 | + + "\n" |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +warnings.formatwarning = format_Warning |
| 37 | + |
| 38 | +class Loader(QWidget): |
| 39 | + def __init__(self, parent: "napari.viewer.Viewer"): |
| 40 | + super().__init__() |
| 41 | + # self.master = parent |
| 42 | + self._viewer = parent |
| 43 | + self.opath = "" |
| 44 | + self.modpath = "" |
| 45 | + self.btn1 = QPushButton("Open", self) |
| 46 | + self.btn1.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) |
| 47 | + self.btn1.clicked.connect(self.show_dialog_o) |
| 48 | + self.btn2 = QPushButton("Open", self) |
| 49 | + self.btn2.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) |
| 50 | + self.btn2.clicked.connect(self.show_dialog_mod) |
| 51 | + |
| 52 | + self.textbox = QLineEdit(self) |
| 53 | + self.textbox.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) |
| 54 | + |
| 55 | + self.checkBox = QCheckBox("Create new dataset?") |
| 56 | + self.checkBox.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) |
| 57 | + |
| 58 | + self.btn4 = QPushButton("Start reviewing", self) |
| 59 | + self.btn4.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) |
| 60 | + self.btn4.clicked.connect(self.launch_napari) |
| 61 | + self.btn4.clicked.connect(self.close) |
| 62 | + self.btnb = QPushButton("Close", self) |
| 63 | + self.btnb.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) |
| 64 | + self.btnb.clicked.connect(self.close) |
| 65 | + self.lbl = QLabel("Images directory", self) |
| 66 | + self.lbl2 = QLabel("Labels directory", self) |
| 67 | + self.lbl4 = QLabel("Model name", self) |
| 68 | + ##################################################################### |
| 69 | + # TODO |
| 70 | + self.btntest = QPushButton("test", self) |
| 71 | + self.btntest.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) |
| 72 | + self.btntest.clicked.connect(self.run_test) |
| 73 | + ##################################################################### |
| 74 | + self.build() |
| 75 | + |
| 76 | + def build(self): |
| 77 | + vbox = QVBoxLayout() |
| 78 | + vbox.addWidget(utils.combine_blocks(self.btn1, self.lbl)) |
| 79 | + vbox.addWidget(utils.combine_blocks(self.btn2, self.lbl2)) |
| 80 | + vbox.addWidget(utils.combine_blocks(self.textbox, self.lbl4)) |
| 81 | + vbox.addWidget(self.checkBox) |
| 82 | + vbox.addWidget(self.btn4) |
| 83 | + vbox.addWidget(self.btnb) |
| 84 | + ################################################################## |
| 85 | + # TODO : remove once done |
| 86 | + test = True |
| 87 | + if test: |
| 88 | + vbox.addWidget(self.btntest) |
| 89 | + ################################################################## |
| 90 | + self.setLayout(vbox) |
| 91 | + self.show() |
| 92 | + |
| 93 | + def show_dialog_o(self): |
| 94 | + default_path = max(self.opath, self.modpath, os.path.expanduser("~")) |
| 95 | + f_name = QFileDialog.getExistingDirectory(self, "Open directory", default_path) |
| 96 | + if f_name: |
| 97 | + self.opath = f_name |
| 98 | + self.lbl.setText(self.opath) |
| 99 | + |
| 100 | + def show_dialog_mod(self): |
| 101 | + default_path = max(self.opath, self.modpath, os.path.expanduser("~")) |
| 102 | + f_name = QFileDialog.getExistingDirectory(self, "Open directory", default_path) |
| 103 | + if f_name: |
| 104 | + self.modpath = f_name |
| 105 | + self.lbl2.setText(self.modpath) |
| 106 | + |
| 107 | + def close(self): |
| 108 | + # self.master.setCurrentIndex(0) |
| 109 | + self._viewer.window.remove_dock_widget(self) |
| 110 | + |
| 111 | + def launch_napari(self): |
| 112 | + images = utils.load_images(self.opath) |
| 113 | + if self.modpath == "": # saves empty images of the same size as original images |
| 114 | + labels = np.zeros_like(images.compute()) # dask to numpy |
| 115 | + self.modpath = os.path.join( |
| 116 | + os.path.dirname(self.opath), self.textbox.text() |
| 117 | + ) |
| 118 | + os.makedirs(self.modpath, exist_ok=True) |
| 119 | + filenames = [ |
| 120 | + fn.name for fn in sorted(list(Path(self.opath).glob("./*png"))) |
| 121 | + ] |
| 122 | + for i in range(len(labels)): |
| 123 | + io.imsave( |
| 124 | + os.path.join(self.modpath, str(i).zfill(4) + ".png"), labels[i] |
| 125 | + ) |
| 126 | + else: |
| 127 | + labels = utils.load_saved_masks(self.modpath) |
| 128 | + try: |
| 129 | + labels_raw = utils.load_raw_masks(self.modpath + "_raw") |
| 130 | + except: |
| 131 | + labels_raw = None |
| 132 | + # TODO: viewer argument ? |
| 133 | + view1 = launch_viewers( |
| 134 | + self._viewer, |
| 135 | + images, |
| 136 | + labels, |
| 137 | + labels_raw, |
| 138 | + self.modpath, |
| 139 | + self.textbox.text(), |
| 140 | + self.checkBox.isChecked(), |
| 141 | + ) |
| 142 | + |
| 143 | + # global view_l |
| 144 | + # view_l.close() # why does it not close the window ?? #TODO use self.close() ? |
| 145 | + # self.close |
| 146 | + return view1 |
| 147 | + |
| 148 | + ######################## |
| 149 | + # TODO : remove once done |
| 150 | + def run_test(self): |
| 151 | + tif = False |
| 152 | + |
| 153 | + self.opath = "C:/Users/Cyril/Desktop/Proj_bachelor/data/visual_png/sample" |
| 154 | + self.modpath = ( |
| 155 | + "C:/Users/Cyril/Desktop/Proj_bachelor/data/visual_png/sample_labels" |
| 156 | + ) |
| 157 | + if tif: |
| 158 | + self.opath = "C:/Users/Cyril/Desktop/Proj_bachelor/data/visual_tif/volumes" |
| 159 | + self.modpath = "C:/Users/Cyril/Desktop/Proj_bachelor/data/visual_tif/labels" |
| 160 | + self.launch_napari() |
| 161 | + self.close() |
| 162 | + |
| 163 | + ######################## |
0 commit comments