From cc3b07364a113bca696ed897f443a8ea0dfaeeb1 Mon Sep 17 00:00:00 2001 From: jaimerzp Date: Mon, 22 Jul 2024 15:06:57 +0100 Subject: [PATCH 01/16] no redshift needed --- sacc/tracers.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/sacc/tracers.py b/sacc/tracers.py index ac8db0a..a0c71e4 100644 --- a/sacc/tracers.py +++ b/sacc/tracers.py @@ -666,7 +666,7 @@ class QPNZTracer(BaseTracer, tracer_type='QPNZ'): The qp.ensemble in questions """ - def __init__(self, name, ens, z, **kwargs): + def __init__(self, name, ens, z=None, **kwargs): """ Create a tracer corresponding to a distribution in redshift n(z), for example of galaxies. @@ -679,11 +679,6 @@ def __init__(self, name, ens, z, **kwargs): ensemble: qp.Ensemble The qp.ensemble in questions - - z: array - A suggested array of redshifts to evaluate the - ensemble. Note that the QP ensemble does not assume - this grid. Returns ------- @@ -692,6 +687,12 @@ def __init__(self, name, ens, z, **kwargs): """ super().__init__(name, **kwargs) self.ensemble = ens + if z is None: + ens_meta = ens.metadata() + if 'bins' is in list(ens_meta.keys()): + z = ens_meta['bins'] + else: + raise ValueError("No redshift bins provided or found in ensemble metadata") self.nz = np.mean(ens.pdf(z),axis=0) self.z = z From 565f833c5bab4069d8c67cc470da686309ab5fb0 Mon Sep 17 00:00:00 2001 From: jaimerzp Date: Mon, 22 Jul 2024 15:22:50 +0100 Subject: [PATCH 02/16] bug --- sacc/tracers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sacc/tracers.py b/sacc/tracers.py index a0c71e4..5cce43e 100644 --- a/sacc/tracers.py +++ b/sacc/tracers.py @@ -689,7 +689,7 @@ def __init__(self, name, ens, z=None, **kwargs): self.ensemble = ens if z is None: ens_meta = ens.metadata() - if 'bins' is in list(ens_meta.keys()): + if 'bins' in list(ens_meta.keys()): z = ens_meta['bins'] else: raise ValueError("No redshift bins provided or found in ensemble metadata") From 4df11fba32474e9df3deecad8fdf96a2a926a052 Mon Sep 17 00:00:00 2001 From: jaimerzp Date: Mon, 22 Jul 2024 16:34:16 +0100 Subject: [PATCH 03/16] z needs to [0] of bins --- sacc/tracers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sacc/tracers.py b/sacc/tracers.py index 5cce43e..9ae5b21 100644 --- a/sacc/tracers.py +++ b/sacc/tracers.py @@ -690,7 +690,7 @@ def __init__(self, name, ens, z=None, **kwargs): if z is None: ens_meta = ens.metadata() if 'bins' in list(ens_meta.keys()): - z = ens_meta['bins'] + z = ens_meta['bins'][0] else: raise ValueError("No redshift bins provided or found in ensemble metadata") self.nz = np.mean(ens.pdf(z),axis=0) From e90be1a25d4f76e860f3daad68792259fadee705 Mon Sep 17 00:00:00 2001 From: jaimerzp Date: Mon, 26 Aug 2024 11:55:40 +0200 Subject: [PATCH 04/16] remove failure mode --- sacc/tracers.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/sacc/tracers.py b/sacc/tracers.py index 9ae5b21..10b2c79 100644 --- a/sacc/tracers.py +++ b/sacc/tracers.py @@ -687,14 +687,12 @@ def __init__(self, name, ens, z=None, **kwargs): """ super().__init__(name, **kwargs) self.ensemble = ens - if z is None: - ens_meta = ens.metadata() - if 'bins' in list(ens_meta.keys()): - z = ens_meta['bins'][0] - else: - raise ValueError("No redshift bins provided or found in ensemble metadata") + ens_meta = ens.metadata() + if (z is None) and ('bins' in ens_meta.keys()): + self.z = ens_meta['bins'][0] + else: + self.z = z self.nz = np.mean(ens.pdf(z),axis=0) - self.z = z @classmethod def to_tables(cls, instance_list): From e08a825a943da47bf09956a029380c0dbcfa243a Mon Sep 17 00:00:00 2001 From: jaimerzp Date: Mon, 26 Aug 2024 12:19:32 +0200 Subject: [PATCH 05/16] default grid --- sacc/tracers.py | 14 +++++++++----- test/test_sacc2.py | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/sacc/tracers.py b/sacc/tracers.py index 10b2c79..1674e5f 100644 --- a/sacc/tracers.py +++ b/sacc/tracers.py @@ -688,11 +688,15 @@ def __init__(self, name, ens, z=None, **kwargs): super().__init__(name, **kwargs) self.ensemble = ens ens_meta = ens.metadata() - if (z is None) and ('bins' in ens_meta.keys()): - self.z = ens_meta['bins'][0] - else: - self.z = z - self.nz = np.mean(ens.pdf(z),axis=0) + if z is None: + if 'bins' in ens_meta.keys(): + # Only true if QP ensemble is a histogram + self.z = ens_meta['bins'][0] + else: + # Default grid. Might be sub-optimal or + # not needed at all depending on the analysis. + self.z = np.linspace(0, 3, 100) + self.nz = np.mean(ens.pdf(self.z),axis=0) @classmethod def to_tables(cls, instance_list): diff --git a/test/test_sacc2.py b/test/test_sacc2.py index e9bfe3f..6ee781f 100644 --- a/test/test_sacc2.py +++ b/test/test_sacc2.py @@ -833,7 +833,7 @@ def test_qpnz_tracer(): nz_qp_interp = qp.Ensemble(qp.interp, data=dict(xvals=z, yvals=np.ones(shape=(1, 101)))) nz_qp_hist = qp.Ensemble(qp.hist, data=dict(bins=z, pdfs=np.ones(shape=(1, 100)))) - T1 = sacc.BaseTracer.make('QPNZ', 'tracer1', nz_qp_interp, z, + T1 = sacc.BaseTracer.make('QPNZ', 'tracer1', nz_qp_interp, quantity='galaxy_density', metadata=md1) T2 = sacc.BaseTracer.make('QPNZ', 'tracer2', nz_qp_hist, z, From bb4aba1ec102a2bd96c884e53dc673bb4e2f92e4 Mon Sep 17 00:00:00 2001 From: jaimerzp Date: Mon, 26 Aug 2024 12:29:09 +0200 Subject: [PATCH 06/16] solved bug --- test/test_sacc2.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/test_sacc2.py b/test/test_sacc2.py index 6ee781f..1a25e5d 100644 --- a/test/test_sacc2.py +++ b/test/test_sacc2.py @@ -836,7 +836,8 @@ def test_qpnz_tracer(): T1 = sacc.BaseTracer.make('QPNZ', 'tracer1', nz_qp_interp, quantity='galaxy_density', metadata=md1) - T2 = sacc.BaseTracer.make('QPNZ', 'tracer2', nz_qp_hist, z, + T2 = sacc.BaseTracer.make('QPNZ', 'tracer2', nz_qp_hist, + z=z, quantity='galaxy_shear', metadata=md2) assert T1.metadata == md1 @@ -859,7 +860,7 @@ def test_io_qp(): nz = np.expand_dims((z-0.5)**2/0.1**2, 0) ens = qp.Ensemble(qp.interp, data=dict(xvals=z, yvals=nz)) ens.set_ancil(dict(modes = ens.mode(z))) - s.add_tracer('QpnZ', 'source_0', ens, z) + s.add_tracer('QPNZ', 'source_0', ens, z=z) for i in range(20): ee = 0.1 * i From 3f99c9c7f21c9eb256e99220d8d174a98d08c589 Mon Sep 17 00:00:00 2001 From: jaimerzp Date: Mon, 26 Aug 2024 12:37:30 +0200 Subject: [PATCH 07/16] another bug --- sacc/tracers.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sacc/tracers.py b/sacc/tracers.py index 1674e5f..de34512 100644 --- a/sacc/tracers.py +++ b/sacc/tracers.py @@ -696,7 +696,9 @@ def __init__(self, name, ens, z=None, **kwargs): # Default grid. Might be sub-optimal or # not needed at all depending on the analysis. self.z = np.linspace(0, 3, 100) - self.nz = np.mean(ens.pdf(self.z),axis=0) + else: + self.z = z + self.nz = np.mean(ens.pdf(self.z), axis=0) @classmethod def to_tables(cls, instance_list): From 040168ed4e8bfa57ccf29307a74acbafde5b04e2 Mon Sep 17 00:00:00 2001 From: jaimerzp Date: Mon, 26 Aug 2024 15:09:49 +0200 Subject: [PATCH 08/16] no default grid --- sacc/tracers.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/sacc/tracers.py b/sacc/tracers.py index de34512..c0973f6 100644 --- a/sacc/tracers.py +++ b/sacc/tracers.py @@ -690,15 +690,18 @@ def __init__(self, name, ens, z=None, **kwargs): ens_meta = ens.metadata() if z is None: if 'bins' in ens_meta.keys(): - # Only true if QP ensemble is a histogram + # Find z in the QP ensemble metadata + # Only true if the QP ensemble is a histogram self.z = ens_meta['bins'][0] + self.nz = np.mean(ens.pdf(self.z), axis=0) else: - # Default grid. Might be sub-optimal or - # not needed at all depending on the analysis. - self.z = np.linspace(0, 3, 100) + # Set both to None if z cannot be found + self.z = z + self.nz = None else: + # Normal mode self.z = z - self.nz = np.mean(ens.pdf(self.z), axis=0) + self.nz = np.mean(ens.pdf(self.z), axis=0) @classmethod def to_tables(cls, instance_list): From 693fc652bb729b655da3b3b1e7dbee2c47a09eca Mon Sep 17 00:00:00 2001 From: jaimerzp Date: Mon, 26 Aug 2024 15:22:32 +0200 Subject: [PATCH 09/16] return to failure mode --- sacc/tracers.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/sacc/tracers.py b/sacc/tracers.py index c0973f6..63d1b40 100644 --- a/sacc/tracers.py +++ b/sacc/tracers.py @@ -689,19 +689,13 @@ def __init__(self, name, ens, z=None, **kwargs): self.ensemble = ens ens_meta = ens.metadata() if z is None: - if 'bins' in ens_meta.keys(): - # Find z in the QP ensemble metadata - # Only true if the QP ensemble is a histogram - self.z = ens_meta['bins'][0] - self.nz = np.mean(ens.pdf(self.z), axis=0) + ens_meta = ens.metadata() + if 'bins' in list(ens_meta.keys()): + z = ens_meta['bins'] + z = ens_meta['bins'][0] else: - # Set both to None if z cannot be found - self.z = z - self.nz = None - else: - # Normal mode - self.z = z - self.nz = np.mean(ens.pdf(self.z), axis=0) + raise ValueError("No redshift bins provided or found in ensemble metadata") + self.nz = np.mean(ens.pdf(z), axis=0) @classmethod def to_tables(cls, instance_list): From e91398cf5c0470eb7b0dcab1678053c3b0f9967a Mon Sep 17 00:00:00 2001 From: jaimerzp Date: Mon, 26 Aug 2024 15:49:31 +0200 Subject: [PATCH 10/16] added to test z=z --- test/test_sacc2.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_sacc2.py b/test/test_sacc2.py index 1a25e5d..435129e 100644 --- a/test/test_sacc2.py +++ b/test/test_sacc2.py @@ -834,6 +834,7 @@ def test_qpnz_tracer(): nz_qp_hist = qp.Ensemble(qp.hist, data=dict(bins=z, pdfs=np.ones(shape=(1, 100)))) T1 = sacc.BaseTracer.make('QPNZ', 'tracer1', nz_qp_interp, + z=z, quantity='galaxy_density', metadata=md1) T2 = sacc.BaseTracer.make('QPNZ', 'tracer2', nz_qp_hist, From 93beda0f8c77475a11deb7b56254e8eb69828f83 Mon Sep 17 00:00:00 2001 From: jaimerzp Date: Mon, 26 Aug 2024 15:55:57 +0200 Subject: [PATCH 11/16] bug --- test/test_sacc2.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/test_sacc2.py b/test/test_sacc2.py index 435129e..3aff993 100644 --- a/test/test_sacc2.py +++ b/test/test_sacc2.py @@ -833,12 +833,10 @@ def test_qpnz_tracer(): nz_qp_interp = qp.Ensemble(qp.interp, data=dict(xvals=z, yvals=np.ones(shape=(1, 101)))) nz_qp_hist = qp.Ensemble(qp.hist, data=dict(bins=z, pdfs=np.ones(shape=(1, 100)))) - T1 = sacc.BaseTracer.make('QPNZ', 'tracer1', nz_qp_interp, - z=z, + T1 = sacc.BaseTracer.make('QPNZ', 'tracer1', nz_qp_interp, z, quantity='galaxy_density', metadata=md1) - T2 = sacc.BaseTracer.make('QPNZ', 'tracer2', nz_qp_hist, - z=z, + T2 = sacc.BaseTracer.make('QPNZ', 'tracer2', nz_qp_hist, z, quantity='galaxy_shear', metadata=md2) assert T1.metadata == md1 @@ -861,7 +859,7 @@ def test_io_qp(): nz = np.expand_dims((z-0.5)**2/0.1**2, 0) ens = qp.Ensemble(qp.interp, data=dict(xvals=z, yvals=nz)) ens.set_ancil(dict(modes = ens.mode(z))) - s.add_tracer('QPNZ', 'source_0', ens, z=z) + s.add_tracer('QPNZ', 'source_0', ens, z) for i in range(20): ee = 0.1 * i From 4f2d0e38d60faf460599d39fe2f79283a9f5319f Mon Sep 17 00:00:00 2001 From: jaimerzp Date: Mon, 26 Aug 2024 16:01:30 +0200 Subject: [PATCH 12/16] bugs galore --- sacc/tracers.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sacc/tracers.py b/sacc/tracers.py index 9340189..11df68a 100644 --- a/sacc/tracers.py +++ b/sacc/tracers.py @@ -689,13 +689,12 @@ def __init__(self, name, ens, z=None, **kwargs): self.ensemble = ens ens_meta = ens.metadata() if z is None: - ens_meta = ens.metadata() if 'bins' in list(ens_meta.keys()): - z = ens_meta['bins'] z = ens_meta['bins'][0] else: raise ValueError("No redshift bins provided or found in ensemble metadata") - self.nz = np.mean(ens.pdf(z), axis=0) + self.z = z + self.nz = np.mean(ens.pdf(self.z), axis=0) @classmethod def to_tables(cls, instance_list): From b87a8ce464a018b9bc4996b16c098235f8125ddc Mon Sep 17 00:00:00 2001 From: jaimerzp Date: Mon, 26 Aug 2024 16:05:36 +0200 Subject: [PATCH 13/16] bugs galore --- test/test_sacc2.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_sacc2.py b/test/test_sacc2.py index 3aff993..f47a8c5 100644 --- a/test/test_sacc2.py +++ b/test/test_sacc2.py @@ -833,10 +833,10 @@ def test_qpnz_tracer(): nz_qp_interp = qp.Ensemble(qp.interp, data=dict(xvals=z, yvals=np.ones(shape=(1, 101)))) nz_qp_hist = qp.Ensemble(qp.hist, data=dict(bins=z, pdfs=np.ones(shape=(1, 100)))) - T1 = sacc.BaseTracer.make('QPNZ', 'tracer1', nz_qp_interp, z, + T1 = sacc.BaseTracer.make('QPNZ', 'tracer1', nz_qp_interp, z=z, quantity='galaxy_density', metadata=md1) - T2 = sacc.BaseTracer.make('QPNZ', 'tracer2', nz_qp_hist, z, + T2 = sacc.BaseTracer.make('QPNZ', 'tracer2', nz_qp_hist, z=z, quantity='galaxy_shear', metadata=md2) assert T1.metadata == md1 @@ -859,7 +859,7 @@ def test_io_qp(): nz = np.expand_dims((z-0.5)**2/0.1**2, 0) ens = qp.Ensemble(qp.interp, data=dict(xvals=z, yvals=nz)) ens.set_ancil(dict(modes = ens.mode(z))) - s.add_tracer('QPNZ', 'source_0', ens, z) + s.add_tracer('QPNZ', 'source_0', ens, z=z) for i in range(20): ee = 0.1 * i From e533aa26fc5794f8fca0cbe9d86a532298261709 Mon Sep 17 00:00:00 2001 From: jaimerzp Date: Mon, 26 Aug 2024 16:28:34 +0200 Subject: [PATCH 14/16] soft revert to failure mode commit --- sacc/tracers.py | 22 +++++++++++++++++----- test/test_sacc2.py | 6 +++--- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/sacc/tracers.py b/sacc/tracers.py index 11df68a..83de8b8 100644 --- a/sacc/tracers.py +++ b/sacc/tracers.py @@ -652,7 +652,7 @@ class QPNZTracer(BaseTracer, tracer_type='QPNZ'): """ A Tracer type for tomographic n(z) data represented as a `qp.Ensemble` - Takes a `qp.Ensemble` + Takes a `qp.Ensemble` and a redshift array. Requires the `qp` and `tables_io` packages to be installed. @@ -687,15 +687,15 @@ def __init__(self, name, ens, z=None, **kwargs): """ super().__init__(name, **kwargs) self.ensemble = ens - ens_meta = ens.metadata() if z is None: + ens_meta = ens.metadata() if 'bins' in list(ens_meta.keys()): z = ens_meta['bins'][0] else: raise ValueError("No redshift bins provided or found in ensemble metadata") self.z = z - self.nz = np.mean(ens.pdf(self.z), axis=0) - + self.nz = np.mean(ens.pdf(self.z),axis=0) + @classmethod def to_tables(cls, instance_list): """Convert a list of NZTracers to a list of astropy tables @@ -718,6 +718,15 @@ def to_tables(cls, instance_list): tables = [] for tracer in instance_list: + names = ['z', 'nz'] + cols = [tracer.z, tracer.nz] + fid_table = Table(data=cols, names=names) + fid_table.meta['SACCTYPE'] = 'tracer' + fid_table.meta['SACCCLSS'] = cls.tracer_type + fid_table.meta['SACCNAME'] = tracer.name + fid_table.meta['SACCQTTY'] = tracer.quantity + fid_table.meta['EXTNAME'] = f'tracer:{cls.tracer_type}:{tracer.name}:fid' + table_dict = tracer.ensemble.build_tables() ap_tables = convertToApTables(table_dict) data_table = ap_tables['data'] @@ -739,6 +748,7 @@ def to_tables(cls, instance_list): meta_table.meta['META_'+kk] = vv tables.append(data_table) tables.append(meta_table) + tables.append(fid_table) if ancil_table: ancil_table.meta['SACCTYPE'] = 'tracer' ancil_table.meta['SACCCLSS'] = cls.tracer_type @@ -783,6 +793,8 @@ def from_tables(cls, table_list): for val in sorted_dict.values(): meta_table = val['meta'] + fid_table = val['fid'] + z = fid_table['z'] ensemble = qp.from_tables(val) name = meta_table.meta['SACCNAME'] quantity = meta_table.meta.get('SACCQTTY', 'generic') @@ -791,7 +803,7 @@ def from_tables(cls, table_list): for key, value in meta_table.meta.items(): if key.startswith("META_"): metadata[key[5:]] = value - tracers[name] = cls(name, ensemble, + tracers[name] = cls(name, ensemble, z, quantity=quantity, metadata=metadata) return tracers diff --git a/test/test_sacc2.py b/test/test_sacc2.py index f47a8c5..e9bfe3f 100644 --- a/test/test_sacc2.py +++ b/test/test_sacc2.py @@ -833,10 +833,10 @@ def test_qpnz_tracer(): nz_qp_interp = qp.Ensemble(qp.interp, data=dict(xvals=z, yvals=np.ones(shape=(1, 101)))) nz_qp_hist = qp.Ensemble(qp.hist, data=dict(bins=z, pdfs=np.ones(shape=(1, 100)))) - T1 = sacc.BaseTracer.make('QPNZ', 'tracer1', nz_qp_interp, z=z, + T1 = sacc.BaseTracer.make('QPNZ', 'tracer1', nz_qp_interp, z, quantity='galaxy_density', metadata=md1) - T2 = sacc.BaseTracer.make('QPNZ', 'tracer2', nz_qp_hist, z=z, + T2 = sacc.BaseTracer.make('QPNZ', 'tracer2', nz_qp_hist, z, quantity='galaxy_shear', metadata=md2) assert T1.metadata == md1 @@ -859,7 +859,7 @@ def test_io_qp(): nz = np.expand_dims((z-0.5)**2/0.1**2, 0) ens = qp.Ensemble(qp.interp, data=dict(xvals=z, yvals=nz)) ens.set_ancil(dict(modes = ens.mode(z))) - s.add_tracer('QPNZ', 'source_0', ens, z=z) + s.add_tracer('QpnZ', 'source_0', ens, z) for i in range(20): ee = 0.1 * i From 3f083c97dd0e344fa6ab82024b5dfaf061db11d6 Mon Sep 17 00:00:00 2001 From: Joe Zuntz Date: Wed, 18 Sep 2024 13:33:08 +0100 Subject: [PATCH 15/16] allow QP tracer redshift to be left unspecified --- sacc/tracers.py | 35 ++++++++++++++++++++--------------- test/test_sacc2.py | 9 +++++++++ 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/sacc/tracers.py b/sacc/tracers.py index 83de8b8..54a4ba3 100644 --- a/sacc/tracers.py +++ b/sacc/tracers.py @@ -691,10 +691,11 @@ def __init__(self, name, ens, z=None, **kwargs): ens_meta = ens.metadata() if 'bins' in list(ens_meta.keys()): z = ens_meta['bins'][0] - else: - raise ValueError("No redshift bins provided or found in ensemble metadata") self.z = z - self.nz = np.mean(ens.pdf(self.z),axis=0) + if z is None: + self.nz = None + else: + self.nz = np.mean(ens.pdf(self.z),axis=0) @classmethod def to_tables(cls, instance_list): @@ -718,14 +719,15 @@ def to_tables(cls, instance_list): tables = [] for tracer in instance_list: - names = ['z', 'nz'] - cols = [tracer.z, tracer.nz] - fid_table = Table(data=cols, names=names) - fid_table.meta['SACCTYPE'] = 'tracer' - fid_table.meta['SACCCLSS'] = cls.tracer_type - fid_table.meta['SACCNAME'] = tracer.name - fid_table.meta['SACCQTTY'] = tracer.quantity - fid_table.meta['EXTNAME'] = f'tracer:{cls.tracer_type}:{tracer.name}:fid' + if tracer.z is not None: + names = ['z', 'nz'] + cols = [tracer.z, tracer.nz] + fid_table = Table(data=cols, names=names) + fid_table.meta['SACCTYPE'] = 'tracer' + fid_table.meta['SACCCLSS'] = cls.tracer_type + fid_table.meta['SACCNAME'] = tracer.name + fid_table.meta['SACCQTTY'] = tracer.quantity + fid_table.meta['EXTNAME'] = f'tracer:{cls.tracer_type}:{tracer.name}:fid' table_dict = tracer.ensemble.build_tables() ap_tables = convertToApTables(table_dict) @@ -748,7 +750,8 @@ def to_tables(cls, instance_list): meta_table.meta['META_'+kk] = vv tables.append(data_table) tables.append(meta_table) - tables.append(fid_table) + if tracer.z is not None: + tables.append(fid_table) if ancil_table: ancil_table.meta['SACCTYPE'] = 'tracer' ancil_table.meta['SACCCLSS'] = cls.tracer_type @@ -793,8 +796,10 @@ def from_tables(cls, table_list): for val in sorted_dict.values(): meta_table = val['meta'] - fid_table = val['fid'] - z = fid_table['z'] + if 'fid' in val: + z = val['fid']['z'] + else: + z = None ensemble = qp.from_tables(val) name = meta_table.meta['SACCNAME'] quantity = meta_table.meta.get('SACCQTTY', 'generic') @@ -803,7 +808,7 @@ def from_tables(cls, table_list): for key, value in meta_table.meta.items(): if key.startswith("META_"): metadata[key[5:]] = value - tracers[name] = cls(name, ensemble, z, + tracers[name] = cls(name, ensemble, z=z, quantity=quantity, metadata=metadata) return tracers diff --git a/test/test_sacc2.py b/test/test_sacc2.py index e9bfe3f..a50379a 100644 --- a/test/test_sacc2.py +++ b/test/test_sacc2.py @@ -850,6 +850,15 @@ def test_qpnz_tracer(): assert T1a.metadata == md1 assert T2a.metadata == md2 + # test version without saved z + T3 = sacc.BaseTracer.make('QPNZ', 'tracer3', nz_qp_interp, + quantity='galaxy_density', + metadata=md1) + tables = sacc.BaseTracer.to_tables([T3]) + D = sacc.BaseTracer.from_tables(tables) + assert D['tracer3'].z is None + + def test_io_qp(): s = sacc.Sacc() From 5938593d889403c970d0422aa537471bf6a70768 Mon Sep 17 00:00:00 2001 From: Joe Zuntz Date: Wed, 18 Sep 2024 13:38:44 +0100 Subject: [PATCH 16/16] update comments --- sacc/tracers.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sacc/tracers.py b/sacc/tracers.py index 54a4ba3..5f501a8 100644 --- a/sacc/tracers.py +++ b/sacc/tracers.py @@ -652,7 +652,7 @@ class QPNZTracer(BaseTracer, tracer_type='QPNZ'): """ A Tracer type for tomographic n(z) data represented as a `qp.Ensemble` - Takes a `qp.Ensemble` and a redshift array. + Takes a `qp.Ensemble` and optionally a redshift array. Requires the `qp` and `tables_io` packages to be installed. @@ -680,6 +680,11 @@ def __init__(self, name, ens, z=None, **kwargs): ensemble: qp.Ensemble The qp.ensemble in questions + z: array + Optional grid of redshift values at which to evaluate the ensemble. + If left as None then the ensemble metadata is checked for a grid. + If that is not present then no redshift grid is saved. + Returns ------- instance: NZTracer object