Skip to content

Commit

Permalink
Use monotonic(), add precision to %f
Browse files Browse the repository at this point in the history
  • Loading branch information
snbianco committed Oct 21, 2024
1 parent 5efd55b commit 266f11c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
12 changes: 6 additions & 6 deletions astrocut/cube_cut.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import warnings
from concurrent.futures import ThreadPoolExecutor
from itertools import product
from time import time
from time import monotonic
from typing import Any, Dict, Literal, Union

import astropy.units as u
Expand Down Expand Up @@ -843,7 +843,7 @@ def cube_cut(
if unsuccessful returns None.
"""

start_time = time()
start_time = monotonic()
# Log messages based on verbosity
_handle_verbose(verbose)

Expand Down Expand Up @@ -903,14 +903,14 @@ def cube_cut(
cutout_wcs_full = self._get_full_cutout_wcs(cube[2].header)
max_dist, sigma = self._fit_cutout_wcs(cutout_wcs_full, img_cutout.shape[1:])
log.debug("Maximum distance between approximate and true location: %s", max_dist)
log.debug("Error in approximate WCS (sigma): %f", sigma)
log.debug("Error in approximate WCS (sigma): %.4f", sigma)

cutout_wcs_dict = self._get_cutout_wcs_dict()

# Build the TPF
tpf_object = self._build_tpf(cube, img_cutout, uncert_cutout, cutout_wcs_dict, aperture)

write_time = time()
write_time = monotonic()

if not target_pixel_file:
_, flename = os.path.split(cube_file)
Expand All @@ -934,7 +934,7 @@ def cube_cut(
# Write the TPF
tpf_object.writeto(target_pixel_file, overwrite=True, checksum=True)

log.debug("Write time: %.2f sec", (time() - write_time))
log.debug("Total time: %.2f sec", (time() - start_time))
log.debug("Write time: %.2f sec", (monotonic() - write_time))
log.debug("Total time: %.2f sec", (monotonic() - start_time))

return target_pixel_file
11 changes: 5 additions & 6 deletions astrocut/cutouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import os
import warnings
import numpy as np

from time import time
from time import monotonic

from astropy import log as astropy_log
from astropy.io import fits
Expand Down Expand Up @@ -243,7 +242,7 @@ def fits_cut(input_files, coordinates, cutout_size, correct_wcs=False, extension
"""
# Log messages based on verbosity
_handle_verbose(verbose)
start_time = time()
start_time = monotonic()

# Making sure we have an array of images
if isinstance(input_files, str):
Expand Down Expand Up @@ -369,7 +368,7 @@ def fits_cut(input_files, coordinates, cutout_size, correct_wcs=False, extension
all_paths.append(cutout_path)
log.debug(cutout_path)

log.debug("Total time: %.2f sec", time() - start_time)
log.debug("Total time: %.2f sec", monotonic() - start_time)

if memory_only:
return all_hdus
Expand Down Expand Up @@ -508,7 +507,7 @@ def img_cut(input_files, coordinates, cutout_size, stretch='asinh', minmax_perce
"""
# Log messages based on verbosity
_handle_verbose(verbose)
start_time = time()
start_time = monotonic()

# Making sure we have an array of images
if isinstance(input_files, str):
Expand Down Expand Up @@ -613,6 +612,6 @@ def img_cut(input_files, coordinates, cutout_size, stretch='asinh', minmax_perce
Image.fromarray(cutout).save(file_path)

log.debug("Cutout fits file(s): %s", cutout_path)
log.debug("Total time: %.2f sec", time() - start_time)
log.debug("Total time: %.2f sec", monotonic() - start_time)

return cutout_path
22 changes: 11 additions & 11 deletions astrocut/make_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from copy import deepcopy
from datetime import date
from sys import platform, version_info
from time import time
from time import monotonic

import numpy as np
from astropy.io import fits
Expand Down Expand Up @@ -207,7 +207,7 @@ def _write_block(self, cube_hdu, start_row=0, end_row=None, fill_info_table=Fals

# Loop through files
for i, fle in enumerate(self.file_list):
st = time()
st = monotonic()
with fits.open(fle, mode='denywrite', memmap=True) as ffi_data:

# add the image and info to the arrays
Expand All @@ -230,7 +230,7 @@ def _write_block(self, cube_hdu, start_row=0, end_row=None, fill_info_table=Fals
nulval = ""
self.info_table[kwd][i] = ffi_data[1].header.get(kwd, nulval)

log.debug("Completed file %d in %.3f sec.", i, time() - st)
log.debug("Completed file %d in %.3f sec.", i, monotonic() - st)

# Fill block and flush to disk
cube_hdu[1].data[start_row:end_row, :, :, :] = sub_cube
Expand Down Expand Up @@ -310,7 +310,7 @@ def make_cube(self, file_list, cube_file="img-cube.fits", sector=None, max_memor
"""
# Log messages based on verbosity
_handle_verbose(verbose)
startTime = time()
startTime = monotonic()

self.max_memory = max_memory

Expand Down Expand Up @@ -349,7 +349,7 @@ def make_cube(self, file_list, cube_file="img-cube.fits", sector=None, max_memor

# Add the info table to the cube file
self._write_info_table()
log.debug("Total time elapsed: %.2f min", (time() - startTime) / 60)
log.debug("Total time elapsed: %.2f min", (monotonic() - startTime) / 60)

return self.cube_file

Expand Down Expand Up @@ -574,7 +574,7 @@ def _write_block(self, cube_hdu, start_row=0, end_row=None, fill_info_table=Fals
# Loop through files
for i, fle in enumerate(self.file_list):

st = time()
st = monotonic()

# In this section we will take the SCI data from self.file_list above
# and "paste" a cutout of the full SCI array into a 4d array called
Expand Down Expand Up @@ -625,7 +625,7 @@ def _write_block(self, cube_hdu, start_row=0, end_row=None, fill_info_table=Fals
else:
raise

log.debug("Completed file %d in %.3f sec.", i, time() - st)
log.debug("Completed file %d in %.3f sec.", i, monotonic() - st)

# Fill block and flush to disk
if not self.update:
Expand Down Expand Up @@ -730,7 +730,7 @@ def _update_cube(self, file_list, cube_file, sector=None, max_memory=50, verbose
"""
# Log messages based on verbosity
_handle_verbose(verbose)
startTime = time()
startTime = monotonic()
self.update = True # we're updating!
self.max_memory = max_memory

Expand Down Expand Up @@ -816,7 +816,7 @@ def _update_cube(self, file_list, cube_file, sector=None, max_memory=50, verbose

# Writing the info table to EXT2 of the FITS file
self._write_info_table()
log.debug("Total time elapsed: %.2f min", (time() - startTime) / 60)
log.debug("Total time elapsed: %.2f min", (monotonic() - startTime) / 60)

return self.cube_file

Expand Down Expand Up @@ -859,7 +859,7 @@ def make_cube(self, file_list, cube_file="img-cube.fits", sector=None, max_memor
"""
# Log messages based on verbosity
_handle_verbose(verbose)
startTime = time()
startTime = monotonic()

self.max_memory = max_memory

Expand Down Expand Up @@ -895,7 +895,7 @@ def make_cube(self, file_list, cube_file="img-cube.fits", sector=None, max_memor

# Add the info table to the cube file
self._write_info_table()
log.debug("Total time elapsed: %.2f min", (time() - startTime) / 60)
log.debug("Total time elapsed: %.2f min", (monotonic() - startTime) / 60)

return self.cube_file

Expand Down

0 comments on commit 266f11c

Please sign in to comment.