Skip to content
Merged
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
52 changes: 44 additions & 8 deletions src/smappy/calibrate/psf_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ def torch_missing():
f"into this Python; or, in a terminal:\n{shell_line(torch_install.command())}")


def torch_slow():
"""What the window says when torch works but leaves the NVIDIA card unused (`torch_install.replacement`),
with the command that brings the card's build; None otherwise."""
from .. import torch_install
reason = torch_install.replacement()
if reason is None:
return None
return (f"{reason}. Install PyTorch replaces it with the build for the card; or, in a terminal:\n"
f"{shell_line(torch_install.command(replace=True))}")


def learning_device() -> str:
""""on the graphics card (…)", "on Apple's GPU" or "on the CPU" (`uipsf.device.describe`); empty where torch
cannot be loaded, which the notice says already."""
try:
from uipsf.device import describe
return describe()
except Exception:
return ""


def shell_line(parts, windows: Optional[bool] = None) -> str:
"""``parts`` as a line to paste into a terminal.

Expand Down Expand Up @@ -351,6 +372,7 @@ def choice(label, items, slot):
self.install_button.clicked.connect(self.install_torch)
layout.addWidget(self.install_button)
self._installer = None
self._device = None # where the pupil model learns, found once (`learning_device`)

self.detect_button = QPushButton("Detect beads")
self.detect_button.setToolTip("find the beads, ring them on the Stacks page and say how long "
Expand Down Expand Up @@ -443,10 +465,12 @@ def _show_method(self) -> None:
form.set_shown({name: reads["averaging"] for name in AVERAGE_ONLY if name in shared})
pupil = self.method.key in ("pupil", "insitu", "molecules") or getattr(self.method, "needs_torch", False)
missing = torch_missing()
self.notice.setText(missing or "")
self.notice.setVisible(pupil and missing is not None)
# shown only while torch is missing or too old: nothing to press once it is right
self.install_button.setVisible(pupil and (missing is not None or self._installer is not None))
# a CPU build on a machine with an NVIDIA card works, slowly: said and offered, never blocking
note = missing or torch_slow()
self.notice.setText(note or "")
self.notice.setVisible(pupil and note is not None)
# shown only while torch is missing, too old or the CPU's beside a card: nothing to press once it is right
self.install_button.setVisible(pupil and (note is not None or self._installer is not None))
self.stop_button.setVisible(pupil)
self.browse_button.setVisible(self.method.has_stack)
self.toggle_button.setVisible(getattr(self.method, "marks", True))
Expand Down Expand Up @@ -1073,7 +1097,13 @@ def _show_detected_text(self) -> None:
"""The detection's line, with the time calibrating the beads as marked takes where the method knows
it (`detected_text`): excluding beads shortens it."""
text_of = getattr(self.method, "detected_text", None)
self.found_label.setText(text_of(self.detected, self.marks) if text_of else self.detected.text)
text = text_of(self.detected, self.marks) if text_of else self.detected.text
if self.method.key in ("pupil", "insitu", "molecules") or getattr(self.method, "needs_torch", False):
if self._device is None:
self._device = learning_device()
if self._device:
text += f"; learnt {self._device}"
self.found_label.setText(text)

def detect(self) -> None:
"""Find the beads and say how many and how long calibrating them will take; nothing is learnt."""
Expand Down Expand Up @@ -1456,7 +1486,8 @@ def install_torch(self) -> None:
self._installer.kill()
return
from .. import torch_install
program, *arguments = torch_install.command()
self._replacing = torch_install.cpu_build_on_nvidia()
program, *arguments = torch_install.command(replace=self._replacing)
process = QProcess(self)
process.setProcessChannelMode(QProcess.MergedChannels)
process.readyReadStandardOutput.connect(self._installer_output)
Expand All @@ -1478,8 +1509,13 @@ def _installer_finished(self, code, _status=None) -> None:
self._installer = None
self.install_button.setText("Install PyTorch")
missing = torch_missing()
self.status_bar.showMessage("PyTorch is installed: the pupil model is ready" if missing is None
else f"the install stopped (exit code {code}); {missing.splitlines()[0]}")
if missing is not None:
self.status_bar.showMessage(f"the install stopped (exit code {code}); {missing.splitlines()[0]}")
elif getattr(self, "_replacing", False) and "torch" in sys.modules:
# the CPU build is loaded in this process already: the card's is used from the next start
self.status_bar.showMessage("PyTorch for the card is installed: restart SMAPpy to learn on it")
else:
self.status_bar.showMessage("PyTorch is installed: the pupil model is ready")
self._show_method()

# ------------------------------------------------------------- saving
Expand Down
17 changes: 12 additions & 5 deletions src/smappy/docs/panels/PSF calibration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: PSF calibration
summary: Makes the PSF calibration the Spline 3D fitters read, from beads in a z stack, from single molecules on the coverslip in a z stack, or in situ from the blinking molecules of a measurement -- the bead average or a physical model of the microscope's pupil learnt with uiPSF v2 -- or, as a fourth, the curves of an elliptical Gaussian's widths in z that Astigmatic z reads, or, in the 4Pi mode, a 4Pi model for Spline 4Pi from 4Pi bead scans, and shows what each found and how well it fits.
widget: smappy.calibrate.psf_window.PSFCalibrationWindow
covers: [smappy.calibrate.pupil.half_split_uncertainty, smappy.calibrate.pupil.fit_quality, smappy.calibrate.plots.draw_pupil_fit_quality, smappy.torch_install.command, smappy.calibrate.psf_methods.BeadAverage, smappy.calibrate.psf_methods.PupilModel, smappy.calibrate.pupil.run_calibration, smappy.calibrate.pupil.bead_rois, smappy.calibrate.pupil.single_rois, smappy.calibrate.pupil.dual_rois, smappy.calibrate.pupil.spline_calibration, smappy.calibrate.pupil.dual_calibration, smappy.calibrate.pupil.save, smappy.calibrate.pupil.OpticsSettings, smappy.calibrate.pupil.BeadSettings, smappy.calibrate.pupil.learns_tilt, uipsf.diagnostics.figures.coverslip_tilt, smappy.calibrate.core.find_stack_beads, smappy.calibrate.psf_window.PSFCalibrationWindow, smappy.calibrate.pupil.estimate_seconds, smappy.calibrate.psf_methods.LearningProgress, smappy.calibrate.model_views.average_views, smappy.calibrate.model_views.pupil_views, smappy.calibrate.model_views.draw_model_vs_data, smappy.calibrate.model_views.planes, smappy.calibrate.stack_browser.StackBrowser, smappy.calibrate.stack_view.StackViewer, smappy.calibrate.pupil.covered_z_nm, smappy.calibrate.psf_methods.InSitu, smappy.calibrate.psf_methods.EllipticalGaussian, smappy.calibrate.elliptical.bead_widths, smappy.calibrate.elliptical.align_beads, smappy.calibrate.elliptical.calibrate_widths, smappy.calibrate.elliptical.save_path, smappy.calibrate.elliptical.EllipticalCalibrationSettings, smappy.calibrate.insitu_run.run_insitu, smappy.calibrate.insitu_run.movie_rois, smappy.calibrate.insitu_run.block_order, smappy.calibrate.insitu_run.enough, smappy.calibrate.insitu_run.start_astigmatism, smappy.calibrate.insitu_run.held_pupil, smappy.microscope_db.gamma_of, smappy.calibrate.insitu.quotas, smappy.calibrate.insitu_run.insitu_spline, smappy.calibrate.insitu_run.InSituModelSettings, smappy.calibrate.insitu.InSituSettings, smappy.calibrate.insitu.select, smappy.calibrate.insitu_views.draw_z_bins, smappy.calibrate.insitu_views.draw_rounds, smappy.calibrate.insitu_views.model_vs_data, uipsf.workflows.insitu_rounds.calibrate_insitu, uipsf.workflows.insitu_rounds.coverslip_edge, uipsf.workflows.insitu_rounds.focus_from_data, uipsf.workflows.insitu.twin, smappy.calibrate.psf_methods.FourPiPupil, smappy.calibrate.psf_methods.FourPiAverage, smappy.calibrate.psf_methods.for_mode, smappy.calibrate.fourpi_beads.average_model, smappy.fourpi.iab_from_beads, smappy.calibrate.fourpi_beads.scan_beads, smappy.calibrate.fourpi_beads.read_scan, smappy.calibrate.fourpi_beads.scan_layout, smappy.calibrate.fourpi_beads.scan_step_nm, smappy.calibrate.fourpi_beads.bead_fits, smappy.calibrate.fourpi_beads.FourPiBeadSettings, smappy.calibrate.psf_methods.MoleculeStack, smappy.calibrate.psf_methods.MoleculeElliptical, smappy.calibrate.psf_methods.methods_of, smappy.calibrate.molecule_stack.molecule_rois, smappy.calibrate.molecule_stack.read_file, smappy.calibrate.molecule_stack.stage_z_um, smappy.calibrate.molecule_stack.split_sweeps, smappy.calibrate.molecule_stack.width_curve, smappy.calibrate.molecule_stack.focus_of, smappy.calibrate.molecule_stack.detect, smappy.calibrate.molecule_stack.select, smappy.calibrate.molecule_stack.draw, smappy.calibrate.molecule_stack.illumination_radius, smappy.calibrate.molecule_stack.molecule_widths, smappy.calibrate.molecule_stack.MoleculeStackSettings, smappy.calibrate.molecule_dual.dual_molecule_rois, smappy.calibrate.molecule_dual.registration_from_molecules, smappy.calibrate.molecule_stack_run.run_molecule_stack, smappy.calibrate.molecule_stack_run.spline_range, smappy.calibrate.molecule_stack_run.estimate_seconds, smappy.calibrate.molecule_views.draw_selection, smappy.calibrate.molecule_views.draw_z_check, smappy.calibrate.molecule_views.draw_foci, smappy.calibrate.molecule_views.model_vs_data, uipsf.workflows.molecule_stack.learn_molecule_stack, uipsf.workflows.molecule_stack.z_check, uipsf.workflows.molecule_stack.molecule_chunk, uipsf.model.geometry.CoverslipStackGeometry]
covers: [smappy.calibrate.pupil.half_split_uncertainty, smappy.calibrate.pupil.fit_quality, smappy.calibrate.plots.draw_pupil_fit_quality, smappy.torch_install.command, smappy.torch_install.cpu_build_on_nvidia, uipsf.device.describe, smappy.calibrate.psf_methods.BeadAverage, smappy.calibrate.psf_methods.PupilModel, smappy.calibrate.pupil.run_calibration, smappy.calibrate.pupil.bead_rois, smappy.calibrate.pupil.single_rois, smappy.calibrate.pupil.dual_rois, smappy.calibrate.pupil.spline_calibration, smappy.calibrate.pupil.dual_calibration, smappy.calibrate.pupil.save, smappy.calibrate.pupil.OpticsSettings, smappy.calibrate.pupil.BeadSettings, smappy.calibrate.pupil.learns_tilt, uipsf.diagnostics.figures.coverslip_tilt, smappy.calibrate.core.find_stack_beads, smappy.calibrate.psf_window.PSFCalibrationWindow, smappy.calibrate.pupil.estimate_seconds, smappy.calibrate.psf_methods.LearningProgress, smappy.calibrate.model_views.average_views, smappy.calibrate.model_views.pupil_views, smappy.calibrate.model_views.draw_model_vs_data, smappy.calibrate.model_views.planes, smappy.calibrate.stack_browser.StackBrowser, smappy.calibrate.stack_view.StackViewer, smappy.calibrate.pupil.covered_z_nm, smappy.calibrate.psf_methods.InSitu, smappy.calibrate.psf_methods.EllipticalGaussian, smappy.calibrate.elliptical.bead_widths, smappy.calibrate.elliptical.align_beads, smappy.calibrate.elliptical.calibrate_widths, smappy.calibrate.elliptical.save_path, smappy.calibrate.elliptical.EllipticalCalibrationSettings, smappy.calibrate.insitu_run.run_insitu, smappy.calibrate.insitu_run.movie_rois, smappy.calibrate.insitu_run.block_order, smappy.calibrate.insitu_run.enough, smappy.calibrate.insitu_run.start_astigmatism, smappy.calibrate.insitu_run.held_pupil, smappy.microscope_db.gamma_of, smappy.calibrate.insitu.quotas, smappy.calibrate.insitu_run.insitu_spline, smappy.calibrate.insitu_run.InSituModelSettings, smappy.calibrate.insitu.InSituSettings, smappy.calibrate.insitu.select, smappy.calibrate.insitu_views.draw_z_bins, smappy.calibrate.insitu_views.draw_rounds, smappy.calibrate.insitu_views.model_vs_data, uipsf.workflows.insitu_rounds.calibrate_insitu, uipsf.workflows.insitu_rounds.coverslip_edge, uipsf.workflows.insitu_rounds.focus_from_data, uipsf.workflows.insitu.twin, smappy.calibrate.psf_methods.FourPiPupil, smappy.calibrate.psf_methods.FourPiAverage, smappy.calibrate.psf_methods.for_mode, smappy.calibrate.fourpi_beads.average_model, smappy.fourpi.iab_from_beads, smappy.calibrate.fourpi_beads.scan_beads, smappy.calibrate.fourpi_beads.read_scan, smappy.calibrate.fourpi_beads.scan_layout, smappy.calibrate.fourpi_beads.scan_step_nm, smappy.calibrate.fourpi_beads.bead_fits, smappy.calibrate.fourpi_beads.FourPiBeadSettings, smappy.calibrate.psf_methods.MoleculeStack, smappy.calibrate.psf_methods.MoleculeElliptical, smappy.calibrate.psf_methods.methods_of, smappy.calibrate.molecule_stack.molecule_rois, smappy.calibrate.molecule_stack.read_file, smappy.calibrate.molecule_stack.stage_z_um, smappy.calibrate.molecule_stack.split_sweeps, smappy.calibrate.molecule_stack.width_curve, smappy.calibrate.molecule_stack.focus_of, smappy.calibrate.molecule_stack.detect, smappy.calibrate.molecule_stack.select, smappy.calibrate.molecule_stack.draw, smappy.calibrate.molecule_stack.illumination_radius, smappy.calibrate.molecule_stack.molecule_widths, smappy.calibrate.molecule_stack.MoleculeStackSettings, smappy.calibrate.molecule_dual.dual_molecule_rois, smappy.calibrate.molecule_dual.registration_from_molecules, smappy.calibrate.molecule_stack_run.run_molecule_stack, smappy.calibrate.molecule_stack_run.spline_range, smappy.calibrate.molecule_stack_run.estimate_seconds, smappy.calibrate.molecule_views.draw_selection, smappy.calibrate.molecule_views.draw_z_check, smappy.calibrate.molecule_views.draw_foci, smappy.calibrate.molecule_views.model_vs_data, uipsf.workflows.molecule_stack.learn_molecule_stack, uipsf.workflows.molecule_stack.z_check, uipsf.workflows.molecule_stack.molecule_chunk, uipsf.model.geometry.CoverslipStackGeometry]
---

## What it does
Expand Down Expand Up @@ -83,7 +83,12 @@ with one too old, *Install PyTorch* installs the right build for the computer
(for Apple's GPU, an NVIDIA card or the CPU) into the Python running SMAPpy;
the window also shows the command to type instead. The bead average works
without it. It uses the
GPU where PyTorch finds one (NVIDIA, or Apple's).
GPU where PyTorch finds one (NVIDIA, or Apple's), and the line *Detect* writes
says where: *learnt on the graphics card*, *on Apple's GPU* or *on the CPU*.
A PyTorch built for the CPU only on a computer with an NVIDIA card -- a plain
`pip install torch` on Windows brings that one -- works, several times
slower; the window says so and offers *Install PyTorch* to replace it with
the card's build.

## How it works

Expand Down Expand Up @@ -1266,9 +1271,11 @@ beads can be minutes away. Nothing is saved, and the beads are neither
scored nor drawn against the model.

### Install PyTorch
Shown with the pupil model while PyTorch is missing or too old: installs it
into the Python running SMAPpy, with pip's progress in the status line;
pressed again, it stops. The pupil model can be used as soon as it is done.
Shown with the pupil model while PyTorch is missing or too old, or built for
the CPU only beside an NVIDIA card: installs it into the Python running
SMAPpy, with pip's progress in the status line; pressed again, it stops. The
pupil model can be used as soon as it is done -- after replacing a CPU build
once SMAPpy is started again, if a calibration had already loaded the old one.

### Save as...
Saves the calibration under another name, or one made with *save model* off.
Expand Down
51 changes: 49 additions & 2 deletions src/smappy/torch_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

Nothing here imports torch: what is installed is read from its package metadata, so asking costs nothing
and an old torch is not loaded into the process before it is replaced.

**A build for the CPU on a machine with an NVIDIA card** (`cpu_build_on_nvidia`) works, and learns several
times slower without saying so: uipsf takes the card only when torch was built for it (`uipsf.device`). It
happens when torch came from elsewhere than the window -- a plain ``pip install torch`` on Windows, whose PyPI
wheel is CPU only. The window then warns and offers the button again (`replacement`); its command replaces
the build rather than finding the installed version good enough.
"""
from __future__ import annotations

Expand Down Expand Up @@ -56,6 +62,38 @@ def problem() -> Optional[str]:
return None


def _installed() -> Optional[str]:
from importlib import metadata
try:
return metadata.version("torch")
except metadata.PackageNotFoundError:
return None


def cpu_build_on_nvidia(version: Optional[str] = None, system: Optional[str] = None,
nvidia: Optional[bool] = None) -> bool:
"""Whether the installed torch is built for the CPU only on a machine with an NVIDIA card (module docstring),
read from its version's local label: PyTorch's CPU index marks its wheels ``+cpu``; on Windows a CUDA build
is always ``+cu…`` (PyPI's wheel, without a label, is CPU only), on Linux PyPI's unlabelled wheel is CUDA's."""
version = _installed() if version is None else version
system = system or platform.system()
nvidia = has_nvidia() if nvidia is None else nvidia
if not version or not nvidia or system == "Darwin":
return False
label = version.partition("+")[2].lower()
if label == "cpu":
return True
return system == "Windows" and not label.startswith("cu")


def replacement() -> Optional[str]:
"""What the window says when torch works but leaves the NVIDIA card unused; None when it does not."""
if problem() is not None or not cpu_build_on_nvidia():
return None
return (f"PyTorch {_installed()} is built for the CPU only, though this computer has an NVIDIA card: the pupil "
f"model learns on the CPU, several times slower")


def has_nvidia() -> bool:
return shutil.which("nvidia-smi") is not None

Expand Down Expand Up @@ -84,11 +122,20 @@ def installer(has_pip: Optional[bool] = None, uv: Optional[str] = None) -> List[


def command(system: Optional[str] = None, nvidia: Optional[bool] = None,
start: Optional[List[str]] = None) -> List[str]:
"""The install command, as a list for a process, that installs the right torch here."""
start: Optional[List[str]] = None, replace: bool = False) -> List[str]:
"""The install command, as a list for a process, that installs the right torch here.

``replace``: a CPU build is there and the card's is wanted (`cpu_build_on_nvidia`). ``--upgrade`` alone
can keep it -- PyPI's unlabelled 2.x sorts below a 2.x+cpu, and an installed version newer than the CUDA
index's is "satisfied" -- so the build is reinstalled from the CUDA index. On Windows without its
dependencies, which a CUDA wheel there shares with the CPU one (it carries CUDA inside); on Linux the CUDA
wheel needs NVIDIA's packages, so only the higher local label (+cu126 above +cpu) moves it."""
system = system or platform.system()
nvidia = has_nvidia() if nvidia is None else nvidia
pip = (start or installer()) + ["--upgrade", f"torch>={'.'.join(map(str, MINIMUM))}"]
if replace and nvidia and system in ("Windows", "Linux"):
extra = ["--force-reinstall", "--no-deps"] if system == "Windows" else []
return pip + extra + ["--index-url", CUDA_INDEX]
if system == "Linux" and not nvidia:
return pip + ["--index-url", CPU_INDEX]
if system == "Windows" and nvidia:
Expand Down
Loading
Loading