Skip to content

Commit

Permalink
More minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gselzer committed Nov 5, 2024
1 parent c5a5635 commit adcee57
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 118 deletions.
82 changes: 82 additions & 0 deletions examples/histogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import numpy as np
from qtpy.QtCore import QTimer
from qtpy.QtWidgets import (
QApplication,
QPushButton,
QVBoxLayout,
QWidget,
)

from ndv.histogram._model import StatsModel
from ndv.histogram._vispy import VispyHistogramView

if TYPE_CHECKING:
from typing import Any


class Controller:
"""A (Qt) wrapper around another HistogramView with some additional controls."""

def __init__(self) -> None:
self._wdg = QWidget()
self._model = StatsModel()
self._view = VispyHistogramView()

# A HistogramView is both a StatsView and a LUTView
# StatModel <-> StatsView
self._model.events.histogram.connect(
lambda data: self._view.set_histogram(*data)
)
# LutModel <-> LutView (TODO)
# LutView -> LutModel (TODO: Currently LutView <-> LutView)
self._view.gammaChanged.connect(self._view.set_gamma)
self._view.climsChanged.connect(self._view.set_clims)

# Vertical box
self._vert = QPushButton("Vertical")
self._vert.setCheckable(True)
self._vert.toggled.connect(self._view.set_vertical)

# Log box
self._log = QPushButton("Logarithmic")
self._log.setCheckable(True)
self._log.toggled.connect(self._view.enable_range_log)

# Data updates
self._data_btn = QPushButton("Change Data")
self._data_btn.setCheckable(True)
self._data_btn.toggled.connect(
lambda toggle: self.timer.blockSignals(not toggle)
)

def _update_data() -> None:
"""Replaces the displayed data."""
self._model.data = np.random.normal(10, 10, 10000)

self.timer = QTimer()
self.timer.setInterval(10)
self.timer.blockSignals(True)
self.timer.timeout.connect(_update_data)
self.timer.start()

# Layout
self._layout = QVBoxLayout(self._wdg)
self._layout.addWidget(self._view.view())
self._layout.addWidget(self._vert)
self._layout.addWidget(self._log)
self._layout.addWidget(self._data_btn)

def view(self) -> Any:
"""Returns an object that can be displayed by the active backend."""
return self._wdg


app = QApplication([])

widget = Controller()
widget.view().show()
app.exec()
5 changes: 3 additions & 2 deletions src/ndv/histogram/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ class StatsModel:
standard_deviation: float | None = None
average: float | None = None
histogram: tuple[np.ndarray, np.ndarray] | None = None
bins: int = 256
bin_edges: np.ndarray | None = None
_data: np.ndarray | None = None

Check warning on line 21 in src/ndv/histogram/_model.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/_model.py#L16-L21

Added lines #L16 - L21 were not covered by tests

@property
def data(self) -> np.ndarray:
if self._data is None:
if self._data is not None:
return self._data
raise Exception("Data has not yet been set!")

Check warning on line 27 in src/ndv/histogram/_model.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/_model.py#L23-L27

Added lines #L23 - L27 were not covered by tests

Expand All @@ -30,6 +31,6 @@ def data(self, data: np.ndarray) -> None:
if data is None:
return
self._data = data
self.histogram = np.histogram(self._data)
self.histogram = np.histogram(self._data, bins=self.bins)
self.average = np.average(self._data)
self.standard_deviation = np.std(self._data)

Check warning on line 36 in src/ndv/histogram/_model.py

View check run for this annotation

Codecov / codecov/patch

src/ndv/histogram/_model.py#L29-L36

Added lines #L29 - L36 were not covered by tests
52 changes: 0 additions & 52 deletions src/ndv/histogram/_qt.py

This file was deleted.

64 changes: 0 additions & 64 deletions x.py

This file was deleted.

0 comments on commit adcee57

Please sign in to comment.