From d8901d6487981fd16009f088c8749fe32e850922 Mon Sep 17 00:00:00 2001 From: Joanne Bogart Date: Thu, 2 May 2024 15:01:38 -0700 Subject: [PATCH 1/3] Fix handling of slice by GaiaCollection; eliminate unnecessary _id member --- skycatalogs/objects/gaia_object.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/skycatalogs/objects/gaia_object.py b/skycatalogs/objects/gaia_object.py index 2a13ecbb..cfc8bf9e 100644 --- a/skycatalogs/objects/gaia_object.py +++ b/skycatalogs/objects/gaia_object.py @@ -350,7 +350,6 @@ def __init__(self, df, sky_catalog, source_type, use_lut, mjd): self.df = df self._sky_catalog = sky_catalog self._partition_id = None - self._id = np.array([f"gaia_dr2_{df.iloc[key]['id']}" for key in range(len(df))]) self._mask = None self._object_type_unique = source_type self._use_lut = use_lut @@ -372,22 +371,19 @@ def mjd(self): return self._mjd def __getitem__(self, key): + cols = ('id', 'ra_deg', 'dec_deg', 'phot_bp_mean_flux', + 'phot_rp_mean_flux') if isinstance(key, int) or isinstance(key, np.int64): - row = {col: self.df[col][key] for col in ('id', 'ra_deg', - 'dec_deg', - 'phot_bp_mean_flux', - 'phot_rp_mean_flux')} + row = {col: self.df[col][key] for col in cols} return GaiaObject(row, self, key) elif type(key) == slice: - ixdata = [i for i in range(min(key.stop, len(self._id)))] + ixdata = [i for i in range(min(key.stop, len(self.df['id'])))] ixes = itertools.islice(ixdata, key.start, key.stop, key.step) - return [self._object_class(self.df.iloc[i], self, i) for i in ixes] + return [self.__getitem__(i) for i in ixes] elif type(key) == tuple and isinstance(key[0], Iterable): - # check it's a list of int-like? - return [self._object_class(self.df.iloc[i], self, - i) for i in key[0]] + return [self.__getitem__(i) for i in key[0]] def __len__(self): return len(self.df) From aaa953de56d549f17e99d7a0c95f5050c4380ea2 Mon Sep 17 00:00:00 2001 From: Joanne Bogart Date: Thu, 2 May 2024 15:22:16 -0700 Subject: [PATCH 2/3] make it easier to get id_prefix --- skycatalogs/objects/gaia_object.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/skycatalogs/objects/gaia_object.py b/skycatalogs/objects/gaia_object.py index cfc8bf9e..3024a917 100644 --- a/skycatalogs/objects/gaia_object.py +++ b/skycatalogs/objects/gaia_object.py @@ -85,9 +85,9 @@ def __init__(self, obj_pars, parent_collection, index): # dec = np.degrees(obj_pars['coord_dec']) ra = obj_pars['ra_deg'] dec = obj_pars['dec_deg'] - # Form the object id from the GAIA catalog id with the string - # 'gaia_dr2_' prepended. - id_prefix = GaiaCollection.get_config()['id_prefix'] + # Form the object id from the GAIA catalog id with a string + # like 'gaia_dr2_' prepended. + id_prefix = GaiaCollection._id_prefix obj_id = f"{id_prefix}{obj_pars['id']}" super().__init__(ra, dec, obj_id, 'gaia_star', belongs_to=parent_collection, belongs_index=index) @@ -235,7 +235,7 @@ class GaiaCollection(ObjectCollection): @classmethod def set_config(cls, config): GaiaCollection._gaia_config = config - + GaiaCollection._id_prefix = config['id_prefix'] @classmethod def get_config(cls): return GaiaCollection._gaia_config From 263539802c929f66a1c5685117680f28db4b1ac0 Mon Sep 17 00:00:00 2001 From: Joanne Bogart Date: Thu, 9 May 2024 11:44:55 -0700 Subject: [PATCH 3/3] address reviewer comments --- skycatalogs/objects/base_object.py | 15 ++++----------- skycatalogs/objects/gaia_object.py | 9 +++++---- skycatalogs/objects/sso_object.py | 11 ++--------- 3 files changed, 11 insertions(+), 24 deletions(-) diff --git a/skycatalogs/objects/base_object.py b/skycatalogs/objects/base_object.py index 1ff4b07c..c319317d 100644 --- a/skycatalogs/objects/base_object.py +++ b/skycatalogs/objects/base_object.py @@ -600,17 +600,10 @@ def __getitem__(self, key): elif type(key) == slice: if key.start is None: key.start = 0 - ixdata = [i for i in range(min(key.stop, len(self._ra)))] - ixes = itertools.islice(ixdata, key.start, key.stop, key.step) - return [self._object_class(self._ra[i], self._dec[i], self._id[i], - object_type, self, i) - for i in ixes] + return [self.__getitem__(i) for i in range(self.__len__())[key]] elif type(key) == tuple and isinstance(key[0], Iterable): - # check it's a list of int-like? - return [self._object_class(self._ra[i], self._dec[i], self._id[i], - object_type, self, i) - for i in key[0]] + return[self.__getitem__(i) for i in key[0]] def get_partition_id(self): return self._partition_id @@ -725,8 +718,8 @@ def __getitem__(self, key): If key is a slice return a list of object ''' one_only = isinstance(key, int) or isinstance(key, np.int64) - is_slice = type(key) == slice - is_list = type(key) == tuple and isinstance(key[0], Iterable) + is_slice = isinstance(key, slice) + is_list = isinstance(key, tuple) and isinstance(key[0], Iterable) if one_only: start = key diff --git a/skycatalogs/objects/gaia_object.py b/skycatalogs/objects/gaia_object.py index 3024a917..989e31cb 100644 --- a/skycatalogs/objects/gaia_object.py +++ b/skycatalogs/objects/gaia_object.py @@ -377,10 +377,11 @@ def __getitem__(self, key): row = {col: self.df[col][key] for col in cols} return GaiaObject(row, self, key) - elif type(key) == slice: - ixdata = [i for i in range(min(key.stop, len(self.df['id'])))] - ixes = itertools.islice(ixdata, key.start, key.stop, key.step) - return [self.__getitem__(i) for i in ixes] + elif isinstance(key, slice): + # ixdata = [i for i in range(min(key.stop, len(self.df['id'])))] + # ixes = itertools.islice(ixdata, key.start, key.stop, key.step) + # return [self.__getitem__(i) for i in ixes] + return [self.__getitem__(i) for i in range(len(self.df))[key]] elif type(key) == tuple and isinstance(key[0], Iterable): return [self.__getitem__(i) for i in key[0]] diff --git a/skycatalogs/objects/sso_object.py b/skycatalogs/objects/sso_object.py index 6ec661d4..df706a8a 100644 --- a/skycatalogs/objects/sso_object.py +++ b/skycatalogs/objects/sso_object.py @@ -163,14 +163,7 @@ def __getitem__(self, key): elif type(key) == slice: if key.start is None: key.start = 0 - ixdata = [i for i in range(min(key.stop, len(self._ra)))] - ixes = itertools.islice(ixdata, key.start, key.stop, key.step) - return [self._object_class(self._ra[i], self._dec[i], self._id[i], - object_type, self, i, self._mjds[i]) - for i in ixes] + return [self.__getitem__(i) for i in range(self.__len__())[key]] elif type(key) == tuple and isinstance(key[0], Iterable): - # check it's a list of int-like? - return [self._object_class(self._ra[i], self._dec[i], self._id[i], - object_type, self, i, self._mjds[i]) - for i in key[0]] + return [self.__getitem__(i) for i in key[0]]