anyplotlib 0.7.2__py3-none-any.whl → 0.8.0__py3-none-any.whl

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.
anyplotlib/figure_esm.js CHANGED
@@ -2415,10 +2415,31 @@ function render({ model, el, onResize }) {
2415
2415
  };
2416
2416
  }
2417
2417
 
2418
+ // The quantisation band the uint8 image bytes were encoded over, as [lo, hi].
2419
+ // A DEGENERATE band (raw_max <= raw_min) means DIFFERENT things on the two
2420
+ // encoder paths, and the LUT has to read it the way the bytes were written:
2421
+ //
2422
+ // plain (_normalize_image): a constant frame quantises to ALL-ZERO bytes and
2423
+ // reports (c, c) — the band NAMES the single value code 0 stands for, and
2424
+ // hMin + raw/255 * (range||1) reconstructs it. Keep honouring it.
2425
+ // tile (_tile_quant_clim): a degenerate band is treated as UNSET and the
2426
+ // bytes are quantised over the DISPLAY window instead — so they are already
2427
+ // display-mapped and the LUT must be the identity window over them.
2428
+ //
2429
+ // Reading a tiled degenerate band the plain way is GH #60: a plot that entered
2430
+ // tile mode on a flat placeholder carries the band (0, 0), every code maps to
2431
+ // hMin + raw/255 * 1 in [0, 1], lands below any sane display floor, and the
2432
+ // panel renders solid black beside perfectly healthy stats.
2433
+ function _rawBand(st) {
2434
+ const lo=st.raw_min, hi=st.raw_max;
2435
+ if(lo==null||hi==null) return [st.display_min, st.display_max];
2436
+ if(hi>lo) return [lo,hi];
2437
+ return st.tile_enabled ? [st.display_min, st.display_max] : [lo,hi];
2438
+ }
2439
+
2418
2440
  function _buildLut32(st) {
2419
2441
  const dMin=st.display_min, dMax=st.display_max;
2420
- const hMin=st.raw_min!=null?st.raw_min:dMin;
2421
- const hMax=st.raw_max!=null?st.raw_max:dMax;
2442
+ const [hMin,hMax]=_rawBand(st);
2422
2443
  const mode=st.scale_mode||'linear';
2423
2444
  const range=hMax-hMin||1;
2424
2445
  const cmapData=st.colormap_data||[];
@@ -3198,8 +3219,7 @@ function render({ model, el, onResize }) {
3198
3219
 
3199
3220
  // display_min / display_max tick marks
3200
3221
  const dMin=st.display_min, dMax=st.display_max;
3201
- const hMin=st.raw_min!=null?st.raw_min:dMin;
3202
- const hMax=st.raw_max!=null?st.raw_max:dMax;
3222
+ const [hMin,hMax]=_rawBand(st);
3203
3223
  const vRange=(hMax-hMin)||1;
3204
3224
  function _vToY(v){return imgH-1-((v-hMin)/vRange)*(imgH-1);}
3205
3225
  ctx.strokeStyle='rgba(255,255,255,0.85)'; ctx.lineWidth=1.5;
@@ -8182,7 +8202,13 @@ fn fs(in : VsOut) -> @location(0) vec4<f32> {
8182
8202
  // outer radius handle
8183
8203
  if (Math.hypot(mx - (ccx + cr), my - ccy) <= HR)
8184
8204
  return { idx:i, mode:'resize_r', snapW:{...w}, startMX:mx, startMY:my };
8185
- // body (inside ring ± tolerance)
8205
+ // body (inside ring ± tolerance). `lock_center` refuses the grab
8206
+ // outright rather than letting the drag run and correcting afterwards:
8207
+ // a centre snapped back on release still tracks the cursor for the
8208
+ // whole drag, which reads as a broken lock. Refusing here also leaves
8209
+ // the hover cursor at 'default' and lets the drag fall through to the
8210
+ // plot's own pan, so the ring is simply not a handle any more.
8211
+ if (w.lock_center) continue;
8186
8212
  if (Math.abs(Math.hypot(mx-ccx, my-ccy) - cr) <= Math.max(HR, cr*0.18) ||
8187
8213
  Math.hypot(mx-ccx, my-ccy) <= HR)
8188
8214
  return { idx:i, mode:'move', snapW:{...w}, startMX:mx, startMY:my };
@@ -8441,7 +8467,9 @@ fn fs(in : VsOut) -> @location(0) vec4<f32> {
8441
8467
  }
8442
8468
 
8443
8469
  if (w.type === 'circle') {
8444
- if (d.mode === 'move') {
8470
+ // A drag already in flight when `lock_center` is turned on must not keep
8471
+ // translating the widget: the hit-test gate only runs at grab time.
8472
+ if (d.mode === 'move' && !w.lock_center) {
8445
8473
  w.cx = s.cx + dix; w.cy = s.cy + diy;
8446
8474
  } else if (d.mode === 'resize_r') {
8447
8475
  // distance from centre in image-px
@@ -507,11 +507,7 @@ class Plot2D(_BasePlot, _PanelMixin, _MarkerMixin):
507
507
  # (raw_min/raw_max fixed, display_min/display_max move) with NO pixel re-encode
508
508
  # or re-transfer — set_clim just moves the display window (see set_clim). Set
509
509
  # once from the full-res range; keep any existing band on a re-enable.
510
- if (self._state.get("raw_min") is None
511
- or not (self._state.get("raw_max", 0) > self._state.get("raw_min", 0))):
512
- rng = self._backend_display_range(self._tile_backend)
513
- if rng is not None:
514
- self._state["raw_min"], self._state["raw_max"] = rng
510
+ self._ensure_tile_band()
515
511
  _was_on = self._tile_on
516
512
  if not self._tile_on:
517
513
  self._tile_on = True
@@ -561,6 +557,13 @@ class Plot2D(_BasePlot, _PanelMixin, _MarkerMixin):
561
557
  setter = getattr(self._tile_backend, "set_array", None)
562
558
  if setter is not None:
563
559
  setter(array)
560
+ # The quantisation band is derived ONCE (enable_tile) and then held fixed, so
561
+ # a contrast change re-windows in the LUT with no pixel re-encode. But if it
562
+ # was never VALIDLY derived — tile mode started on a flat placeholder, so the
563
+ # probe refused and the band stayed degenerate — the new data in the backend
564
+ # is the first chance to get it right. Must run BEFORE the overview/detail are
565
+ # re-sampled below so they quantise over the corrected band in the SAME push.
566
+ self._ensure_tile_band()
564
567
  reg = self._state.get("detail_region") or []
565
568
  has_detail = len(reg) == 4
566
569
  # Refresh the overview base ONLY when no detail tile is shown (zoomed out) —
@@ -622,6 +625,70 @@ class Plot2D(_BasePlot, _PanelMixin, _MarkerMixin):
622
625
  except Exception as e:
623
626
  _TLOG.warning("[TILEDBG] overview refresh FAILED: %s", e)
624
627
 
628
+ def _ensure_tile_band(self) -> bool:
629
+ """Derive the fixed quantisation band (raw_min/raw_max) from the CURRENT tile
630
+ backend when it is unset or DEGENERATE, by the same rule _tile_quant_clim
631
+ uses to read it back. Returns True iff the band was (re-)derived.
632
+
633
+ The degenerate case is the one that bites: a plot that entered tile mode on a
634
+ flat placeholder (e.g. zeros before real data exists) probes a range of
635
+ (0, 0) — _backend_display_range refuses it, so the band keeps the
636
+ constructor's raw min/max, which for zeros is also (0, 0). Every later
637
+ set_data then re-sampled the overview against a band the encoder treated as
638
+ unset (quantising over the display window) while the frontend LUT honoured
639
+ (0, 0) literally and painted the panel solid black. Calling this on each data
640
+ swap re-derives the band as soon as a frame with real range arrives, so the
641
+ two ends of the protocol agree on what the bytes mean.
642
+ """
643
+ lo, hi = self._state.get("raw_min"), self._state.get("raw_max")
644
+ if lo is not None and hi is not None and hi > lo:
645
+ return False # a valid band is kept, never re-derived
646
+ if self._tile_backend is None:
647
+ return False
648
+ rng = self._backend_display_range(self._tile_backend)
649
+ if rng is None:
650
+ # Still flat (an all-constant frame): leave the band unset/degenerate —
651
+ # _tile_quant_clim and the JS LUT both fall back to the display window.
652
+ return False
653
+ self._state["raw_min"], self._state["raw_max"] = rng
654
+ _TLOG.debug("[TILEDBG] tile band (re-)derived from backend → raw=(%s,%s)",
655
+ *rng)
656
+ return True
657
+
658
+ def set_tile_band(self, vmin: float, vmax: float) -> None:
659
+ """PIN the fixed quantisation band the tile bytes are encoded over.
660
+
661
+ Tile mode normally derives this band once, from the full-res data (see
662
+ ``enable_tile``), and then holds it: a contrast change re-windows in the LUT
663
+ with no pixel re-encode or re-transfer. A live consumer that already KNOWS the
664
+ honest range — a camera's bit depth, a detector's saturation point — can pin
665
+ it here instead of letting the first frame decide, so the contrast doesn't
666
+ shift when a later frame happens to contain a brighter pixel.
667
+
668
+ Pinning also survives what auto-derivation cannot: a source whose first frames
669
+ are flat (a placeholder, a closed shutter) has no range to derive from, and
670
+ the band stays unset until a frame with real content arrives.
671
+
672
+ Re-samples the overview (and any active detail tile) over the new band in a
673
+ single push, so the pixels and the band the renderer reads them with never
674
+ disagree. Raises if the plot is not in tile mode or the range is degenerate."""
675
+ if not self._tile_on:
676
+ raise RuntimeError(
677
+ "set_tile_band requires tile mode — the band is the quantisation "
678
+ "range for the TILE bytes. Call enable_tile() first (or use "
679
+ "set_clim() to change the display window on a plain plot).")
680
+ lo, hi = float(vmin), float(vmax)
681
+ if not (hi > lo):
682
+ raise ValueError(
683
+ f"set_tile_band requires vmax > vmin, got ({lo}, {hi}). A degenerate "
684
+ f"band is what 'unset' means here — both the encoder and the "
685
+ f"renderer then fall back to the display window.")
686
+ self._state["raw_min"], self._state["raw_max"] = lo, hi
687
+ # Re-encode over the new band. update_tile_source() with no array re-samples
688
+ # from the backend in place (keeping zoom/center and the detail region) and
689
+ # pushes — so the band and the bytes it describes ship together.
690
+ self.update_tile_source()
691
+
625
692
  def _tile_quant_clim(self):
626
693
  """The FIXED quantisation band (raw_min/raw_max) the tile bytes are encoded
627
694
  over, so a contrast change re-windows in the LUT without re-encoding pixels.
@@ -1470,7 +1537,11 @@ class Plot2D(_BasePlot, _PanelMixin, _MarkerMixin):
1470
1537
  raw frame, not merely re-windowing the existing codes (which are saturated
1471
1538
  outside the previous band and so couldn't widen past it). For an RGB frame
1472
1539
  (no scalar quantisation) or when no raw frame is cached, fall back to a pure
1473
- display-window update."""
1540
+ display-window update.
1541
+
1542
+ See :meth:`set_display_window` for the non-destructive counterpart — the
1543
+ one to reach for when the pixels must not move (a tiled plot, or a
1544
+ serialised figure being re-windowed with no Python behind it)."""
1474
1545
  new_min = float(vmin) if vmin is not None else self._state.get("display_min")
1475
1546
  new_max = float(vmax) if vmax is not None else self._state.get("display_max")
1476
1547
 
@@ -1512,6 +1583,40 @@ class Plot2D(_BasePlot, _PanelMixin, _MarkerMixin):
1512
1583
  self._state["display_max"] = float(vmax)
1513
1584
  self._push()
1514
1585
 
1586
+ def set_display_window(self, vmin=None, vmax=None) -> None:
1587
+ """Move the display window WITHOUT re-quantising the pixels.
1588
+
1589
+ The non-destructive counterpart to :meth:`set_clim`. Both change the
1590
+ contrast; they differ in what they do to the data behind it:
1591
+
1592
+ ``set_clim``
1593
+ re-encodes the cached raw frame over the new range, so the codes
1594
+ always span exactly the visible band — maximum precision for what is
1595
+ on screen, but the pixels are re-encoded and re-sent, and the old
1596
+ band is gone.
1597
+ ``set_display_window``
1598
+ leaves the codes and their ``raw_min``/``raw_max`` band alone and
1599
+ moves only the window the LUT maps through it. Nothing is re-encoded
1600
+ and nothing travels but two floats.
1601
+
1602
+ Use it when the pixels must stay put: a tiled plot, where re-quantising
1603
+ would re-encode the full-res frame on every drag tick (``set_clim``
1604
+ already routes there internally), or a figure that has been serialised
1605
+ and is being re-windowed with no Python behind it — which is how a saved
1606
+ page gets a working contrast control at all.
1607
+
1608
+ The trade is precision. Quantisation spans ``[raw_min, raw_max]``, so a
1609
+ window much narrower than that band resolves in coarse steps, and one
1610
+ WIDER than it recovers nothing: values outside the band were saturated
1611
+ to 0/255 when the frame was encoded. Quantise over the range you want to
1612
+ be able to reach.
1613
+ """
1614
+ if vmin is not None:
1615
+ self._state["display_min"] = float(vmin)
1616
+ if vmax is not None:
1617
+ self._state["display_max"] = float(vmax)
1618
+ self._push()
1619
+
1515
1620
  def set_detail(self, tile=None, x0=None, x1=None, y0=None, y1=None) -> None:
1516
1621
  """Upload a HIGH-RES detail tile covering the LOGICAL image-pixel rectangle
1517
1622
  ``[x0:x1, y0:y1]`` of the base image (in the SAME orientation as the frame
@@ -1731,15 +1836,24 @@ class Plot2D(_BasePlot, _PanelMixin, _MarkerMixin):
1731
1836
  def add_circle_widget(self, cx: float | None = None, cy: float | None = None,
1732
1837
  r: float | None = None, color: str = "#00e5ff",
1733
1838
  linewidth: float = 2,
1734
- show_handles: bool = True) -> CircleWidget:
1735
- """Add a draggable circle overlay."""
1839
+ show_handles: bool = True,
1840
+ lock_center: bool = False) -> CircleWidget:
1841
+ """Add a draggable circle overlay.
1842
+
1843
+ ``lock_center`` pins the centre and leaves only the radius draggable —
1844
+ a grab on the ring body is refused and pans the plot instead. Use it
1845
+ when the centre is fixed by the data (a ring on a power spectrum is
1846
+ centred on the DC term), so it cannot be nudged off and silently
1847
+ corrupt every radius measured from it.
1848
+ """
1736
1849
  iw, ih = self._state["image_width"], self._state["image_height"]
1737
1850
  widget = CircleWidget(lambda: None,
1738
1851
  cx=float(cx) if cx is not None else iw / 2,
1739
1852
  cy=float(cy) if cy is not None else ih / 2,
1740
1853
  r=float(r) if r is not None else iw * 0.1,
1741
1854
  color=color, linewidth=linewidth,
1742
- show_handles=show_handles)
1855
+ show_handles=show_handles,
1856
+ lock_center=lock_center)
1743
1857
  widget._push_fn = self._make_widget_push_fn(widget)
1744
1858
  self._widgets[widget.id] = widget
1745
1859
  self._push()
@@ -74,13 +74,29 @@ class CircleWidget(Widget):
74
74
  Outline stroke width in px. Default 2.
75
75
  show_handles : bool, optional
76
76
  Draw the radius grab handle. Default ``True``.
77
+ lock_center : bool, optional
78
+ Pin ``cx, cy`` and let only the radius change. Default ``False``.
79
+
80
+ The centre is refused at HIT-TEST time, not corrected afterwards: a
81
+ grab on the ring body is simply not a widget grab, so it falls through
82
+ to the plot's own pan and the hover cursor never promises a move.
83
+ Enforcing it from Python instead — snapping the centre back when the
84
+ drag settles — cannot work, because the JS drag recomputes the position
85
+ from its own grab-time snapshot on every frame; the ring tracks the
86
+ cursor for the whole drag and only jumps back on release.
87
+
88
+ Use it when the centre is not a free parameter — a ring on a power
89
+ spectrum is centred on the DC term, so a draggable centre is a control
90
+ that can only ever be wrong, and one nudged off-centre silently
91
+ corrupts every radius measured from it.
77
92
  """
78
93
  def __init__(self, push_fn, *, cx, cy, r, color="#00e5ff",
79
- linewidth=2, show_handles=True):
94
+ linewidth=2, show_handles=True, lock_center=False):
80
95
  super().__init__("circle", push_fn,
81
96
  cx=float(cx), cy=float(cy), r=float(r), color=color,
82
97
  linewidth=float(linewidth),
83
- show_handles=bool(show_handles))
98
+ show_handles=bool(show_handles),
99
+ lock_center=bool(lock_center))
84
100
 
85
101
 
86
102
  class AnnularWidget(Widget):
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: anyplotlib
3
- Version: 0.7.2
3
+ Version: 0.8.0
4
4
  Summary: A plotting library using python, javascript and anywidget for performant in browser plotting.
5
5
  Project-URL: Homepage, https://cssfrancis.github.io/anyplotlib/
6
6
  Project-URL: Repository, https://github.com/CSSFrancis/anyplotlib
@@ -6,7 +6,7 @@ anyplotlib/_repr_utils.py,sha256=DZM2mTTX7lpg9LaqHjOVNvnYSVRKNaKrJvEYBmdQ5A4,191
6
6
  anyplotlib/_utils.py,sha256=fvkMnNiCljG11p6Fsn2engKzsYhI1ILcZE5wog5Rcu0,14584
7
7
  anyplotlib/callbacks.py,sha256=lTmL-UYPN6y0VY82MqDKSMTqBgUgezbSM7SoP8t5s0o,14620
8
8
  anyplotlib/embed.py,sha256=cSDgjRSwqaJb6OvuJ5g3QFlNg0k0EEQH48VtVaR0K_I,7389
9
- anyplotlib/figure_esm.js,sha256=bPU56QCD5lA0Ut_kjvWV79PeX9g4ABbrO5kMyTtjWEY,477090
9
+ anyplotlib/figure_esm.js,sha256=j05Ksuz6fC9ZfnjdHbdoLDZOqsxiKXPUwoHHBvK6oNE,478865
10
10
  anyplotlib/keys.py,sha256=nmzrppnuuh3K-JYV6rsat-MGWiaZxfJjO-IDN8Ruzdo,9307
11
11
  anyplotlib/markers.py,sha256=e6r2t8lFs9c5OjkxeRsV1Le34G7icJRgO2ciPx3e5nM,26870
12
12
  anyplotlib/axes/__init__.py,sha256=vsMU_WjFcha17Xozxe7v4_oYQ_6qatk8GZlbwKm-cAc,186
@@ -21,7 +21,7 @@ anyplotlib/plot1d/_plot1d.py,sha256=H0bK4ThKV-3haxs0ish48Ye65M-K9KRNwzoFRNTXjso,
21
21
  anyplotlib/plot1d/_plotbar.py,sha256=2hbj3I86TJr5Zds4a179iNQ9CyQTG8ys9MqY8xBTX_Y,17356
22
22
  anyplotlib/plot2d/__init__.py,sha256=_BVLJeGKuTmlZPpfPW2d99qRamBvOSyx3hsru7ylTzk,264
23
23
  anyplotlib/plot2d/_layer.py,sha256=gONf5R2bYsf_Oymx3Y0RmPrTvFDzVnbDGBKFWkhyAKM,5598
24
- anyplotlib/plot2d/_plot2d.py,sha256=w1p0eIm_MQnacBZsvZ5yKV9YpR3ybM0qH-aHHwH-7Rc,119353
24
+ anyplotlib/plot2d/_plot2d.py,sha256=522xsJWhx5_ygKa1mRNaoAs4rF4mMsbLwzsXuZAggHs,125937
25
25
  anyplotlib/plot2d/_plotmesh.py,sha256=qXOsiQQ13yhPar5KToAIz02sqivdKz3eXcHI7QZsv04,4547
26
26
  anyplotlib/plot2d/_tile_backend.py,sha256=BfJ0X-AbWJvImeNcBW9PzkO3b-JQ_CDBP3vOfdNocOw,7225
27
27
  anyplotlib/plot3d/__init__.py,sha256=m851XQKGzx5bywIbBK0qntBxzJU7zwxqf6xnOQ8pD1M,138
@@ -38,9 +38,9 @@ anyplotlib/sphinx_anywidget/static/anywidget_overlay.css,sha256=TlIWo4OUsEZFNROH
38
38
  anyplotlib/widgets/__init__.py,sha256=uzGX0R_X4xw8c2MJTGPBeaoOucJznWN1Vzcj2YNRGG8,723
39
39
  anyplotlib/widgets/_base.py,sha256=WMY_s_KKNVpd-3VceAwZeY5JHhtvJp1nuVbYkmqNiBw,9512
40
40
  anyplotlib/widgets/_widgets1d.py,sha256=SXnBDm25th1u9lPHlpQIdVACaQcVCvKUeF-ZJ9VMeVs,7784
41
- anyplotlib/widgets/_widgets2d.py,sha256=XZmqmgRq7g0P2-xkC2hMRPfZFtq8t-nfjbRc7w2wIXA,19938
41
+ anyplotlib/widgets/_widgets2d.py,sha256=mP0m6yA4cSB2hD71AVpik_FwUHzPCiLYBfju21G5DWk,20930
42
42
  anyplotlib/widgets/_widgets3d.py,sha256=zRd2q5O2KePxU5raCnvO-LatMlpGQjnDMPhG61d40Ks,1638
43
- anyplotlib-0.7.2.dist-info/METADATA,sha256=IyT9uH8qJxx6r-0zp3yeeayG9yBxI0B3DXk9rfImZY0,7163
44
- anyplotlib-0.7.2.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
45
- anyplotlib-0.7.2.dist-info/licenses/LICENSE,sha256=NonSXCjWxcYbaMxPnkgFzPpP0bVBTwUDpuDKyKC2uvQ,1071
46
- anyplotlib-0.7.2.dist-info/RECORD,,
43
+ anyplotlib-0.8.0.dist-info/METADATA,sha256=aePD0Ago3czgs60CU-Fu5D8vUVC9Jw0YXQ-nBeaF5pE,7163
44
+ anyplotlib-0.8.0.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
45
+ anyplotlib-0.8.0.dist-info/licenses/LICENSE,sha256=NonSXCjWxcYbaMxPnkgFzPpP0bVBTwUDpuDKyKC2uvQ,1071
46
+ anyplotlib-0.8.0.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.31.0
2
+ Generator: hatchling 1.32.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any