From 01fee3733a8c98d85991163113f026e89e0bf788 Mon Sep 17 00:00:00 2001 From: Mike Jarvis Date: Mon, 8 Dec 2025 17:26:02 -0500 Subject: [PATCH 1/3] Add docs to RealGalaxyCatalog.close() (#1333) --- galsim/real.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/galsim/real.py b/galsim/real.py index 1d58eb5904..ca1af17839 100644 --- a/galsim/real.py +++ b/galsim/real.py @@ -548,8 +548,8 @@ def __init__(self, file_name=None, sample=None, dir=None, preload=False, logger= logger.debug('RealGalaxyCatalog %s has %d objects',self.file_name,self.nobjects) self._preload = preload - self.loaded_files = {} self.saved_noise_im = {} + self.loaded_files = {} # The pyfits commands aren't thread safe. So we need to make sure the methods that # use pyfits are not run concurrently from multiple threads. self.gal_lock = Lock() # Use this when accessing gal files @@ -647,13 +647,22 @@ def __del__(self): self.close() def close(self): + """Close all loaded files. + + Usually it is a performance improvement to keep the various input files open while using + the RealGalaxyCatalog, especially if the access of the galaxies is in semi-random order. + However, if you want to release the memory tied up in these files before the + RealGalaxyCatalog goes out of scope, then this method will release the memroy directly. + """ # Need to close any open files. # Make sure to check if loaded_files exists, since the constructor could abort # before it gets to the place where loaded_files is built. if hasattr(self, 'loaded_files'): for f in self.loaded_files.values(): f.close() - self.loaded_files = {} + self.loaded_files.clear() + # Also release the memory in the noise images, even though this is usually negligible. + self.saved_noise_im.clear() def getNObjects(self) : return self.nobjects def __len__(self): return self.nobjects From 6fe001fcc389d9078231bb13eb01ade925fd7bc6 Mon Sep 17 00:00:00 2001 From: Mike Jarvis Date: Tue, 9 Dec 2025 00:29:52 -0500 Subject: [PATCH 2/3] Update galsim/real.py Co-authored-by: Rachel Mandelbaum --- galsim/real.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/galsim/real.py b/galsim/real.py index ca1af17839..a39e5ed2bc 100644 --- a/galsim/real.py +++ b/galsim/real.py @@ -652,7 +652,7 @@ def close(self): Usually it is a performance improvement to keep the various input files open while using the RealGalaxyCatalog, especially if the access of the galaxies is in semi-random order. However, if you want to release the memory tied up in these files before the - RealGalaxyCatalog goes out of scope, then this method will release the memroy directly. + RealGalaxyCatalog goes out of scope, then this method will release the memory directly. """ # Need to close any open files. # Make sure to check if loaded_files exists, since the constructor could abort From d8726fe836abc099a9321b1e4c207c75f348da8a Mon Sep 17 00:00:00 2001 From: Mike Jarvis Date: Tue, 9 Dec 2025 00:37:20 -0500 Subject: [PATCH 3/3] Add note in RGC class doc about close method (#1333) --- galsim/real.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/galsim/real.py b/galsim/real.py index a39e5ed2bc..01c144ea16 100644 --- a/galsim/real.py +++ b/galsim/real.py @@ -508,6 +508,14 @@ class RealGalaxyCatalog: GalSim knows the location of the installation share directory, so it will automatically look for it there. + .. note:: + + A RealGalaxyCatalog instance will cache data from the input files that it loads. + Normally, this leads to a performance improvement, especially if objects are selected + from the catalog semi-randomly, since it is faster to access the data in memory. + If you want to free the cached memory before the RealGalaxyCatalog goes out of + scope, you can use the `close` method to do so. + Parameters: file_name: The file containing the catalog. [default: None, which will look for the F814W<25.2 COSMOS catalog in $PREFIX/share/galsim. It will raise an