Skip to content

Commit 1728628

Browse files
hongquanliclaude
andcommitted
Address PR review comments #8, #14, #16
- Fix #8: Update docstring to clarify dtype casting behavior (may clip) - Fix #14: Add OSError handling in load_flatfield for file access errors - Fix #16: Sync flatfield checkbox state with loaded/cleared status 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent ed4e8d4 commit 1728628

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

gui/app.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,8 @@ def on_file_dropped(self, file_path):
12211221
self.view_flatfield_button.setEnabled(False)
12221222
self.clear_flatfield_button.setEnabled(False)
12231223
self.save_flatfield_button.setEnabled(False)
1224+
# Uncheck flatfield correction when no flatfield is loaded
1225+
self.flatfield_checkbox.setChecked(False)
12241226

12251227
# Auto-load existing flatfield if present
12261228
flatfield_path = path.parent / f"{path.stem}_flatfield.npy"
@@ -1340,6 +1342,8 @@ def on_flatfield_dropped(self, file_path):
13401342
)
13411343
self.view_flatfield_button.setEnabled(True)
13421344
self.clear_flatfield_button.setEnabled(True)
1345+
# Enable flatfield correction when successfully loaded
1346+
self.flatfield_checkbox.setChecked(True)
13431347
self.log(f"Loaded flatfield from {file_path}: {self.flatfield.shape}")
13441348
except Exception as e:
13451349
self.flatfield_status.setText(f"Load failed: {e}")

src/tilefusion/flatfield.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ def apply_flatfield(
120120
Returns
121121
-------
122122
corrected : ndarray
123-
Corrected tile with shape (C, Y, X), same dtype as input.
123+
Corrected tile with shape (C, Y, X), cast back to the input dtype.
124+
Note: Values are computed in float then cast to the original dtype,
125+
which may result in clipping for values outside the dtype's valid
126+
range (e.g., negative values clipped to 0 for unsigned types).
124127
125128
Raises
126129
------
@@ -248,10 +251,16 @@ def load_flatfield(path: Path) -> Tuple[np.ndarray, Optional[np.ndarray]]:
248251
249252
Raises
250253
------
254+
OSError
255+
If the file cannot be read (not found, permission denied, etc.).
251256
ValueError
252257
If the file format is invalid (not a dictionary with 'flatfield' key).
253258
"""
254-
loaded = np.load(path, allow_pickle=True)
259+
try:
260+
loaded = np.load(path, allow_pickle=True)
261+
except OSError as exc:
262+
raise OSError(f"Cannot read flatfield file '{path}': {exc}") from exc
263+
255264
try:
256265
data = loaded.item()
257266
except (AttributeError, ValueError) as exc:

0 commit comments

Comments
 (0)