LSSTDESC/healsparse

healsparse won't read a `pathlib.Path`

Closed this issue · 0 comments

When trying to read from a pathlib.Path object, healsparse will throw an error about only supporting fits and parquet files; e.g.,

(Pdb) hsp_plus
PosixPath('/pscratch/sd/s/smau/y6-image-sims/debug-cosmos-rand-piff-median_color-seed/DES0608-5414/16200/plus/des-pizza-slices-y6/DES0608-5414/metadetect/DES0608-5414_metadetect-config_mdetcat_part0000-healsparse-mask.hs')
(Pdb) healsparse.HealSparseMap.read(hsp_plus)
*** NotImplementedError: HealSparse only supports fits and parquet files.
(Pdb) healsparse.HealSparseMap.read(hsp_plus.as_posix())
HealSparseMap: nside_coverage = 128, nside_sparse = 131072, int32, 2551130 valid pixels

the error is also slightly confusing as the file format is not a problem.

This is happening because a Path doesn't match as a str (

if isinstance(filename_or_fits, str):
try:
fits = HealSparseFits(filename_or_fits)
is_fits = True
fits.close()
except OSError:
is_fits = False
if not is_fits:
is_parquet_file = check_parquet_dataset(filename_or_fits)
elif isinstance(filename_or_fits, HealSparseFits):
is_fits = True
else:
raise NotImplementedError("HealSparse only supports fits and parquet files.")
)

One possible solution would be checking

isinstance(filename_or_fits, (str, Path))

instead of

isinstance(filename_or_fits, str)

I haven't checked if any of the code downstream would complain about a Path...