Skip to content

Commit

Permalink
error change, removing tempfile and memmap from fits.open since it do…
Browse files Browse the repository at this point in the history
…es that by default already, also there was a memmap error in prod
  • Loading branch information
LTDakin committed May 20, 2024
1 parent 2f3aab0 commit 2d6bcae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
47 changes: 20 additions & 27 deletions datalab/datalab_session/data_operations/data_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,30 +136,23 @@ def create_and_store_fits(self, hdu_list: fits.HDUList, percent=None, cur_percen

def get_fits_npdata(self, input_files: list[dict], percent=None, cur_percent=None) -> list[np.memmap]:
total_files = len(input_files)
memmap_paths = []

# get the fits urls, download their file, extract the image data, and store in a list
with tempfile.TemporaryDirectory() as temp_dir:
for index, file_info in enumerate(input_files, start=1):
basename = file_info.get('basename', 'No basename found')
archive_record = get_archive_from_basename(basename)

try:
fits_url = archive_record[0].get('url', 'No URL found')
except IndexError:
continue

with fits.open(fits_url) as hdu_list:
data = hdu_list['SCI'].data
memmap_path = os.path.join(temp_dir, f'memmap_{index}.dat')
memmap_array = np.memmap(memmap_path, dtype=data.dtype, mode='w+', shape=data.shape)
memmap_array[:] = data[:]
memmap_paths.append(memmap_path)

if percent is not None and cur_percent is not None:
self.set_percent_completion(cur_percent + index/total_files * percent)

return [
np.memmap(path, dtype=np.float32, mode='r', shape=memmap_array.shape)
for path in memmap_paths
]
image_data_list = []

# get the fits urls and extract the image data
for index, file_info in enumerate(input_files, start=1):
basename = file_info.get('basename', 'No basename found')
archive_record = get_archive_from_basename(basename)

try:
fits_url = archive_record[0].get('url', 'No URL found')
except Exception as e:
raise FileNotFoundError(f"No image found with specified basename: {basename} Error: {e}")

with fits.open(fits_url) as hdu_list:
data = hdu_list['SCI'].data
image_data_list.append(data)

if percent is not None and cur_percent is not None:
self.set_percent_completion(cur_percent + index/total_files * percent)

return image_data_list
3 changes: 1 addition & 2 deletions datalab/datalab_session/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ def get_archive_from_basename(basename: str) -> dict:
image_data = response.json()
results = image_data.get('results', None)
except Exception as e:
log.error(f"failed to fetch {basename} from archive, Error: {e}")
raise FileNotFoundError
raise FileNotFoundError(f"Error fetching image data from archive: {e}")

return results

Expand Down

0 comments on commit 2d6bcae

Please sign in to comment.