Spaces:
Sleeping

File size: 12,209 Bytes
230ea06
 
 
 
 
d40edc5
230ea06
b687153
230ea06
 
 
 
 
 
 
 
 
 
35de79c
230ea06
35de79c
230ea06
35de79c
 
 
230ea06
 
d40edc5
35de79c
 
 
 
 
2dc366b
 
 
 
 
 
 
 
 
 
 
35de79c
 
 
 
2dc366b
35de79c
2dc366b
230ea06
 
 
35de79c
230ea06
35de79c
230ea06
 
d40edc5
230ea06
 
 
 
35de79c
230ea06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d40edc5
230ea06
 
 
 
d40edc5
230ea06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2dc366b
230ea06
 
 
 
d40edc5
230ea06
d40edc5
230ea06
 
 
 
 
2dc366b
230ea06
 
d40edc5
230ea06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35de79c
230ea06
 
d40edc5
230ea06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d40edc5
230ea06
 
 
 
 
 
 
2dc366b
 
 
 
 
 
 
b687153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2dc366b
 
 
b687153
2dc366b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b687153
2dc366b
 
 
b687153
 
 
 
 
 
 
 
 
 
 
 
71192b4
2dc366b
71192b4
 
 
 
2dc366b
71192b4
2dc366b
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# Visualization Layer

The visualization layer builds Plotly figure dictionaries from data. These dicts are sent to the browser as JSON, where `Plotly.react()` renders them. All functions are pure β€” they take data as input and return Plotly-compatible dicts.

**Files covered**:

- [visualization/image_viewer.py](../src/jellyscope/visualization/image_viewer.py) β€” Galaxy heatmap + clump overlays
- [visualization/rgb_composite.py](../src/jellyscope/visualization/rgb_composite.py) β€” RGB composite (percentile+asinh and Lupton)
- [visualization/properties_panel.py](../src/jellyscope/visualization/properties_panel.py) β€” Property formatting

---

## visualization/image_viewer.py β€” Galaxy Viewer

**Location**: `src/jellyscope/visualization/image_viewer.py`

Builds the main galaxy image as a Plotly figure with three types of traces layered together: a heatmap (the image), boundary polygons (clump outlines), and centroid markers (clump labels).

### Stretch Functions

The module provides three intensity stretch functions, selectable via `_normalize_stretch()`. The default is `_log_stretch`.

#### `_log_stretch(data: np.ndarray) -> np.ndarray` (default)

Applies log stretch using astropy's `LogStretch(a=200)` after normalizing to the [10th, 99.98th] percentile range via `AsymmetricPercentileInterval`. This more aggressively boosts faint structure than arcsinh.

**Algorithm**:

1. Mask NaN and non-positive values
2. Compute bounds via `AsymmetricPercentileInterval(10.0, 99.98)`
3. Clip and normalize to [0, 1]
4. Apply `log(200*x + 1) / log(201)`

#### `_lupton_asinh_stretch(data: np.ndarray, softening=8.0, alpha=None) -> np.ndarray`

Lupton et al. (2004) single-band asinh stretch: `f(x) = arcsinh(alpha*Q*(x-m)) / Q`. Linear for faint features, logarithmic for bright features. Background level `m` and noise `sigma` are estimated via sigma-clipped statistics. If `alpha` is not provided, it is auto-computed from the noise level as `0.02 / sigma`.

**Algorithm**:

1. Estimate background `(m, sigma)` via sigma-clipped stats
2. Compute `alpha = 0.02 / sigma` if not given
3. Apply `arcsinh(alpha * Q * (x - m)) / Q`
4. Normalize to [0, 1] using 99.5th percentile

#### `_power_stretch(data: np.ndarray) -> np.ndarray`

Power stretch using astropy's `PowerStretch(a=0.5)`. Lower exponent = more aggressive on faint features. Uses [20th, 99.5th] percentile bounds.

#### `_normalize_stretch(data, stretch="log") -> np.ndarray`

Dispatcher that applies the given stretch function. Accepts `"log"`, `"lupton_asinh"`, or `"power"`. Defaults to `"log"`.

---

### `create_galaxy_heatmap(slice_data, colorscale="viridis") -> dict`

Creates a single Plotly `heatmap` trace for a 2D image slice. Internally applies `_normalize_stretch()` (log stretch by default).

**Returns** a Plotly trace dict:

```python
{
    "type": "heatmap",
    "z": [[0.12, 0.34, None, ...], ...],  # 2D array, None = transparent
    "colorscale": "viridis",
    "hoverongaps": False,                   # Don't show hover for None pixels
    "hovertemplate": "x: %{x}<br>y: %{y}<br>flux: %{z:.4f}<extra></extra>",
    "showscale": True,
    "colorbar": {"title": "Flux (stretched)", "thickness": 15},
}
```

The `z` array has NaN replaced with `None` so Plotly renders gaps as transparent. The `hoverongaps: False` setting prevents hover tooltips on empty regions.

---

### `create_clump_boundary_traces(clumps, selected_ids=None) -> list[dict]`

Creates one Plotly `scatter` trace per clump, rendering its boundary as a closed polygon.

**Visual styling**:

- **Default clumps**: cyan (`#00ccff`), thin lines (width 1.2)
- **Selected clumps**: red (`#ff4444`), thicker lines (width 2.5)

Each trace contains:

```python
{
    "type": "scatter",
    "x": [x0, x1, ..., x0],       # Boundary x coords (closed polygon)
    "y": [y0, y1, ..., y0],       # Boundary y coords
    "mode": "lines",
    "line": {"color": "#00ccff", "width": 1.2},
    "name": "Clump 4",
    "hoverinfo": "text",
    "text": "Clump 4 (disk)",      # Shown on hover
    "showlegend": False,
}
```

Returns a list of 23 traces (one per clump in the toy data).

---

### `create_centroid_markers(clumps) -> dict`

Creates a single scatter trace with all clump centroids as `x` markers with ID labels.

```python
{
    "type": "scatter",
    "x": [71.8, 77.4, ...],           # Centroid x coords
    "y": [19.9, 96.2, ...],           # Centroid y coords
    "mode": "markers+text",
    "marker": {"color": "#ffffff", "size": 5, "symbol": "x"},
    "text": ["0", "1", ...],          # Clump IDs as labels
    "textposition": "top right",
    "textfont": {"color": "#cccccc", "size": 9},
    "hovertemplate": "Clump %{text}<br>x: %{x:.1f}<br>y: %{y:.1f}<extra></extra>",
    "name": "Centroids",
    "showlegend": False,
}
```

---

### `build_viewer_figure(datacube, channel_index, clumps, selected_ids=None, colorscale="Viridis", stretch="log") -> dict`

**Main entry point**. Assembles the complete Plotly figure by combining all the traces above with a layout.

**Parameters**:

| Param | Type | Description |
| ------- | ------ | ------------- |
| `datacube` | `DataCube` | Datacube to visualize |
| `channel_index` | `int` | Which filter channel to display |
| `clumps` | `ClumpCatalog` | Clump catalog for overlays |
| `selected_ids` | `list[int] \| None` | Clumps to highlight in red |
| `colorscale` | `str` | Plotly colorscale name |
| `stretch` | `str` | `"log"`, `"lupton_asinh"`, or `"power"` |

**Returns**: Complete Plotly figure dict:

```python
{
    "data": [
        { heatmap trace },
        { boundary trace for clump 0 },
        { boundary trace for clump 1 },
        ...
        { boundary trace for clump 22 },
        { centroid markers trace },
    ],
    "layout": {
        "title": {"text": "cut_datacube_nircam β€” F200W"},
        "xaxis": {"title": "x (pixels)", "scaleanchor": "y"},
        "yaxis": {"title": "y (pixels)"},
        "plot_bgcolor": "#1a1a2e",      # Dark theme
        "paper_bgcolor": "#16213e",
        "dragmode": "pan",               # Default interaction mode
        "margin": {"l": 50, "r": 20, "t": 40, "b": 50},
    }
}
```

The `scaleanchor: "y"` ensures the aspect ratio is preserved (1 pixel = 1 pixel on screen).

Total traces: 1 (heatmap) + 23 (boundaries) + 1 (centroids) = **25 traces**.

---

## visualization/properties_panel.py β€” Property Formatting

**Location**: `src/jellyscope/visualization/properties_panel.py`

### `format_clump_properties(clump: ClumpProperties) -> dict`

Converts a `ClumpProperties` model into a human-readable display dict.

**Returns**:

```python
{
    "Clump ID": 4,
    "Component": "Disk",                # Capitalized
    "Inside disk": "Yes",               # Bool β†’ "Yes"/"No"
    "Area (pixels)": 144,
    "Area (arcsecΒ²)": "0.0576",         # Formatted to 4 decimals
    "Area (kpcΒ²)": "1.1165",
    "R_eff (arcsec)": "0.1354",
    "R_eff (kpc)": "0.5961",
    "Centroid x": "115.3",              # Formatted to 1 decimal
    "Centroid y": "159.1",
}
```

This dict is sent to the frontend, where `app.js` renders it as an HTML table:

```html
<table class="prop-table">
    <tr><td>Clump ID</td><td>4</td></tr>
    <tr><td>Component</td><td>Disk</td></tr>
    ...
</table>
```

---

## visualization/rgb_composite.py β€” RGB Composite

**Location**: `src/jellyscope/visualization/rgb_composite.py`

The module offers two RGB composite methods, selectable at call time:

- **`percentile_asinh`** (default) β€” per-band median subtract, percentile clip, asinh stretch, pedestal cut. Not strictly color-preserving, but produces clean, deep-field-style images. Recipe contributed by Andressa.
- **`lupton`** β€” Lupton et al. (2004) Eq. 2 color-preserving mapping. The output color of an object depends only on its flux ratios, not its brightness.

### `RGBMethod` type alias

```python
RGBMethod = Literal["percentile_asinh", "lupton"]
```

Used by `build_rgb_figure` and the corresponding FastAPI endpoint to dispatch between the two methods.

### `percentile_asinh_composite(r_data, g_data, b_data, pmin=10.0, pmax=99.9, scale=0.1, floor=0.05, weights=(1.0, 1.02, 1.02)) -> np.ndarray`

Per-band, independent stretch pipeline.

**Parameters**:

| Param | Type | Description |
| ------- | ------ | ------------- |
| `r_data`, `g_data`, `b_data` | `np.ndarray` | 2D flux arrays |
| `pmin`, `pmax` | `float` | Percentile bounds for the linear clip (in percent) |
| `scale` | `float` | asinh softening; smaller boosts faint features more |
| `floor` | `float` | Pedestal cut applied after the stretch β€” pixels below become 0 |
| `weights` | `tuple[float, float, float]` | Per-channel multipliers `(wR, wG, wB)` applied at the end |

**Algorithm** (per band, then combined): median-subtract β†’ percentile-clip to `[pmin, pmax]` β†’ asinh stretch via `arcsinh(y/scale) / arcsinh(1/scale)` β†’ floor cut β†’ channel weight. NaN-safe: any pixel non-finite in any band becomes black.

The actual per-band work is done by the private helper `_normalize_band_asinh`.

### `lupton_rgb_composite(r_data, g_data, b_data, softening=8.0, alpha=None) -> np.ndarray`

Lupton et al. (2004) Eq. 2.

**Parameters**:

| Param | Type | Description |
| ------- | ------ | ------------- |
| `r_data` | `np.ndarray` | 2D flux array for the red channel |
| `g_data` | `np.ndarray` | 2D flux array for the green channel |
| `b_data` | `np.ndarray` | 2D flux array for the blue channel |
| `softening` | `float` | Q parameter controlling linear-to-log transition (typical: 8-9) |
| `alpha` | `float \| None` | Linear stretch factor. Auto-estimated from noise if None |

**Algorithm**:

1. Compute total intensity: `I = (R + G + B) / 3`
2. Estimate background `(m, sigma)` via sigma-clipped stats
3. Compute `alpha = 0.02 / sigma` if not given
4. Apply asinh stretch to intensity: `f(I) = arcsinh(alpha * Q * (I - m)) / Q`
5. Color-preserving scale: `ratio = f(I) / (I - m)`
6. Scale each band: `R_out = (R - m) * ratio`, etc.
7. Per-pixel saturation clamping: if max(R,G,B) > 1, scale down to preserve hue
8. Global normalization to [0,1] using 99.5th percentile
9. Return uint8 array of shape `(ny, nx, 3)`

### `build_rgb_figure(datacube, r_index, g_index, b_index, clumps, selected_ids=None, method="percentile_asinh", softening=8.0, alpha=None) -> dict`

Assembles the Plotly figure with RGB image + clump overlays.

**Parameters**:

| Param | Type | Description |
| ------- | ------ | ------------- |
| `datacube` | `DataCube` | Datacube to visualize |
| `r_index`, `g_index`, `b_index` | `int` | Channel indices for the R, G, B bands |
| `clumps` | `ClumpCatalog` | Clump catalog for overlays |
| `selected_ids` | `list[int] \| None` | Clumps to highlight in red |
| `method` | `RGBMethod` | `"percentile_asinh"` (default) or `"lupton"` |
| `softening` | `float` | Lupton Q β€” only used when `method="lupton"` |
| `alpha` | `float \| None` | Lupton linear stretch factor β€” only used when `method="lupton"` |

**Coordinate system note**: The RGB image is rendered as a PNG embedded in `layout.images[]` rather than as a `go.Image` trace. The figure pipeline:

1. Encode the `(ny, nx, 3)` uint8 array as a base64 PNG via `PIL.Image.fromarray(np.flipud(rgb)).save(buf, format="PNG")`. The `np.flipud` is the only coordinate transform β€” it compensates for PNG's top-down row order so the image displays with row 0 at the bottom (matching heatmap convention).
2. Place the PNG via `layout.images = [{source: "data:image/png;base64,...", x: 0, y: 0, sizex: nx, sizey: ny, xanchor: "left", yanchor: "bottom", layer: "below", sizing: "stretch"}]`.
3. Add an invisible `go.Heatmap` trace (`opacity: 0`, identical extent) to carry click/hover events. Plotly's `plotly_click` returns `point.x`/`point.y` in raw FITS array space directly from this trace.
4. Boundary and centroid scatter traces use raw FITS pixel coordinates β€” **no `ny-1-y` transform** at the trace level, and **no `autorange: "reversed"`** on the y-axis.

This replaces the prior `go.Image` approach, which forced `autorange: "reversed"` and required mirrored y-coordinates on every overlay trace. The current scheme keeps overlay code identical between single-band and RGB views.

---