From 26f1f1c367a928ca154138e2341de6676f66e9bb Mon Sep 17 00:00:00 2001 From: Terrier Date: Thu, 21 Nov 2019 08:25:12 +0100 Subject: [PATCH 1/4] introduce faster observations.select_time --- gammapy/data/observations.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/gammapy/data/observations.py b/gammapy/data/observations.py index 03907f45e2..a3df385b15 100644 --- a/gammapy/data/observations.py +++ b/gammapy/data/observations.py @@ -320,6 +320,14 @@ def __str__(self): s += str(obs) return s + @property + def tstart(self): + return Time([_.tstart for _ in self]) + + @property + def tstop(self): + return Time([_.tstop for _ in self]) + @property def ids(self): """List of obs IDs (`list`)""" @@ -340,9 +348,16 @@ def select_time(self, time_interval): A new observations instance of the specified time interval """ new_obs_list = [] - for obs in self: - new_obs = obs.select_time(time_interval) - if len(new_obs.gti.table) > 0: + mask = self.tstart < time_interval[1] + mask &= self.tstop > time_interval[0] + + indices = np.where(mask)[0] + print(indices) + if len(indices)>0: + for index in indices: + obs = self[index] + new_obs = obs.select_time(time_interval) +# if len(new_obs.gti.table>0): new_obs_list.append(new_obs) return self.__class__(new_obs_list) From c53be3bc48c032d078fb3a5fe346d1f4a1fcc9a6 Mon Sep 17 00:00:00 2001 From: Terrier Date: Thu, 21 Nov 2019 08:28:28 +0100 Subject: [PATCH 2/4] tiny update --- gammapy/data/observations.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/gammapy/data/observations.py b/gammapy/data/observations.py index a3df385b15..17ba4377db 100644 --- a/gammapy/data/observations.py +++ b/gammapy/data/observations.py @@ -352,12 +352,10 @@ def select_time(self, time_interval): mask &= self.tstop > time_interval[0] indices = np.where(mask)[0] - print(indices) if len(indices)>0: for index in indices: obs = self[index] new_obs = obs.select_time(time_interval) -# if len(new_obs.gti.table>0): new_obs_list.append(new_obs) return self.__class__(new_obs_list) From e8e512d841cf9b40e44f5e89a9aecf7493ffe7f4 Mon Sep 17 00:00:00 2001 From: Terrier Date: Thu, 21 Nov 2019 08:35:12 +0100 Subject: [PATCH 3/4] Removed tstart and tstop properties --- gammapy/data/observations.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/gammapy/data/observations.py b/gammapy/data/observations.py index 17ba4377db..b3920903c7 100644 --- a/gammapy/data/observations.py +++ b/gammapy/data/observations.py @@ -320,14 +320,6 @@ def __str__(self): s += str(obs) return s - @property - def tstart(self): - return Time([_.tstart for _ in self]) - - @property - def tstop(self): - return Time([_.tstop for _ in self]) - @property def ids(self): """List of obs IDs (`list`)""" @@ -347,9 +339,13 @@ def select_time(self, time_interval): new_observations : `~gammapy.data.Observations` A new observations instance of the specified time interval """ + + tstart = Time([_.tstart for _ in self]) + tstop = Time([_.tstop for _ in self]) + new_obs_list = [] - mask = self.tstart < time_interval[1] - mask &= self.tstop > time_interval[0] + mask = tstart < time_interval[1] + mask &= tstop > time_interval[0] indices = np.where(mask)[0] if len(indices)>0: From b1d48c7c35d8b4115a3725e66ee19bd70d58cb00 Mon Sep 17 00:00:00 2001 From: Terrier Date: Thu, 21 Nov 2019 10:47:41 +0100 Subject: [PATCH 4/4] cleaner implementation without array --- gammapy/data/observations.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/gammapy/data/observations.py b/gammapy/data/observations.py index b3920903c7..feb27eb776 100644 --- a/gammapy/data/observations.py +++ b/gammapy/data/observations.py @@ -339,18 +339,10 @@ def select_time(self, time_interval): new_observations : `~gammapy.data.Observations` A new observations instance of the specified time interval """ - - tstart = Time([_.tstart for _ in self]) - tstop = Time([_.tstop for _ in self]) - new_obs_list = [] - mask = tstart < time_interval[1] - mask &= tstop > time_interval[0] - indices = np.where(mask)[0] - if len(indices)>0: - for index in indices: - obs = self[index] + for obs in self: + if (obs.tstart < time_interval[1]) & (obs.tstop > time_interval[0]): new_obs = obs.select_time(time_interval) new_obs_list.append(new_obs)