niftiview-app 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- niftiview_app-1.0.0/LICENSE +21 -0
- niftiview_app-1.0.0/PKG-INFO +71 -0
- niftiview_app-1.0.0/README.md +44 -0
- niftiview_app-1.0.0/niftiview_app/__init__.py +1 -0
- niftiview_app-1.0.0/niftiview_app/data/config.json +46 -0
- niftiview_app-1.0.0/niftiview_app/data/niftiview.icns +0 -0
- niftiview_app-1.0.0/niftiview_app/data/niftiview.ico +0 -0
- niftiview_app-1.0.0/niftiview_app/data/niftiview.png +0 -0
- niftiview_app-1.0.0/niftiview_app/main.py +885 -0
- niftiview_app-1.0.0/niftiview_app/utils.py +44 -0
- niftiview_app-1.0.0/pyproject.toml +29 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Lukas Fisch
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: niftiview-app
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Standalone app for viewing volumetric images 🧠 based on Python
|
|
5
|
+
Home-page: https://github.com/codingfisch/niftiview-app
|
|
6
|
+
License: MIT
|
|
7
|
+
Author: codingfisch
|
|
8
|
+
Author-email: l_fisc17@wwu.de
|
|
9
|
+
Requires-Python: >=3.9,<4.0
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Requires-Dist: dcm2niix
|
|
20
|
+
Requires-Dist: niftiview (>=1.0.0)
|
|
21
|
+
Requires-Dist: sv-ttk
|
|
22
|
+
Requires-Dist: tkinterdnd2 ; sys_platform != "darwin"
|
|
23
|
+
Requires-Dist: tkinterdnd2-universal ; sys_platform == "darwin"
|
|
24
|
+
Project-URL: Repository, https://github.com/codingfisch/niftiview-app
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+

|
|
28
|
+
|
|
29
|
+
NiftiView is a medical 3D image viewer **made for amateurs** 👨, **clinicians** 👩⚕️, **scientists** 👨🔬 and **coders** 👩💻
|
|
30
|
+
|
|
31
|
+
**Highlight features** for each user group are
|
|
32
|
+
|
|
33
|
+
- 👨 **Drag&Drop** to easily **view multiple** 3D images in a **beautiful layout** and save as a **GIF**
|
|
34
|
+
- 👩⚕️ **Shortcuts** to quickly set up **optimal contrasts** by intensity windowing and **histogram equalization**
|
|
35
|
+
- 👨🔬 **Custom image layers and overlays** (crosshair, colorbar...) to create **publication-ready figures**
|
|
36
|
+
- 👩💻 Usable via **Python and Command Line Interface** (take a look at [the underlying Python package](https://github.com/codingfisch/niftiview))
|
|
37
|
+
|
|
38
|
+
Besides these highlights, NiftiView also **covers the basics**
|
|
39
|
+
- 💾 Supports **NIfTI** `.nii`/`.nii.gz`, **NumPy** `.npy`, **DICOM** and some **older 3D image formats**
|
|
40
|
+
- 🖼️ **High image quality** via Lanczos interpolation
|
|
41
|
+
- 📥 **Easy installation** for Windows and Linux: [**Download**](https://github.com/codingfisch/niftiview_app/releases), unzip, double-click 🏁
|
|
42
|
+
|
|
43
|
+
Learn how to use NiftiView via [the **YouTube-Tutorial**](https://youtu.be/YyVJ6JKJPRA) 💡
|
|
44
|
+
|
|
45
|
+
## Installation 🛠️
|
|
46
|
+
### Windows 🪟 or Linux 🐧
|
|
47
|
+
Download the app [**here**](https://github.com/codingfisch/niftiview_app/releases), unzip the downloaded file and double-click either
|
|
48
|
+
- NiftiView.exe (+ ignore potential virus alert, if you are using Windows)
|
|
49
|
+
- NiftiView.bin (+ previously run `chmod +x NiftiView.bin` in a terminal, if you are using Linux)
|
|
50
|
+
|
|
51
|
+
to run NiftiView 🧠 If you like it, check out [this short manual](https://github.com/codingfisch/niftiview_app/blob/main/install.md) to finalize the installation 🛠️
|
|
52
|
+
### The Python way 🐍
|
|
53
|
+
Users of macOS 🍏 and/or [Pythonistas](https://en.wiktionary.org/wiki/Pythonista) just **install it in the terminal** via
|
|
54
|
+
```bash
|
|
55
|
+
pip install niftiview-app
|
|
56
|
+
```
|
|
57
|
+
and **run it** via
|
|
58
|
+
```bash
|
|
59
|
+
niftiview-app
|
|
60
|
+
```
|
|
61
|
+
Some extra steps to make NiftiView feel like a macOS app are provided [here](https://github.com/codingfisch/niftiview_app/blob/main/install.md) 🛠️
|
|
62
|
+
|
|
63
|
+
### Bugfixes 🐛
|
|
64
|
+
- If the app does not start, missing packages can be the issue. To fix that:
|
|
65
|
+
- On Linux: Run `sudo apt install libcairo2-dev pkg-config python3-dev`
|
|
66
|
+
- On macOS: Run `brew install cairo pkg-config tcl-tk python-tk`
|
|
67
|
+
- If the app [looks grainy](https://github.com/ContinuumIO/anaconda-issues/issues/6833) in a conda env, run `conda install -c conda-forge tk=*=xft_*` to fix it
|
|
68
|
+
|
|
69
|
+
## Backstory 📖
|
|
70
|
+
Read about the long winding road from an annoying problem during my PhD to the release of NiftiView in [my blog](https://codingfisch.github.io/) (will be posted soon 🤝).
|
|
71
|
+
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
NiftiView is a medical 3D image viewer **made for amateurs** 👨, **clinicians** 👩⚕️, **scientists** 👨🔬 and **coders** 👩💻
|
|
4
|
+
|
|
5
|
+
**Highlight features** for each user group are
|
|
6
|
+
|
|
7
|
+
- 👨 **Drag&Drop** to easily **view multiple** 3D images in a **beautiful layout** and save as a **GIF**
|
|
8
|
+
- 👩⚕️ **Shortcuts** to quickly set up **optimal contrasts** by intensity windowing and **histogram equalization**
|
|
9
|
+
- 👨🔬 **Custom image layers and overlays** (crosshair, colorbar...) to create **publication-ready figures**
|
|
10
|
+
- 👩💻 Usable via **Python and Command Line Interface** (take a look at [the underlying Python package](https://github.com/codingfisch/niftiview))
|
|
11
|
+
|
|
12
|
+
Besides these highlights, NiftiView also **covers the basics**
|
|
13
|
+
- 💾 Supports **NIfTI** `.nii`/`.nii.gz`, **NumPy** `.npy`, **DICOM** and some **older 3D image formats**
|
|
14
|
+
- 🖼️ **High image quality** via Lanczos interpolation
|
|
15
|
+
- 📥 **Easy installation** for Windows and Linux: [**Download**](https://github.com/codingfisch/niftiview_app/releases), unzip, double-click 🏁
|
|
16
|
+
|
|
17
|
+
Learn how to use NiftiView via [the **YouTube-Tutorial**](https://youtu.be/YyVJ6JKJPRA) 💡
|
|
18
|
+
|
|
19
|
+
## Installation 🛠️
|
|
20
|
+
### Windows 🪟 or Linux 🐧
|
|
21
|
+
Download the app [**here**](https://github.com/codingfisch/niftiview_app/releases), unzip the downloaded file and double-click either
|
|
22
|
+
- NiftiView.exe (+ ignore potential virus alert, if you are using Windows)
|
|
23
|
+
- NiftiView.bin (+ previously run `chmod +x NiftiView.bin` in a terminal, if you are using Linux)
|
|
24
|
+
|
|
25
|
+
to run NiftiView 🧠 If you like it, check out [this short manual](https://github.com/codingfisch/niftiview_app/blob/main/install.md) to finalize the installation 🛠️
|
|
26
|
+
### The Python way 🐍
|
|
27
|
+
Users of macOS 🍏 and/or [Pythonistas](https://en.wiktionary.org/wiki/Pythonista) just **install it in the terminal** via
|
|
28
|
+
```bash
|
|
29
|
+
pip install niftiview-app
|
|
30
|
+
```
|
|
31
|
+
and **run it** via
|
|
32
|
+
```bash
|
|
33
|
+
niftiview-app
|
|
34
|
+
```
|
|
35
|
+
Some extra steps to make NiftiView feel like a macOS app are provided [here](https://github.com/codingfisch/niftiview_app/blob/main/install.md) 🛠️
|
|
36
|
+
|
|
37
|
+
### Bugfixes 🐛
|
|
38
|
+
- If the app does not start, missing packages can be the issue. To fix that:
|
|
39
|
+
- On Linux: Run `sudo apt install libcairo2-dev pkg-config python3-dev`
|
|
40
|
+
- On macOS: Run `brew install cairo pkg-config tcl-tk python-tk`
|
|
41
|
+
- If the app [looks grainy](https://github.com/ContinuumIO/anaconda-issues/issues/6833) in a conda env, run `conda install -c conda-forge tk=*=xft_*` to fix it
|
|
42
|
+
|
|
43
|
+
## Backstory 📖
|
|
44
|
+
Read about the long winding road from an annoying problem during my PhD to the release of NiftiView in [my blog](https://codingfisch.github.io/) (will be posted soon 🤝).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = '0.1.0'
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"scaling": null,
|
|
3
|
+
"appearance_mode": "dark",
|
|
4
|
+
"filepaths_view1": null,
|
|
5
|
+
"filepaths_view2": null,
|
|
6
|
+
"view": 1,
|
|
7
|
+
"page": 0,
|
|
8
|
+
"max_samples": 4,
|
|
9
|
+
"origin": null,
|
|
10
|
+
"layout": "sagittal++",
|
|
11
|
+
"height": 480,
|
|
12
|
+
"squeeze": false,
|
|
13
|
+
"coord_sys": "array_mm",
|
|
14
|
+
"glass_mode": null,
|
|
15
|
+
"annotations": false,
|
|
16
|
+
"resizing": null,
|
|
17
|
+
"cmap": null,
|
|
18
|
+
"transp_if": null,
|
|
19
|
+
"qrange": null,
|
|
20
|
+
"vrange": null,
|
|
21
|
+
"is_atlas": null,
|
|
22
|
+
"equal_hist": false,
|
|
23
|
+
"alpha": 0.5,
|
|
24
|
+
"crosshair": false,
|
|
25
|
+
"fpath": 0,
|
|
26
|
+
"coordinates": false,
|
|
27
|
+
"header": false,
|
|
28
|
+
"histogram": false,
|
|
29
|
+
"cbar": false,
|
|
30
|
+
"title": null,
|
|
31
|
+
"fontsize": 20,
|
|
32
|
+
"linecolor": "white",
|
|
33
|
+
"linewidth": 2,
|
|
34
|
+
"tmp_height": 360,
|
|
35
|
+
"nrows": null,
|
|
36
|
+
"cbar_vertical": true,
|
|
37
|
+
"cbar_pad": 0,
|
|
38
|
+
"cbar_pad_color": "k",
|
|
39
|
+
"cbar_x": 0.9,
|
|
40
|
+
"cbar_y": 0.5,
|
|
41
|
+
"cbar_width": 0.05,
|
|
42
|
+
"cbar_length": 0.8,
|
|
43
|
+
"cbar_label": null,
|
|
44
|
+
"cbar_ticks": null,
|
|
45
|
+
"annotation_dict": null
|
|
46
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,885 @@
|
|
|
1
|
+
import sv_ttk
|
|
2
|
+
import numpy as np
|
|
3
|
+
from sys import argv
|
|
4
|
+
from copy import deepcopy
|
|
5
|
+
from PIL import Image, ImageTk
|
|
6
|
+
from platform import system
|
|
7
|
+
from functools import partial
|
|
8
|
+
from webbrowser import open_new_tab
|
|
9
|
+
|
|
10
|
+
from tkinterdnd2 import DND_FILES, TkinterDnD
|
|
11
|
+
from tkinter import ttk, filedialog, Tk, Menu, Frame, Toplevel, EventType, BooleanVar
|
|
12
|
+
from niftiview.cli import save_gif, save_images_or_gifs
|
|
13
|
+
from niftiview.config import PADCOLORS, LINECOLORS, TMP_HEIGHTS, LAYER_ATTRIBUTES, Config
|
|
14
|
+
from niftiview.core import PLANES, ATLASES, TEMPLATES, RESIZINGS, LAYOUT_STRINGS, COORDINATE_SYSTEMS, GLASS_MODES
|
|
15
|
+
from niftiview.image import QRANGE, CMAPS_IMAGE, CMAPS_MASK
|
|
16
|
+
from niftiview.grid import NiftiImageGrid
|
|
17
|
+
|
|
18
|
+
from niftiview_app.utils import DATA_PATH, CONFIG_DICT, dcm2nii, debounce, get_window_frame, parse_dnd_filepaths
|
|
19
|
+
__version__ = '1.0.0'
|
|
20
|
+
PLANES_4D = tuple(list(PLANES) + ['time'])
|
|
21
|
+
SCALINGS = (.5, 2/3, .75, 1, 4/3, 1.5, 2)
|
|
22
|
+
PATH_PLACEHOLDER = {'image': '/path/to/images/*.nii (or drag&drop here)',
|
|
23
|
+
'mask': '/path/to/masks/*.nii (or drag&drop here)'}
|
|
24
|
+
OPTIONS = {'Main': ['Height', 'Layout', 'Colormap', 'Mask colormap', 'Mask opacity [%]', 'Max samples', 'Crosshair'],
|
|
25
|
+
'Image': ['Equalize histogram', 'Percentile range', '', 'Value range', '', 'Transparent if', 'Resizing'],
|
|
26
|
+
'Mask': ['Is atlas', 'Percentile range', '', 'Value range', '', 'Transparent if', 'Resizing'],
|
|
27
|
+
'Overlay': ['Coordinates', 'Header', 'Histogram', 'Colorbar', 'Filepath', 'Title', 'Fontsize'],
|
|
28
|
+
'Colorbar': ['Position [%]', '', 'Size [%]', '', 'Padding', 'Label', 'Ticks']}
|
|
29
|
+
OPTION_TABS = list(OPTIONS)
|
|
30
|
+
FILETYPES = [('Portable Network Graphics', '*.png'), ('JPEG', '*.jpg;*.jpeg'), ('Tagged Image File', '*.tiff;*.tif'),
|
|
31
|
+
('Portable Document Format', '*.pdf'), ('Scalable Vector Graphics', '*.svg'),
|
|
32
|
+
('Encapsulated PostScript', '*.eps'), ('PostScript', '*.ps')]
|
|
33
|
+
TUTORIAL_URL = 'https://youtu.be/YyVJ6JKJPRA'
|
|
34
|
+
HOMEPAGE_URL = 'https://github.com/codingfisch/niftiview_app'
|
|
35
|
+
AUTHOR_URL = 'https://github.com/codingfisch'
|
|
36
|
+
RELEASE_URL = f'{HOMEPAGE_URL}/releases/tag/v{__version__}'
|
|
37
|
+
CMD_KEY = 'Meta' if system() == 'Darwin"' else 'Control'
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class InputFrame(Frame, TkinterDnD.DnDWrapper):
|
|
41
|
+
def __init__(self, *args, **kwargs):
|
|
42
|
+
super().__init__(*args, **kwargs)
|
|
43
|
+
self.TkdndVersion = TkinterDnD._require(self)
|
|
44
|
+
self.grid_columnconfigure(1, weight=1)
|
|
45
|
+
grid_kwargs = {'sticky': 'nsew', 'pady': 2}
|
|
46
|
+
self.image_label = ttk.Label(self, text='Image files')
|
|
47
|
+
self.image_label.grid(row=0, column=0, **grid_kwargs, padx=5)
|
|
48
|
+
self.image_entry = ttk.Entry(self)
|
|
49
|
+
self.image_entry.insert(0, '/path/to/images/*.nii (or drag&drop here)')
|
|
50
|
+
self.image_entry.drop_target_register(DND_FILES)
|
|
51
|
+
self.image_entry.grid(row=0, column=1, **grid_kwargs)
|
|
52
|
+
self.mask_label = ttk.Label(self, text='Mask files')
|
|
53
|
+
self.mask_label.grid(row=1, column=0, **grid_kwargs, padx=5)
|
|
54
|
+
self.mask_entry = ttk.Entry(self)
|
|
55
|
+
self.mask_entry.insert(0, '/path/to/masks/*.nii (or drag&drop here)')
|
|
56
|
+
self.mask_entry.drop_target_register(DND_FILES)
|
|
57
|
+
self.mask_entry.grid(row=1, column=1, **grid_kwargs)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class OptionsFrame(ttk.Frame):
|
|
61
|
+
def __init__(self, *args, **kwargs):
|
|
62
|
+
config = kwargs.pop('config')
|
|
63
|
+
super().__init__(*args, **kwargs)
|
|
64
|
+
self.grid_columnconfigure(0, weight=1)
|
|
65
|
+
self.tabview = ttk.Notebook(self)
|
|
66
|
+
self.tabview.grid_columnconfigure(0, weight=1)
|
|
67
|
+
for i, (tab_name, option_labels) in enumerate(OPTIONS.items()):
|
|
68
|
+
tab_frame = ttk.Frame(self.tabview)
|
|
69
|
+
self.tabview.add(tab_frame, text=tab_name)
|
|
70
|
+
tab_frame.grid_columnconfigure(0, weight=1)
|
|
71
|
+
tab_frame.grid_columnconfigure(1, weight=0)
|
|
72
|
+
for row, label in enumerate(option_labels):
|
|
73
|
+
label_attr = f'{label}_mask_label' if tab_name == 'Mask' else f'{label}_label'
|
|
74
|
+
label_widget = ttk.Label(tab_frame, text=label)
|
|
75
|
+
setattr(self, label_attr, label_widget)
|
|
76
|
+
label_widget.grid(row=row, column=0, sticky='nsw', padx=5)
|
|
77
|
+
setattr(self, f'tab{i}', tab_frame)
|
|
78
|
+
|
|
79
|
+
grid_kwargs = {'pady': 2, 'sticky': 'nswe'}
|
|
80
|
+
self.grid_checkbox_kwargs = {'pady': grid_kwargs['pady'], 'sticky': 'nse'}
|
|
81
|
+
self.height_spinbox = ttk.Spinbox(self.tab0, from_=100, to=2000, increment=100)
|
|
82
|
+
self.height_spinbox.set(config.height)
|
|
83
|
+
self.height_spinbox.grid(row=0, column=1, **grid_kwargs)
|
|
84
|
+
layout_str = LAYOUT_STRINGS[config.layout] if config.layout in LAYOUT_STRINGS else config.layout
|
|
85
|
+
self.layout_options = ttk.Combobox(self.tab0, values=list(LAYOUT_STRINGS), width=5)
|
|
86
|
+
self.layout_options.set(config.layout if layout_str in list(LAYOUT_STRINGS.values()) else None)
|
|
87
|
+
self.layout_options.grid(row=1, column=1, **grid_kwargs)
|
|
88
|
+
self.cmap_options = ttk.Combobox(self.tab0, values=list(CMAPS_IMAGE) + ['CATALOG'])
|
|
89
|
+
self.cmap_options.set(CMAPS_IMAGE[0] if config.cmap is None else config.cmap[0])
|
|
90
|
+
self.cmap_options.grid(row=2, column=1, **grid_kwargs)
|
|
91
|
+
self.cmap_mask_options = ttk.Combobox(self.tab0, values=list(CMAPS_MASK) + ['CATALOG'])
|
|
92
|
+
if config.cmap is None:
|
|
93
|
+
self.cmap_mask_options.set(CMAPS_MASK[0])
|
|
94
|
+
elif isinstance(config.cmap, list) and len(config.cmap) == 1:
|
|
95
|
+
self.cmap_mask_options.set(CMAPS_MASK[0])
|
|
96
|
+
else:
|
|
97
|
+
self.cmap_mask_options.set(config.cmap[-1])
|
|
98
|
+
self.cmap_mask_options.grid(row=3, column=1, **grid_kwargs)
|
|
99
|
+
self.alpha_spinbox = ttk.Spinbox(self.tab0, from_=0, to=100, increment=10)
|
|
100
|
+
self.alpha_spinbox.set(100 * config.alpha)
|
|
101
|
+
self.alpha_spinbox.grid(row=4, column=1, **grid_kwargs)
|
|
102
|
+
self.max_samples_spinbox = ttk.Spinbox(self.tab0, from_=1, to=20)
|
|
103
|
+
self.max_samples_spinbox.set(config.max_samples)
|
|
104
|
+
self.max_samples_spinbox.grid(row=5, column=1, **grid_kwargs)
|
|
105
|
+
self.crosshair_check = BooleanVar(value=config.crosshair)
|
|
106
|
+
self.crosshair_checkbox = ttk.Checkbutton(self.tab0, text='', variable=self.crosshair_check)
|
|
107
|
+
self.crosshair_checkbox.grid(row=6, column=1, **self.grid_checkbox_kwargs)
|
|
108
|
+
self.equal_hist_check = BooleanVar(value=config.equal_hist)
|
|
109
|
+
self.equal_hist_checkbox = ttk.Checkbutton(self.tab1, text='', variable=self.equal_hist_check)
|
|
110
|
+
self.equal_hist_checkbox.grid(row=0, column=1, **self.grid_checkbox_kwargs)
|
|
111
|
+
self.qrange_stop_spinbox = ttk.Spinbox(self.tab1, from_=0, to=100, increment=1)
|
|
112
|
+
self.qrange_stop_spinbox.set(100 * (QRANGE[0][1] if config.qrange[0] is None else config.qrange[0][1]))
|
|
113
|
+
self.qrange_stop_spinbox.grid(row=1, column=1, **grid_kwargs)
|
|
114
|
+
self.qrange_start_spinbox = ttk.Spinbox(self.tab1, from_=0, to=100, increment=1)
|
|
115
|
+
self.qrange_start_spinbox.set(100 * (QRANGE[0][0] if config.qrange[0] is None else config.qrange[0][0]))
|
|
116
|
+
self.qrange_start_spinbox.grid(row=2, column=1, **grid_kwargs)
|
|
117
|
+
self.vrange_stop_spinbox = ttk.Spinbox(self.tab1, from_=-10**-45, to=10**45, increment=.5)
|
|
118
|
+
self.vrange_stop_spinbox.grid(row=3, column=1, **grid_kwargs)
|
|
119
|
+
self.vrange_start_spinbox = ttk.Spinbox(self.tab1, from_=-10**-45, to=10**45, increment=.5)
|
|
120
|
+
self.vrange_start_spinbox.grid(row=4, column=1, **grid_kwargs)
|
|
121
|
+
self.transp_if_entry = ttk.Entry(self.tab1)
|
|
122
|
+
if config.transp_if[0] is not None:
|
|
123
|
+
self.transp_if_entry.insert(0, config.transp_if[0])
|
|
124
|
+
self.transp_if_entry.grid(row=5, column=1, **grid_kwargs)
|
|
125
|
+
self.resizing_options = ttk.Combobox(self.tab1, values=RESIZINGS, state='readonly')
|
|
126
|
+
self.resizing_options.set(RESIZINGS[config.resizing[0]])
|
|
127
|
+
self.resizing_options.grid(row=6, column=1, **grid_kwargs)
|
|
128
|
+
if config.is_atlas is None:
|
|
129
|
+
self.is_atlas_check = BooleanVar(value=True)
|
|
130
|
+
elif isinstance(config.cmap, list) and len(config.cmap) == 1:
|
|
131
|
+
self.is_atlas_check = BooleanVar(value=True)
|
|
132
|
+
else:
|
|
133
|
+
self.is_atlas_check = BooleanVar(value=config.is_atlas[-1])
|
|
134
|
+
self.is_atlas_checkbox = ttk.Checkbutton(self.tab2, text='', variable=self.is_atlas_check)
|
|
135
|
+
self.is_atlas_checkbox.grid(row=0, column=1, **self.grid_checkbox_kwargs)
|
|
136
|
+
self.qrange_stop_mask_spinbox = ttk.Spinbox(self.tab2, from_=0, to=100, increment=1)
|
|
137
|
+
self.qrange_stop_mask_spinbox.set(100 * (config.qrange[-1][1] if len(config.qrange) > 1 else QRANGE[1][1]))
|
|
138
|
+
self.qrange_stop_mask_spinbox.grid(row=1, column=1, **grid_kwargs)
|
|
139
|
+
self.qrange_start_mask_spinbox = ttk.Spinbox(self.tab2, from_=0, to=100, increment=1)
|
|
140
|
+
self.qrange_start_mask_spinbox.set(100 * (config.qrange[-1][0] if len(config.qrange) > 1 else QRANGE[1][0]))
|
|
141
|
+
self.qrange_start_mask_spinbox.grid(row=2, column=1, **grid_kwargs)
|
|
142
|
+
self.vrange_stop_mask_spinbox = ttk.Spinbox(self.tab2, from_=-10**-45, to=10**45, increment=.5)
|
|
143
|
+
self.vrange_stop_mask_spinbox.grid(row=3, column=1, **grid_kwargs)
|
|
144
|
+
self.vrange_start_mask_spinbox = ttk.Spinbox(self.tab2, from_=-10**-45, to=10**45, increment=.5)
|
|
145
|
+
self.vrange_start_mask_spinbox.grid(row=4, column=1, **grid_kwargs)
|
|
146
|
+
self.transp_if_mask_entry = ttk.Entry(self.tab2)
|
|
147
|
+
if config.transp_if[-1] is not None:
|
|
148
|
+
self.transp_if_mask_entry.insert(0, config.transp_if[-1])
|
|
149
|
+
self.transp_if_mask_entry.grid(row=5, column=1, **grid_kwargs)
|
|
150
|
+
self.resizing_mask_options = ttk.Combobox(self.tab2, values=RESIZINGS, state='readonly')
|
|
151
|
+
self.resizing_mask_options.set(RESIZINGS[config.resizing[-1]])
|
|
152
|
+
self.resizing_mask_options.grid(row=6, column=1, **grid_kwargs)
|
|
153
|
+
self.coordinates_check = BooleanVar(value=config.coordinates)
|
|
154
|
+
self.coordinates_checkbox = ttk.Checkbutton(self.tab3, text='', variable=self.coordinates_check)
|
|
155
|
+
self.coordinates_checkbox.grid(row=0, column=1, **self.grid_checkbox_kwargs)
|
|
156
|
+
self.header_check = BooleanVar(value=config.header)
|
|
157
|
+
self.header_checkbox = ttk.Checkbutton(self.tab3, text='', variable=self.header_check)
|
|
158
|
+
self.header_checkbox.grid(row=1, column=1, **self.grid_checkbox_kwargs)
|
|
159
|
+
self.histogram_check = BooleanVar(value=config.histogram)
|
|
160
|
+
self.histogram_checkbox = ttk.Checkbutton(self.tab3, text='', variable=self.histogram_check)
|
|
161
|
+
self.histogram_checkbox.grid(row=2, column=1, **self.grid_checkbox_kwargs)
|
|
162
|
+
self.cbar_options = ttk.Combobox(self.tab3, values=['', 'vertical', 'horizontal'], state='readonly')
|
|
163
|
+
self.cbar_options.set(['horizontal', 'vertical'][int(config.cbar_vertical)] if config.cbar else '')
|
|
164
|
+
self.cbar_options.grid(row=3, column=1, **grid_kwargs)
|
|
165
|
+
self.fpath_spinbox = ttk.Spinbox(self.tab3, from_=0, to=20)
|
|
166
|
+
self.fpath_spinbox.set(int(config.fpath))
|
|
167
|
+
self.fpath_spinbox.grid(row=4, column=1, **grid_kwargs)
|
|
168
|
+
self.title_entry = ttk.Entry(self.tab3)
|
|
169
|
+
if config.title is not None:
|
|
170
|
+
self.title_entry.insert('0', config.title)
|
|
171
|
+
self.title_entry.grid(row=5, column=1, **grid_kwargs)
|
|
172
|
+
self.fontsize_spinbox = ttk.Spinbox(self.tab3, from_=1, to=200)
|
|
173
|
+
self.fontsize_spinbox.set(config.fontsize)
|
|
174
|
+
self.fontsize_spinbox.grid(row=6, column=1, **grid_kwargs)
|
|
175
|
+
self.cbar_x_spinbox = ttk.Spinbox(self.tab4, from_=0, to=100)
|
|
176
|
+
self.cbar_x_spinbox.set(100 * config.cbar_x)
|
|
177
|
+
self.cbar_x_spinbox.grid(row=0, column=1, **grid_kwargs)
|
|
178
|
+
self.cbar_y_spinbox = ttk.Spinbox(self.tab4, from_=0, to=100)
|
|
179
|
+
self.cbar_y_spinbox.set(100 * config.cbar_y)
|
|
180
|
+
self.cbar_y_spinbox.grid(row=1, column=1, **grid_kwargs)
|
|
181
|
+
self.cbar_width_spinbox = ttk.Spinbox(self.tab4, from_=0, to=100)
|
|
182
|
+
self.cbar_width_spinbox.set(100 * config.cbar_width)
|
|
183
|
+
self.cbar_width_spinbox.grid(row=2, column=1, **grid_kwargs)
|
|
184
|
+
self.cbar_length_spinbox = ttk.Spinbox(self.tab4, from_=0, to=100)
|
|
185
|
+
self.cbar_length_spinbox.set(100 * config.cbar_length)
|
|
186
|
+
self.cbar_length_spinbox.grid(row=3, column=1, **grid_kwargs)
|
|
187
|
+
self.cbar_pad_spinbox = ttk.Spinbox(self.tab4, from_=0, to=500, increment=20)
|
|
188
|
+
self.cbar_pad_spinbox.set(config.cbar_pad)
|
|
189
|
+
self.cbar_pad_spinbox.grid(row=4, column=1, **grid_kwargs)
|
|
190
|
+
self.cbar_label_entry = ttk.Entry(self.tab4)
|
|
191
|
+
if config.cbar_label is not None:
|
|
192
|
+
self.cbar_label_entry.insert('0', config.cbar_label)
|
|
193
|
+
self.cbar_label_entry.grid(row=5, column=1, **grid_kwargs)
|
|
194
|
+
self.cbar_ticks_entry = ttk.Entry(self.tab4)
|
|
195
|
+
if config.cbar_ticks is not None:
|
|
196
|
+
self.cbar_ticks_entry.insert('0', ','.join([str(item) for item in config.cbar_ticks]))
|
|
197
|
+
self.cbar_ticks_entry.grid(row=6, column=1, **grid_kwargs)
|
|
198
|
+
self.tabview.grid(row=0, column=0, sticky='nsew')
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
class SliderFrame(ttk.Frame):
|
|
202
|
+
def __init__(self, *args, **kwargs):
|
|
203
|
+
super().__init__(*args, **kwargs)
|
|
204
|
+
self.grid_columnconfigure((0, 1, 2, 3), weight=1)
|
|
205
|
+
self.grid_rowconfigure(1, weight=1)#, minsize=20)
|
|
206
|
+
self.sliders = {}
|
|
207
|
+
for i, plane in enumerate(PLANES_4D):
|
|
208
|
+
label = ttk.Label(self, text=plane.capitalize())
|
|
209
|
+
label.grid(row=0, column=i, sticky='ns', padx=5, pady=5)
|
|
210
|
+
from_to = (0, 400) if plane == 'time' else (-200, 200)
|
|
211
|
+
slider = ttk.Scale(self, orient='vertical', from_=from_to[1], to=from_to[0], length=50)
|
|
212
|
+
slider.set(0)
|
|
213
|
+
slider.grid(row=1, column=i, sticky='nsew', pady=5)
|
|
214
|
+
self.sliders.update({plane: slider})
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
class PagesFrame(ttk.Frame):
|
|
218
|
+
def __init__(self, *args, **kwargs):
|
|
219
|
+
config = kwargs.pop('config')
|
|
220
|
+
super().__init__(*args, **kwargs)
|
|
221
|
+
self.grid_columnconfigure((0, 1, 2), weight=1)
|
|
222
|
+
self.previous_button = ttk.Button(self, text='Previous')
|
|
223
|
+
self.previous_button.grid(row=0, column=0, padx=1, sticky='nsew')
|
|
224
|
+
self.page_label = ttk.Label(self, text=f'Page {config.page + 1} of {config.n_pages}')
|
|
225
|
+
self.page_label.grid(row=0, column=1, padx=1, sticky='ns')
|
|
226
|
+
self.next_button = ttk.Button(self, text='Next')
|
|
227
|
+
self.next_button.grid(row=0, column=2, padx=1, sticky='nsew')
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
class SidebarFrame(ttk.Frame):
|
|
231
|
+
def __init__(self, *args, **kwargs):
|
|
232
|
+
config = kwargs.pop('config')
|
|
233
|
+
toplevel = kwargs.pop('toplevel')
|
|
234
|
+
super().__init__(*args, **kwargs)
|
|
235
|
+
self.grid_rowconfigure(3, weight=1)
|
|
236
|
+
grid_kwargs = {'sticky': 'nsew', 'pady': 1, 'columnspan': 2}
|
|
237
|
+
if not toplevel:
|
|
238
|
+
self.input_frame = InputFrame(self)
|
|
239
|
+
self.input_frame.grid(row=0, **grid_kwargs)
|
|
240
|
+
self.view_button = ttk.Button(self, text='Switch to view 2')
|
|
241
|
+
self.view_button.grid(row=1, sticky=grid_kwargs['sticky'], pady=grid_kwargs['pady'])
|
|
242
|
+
self.clear_mask_button = ttk.Button(self, text='Clear masks')
|
|
243
|
+
self.clear_mask_button.grid(row=1, column=1, sticky=grid_kwargs['sticky'], pady=grid_kwargs['pady'])
|
|
244
|
+
self.options_frame = OptionsFrame(self, config=config)
|
|
245
|
+
self.options_frame.grid(row=2, **grid_kwargs)
|
|
246
|
+
self.sliders_frame = SliderFrame(self)
|
|
247
|
+
self.sliders_frame.grid(row=3, **grid_kwargs)
|
|
248
|
+
if not toplevel:
|
|
249
|
+
self.pages_frame = PagesFrame(self, config=config)
|
|
250
|
+
self.pages_frame.grid(row=4, **grid_kwargs)
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
class MainFrame(ttk.Frame):
|
|
254
|
+
def __init__(self, *args, **kwargs):
|
|
255
|
+
config = kwargs.pop('config')
|
|
256
|
+
toplevel = kwargs.pop('toplevel')
|
|
257
|
+
self.toplevel = toplevel
|
|
258
|
+
self.config = Config.from_dict(CONFIG_DICT) if config is None else config
|
|
259
|
+
super().__init__(*args, **kwargs)
|
|
260
|
+
|
|
261
|
+
self.grid_columnconfigure(0)
|
|
262
|
+
self.grid_columnconfigure(1, weight=1)
|
|
263
|
+
|
|
264
|
+
self.image_frame = ttk.Frame(self)
|
|
265
|
+
self.image_frame.grid(row=0, column=1, sticky='nsew')
|
|
266
|
+
self.image_label = ttk.Label(self.image_frame, text='')
|
|
267
|
+
self.image_label.grid(sticky='nsew')
|
|
268
|
+
|
|
269
|
+
self.niigrid1 = None
|
|
270
|
+
self.niigrid2 = None
|
|
271
|
+
self.load_niigrid()
|
|
272
|
+
self.image = None
|
|
273
|
+
self.image_grid_boxes = None
|
|
274
|
+
self.image_grid_numbers = None
|
|
275
|
+
self.image_overlay = None
|
|
276
|
+
self.image_origin_coords = None
|
|
277
|
+
self.annotation_buttons = []
|
|
278
|
+
self.update_image(hd=True)
|
|
279
|
+
|
|
280
|
+
self.sidebar_frame = SidebarFrame(self, config=self.config, toplevel=toplevel, width=100)
|
|
281
|
+
self.sidebar_frame.grid(row=0, column=0, sticky='nsew')
|
|
282
|
+
self.menu = self.init_menu_bar()
|
|
283
|
+
|
|
284
|
+
self.image_label.bind('<Motion>', self.set_image_overlay)
|
|
285
|
+
self.image_label.bind('<Leave>', lambda event: self.set_image_overlay(event, remove_overlay=True))
|
|
286
|
+
self.image_label.bind('<Button-1>', partial(self.update_origin_click, hd=False))
|
|
287
|
+
self.image_label.bind('<B1-Motion>', partial(self.update_origin_click, hd=False))
|
|
288
|
+
self.image_label.bind('<ButtonRelease-1>', partial(self.update_origin_click, hd=True))
|
|
289
|
+
self.master.bind('<space>', lambda e: self.update_image(hd=True))
|
|
290
|
+
self.master.bind('<Button-1>', lambda e: self.update_image(hd=True))
|
|
291
|
+
if not self.toplevel:
|
|
292
|
+
self.set_input_frame()
|
|
293
|
+
self.sidebar_frame.view_button.configure(command=self.set_view)
|
|
294
|
+
self.sidebar_frame.clear_mask_button.configure(command=self.clear_masks)
|
|
295
|
+
self.add_sliders_commands(self.sidebar_frame.sliders_frame.sliders)
|
|
296
|
+
self.set_options_events()
|
|
297
|
+
if not toplevel:
|
|
298
|
+
self.sidebar_frame.pages_frame.previous_button.configure(command=self.set_page)
|
|
299
|
+
self.sidebar_frame.pages_frame.next_button.configure(command=partial(self.set_page, next=True))
|
|
300
|
+
|
|
301
|
+
def init_menu_bar(self):
|
|
302
|
+
menu_bar = Menu(self.master)
|
|
303
|
+
# File Menu
|
|
304
|
+
file_menu = Menu(menu_bar, tearoff=0)
|
|
305
|
+
menu_bar.add_cascade(label='Open', menu=file_menu)
|
|
306
|
+
file_menu.add_command(label='Load 3D image...', command=self.open_files)
|
|
307
|
+
self.master.config(menu=menu_bar)
|
|
308
|
+
template_submenu = Menu(file_menu, tearoff=0)
|
|
309
|
+
file_menu.add_cascade(label='...or template', menu=template_submenu)
|
|
310
|
+
for template in TEMPLATES:
|
|
311
|
+
template_submenu.add_command(label=template, command=partial(self.open_files, filepaths=[TEMPLATES[template]], dropdown=True))
|
|
312
|
+
file_menu.add_separator()
|
|
313
|
+
file_menu.add_command(label='Load 3D mask...', command=self.open_files)
|
|
314
|
+
atlas_submenu = Menu(file_menu, tearoff=0)
|
|
315
|
+
file_menu.add_cascade(label='...or atlas', menu=atlas_submenu)
|
|
316
|
+
for atlas in ATLASES:
|
|
317
|
+
atlas_submenu.add_command(label=atlas, command=partial(self.open_files, filepaths=[ATLASES[atlas]], is_mask=True, dropdown=True))
|
|
318
|
+
file_menu.add_separator()
|
|
319
|
+
file_menu.add_command(label='Load configuration', command=self.load_config)
|
|
320
|
+
# Save Menu
|
|
321
|
+
save_menu = Menu(menu_bar, tearoff=0)
|
|
322
|
+
menu_bar.add_cascade(label='Save', menu=save_menu)
|
|
323
|
+
save_image_submenu = Menu(save_menu, tearoff=0)
|
|
324
|
+
save_menu.add_cascade(label='Save image as', menu=save_image_submenu)
|
|
325
|
+
for ftype in FILETYPES:
|
|
326
|
+
save_image_submenu.add_command(label=ftype[1], command=partial(self.save_image, ftype))
|
|
327
|
+
save_menu.add_command(label='Save all images', command=self.save_all_images_or_gifs)
|
|
328
|
+
save_menu.add_command(label='Save GIF', command=self.save_gif)
|
|
329
|
+
save_menu.add_command(label='Save all GIFs', command=lambda: self.save_all_images_or_gifs(gif=True))
|
|
330
|
+
save_menu.add_command(label='Save annotations', command=self.save_annotations)
|
|
331
|
+
save_menu.add_command(label='Save configuration', command=self.save_config)
|
|
332
|
+
# Appearance Menu
|
|
333
|
+
appearance_menu = Menu(menu_bar, tearoff=0)
|
|
334
|
+
menu_bar.add_cascade(label='Appearance', menu=appearance_menu)
|
|
335
|
+
appearance_menu.add_command(label='Dark mode', command=lambda: sv_ttk.set_theme('dark'))
|
|
336
|
+
appearance_menu.add_command(label='Light mode', command=lambda: sv_ttk.set_theme('light'))
|
|
337
|
+
appearance_menu.add_separator()
|
|
338
|
+
tmp_height_submenu = Menu(appearance_menu, tearoff=0)
|
|
339
|
+
appearance_menu.add_cascade(label='Temp. image height', menu=tmp_height_submenu)
|
|
340
|
+
for tmp_height in [None, *TMP_HEIGHTS]:
|
|
341
|
+
label = 'Disable (can be laggy)' if tmp_height is None else tmp_height
|
|
342
|
+
tmp_height_submenu.add_command(label=label, command=partial(self.update_config, attribute='tmp_height', event=tmp_height))
|
|
343
|
+
appearance_menu.add_separator()
|
|
344
|
+
appearance_menu.add_command(label='Fullscreen', command=lambda: self.master.attributes('-fullscreen', not self.master.attributes('-fullscreen')))
|
|
345
|
+
# Extra Options Menu
|
|
346
|
+
extra_options_menu = Menu(menu_bar, tearoff=0)
|
|
347
|
+
menu_bar.add_cascade(label='Extra Options', menu=extra_options_menu)
|
|
348
|
+
extra_options_menu.add_command(label='Annotations', command=self.set_annotation_buttons)
|
|
349
|
+
linewidth_submenu = Menu(extra_options_menu, tearoff=0)
|
|
350
|
+
extra_options_menu.add_cascade(label='Linewidth', menu=linewidth_submenu)
|
|
351
|
+
for linewidth in list(range(1, 9)):
|
|
352
|
+
linewidth_submenu.add_command(label=str(linewidth), command=partial(self.update_config, attribute='linewidth', event=linewidth))
|
|
353
|
+
linecolor_submenu = Menu(extra_options_menu, tearoff=0)
|
|
354
|
+
extra_options_menu.add_cascade(label='Linecolor', menu=linecolor_submenu)
|
|
355
|
+
for linecolor in LINECOLORS:
|
|
356
|
+
linecolor_submenu.add_command(label=linecolor, command=partial(self.update_config, attribute='linecolor', event=linecolor))
|
|
357
|
+
padcolor_submenu = Menu(extra_options_menu, tearoff=0)
|
|
358
|
+
extra_options_menu.add_cascade(label='Padcolor', menu=padcolor_submenu)
|
|
359
|
+
for padcolor in PADCOLORS:
|
|
360
|
+
padcolor_submenu.add_command(label=padcolor, command=partial(self.update_config, attribute='cbar_pad_color', event=padcolor))
|
|
361
|
+
nrows_submenu = Menu(extra_options_menu, tearoff=0)
|
|
362
|
+
extra_options_menu.add_cascade(label='Number of rows', menu=nrows_submenu)
|
|
363
|
+
for nrows in [None] + list(range(1, 9)):
|
|
364
|
+
option = 'Auto' if nrows is None else str(nrows)
|
|
365
|
+
nrows_submenu.add_command(label=option, command=partial(self.update_config, attribute='nrows', event=nrows))
|
|
366
|
+
glass_mode_submenu = Menu(extra_options_menu, tearoff=0)
|
|
367
|
+
extra_options_menu.add_cascade(label='Glassbrain mode', menu=glass_mode_submenu)
|
|
368
|
+
for glass_mode in [None] + list(GLASS_MODES):
|
|
369
|
+
glass_mode_submenu.add_command(label=glass_mode, command=partial(self.update_config, attribute='glass_mode', event=glass_mode))
|
|
370
|
+
coord_sys_submenu = Menu(extra_options_menu, tearoff=0)
|
|
371
|
+
extra_options_menu.add_cascade(label='Coordinate system', menu=coord_sys_submenu)
|
|
372
|
+
for coord_sys in COORDINATE_SYSTEMS:
|
|
373
|
+
coord_sys_submenu.add_command(label=coord_sys, command=partial(self.update_config, attribute='coord_sys', event=coord_sys))
|
|
374
|
+
extra_options_menu.add_command(label='Squeeze', command=partial(self.update_config, attribute='squeeze', switch=True))
|
|
375
|
+
# Help Menu
|
|
376
|
+
menu_bar.add_command(label='Help', command=lambda: open_new_tab(TUTORIAL_URL))
|
|
377
|
+
# About Menu
|
|
378
|
+
about_menu = Menu(menu_bar, tearoff=0)
|
|
379
|
+
menu_bar.add_cascade(label='About', menu=about_menu)
|
|
380
|
+
about_menu.add_command(label='Homepage', command=lambda: open_new_tab(HOMEPAGE_URL))
|
|
381
|
+
about_menu.add_command(label='Author', command=lambda: open_new_tab(AUTHOR_URL))
|
|
382
|
+
about_menu.add_separator()
|
|
383
|
+
about_menu.add_command(label=f'App-Version {__version__}', command=lambda: open_new_tab(RELEASE_URL))
|
|
384
|
+
return menu_bar
|
|
385
|
+
|
|
386
|
+
def set_input_frame(self, event=None):
|
|
387
|
+
frame = self.sidebar_frame.input_frame
|
|
388
|
+
frame.image_entry.bind('<Button-1>', lambda e: self.clean_entry(frame.image_entry))
|
|
389
|
+
frame.image_entry.bind('<FocusOut>', lambda e: self.placehold_entry(frame.image_entry))
|
|
390
|
+
frame.image_entry.bind('<Return>', lambda e: self.open_files(filepaths=frame.image_entry.get()))
|
|
391
|
+
frame.image_entry.dnd_bind('<<Drop>>', self.open_files)
|
|
392
|
+
frame.mask_entry.bind('<Button-1>', lambda e:self.clean_entry(frame.mask_entry, is_mask=True))
|
|
393
|
+
frame.mask_entry.bind('<FocusOut>', lambda e: self.placehold_entry(frame.mask_entry, is_mask=True))
|
|
394
|
+
frame.mask_entry.bind('<Return>', lambda e: self.open_files(filepaths=frame.mask_entry.get(), is_mask=True))
|
|
395
|
+
frame.mask_entry.bind(f'<{CMD_KEY}-n>', lambda e: self.convert_dicom_and_open(input_filepath=frame.image_entry.get(),
|
|
396
|
+
output_dirpath=frame.mask_entry.get()))
|
|
397
|
+
frame.mask_entry.dnd_bind('<<Drop>>', partial(self.open_files, is_mask=True))
|
|
398
|
+
|
|
399
|
+
def set_options_events(self):
|
|
400
|
+
frame = self.sidebar_frame.options_frame
|
|
401
|
+
# TAB: MAIN
|
|
402
|
+
frame.height_spinbox.config(command=lambda: self.set_height(int(frame.height_spinbox.get())))
|
|
403
|
+
frame.height_spinbox.bind('<Return>', lambda e: self.set_height(int(frame.height_spinbox.get())))
|
|
404
|
+
frame.layout_options.bind('<<ComboboxSelected>>', lambda e: self.update_config('layout', frame.layout_options.get()))
|
|
405
|
+
frame.layout_options.bind('<Return>', lambda e: self.update_config('layout', frame.layout_options.get()))
|
|
406
|
+
frame.cmap_options.bind('<<ComboboxSelected>>', lambda e: self.set_cmap(frame.cmap_options.get()))
|
|
407
|
+
frame.cmap_options.bind('<Return>', lambda e: self.set_cmap(frame.cmap_options.get()))
|
|
408
|
+
frame.cmap_mask_options.bind('<<ComboboxSelected>>', lambda e: self.set_cmap(frame.cmap_mask_options.get(), is_mask=True))
|
|
409
|
+
frame.cmap_mask_options.bind('<Return>', lambda e: self.set_cmap(frame.cmap_mask_options.get(), is_mask=True))
|
|
410
|
+
frame.alpha_spinbox.config(command=lambda: self.update_config('alpha', float(frame.alpha_spinbox.get()) / 100))
|
|
411
|
+
frame.alpha_spinbox.bind('<Return>', lambda e: self.update_config('alpha', float(frame.alpha_spinbox.get()) / 100))
|
|
412
|
+
frame.max_samples_spinbox.config(command=lambda: self.set_max_samples(int(frame.max_samples_spinbox.get())))
|
|
413
|
+
frame.max_samples_spinbox.bind('<Return>', lambda e: self.set_max_samples(int(frame.max_samples_spinbox.get())))
|
|
414
|
+
frame.crosshair_checkbox.config(command=lambda: self.update_config('crosshair', switch=True))
|
|
415
|
+
# TAB: IMAGE
|
|
416
|
+
frame.equal_hist_checkbox.config(command=partial(self.update_config, attribute='equal_hist', event=frame.equal_hist_check, switch=True))
|
|
417
|
+
frame.qrange_start_spinbox.config(command=lambda: self.set_quantile_range(float(frame.qrange_start_spinbox.get())))
|
|
418
|
+
frame.qrange_start_spinbox.bind('<Return>', lambda e: self.set_quantile_range(float(frame.qrange_start_spinbox.get())))
|
|
419
|
+
frame.qrange_stop_spinbox.config(command=lambda: self.set_quantile_range(float(frame.qrange_stop_spinbox.get()), stop=True))
|
|
420
|
+
frame.qrange_stop_spinbox.bind('<Return>', lambda e: self.set_quantile_range(float(frame.qrange_stop_spinbox.get()), stop=True))
|
|
421
|
+
frame.vrange_start_spinbox.config(command=lambda: self.set_value_range(float(frame.vrange_start_spinbox.get())))
|
|
422
|
+
frame.vrange_start_spinbox.bind('<Return>', lambda e: self.set_value_range(float(frame.vrange_start_spinbox.get())))
|
|
423
|
+
frame.vrange_stop_spinbox.config(command=lambda: self.set_value_range(float(frame.vrange_stop_spinbox.get()), stop=True))
|
|
424
|
+
frame.vrange_stop_spinbox.bind('<Return>', lambda e: self.set_value_range(float(frame.vrange_stop_spinbox.get()), stop=True))
|
|
425
|
+
frame.transp_if_entry.bind('<Return>', self.set_transp_if)
|
|
426
|
+
frame.resizing_options.bind('<<ComboboxSelected>>', lambda e: self.update_config('resizing', RESIZINGS.index(frame.resizing_options.get())))
|
|
427
|
+
frame.resizing_options.bind('<Return>', lambda e: self.update_config('resizing', RESIZINGS.index(frame.resizing_options.get())))
|
|
428
|
+
# TAB: MASK
|
|
429
|
+
frame.qrange_start_mask_spinbox.config(command=lambda: self.set_quantile_range(float(frame.qrange_start_mask_spinbox.get()), is_mask=True))
|
|
430
|
+
frame.qrange_start_mask_spinbox.bind('<Return>', lambda e: self.set_quantile_range(float(frame.qrange_start_mask_spinbox.get()), is_mask=True))
|
|
431
|
+
frame.qrange_stop_mask_spinbox.config(command=lambda: self.set_quantile_range(float(frame.qrange_stop_mask_spinbox.get()), stop=True, is_mask=True))
|
|
432
|
+
frame.qrange_stop_mask_spinbox.bind('<Return>', lambda e: self.set_quantile_range(float(frame.qrange_stop_mask_spinbox.get()), stop=True, is_mask=True))
|
|
433
|
+
frame.vrange_start_mask_spinbox.config(command=lambda: self.set_value_range(float(frame.vrange_start_mask_spinbox.get()), is_mask=True))
|
|
434
|
+
frame.vrange_start_mask_spinbox.bind('<Return>', lambda e: self.set_value_range(float(frame.vrange_start_mask_spinbox.get()), is_mask=True))
|
|
435
|
+
frame.vrange_stop_mask_spinbox.config(command=lambda: self.set_value_range(float(frame.vrange_stop_mask_spinbox.get()), stop=True, is_mask=True))
|
|
436
|
+
frame.vrange_stop_mask_spinbox.bind('<Return>', lambda e: self.set_value_range(float(frame.vrange_stop_mask_spinbox.get()), stop=True, is_mask=True))
|
|
437
|
+
frame.transp_if_mask_entry.bind('<Return>', lambda e: self.set_transp_if(is_mask=True))
|
|
438
|
+
frame.resizing_mask_options.bind('<<ComboboxSelected>>', lambda e: self.update_config('resizing', RESIZINGS.index(frame.resizing_mask_options.get()), is_mask=True))
|
|
439
|
+
frame.resizing_mask_options.bind('<Return>', lambda e: self.update_config('resizing', RESIZINGS.index(frame.resizing_mask_options.get()), is_mask=True))
|
|
440
|
+
frame.is_atlas_checkbox.config(command=self.set_is_atlas)
|
|
441
|
+
# TAB: OVERLAY
|
|
442
|
+
frame.coordinates_checkbox.config(command=lambda: self.update_config('coordinates', switch=True))
|
|
443
|
+
frame.header_checkbox.config(command=lambda: self.update_config('header', switch=True))
|
|
444
|
+
frame.histogram_checkbox.config(command=lambda: self.update_config('histogram', switch=True))
|
|
445
|
+
frame.cbar_options.bind('<<ComboboxSelected>>', lambda e: self.set_cbar(frame.cbar_options.get()))
|
|
446
|
+
frame.fpath_spinbox.config(command=lambda: self.update_config('fpath', int(frame.fpath_spinbox.get())))
|
|
447
|
+
frame.fpath_spinbox.bind('<Return>', lambda e: self.update_config('fpath', int(frame.fpath_spinbox.get())))
|
|
448
|
+
frame.title_entry.bind('<Return>', lambda e: self.set_title(frame.title_entry.get()))
|
|
449
|
+
frame.fontsize_spinbox.config(command=lambda: self.update_config('fontsize', int(frame.fontsize_spinbox.get())))
|
|
450
|
+
frame.fontsize_spinbox.bind('<Return>', lambda e: self.update_config('fontsize', int(frame.fontsize_spinbox.get())))
|
|
451
|
+
# TAB: COLORBAR
|
|
452
|
+
frame.cbar_x_spinbox.config(command=lambda: self.update_config('cbar_x', float(frame.cbar_x_spinbox.get()) / 100))
|
|
453
|
+
frame.cbar_x_spinbox.bind('<Return>', lambda e: self.update_config('cbar_x', float(frame.cbar_x_spinbox.get()) / 100))
|
|
454
|
+
frame.cbar_y_spinbox.config(command=lambda: self.update_config('cbar_y', float(frame.cbar_y_spinbox.get()) / 100))
|
|
455
|
+
frame.cbar_y_spinbox.bind('<Return>', lambda e: self.update_config('cbar_y', float(frame.cbar_y_spinbox.get()) / 100))
|
|
456
|
+
frame.cbar_width_spinbox.config(command=lambda: self.update_config('cbar_width', float(frame.cbar_width_spinbox.get()) / 100))
|
|
457
|
+
frame.cbar_width_spinbox.bind('<Return>', lambda e: self.update_config('cbar_width', float(frame.cbar_width_spinbox.get()) / 100))
|
|
458
|
+
frame.cbar_length_spinbox.config(command=lambda: self.update_config('cbar_length', float(frame.cbar_length_spinbox.get()) / 100))
|
|
459
|
+
frame.cbar_length_spinbox.bind('<Return>', lambda e: self.update_config('cbar_length', float(frame.cbar_length_spinbox.get()) / 100))
|
|
460
|
+
frame.cbar_pad_spinbox.config(command=lambda: self.update_config('cbar_pad', int(frame.cbar_pad_spinbox.get())))
|
|
461
|
+
frame.cbar_pad_spinbox.bind('<Return>', lambda e: self.update_config('cbar_pad', int(frame.cbar_pad_spinbox.get())))
|
|
462
|
+
frame.cbar_label_entry.bind('<Return>', lambda e: self.update_config('cbar_label', frame.cbar_label_entry.get()))
|
|
463
|
+
frame.cbar_ticks_entry.bind('<Return>', self.set_cbar_ticks)
|
|
464
|
+
|
|
465
|
+
@property
|
|
466
|
+
def niigrid(self):
|
|
467
|
+
return [self.niigrid1, self.niigrid2][self.config.view - 1]
|
|
468
|
+
|
|
469
|
+
def load_niigrid(self):
|
|
470
|
+
setattr(self, f'niigrid{self.config.view}', NiftiImageGrid(self.config.get_filepaths()))
|
|
471
|
+
other_view = {1: 2, 2: 1}[self.config.view]
|
|
472
|
+
if getattr(self, f'niigrid{other_view}') is not None:
|
|
473
|
+
setattr(self, f'niigrid{other_view}', NiftiImageGrid(self.config.get_filepaths(view=other_view)))
|
|
474
|
+
|
|
475
|
+
def clean_entry(self, entry, is_mask=False):
|
|
476
|
+
prefix = 'mask' if is_mask else 'image'
|
|
477
|
+
if entry.get() == PATH_PLACEHOLDER[prefix]:
|
|
478
|
+
entry.delete(0, 'end')
|
|
479
|
+
|
|
480
|
+
def placehold_entry(self, entry, is_mask=False):
|
|
481
|
+
prefix = 'mask' if is_mask else 'image'
|
|
482
|
+
if entry.get() == '':
|
|
483
|
+
entry.insert(0, PATH_PLACEHOLDER[prefix])
|
|
484
|
+
|
|
485
|
+
def set_view(self, event=None):
|
|
486
|
+
self.sidebar_frame.view_button.configure(text=f'Switch to view {self.config.view}')
|
|
487
|
+
self.config.view = 2 if self.config.view == 1 else 1
|
|
488
|
+
if getattr(self, f'niigrid{self.config.view}') is None:
|
|
489
|
+
self.load_niigrid()
|
|
490
|
+
self.update_image()
|
|
491
|
+
|
|
492
|
+
def clear_masks(self):
|
|
493
|
+
self.config.remove_mask_layers()
|
|
494
|
+
self.load_niigrid()
|
|
495
|
+
self.update_image()
|
|
496
|
+
|
|
497
|
+
def add_sliders_commands(self, sliders):
|
|
498
|
+
for i, plane in enumerate(PLANES_4D):
|
|
499
|
+
sliders[plane].bind('<Button-4>', partial(self.update_origin_scroll, plane=plane))
|
|
500
|
+
sliders[plane].bind('<Button-5>', partial(self.update_origin_scroll, plane=plane, scroll_up=False))
|
|
501
|
+
sliders[plane].bind('<Button-1>', lambda e: self.update_origin(hd=False))
|
|
502
|
+
sliders[plane].bind('<B1-Motion>', lambda e: self.update_origin(hd=False))
|
|
503
|
+
sliders[plane].bind('<ButtonRelease-1>', lambda e: self.update_origin(hd=True))
|
|
504
|
+
|
|
505
|
+
def set_max_samples(self, max_samples):
|
|
506
|
+
self.config.set_max_samples(max_samples)
|
|
507
|
+
self.load_niigrid()
|
|
508
|
+
self.update_image()
|
|
509
|
+
self.focus_set()
|
|
510
|
+
|
|
511
|
+
def set_annotation_buttons(self):
|
|
512
|
+
self.config.annotations = not self.config.annotations
|
|
513
|
+
if len(self.annotation_buttons) > 0:
|
|
514
|
+
self.destroy_annotation_buttons()
|
|
515
|
+
else:
|
|
516
|
+
self.create_annotation_buttons()
|
|
517
|
+
|
|
518
|
+
def destroy_annotation_buttons(self):
|
|
519
|
+
if len(self.annotation_buttons) > 0:
|
|
520
|
+
for i in range(len(self.annotation_buttons)):
|
|
521
|
+
self.annotation_buttons[i].destroy()
|
|
522
|
+
self.annotation_buttons = []
|
|
523
|
+
|
|
524
|
+
def create_annotation_buttons(self, annotations_=('0', '1', '2')):
|
|
525
|
+
self.annotation_buttons = []
|
|
526
|
+
for nimage, box in zip(self.niigrid1.niis, self.image_grid_boxes):
|
|
527
|
+
button = ttk.Combobox(self.image_frame, values=annotations_)
|
|
528
|
+
button.bind('<<ComboboxSelected>>', partial(self.set_annotation, filepath=nimage.nics[0].filepath))
|
|
529
|
+
button.bind('<Return>', partial(self.set_annotation, filepath=nimage.nics[0].filepath))
|
|
530
|
+
button.set(self.config.annotation_dict[nimage.nics[0].filepath])
|
|
531
|
+
button.place(x=box[2], y=box[1], anchor='ne')
|
|
532
|
+
self.annotation_buttons.append(button)
|
|
533
|
+
|
|
534
|
+
def set_annotation(self, event, filepath):
|
|
535
|
+
self.config.annotation_dict.update({filepath: event.widget.get()})
|
|
536
|
+
|
|
537
|
+
def set_cmap(self, event, is_mask=False):
|
|
538
|
+
if event == 'CATALOG':
|
|
539
|
+
open_new_tab('https://cmap-docs.readthedocs.io/en/latest/catalog/')
|
|
540
|
+
else:
|
|
541
|
+
self.update_config('cmap', event, is_mask)
|
|
542
|
+
|
|
543
|
+
def set_height(self, height):
|
|
544
|
+
if self.master.is_fullscreen:
|
|
545
|
+
self.update_config('height', height)
|
|
546
|
+
else:
|
|
547
|
+
width = self.image.size[0] * height / self.image.size[1] + self.sidebar_frame.winfo_width()
|
|
548
|
+
self.master.geometry(f'{int(width) + 1}x{height + self.menu.winfo_height() + 1}')
|
|
549
|
+
self.focus_set()
|
|
550
|
+
|
|
551
|
+
def set_title(self, title):
|
|
552
|
+
self.config.set_title(title)
|
|
553
|
+
self.update_image()
|
|
554
|
+
self.focus_set()
|
|
555
|
+
|
|
556
|
+
def unset_title(self):
|
|
557
|
+
self.sidebar_frame.options_frame.title_entry.delete(0, 'end')
|
|
558
|
+
self.config.set_title('')
|
|
559
|
+
self.update_image()
|
|
560
|
+
self.focus_set()
|
|
561
|
+
|
|
562
|
+
def set_equal_hist(self, event=None):
|
|
563
|
+
self.sidebar_frame.options_frame.equal_hist_check.set(not self.config.equal_hist)
|
|
564
|
+
self.update_config('equal_hist', switch=True)
|
|
565
|
+
|
|
566
|
+
def set_transp_if(self, event=None, is_mask=False):
|
|
567
|
+
substr = '_mask' if is_mask else ''
|
|
568
|
+
transp_if = getattr(self.sidebar_frame.options_frame, f'transp_if{substr}_entry').get()
|
|
569
|
+
transp_if = None if transp_if == '' else transp_if
|
|
570
|
+
self.update_config('transp_if', transp_if, is_mask=is_mask)
|
|
571
|
+
|
|
572
|
+
def set_quantile_range(self, event, is_mask=False, stop=False, increment=None):
|
|
573
|
+
if self.config.qrange[-1 if is_mask else 0] is None:
|
|
574
|
+
qrange = list(QRANGE[int(is_mask)])
|
|
575
|
+
else:
|
|
576
|
+
qrange = list(self.config.qrange[-1 if is_mask else 0])
|
|
577
|
+
qrange[int(stop)] = event / 100 if increment is None else qrange[int(stop)] + increment / 100
|
|
578
|
+
qrange[int(stop)] = min(max(0, qrange[int(stop)]), 1)
|
|
579
|
+
self.update_config('qrange', qrange, is_mask)
|
|
580
|
+
value_range = self.niigrid.niis[0].cmaps[-1 if is_mask else 0].vrange
|
|
581
|
+
self.config.set_layer_attribute('vrange', None, is_mask) # Setting vrange to None such that qrange has effect
|
|
582
|
+
if is_mask:
|
|
583
|
+
self.sidebar_frame.options_frame.vrange_start_mask_spinbox.set(value_range[0])
|
|
584
|
+
self.sidebar_frame.options_frame.vrange_stop_mask_spinbox.set(value_range[-1])
|
|
585
|
+
if increment is not None:
|
|
586
|
+
self.sidebar_frame.options_frame.qrange_start_mask_spinbox.set(qrange[0] * 100)
|
|
587
|
+
self.sidebar_frame.options_frame.qrange_stop_mask_spinbox.set(qrange[1] * 100)
|
|
588
|
+
else:
|
|
589
|
+
self.sidebar_frame.options_frame.vrange_start_spinbox.set(value_range[0])
|
|
590
|
+
self.sidebar_frame.options_frame.vrange_stop_spinbox.set(value_range[-1])
|
|
591
|
+
if increment is not None:
|
|
592
|
+
self.sidebar_frame.options_frame.qrange_start_spinbox.set(qrange[0] * 100)
|
|
593
|
+
self.sidebar_frame.options_frame.qrange_stop_spinbox.set(qrange[1] * 100)
|
|
594
|
+
|
|
595
|
+
def set_value_range(self, event, is_mask=False, stop=False):
|
|
596
|
+
vrange = self.niigrid.niis[0].cmaps[-1 if is_mask else 0].vrange
|
|
597
|
+
vrange[-1 if stop else 0] = event
|
|
598
|
+
self.update_config('vrange', vrange, is_mask)
|
|
599
|
+
|
|
600
|
+
def set_is_atlas(self):
|
|
601
|
+
is_atlas = self.config.is_atlas
|
|
602
|
+
self.update_config('is_atlas', not is_atlas[-1], is_mask=True)
|
|
603
|
+
|
|
604
|
+
def set_cbar(self, event):
|
|
605
|
+
self.config.cbar_vertical = event == 'vertical'
|
|
606
|
+
self.update_config('cbar', event != '')
|
|
607
|
+
|
|
608
|
+
def set_cbar_ticks(self, event):
|
|
609
|
+
self.config.set_cbar_ticks(event.widget.get())
|
|
610
|
+
self.update_image()
|
|
611
|
+
self.focus_set()
|
|
612
|
+
|
|
613
|
+
def set_page(self, next=False):
|
|
614
|
+
page = self.config.page + 1 if next else self.config.page - 1
|
|
615
|
+
if page in list(range(self.config.n_pages)):
|
|
616
|
+
self.config.page = page
|
|
617
|
+
self.load_niigrid()
|
|
618
|
+
self.update_image()
|
|
619
|
+
self.sidebar_frame.pages_frame.page_label.configure(text=f'Page {page + 1} of {self.config.n_pages}')
|
|
620
|
+
if self.config.annotations:
|
|
621
|
+
self.destroy_annotation_buttons()
|
|
622
|
+
self.create_annotation_buttons()
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
def update_config(self, attribute, event=None, is_mask=False, switch=False):
|
|
626
|
+
if attribute in LAYER_ATTRIBUTES:
|
|
627
|
+
self.config.set_layer_attribute(attribute, event, is_mask)
|
|
628
|
+
else:
|
|
629
|
+
setattr(self.config, attribute, not getattr(self.config, attribute) if switch else event)
|
|
630
|
+
self.update_image()
|
|
631
|
+
self.focus_set()
|
|
632
|
+
|
|
633
|
+
def convert_dicom_and_open(self, input_filepath=None, output_dirpath=None):
|
|
634
|
+
filepaths = dcm2nii(input_filepath, output_dirpath)
|
|
635
|
+
if len(filepaths) > 0:
|
|
636
|
+
image_path, mask_path = f'{output_dirpath}/*.ni*', ''
|
|
637
|
+
self.sidebar_frame.input_frame.image_entry.insert('0', image_path)
|
|
638
|
+
self.sidebar_frame.input_frame.image_entry.delete(len(image_path), 'end')
|
|
639
|
+
self.sidebar_frame.input_frame.mask_entry.insert('0', mask_path)
|
|
640
|
+
self.sidebar_frame.input_frame.mask_entry.delete(len(mask_path), 'end')
|
|
641
|
+
self.open_files(filepaths=filepaths)
|
|
642
|
+
|
|
643
|
+
def open_files(self, event=None, filepaths=None, is_mask=False, title='Open Nifti Files', dropdown=False):
|
|
644
|
+
if filepaths is None:
|
|
645
|
+
if isinstance(event, TkinterDnD.DnDEvent):
|
|
646
|
+
filepaths = parse_dnd_filepaths(event.data)
|
|
647
|
+
else:
|
|
648
|
+
filepaths = filedialog.askopenfilenames(title=title, filetypes=[('All Files', '*.*')])
|
|
649
|
+
if filepaths:
|
|
650
|
+
self.unset_title()
|
|
651
|
+
self.config.add_filepaths(filepaths, is_mask)
|
|
652
|
+
self.load_niigrid()
|
|
653
|
+
self.update_image()
|
|
654
|
+
self.focus_set()
|
|
655
|
+
|
|
656
|
+
def remove_mask_layers(self):
|
|
657
|
+
self.config.remove_mask_layers()
|
|
658
|
+
setattr(self, f'niigrid{self.config.view}', NiftiImageGrid(self.config.get_filepaths()))
|
|
659
|
+
self.update_image()
|
|
660
|
+
|
|
661
|
+
def update_origin_click(self, event, hd=True):
|
|
662
|
+
if 0 <= event.x < self.image.size[0] and 0 <= event.y < self.image.size[1]:
|
|
663
|
+
for plane, v in zip(PLANES, self.image_origin_coords[event.x, event.y]):
|
|
664
|
+
if not np.isnan(v):
|
|
665
|
+
self.sidebar_frame.sliders_frame.sliders[plane].set(v)
|
|
666
|
+
self.update_origin(hd)
|
|
667
|
+
|
|
668
|
+
def update_origin_scroll(self, event=None, plane=None, scroll_up=True, scroll_speed=1, hd=True):
|
|
669
|
+
if (not isinstance(self.focus_displayof(), (ttk.Spinbox, ttk.Entry, ttk.Combobox))) and event.type == EventType.KeyPress:
|
|
670
|
+
value = self.config.origin[PLANES_4D.index(plane)] + (1 if scroll_up else -1) * scroll_speed
|
|
671
|
+
self.sidebar_frame.sliders_frame.sliders[plane].set(value)
|
|
672
|
+
self.update_origin(hd)
|
|
673
|
+
|
|
674
|
+
def update_origin(self, hd=True):
|
|
675
|
+
self.config.origin = [self.sidebar_frame.sliders_frame.sliders[p].get() for p in PLANES_4D]
|
|
676
|
+
self.update_image(hd)
|
|
677
|
+
|
|
678
|
+
def update_image(self, hd=True):
|
|
679
|
+
self.image = self.get_image(hd)
|
|
680
|
+
self.update_overlay_and_annotations()
|
|
681
|
+
tk_image = ImageTk.PhotoImage(self.image)
|
|
682
|
+
self.image_label.config(image=tk_image)
|
|
683
|
+
self.image_label.image = tk_image
|
|
684
|
+
if hasattr(self, 'sidebar_frame'):
|
|
685
|
+
self.update_sidebar()
|
|
686
|
+
|
|
687
|
+
def get_image(self, hd=True):
|
|
688
|
+
config_dict = self.config.to_dict(grid_kwargs_only=True, hd=hd)
|
|
689
|
+
return self.niigrid.get_image(**config_dict)
|
|
690
|
+
|
|
691
|
+
def update_overlay_and_annotations(self):
|
|
692
|
+
updated_grid_boxes = self.niigrid.boxes
|
|
693
|
+
if self.image_grid_boxes is None or updated_grid_boxes != self.image_grid_boxes:
|
|
694
|
+
self.image_grid_boxes = updated_grid_boxes
|
|
695
|
+
self.image_grid_numbers = self.get_grid_numbers()
|
|
696
|
+
self.image_origin_coords = self.get_origin_coordinates()
|
|
697
|
+
if self.config.annotations:
|
|
698
|
+
self.destroy_annotation_buttons()
|
|
699
|
+
self.create_annotation_buttons()
|
|
700
|
+
|
|
701
|
+
def update_sidebar(self):
|
|
702
|
+
if hasattr(self.sidebar_frame.options_frame, 'tabview'):
|
|
703
|
+
frame = self.sidebar_frame
|
|
704
|
+
nimage = self.niigrid.niis[0]
|
|
705
|
+
frame.options_frame.vrange_start_spinbox.set(nimage.cmaps[0].vrange[0])
|
|
706
|
+
frame.options_frame.vrange_stop_spinbox.set(nimage.cmaps[0].vrange[-1])
|
|
707
|
+
frame.options_frame.resizing_options.set(RESIZINGS[self.config.resizing[0]])
|
|
708
|
+
frame.options_frame.resizing_mask_options.set(RESIZINGS[self.config.resizing[-1]])
|
|
709
|
+
if self.config.n_layers > 1:
|
|
710
|
+
frame.options_frame.vrange_start_mask_spinbox.set(nimage.cmaps[-1].vrange[0])
|
|
711
|
+
frame.options_frame.vrange_stop_mask_spinbox.set(nimage.cmaps[-1].vrange[-1])
|
|
712
|
+
if hasattr(self.sidebar_frame, 'pages_frame'):
|
|
713
|
+
page = min(self.config.page, self.config.n_pages - 1)
|
|
714
|
+
self.sidebar_frame.pages_frame.page_label.configure(text=f'Page {page + 1} of {self.config.n_pages}')
|
|
715
|
+
|
|
716
|
+
def set_image_overlay(self, event, remove_overlay=False):
|
|
717
|
+
tk_image = ImageTk.PhotoImage(self.image, size=self.image.size)
|
|
718
|
+
if not remove_overlay and 0 <= event.x < self.image.size[0] and 0 <= event.y < self.image.size[1]:
|
|
719
|
+
box_number = self.image_grid_numbers[event.x, event.y]
|
|
720
|
+
if 0 <= box_number < len(self.image_grid_boxes) and len(self.image_grid_boxes) > 1:
|
|
721
|
+
box = self.image_grid_boxes[box_number]
|
|
722
|
+
box_frame = Image.fromarray(get_window_frame(size=(box[2] - box[0], box[3] - box[1])))
|
|
723
|
+
alpha = Image.new('L', self.image.size, 255)
|
|
724
|
+
alpha.paste(box_frame, self.image_grid_boxes[box_number])
|
|
725
|
+
im = Image.composite(self.image, Image.new(self.image.mode, self.image.size, 'white'), alpha)
|
|
726
|
+
tk_image = ImageTk.PhotoImage(im, size=self.image.size)
|
|
727
|
+
self.image_label.configure(image=tk_image)
|
|
728
|
+
self.image_label.image = tk_image
|
|
729
|
+
|
|
730
|
+
def get_grid_numbers(self):
|
|
731
|
+
numbers = -np.ones(self.image.size, dtype=np.int16)
|
|
732
|
+
for i, box in enumerate(self.image_grid_boxes):
|
|
733
|
+
numbers[box[0]:box[2], box[1]:box[3]] = i
|
|
734
|
+
return numbers
|
|
735
|
+
|
|
736
|
+
def get_origin_coordinates(self):
|
|
737
|
+
coords = np.zeros((*self.image.size, 3), dtype=np.float32)
|
|
738
|
+
for nii, grid_box in zip(self.niigrid.niis, self.niigrid.boxes):
|
|
739
|
+
bounds = nii.nics[0].get_origin_bounds(self.config.coord_sys)
|
|
740
|
+
for i, kw in enumerate(nii.nics[0]._image_props):
|
|
741
|
+
size = kw['size']
|
|
742
|
+
dim = PLANES.index(kw['plane'])
|
|
743
|
+
x, y = [ii for ii in range(3) if ii != dim]
|
|
744
|
+
box_coords = np.meshgrid(*[np.linspace(bounds[0, x], bounds[1, x], size[0]),
|
|
745
|
+
np.linspace(bounds[1, y], bounds[0, y], size[1])], indexing='ij', copy=False)
|
|
746
|
+
box_coords = np.stack(box_coords, axis=-1)
|
|
747
|
+
box_coords = np.insert(box_coords, dim, np.nan, axis=-1)
|
|
748
|
+
box = grid_box[0] + kw['box'][0], grid_box[1] + kw['box'][1]
|
|
749
|
+
coords[box[0]:box[0] + size[0], box[1]:box[1] + size[1]] = box_coords
|
|
750
|
+
return coords
|
|
751
|
+
|
|
752
|
+
def save_image(self, filetype):
|
|
753
|
+
extension = filetype[1].split(';')[0][1:]
|
|
754
|
+
filepath = filedialog.asksaveasfilename(defaultextension=extension, filetypes=[filetype])
|
|
755
|
+
if filepath:
|
|
756
|
+
config_dict = self.config.to_dict(grid_kwargs_only=True, hd=True)
|
|
757
|
+
if extension in ['.png', '.jpg', '.jpeg', '.tif', '.tiff']:
|
|
758
|
+
image = self.niigrid.get_image(**config_dict)
|
|
759
|
+
image.save(filepath)
|
|
760
|
+
else:
|
|
761
|
+
self.niigrid.save_image(filepath, **config_dict)
|
|
762
|
+
|
|
763
|
+
def save_gif(self):
|
|
764
|
+
filepath = filedialog.asksaveasfilename(defaultextension='.gif',
|
|
765
|
+
filetypes=[('Graphics Interchange Format', '*.gif')])
|
|
766
|
+
if filepath:
|
|
767
|
+
config_dict = self.config.to_dict(grid_kwargs_only=True, hd=True)
|
|
768
|
+
save_gif(self.niigrid, filepath, duration=50, loop=0, start=None, stop=None, **config_dict)
|
|
769
|
+
|
|
770
|
+
def save_all_images_or_gifs(self, gif=False):
|
|
771
|
+
dirpath = filedialog.askdirectory()
|
|
772
|
+
if dirpath:
|
|
773
|
+
config_dict = self.config.to_dict(grid_kwargs_only=True, hd=True)
|
|
774
|
+
save_images_or_gifs(self.config.filepaths, dirpath, gif, self.config.max_samples, **config_dict)
|
|
775
|
+
|
|
776
|
+
def save_config(self):
|
|
777
|
+
filepath = filedialog.asksaveasfilename(defaultextension='.json', filetypes=[('JSON Files', '.json')])
|
|
778
|
+
if filepath:
|
|
779
|
+
self.config.save(filepath)
|
|
780
|
+
|
|
781
|
+
def load_config(self):
|
|
782
|
+
filepath = filedialog.askopenfilename(title='Open Config File', filetypes=[('JSON Files', '.json')])
|
|
783
|
+
if filepath:
|
|
784
|
+
self.config = Config.from_json(filepath)
|
|
785
|
+
self.load_niigrid()
|
|
786
|
+
self.update_image()
|
|
787
|
+
|
|
788
|
+
def save_annotations(self):
|
|
789
|
+
filepath = filedialog.asksaveasfilename(defaultextension='.csv', filetypes=[('Comma-separated values', '.csv')])
|
|
790
|
+
if filepath:
|
|
791
|
+
self.config.save_annotations(filepath)
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
class ToplevelWindow(Toplevel):
|
|
795
|
+
def __init__(self, *args, **kwargs):
|
|
796
|
+
config = kwargs.pop('config')
|
|
797
|
+
super().__init__(*args, **kwargs)
|
|
798
|
+
self.title('NiftiView')
|
|
799
|
+
set_icon(self)
|
|
800
|
+
self.is_fullscreen = False
|
|
801
|
+
self.mainframe = MainFrame(self, config=config, toplevel=True)
|
|
802
|
+
self.mainframe.pack(anchor='nw', fill='both', expand=True)
|
|
803
|
+
add_key_bindings(self)
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
class NiftiView(Tk):
|
|
807
|
+
def __init__(self, config=None):
|
|
808
|
+
super().__init__()
|
|
809
|
+
self.title('NiftiView')
|
|
810
|
+
set_icon(self)
|
|
811
|
+
sv_ttk.set_theme(config.appearance_mode)
|
|
812
|
+
self.is_fullscreen = False
|
|
813
|
+
self.mainframe = MainFrame(self, config=config, toplevel=False)
|
|
814
|
+
self.mainframe.pack(anchor='nw', fill='both', expand=True)
|
|
815
|
+
self.toplevel_window = None
|
|
816
|
+
self.mainframe.image_label.bind('<Double-Button-1>', self.set_toplevel_window)
|
|
817
|
+
self.bind(f'<{CMD_KEY}-BackSpace>', lambda e: self.mainframe.set_page(next=False))
|
|
818
|
+
self.bind(f'<Button-3>', lambda e: self.mainframe.set_page(next=True))
|
|
819
|
+
add_key_bindings(self)
|
|
820
|
+
|
|
821
|
+
def set_toplevel_window(self, event):
|
|
822
|
+
if self.toplevel_window is not None:
|
|
823
|
+
self.toplevel_window.destroy()
|
|
824
|
+
if 0 <= event.x < self.mainframe.image.size[0] and 0 <= event.y < self.mainframe.image.size[1]:
|
|
825
|
+
window_number = self.mainframe.image_grid_numbers[event.x, event.y]
|
|
826
|
+
config = deepcopy(self.mainframe.config)
|
|
827
|
+
fpaths = config.get_filepaths()
|
|
828
|
+
if 0 <= window_number < len(fpaths):
|
|
829
|
+
setattr(config, f'filepaths_view{config.view}', [fpaths[window_number]])
|
|
830
|
+
self.toplevel_window = ToplevelWindow(self, config=config)
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
def add_key_bindings(app):
|
|
834
|
+
app.bind(f'<{CMD_KEY}-a>', lambda e: app.mainframe.set_quantile_range(None, increment=-5))
|
|
835
|
+
app.bind(f'<{CMD_KEY}-d>', lambda e: app.mainframe.set_quantile_range(None, increment=5))
|
|
836
|
+
app.bind(f'<{CMD_KEY}-s>', lambda e: app.mainframe.set_quantile_range(None, increment=-1, stop=True))
|
|
837
|
+
app.bind(f'<{CMD_KEY}-w>', lambda e: app.mainframe.set_quantile_range(None, increment=1, stop=True))
|
|
838
|
+
app.bind(f'<{CMD_KEY}-Return>', lambda e: app.mainframe.set_equal_hist())
|
|
839
|
+
app.bind('<Escape>', lambda e: app.wm_attributes('-fullscreen', False))
|
|
840
|
+
app.bind('<Configure>', debounce(app, partial(resize_window, app)))
|
|
841
|
+
app.bind(f'<{CMD_KEY}-space>', lambda e: app.mainframe.update_config('alpha', 0.))
|
|
842
|
+
app.bind(f'<{CMD_KEY}-KeyRelease-space>', lambda e: app.mainframe.update_config('alpha', float(app.mainframe.sidebar_frame.options_frame.alpha_spinbox.get()) / 100))
|
|
843
|
+
for plane, keys in zip(PLANES_4D, [('Left', 'Right'), (f'{CMD_KEY}-Left', f'{CMD_KEY}-Right'), ('Down', 'Up'), (f'{CMD_KEY}-Down', f'{CMD_KEY}-Up')]):
|
|
844
|
+
app.bind(f'<{keys[0]}>', partial(app.mainframe.update_origin_scroll, plane=plane, scroll_up=False, hd=False))
|
|
845
|
+
app.bind(f'<{keys[1]}>', partial(app.mainframe.update_origin_scroll, plane=plane, hd=False))
|
|
846
|
+
#app.bind(f'<KeyRelease-{keys[0]}>', partial(app.mainframe.update_origin_scroll, plane=plane, scroll_up=False))
|
|
847
|
+
#app.bind(f'<KeyRelease-{keys[1]}>', partial(app.mainframe.update_origin_scroll, plane=plane))
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
def resize_window(app, *args):
|
|
851
|
+
if str(args[0]._w) in ['.', '.!toplevelwindow']:
|
|
852
|
+
if not app.is_fullscreen:
|
|
853
|
+
size = [app.mainframe.winfo_width() - app.mainframe.sidebar_frame.winfo_width(), app.mainframe.winfo_height()]
|
|
854
|
+
ratio = size[0] / size[1]
|
|
855
|
+
size = [int(round(size[0])), int(round(size[1]))]
|
|
856
|
+
image_ratio = app.mainframe.image.size[0] / app.mainframe.image.size[1]
|
|
857
|
+
height = int(size[1] if ratio >= image_ratio else size[0] / image_ratio)
|
|
858
|
+
if height > 0 and abs(height - app.mainframe.image.size[1]) > 1:
|
|
859
|
+
app.mainframe.config.height = int(height)
|
|
860
|
+
app.mainframe.update_image(hd=False)
|
|
861
|
+
app.mainframe.sidebar_frame.options_frame.height_spinbox.set(height)
|
|
862
|
+
#app.after(30, app.mainframe.update_image(hd=True))
|
|
863
|
+
app.is_fullscreen = window_is_fullscreen_or_maximized(app)
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
def window_is_fullscreen_or_maximized(app):
|
|
867
|
+
return app.attributes('-fullscreen') or app.winfo_height() > app.winfo_screenheight() - 100
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
def set_icon(app):
|
|
871
|
+
app.iconpath = ImageTk.PhotoImage(file=f'{DATA_PATH}/niftiview.ico')
|
|
872
|
+
#app.wm_iconbitmap()
|
|
873
|
+
app.iconphoto(False, app.iconpath)
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
def main(filepaths=None):
|
|
877
|
+
config = Config.from_dict(CONFIG_DICT)
|
|
878
|
+
if len(argv) > 1:
|
|
879
|
+
config.add_filepaths(filepaths)
|
|
880
|
+
app = NiftiView(config)
|
|
881
|
+
app.mainloop()
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
if __name__ == '__main__':
|
|
885
|
+
main(filepaths=argv[1:] if len(argv) > 1 else None)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import glob
|
|
3
|
+
import warnings
|
|
4
|
+
import numpy as np
|
|
5
|
+
import importlib.resources
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from niftiview.utils import load_json
|
|
9
|
+
DATA_PATH = str(importlib.resources.files('niftiview_app')) + '/data'
|
|
10
|
+
CONFIG_DICT = load_json(f'{DATA_PATH}/config.json')
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def parse_dnd_filepaths(filepaths):
|
|
14
|
+
return [fp.strip('{}') for fp in re.findall(r'{[^}]*}|\S+', filepaths)]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def get_window_frame(size, exp=12):
|
|
18
|
+
x, y = np.meshgrid(np.linspace(-1, 1, size[0]), np.linspace(-1, 1, size[1]), indexing='xy', copy=False)
|
|
19
|
+
frame = (x ** exp + y ** exp) / 2
|
|
20
|
+
return (255 * (1 - frame)).astype(np.uint8)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def dcm2nii(input_filepath=None, output_dirpath=None):
|
|
24
|
+
import dcm2niix
|
|
25
|
+
if Path(input_filepath).is_dir() or (Path(input_filepath).is_file() and input_filepath.endswith('.dcm')):
|
|
26
|
+
if Path(output_dirpath).is_dir():
|
|
27
|
+
dcm2niix.main(['-o', output_dirpath, input_filepath])
|
|
28
|
+
else:
|
|
29
|
+
warnings.warn(f'{output_dirpath} is not an existing directory')
|
|
30
|
+
else:
|
|
31
|
+
warnings.warn(f'{input_filepath} is not a dicom file')
|
|
32
|
+
return sorted(glob.glob(f'{output_dirpath}/*.ni*'))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def debounce(app, func, wait=1):
|
|
36
|
+
def debounced(event):
|
|
37
|
+
def call_it():
|
|
38
|
+
func(app)
|
|
39
|
+
debounced._timer = None
|
|
40
|
+
if debounced._timer:
|
|
41
|
+
app.after_cancel(debounced._timer)
|
|
42
|
+
debounced._timer = app.after(wait, call_it)
|
|
43
|
+
debounced._timer = None
|
|
44
|
+
return debounced
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = 'niftiview-app'
|
|
3
|
+
version = '1.0.0'
|
|
4
|
+
description = 'Standalone app for viewing volumetric images 🧠 based on Python'
|
|
5
|
+
authors = ['codingfisch <l_fisc17@wwu.de>']
|
|
6
|
+
license = 'MIT'
|
|
7
|
+
readme = 'README.md'
|
|
8
|
+
repository = 'https://github.com/codingfisch/niftiview-app'
|
|
9
|
+
classifiers = [
|
|
10
|
+
'Programming Language :: Python :: 3',
|
|
11
|
+
'Operating System :: OS Independent',
|
|
12
|
+
'Intended Audience :: Science/Research'
|
|
13
|
+
]
|
|
14
|
+
include = ['data/*']
|
|
15
|
+
|
|
16
|
+
[tool.poetry.dependencies]
|
|
17
|
+
python = '^3.9'
|
|
18
|
+
niftiview = '>=1.0.0'
|
|
19
|
+
dcm2niix = '*'
|
|
20
|
+
sv-ttk = '*'
|
|
21
|
+
tkinterdnd2 = { version = "*", markers = "sys_platform != 'darwin'" }
|
|
22
|
+
tkinterdnd2-universal = { version = "*", markers = "sys_platform == 'darwin'" }
|
|
23
|
+
|
|
24
|
+
[tool.poetry.scripts]
|
|
25
|
+
niftiview-app = 'niftiview-app.main:main'
|
|
26
|
+
|
|
27
|
+
[build-system]
|
|
28
|
+
requires = ['poetry-core']
|
|
29
|
+
build-backend = 'poetry.core.masonry.api'
|