Repository: gwastro/pycbc

Issue:
Need to decode approximant strings in pycbc_condition_strain 
Currently pycbc_condition_strain does not recognize approximants after reading them from the variable parameters table of an injection hdf. For example, when approximant 'SEOBNRv4' is stored as a variable parameter in an injection hdf, reading the hdf gives back a string b'SEOBNRv4' which is not recognized as being the same thing as 'SEOBNRv4'

Running the pycbc live test contained in my pull request  https://github.com/gwastro/pycbc/pull/3322 causes an error when pycbc_condition_strain gets to Applying Injections

2020-07-24 12:14:04,373 Applying injections
Traceback (most recent call last):
  File "/home/max.trevor/dev_env/bin/pycbc_condition_strain", line 4, in <module>
    __import__('pkg_resources').run_script('PyCBC===e9f3da', 'pycbc_condition_strain')
  File "/home/max.trevor/dev_env/lib/python3.6/site-packages/pkg_resources/__init__.py", line 667, in run_script
    self.require(requires)[0].run_script(script_name, ns)
  File "/home/max.trevor/dev_env/lib/python3.6/site-packages/pkg_resources/__init__.py", line 1464, in run_script
    exec(code, namespace, namespace)
  File "/home/max.trevor/dev_env/lib/python3.6/site-packages/PyCBC-e9f3da-py3.6-linux-x86_64.egg/EGG-INFO/scripts/pycbc_condition_strain", line 87, in <module>
    precision=args.output_precision)
  File "/home/max.trevor/dev_env/lib/python3.6/site-packages/PyCBC-e9f3da-py3.6-linux-x86_64.egg/pycbc/strain/strain.py", line 392, in from_cli
    inj_filter_rejector=inj_filter_rejector)
  File "/home/max.trevor/dev_env/lib/python3.6/site-packages/PyCBC-e9f3da-py3.6-linux-x86_64.egg/pycbc/inject/inject.py", line 505, in apply
    detector_name, f_lower=f_l, distance_scale=distance_scale)
  File "/home/max.trevor/dev_env/lib/python3.6/site-packages/PyCBC-e9f3da-py3.6-linux-x86_64.egg/pycbc/inject/inject.py", line 557, in make_strain_from_inj_object
    **self.extra_args)
  File "/home/max.trevor/dev_env/lib/python3.6/site-packages/PyCBC-e9f3da-py3.6-linux-x86_64.egg/pycbc/waveform/waveform.py", line 462, in get_td_waveform
    (input_params['approximant']))
ValueError: Approximant b'SEOBNRv4' not available



Discussion:

Resolving patch:
diff --git a/examples/live/generate_injections.py b/examples/live/generate_injections.py
--- a/examples/live/generate_injections.py
+++ b/examples/live/generate_injections.py
@@ -1,53 +1,30 @@
 #!/usr/bin/env python
 
-import os
 import sys
 from pycbc.io import FieldArray
 from pycbc.inject import InjectionSet
 
 
-if os.path.exists('./test_inj1.hdf'):
-    raise OSError("output-file 1 already exists")
-
-if os.path.exists('./test_inj2.hdf'):
-    raise OSError("output-file 2 already exists")
-
 dtype = [('mass1', float), ('mass2', float),
          ('spin1z', float), ('spin2z', float),
-         ('tc', float), ('distance', float)]
+         ('tc', float), ('distance', float),
+         ('approximant', 'S32')]
 
-# injection 1
-static_params = {'f_lower': 18.0, 'f_ref': 18.0, 'approximant': 'SEOBNRv4',
+static_params = {'f_lower': 18.0, 'f_ref': 18.0,
                  'taper': 'start', 'ra': 45.0, 'dec': 45.0,
                  'inclination': 0.0, 'coa_phase': 0.0, 'polarization': 0.0}
 
-samples = FieldArray(1, dtype=dtype)
+samples = FieldArray(2, dtype=dtype)
 
 # The following 'magic numbers' are intended to match the highest
-# mass injection in the template bank
-samples['mass1'] = [290.929321]
-samples['mass2'] = [3.6755455]
-samples['spin1z'] = [0.9934847]
-samples['spin2z'] = [0.92713535]
-samples['tc'] = [1272790100.1]
-samples['distance'] = [301.5]
-
-InjectionSet.write('test_inj1.hdf', samples, static_args=static_params,
-                   injtype='cbc', cmd=" ".join(sys.argv))
-
-# injection 2
-static_params['approximant'] = 'SpinTaylorT4'
-
-samples = FieldArray(1, dtype=dtype)
-
-# The following 'magic numbers' are intended to match the lowest
-# mass injection in the template bank
-samples['mass1'] = [1.1331687]
-samples['mass2'] = [1.010624]
-samples['spin1z'] = [0.029544285]
-samples['spin2z'] = [0.020993788]
-samples['tc'] = [1272790260.1]
-samples['distance'] = [36.0]
-
-InjectionSet.write('test_inj2.hdf', samples, static_args=static_params,
+# and lowest mass templates in the template bank
+samples['mass1'] = [290.929321, 1.1331687]
+samples['mass2'] = [3.6755455, 1.010624]
+samples['spin1z'] = [0.9934847, 0.029544285]
+samples['spin2z'] = [0.92713535, 0.020993788]
+samples['tc'] = [1272790100.1, 1272790260.1]
+samples['distance'] = [301.5, 36.0]
+samples['approximant'] = ['SEOBNRv4', 'SpinTaylorT4']
+
+InjectionSet.write('injections.hdf', samples, static_args=static_params,
                    injtype='cbc', cmd=" ".join(sys.argv))


Test patch: