Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c124f16
fix: fix loadimage function to robustly load image.
stevenhua0320 Oct 25, 2025
757b223
chore: add news item
stevenhua0320 Oct 25, 2025
3f4f892
chore: change docstring.
stevenhua0320 Oct 25, 2025
9a28a4b
tests: add test to loadimage
stevenhua0320 Oct 25, 2025
b2cc5c5
test: change test format.
stevenhua0320 Oct 26, 2025
0383f1f
test: change test to only catch function behavior.
stevenhua0320 Oct 26, 2025
6124e29
fix: Fix directory display so that CI can identify
stevenhua0320 Oct 26, 2025
709b48c
tests: change file-not-found to raise FileNotFound error.
stevenhua0320 Oct 27, 2025
e34638e
tests: change copy of example to temp folder & not using logic in test
stevenhua0320 Oct 27, 2025
22a2bcd
chore: change test name
stevenhua0320 Oct 27, 2025
e5685ca
chore: delete example.tiff
stevenhua0320 Oct 28, 2025
8b00059
tests: Change case 4 to FileNotFoundError.
stevenhua0320 Oct 29, 2025
98c7220
Fix: Fix news message and rename to comply PEP-8 standard.
stevenhua0320 Oct 31, 2025
46f175b
fix: change test logic to make it constant.
stevenhua0320 Oct 31, 2025
45f2952
style: change parametrization style.
stevenhua0320 Oct 31, 2025
4369cc2
Refactor image loading test with new expected values
sbillinge Oct 31, 2025
556e141
test: add starting point and last point for test.
stevenhua0320 Oct 31, 2025
ac7ece7
test: add test for flip_image mechanism.
stevenhua0320 Nov 1, 2025
1567b45
test: add more points in validating flip.
stevenhua0320 Nov 1, 2025
03ae177
style: change flip_image parameter to make non-flipping default.
stevenhua0320 Nov 1, 2025
c8b83e5
style: change flip_image to pass attribute to make change
stevenhua0320 Nov 3, 2025
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
23 changes: 23 additions & 0 deletions news/load-image-fix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* Fixed `loadimage` function in any directory.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

load_image() function correctly finds files when passed a relative path


**Security:**

* <news item>
52 changes: 31 additions & 21 deletions src/diffpy/srxplanar/loadimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import fnmatch
import os
import time
from pathlib import Path

import numpy as np

Expand Down Expand Up @@ -66,31 +67,40 @@ def flipImage(self, pic):
return pic

def loadImage(self, filename):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's make the function have a pep8 compliant name. We may as well fix these when we touch the function.

"""Load image file, if failed (for example loading an incomplete
file), then it will keep trying loading file for 5s.
"""Load image file using pathlib. If loading fails (e.g.
incomplete file), retry for 5 seconds (10×0.5s).

:param filename: str, image file name
:return: 2d ndarray, 2d image array (flipped)
:param filename: str or Path, image file name or path
:return: 2D ndarray, flipped image array
"""
if os.path.exists(filename):
filename = Path(
filename
).expanduser() # handle "~", make it a Path object
if filename.exists():
filenamefull = filename
else:
filenamefull = os.path.join(self.opendirectory, filename)
image = np.zeros(10000).reshape(100, 100)
if os.path.exists(filenamefull):
i = 0
while i < 10:
try:
if os.path.splitext(filenamefull)[-1] == ".npy":
image = np.load(filenamefull)
else:
image = openImage(filenamefull)
i = 10
except FileNotFoundError:
i = i + 1
time.sleep(0.5)
image = self.flipImage(image)
image[image < 0] = 0
found_files = list(Path.home().rglob(filename.name))
filenamefull = found_files[0] if found_files else None

if filenamefull is None or not filenamefull.exists():
print(f"Warning: file not found: {filename}")
return np.zeros((100, 100))

image = np.zeros((100, 100))
for _ in range(10): # retry 10 times (5 seconds total)
try:
if filenamefull.suffix == ".npy":
image = np.load(filenamefull)
else:
image = openImage(
str(filenamefull)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the conversation above, didn't we want to remove this str? That was my point actually

) # openImage expects str
break
except FileNotFoundError:
time.sleep(0.5)

image = self.flipImage(image)
image[image < 0] = 0
return image

def genFileList(
Expand Down
Loading