Skip to content
Snippets Groups Projects
Commit bb5a892e authored by Edward Fauchon-Jones's avatar Edward Fauchon-Jones
Browse files

Add basic simulations and simulation classes

parent 43e9bbf9
No related branches found
No related tags found
No related merge requests found
# Copyright (C) 2016 Edward Fauchon-Jones
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from h5py import File
import numpy as np
from scipy.interpolate import InterpolatedUnivariateSpline as IUS
class Sim(File):
"""Wrapper to load a single hdf5 NR file
Parameters
----------
name: str
Path to the hdf5 NR file to load
"""
def getAmpIUS(self, l, m):
"""Load amp_lm mode from the hdf5 NR file as a spline
Parameters
----------
l,m: int
load mode of degree l and order m. This corresponds to the hdf5
group `amp_l{l}_m{m}`.
Returns
-------
amp_lm: scipy.interpolate.InterpolatedUnivariateSpline
Return `amp_lm` as an spline using 5th degree interpolants
"""
ampPath = 'amp_l{0:d}_m{1:d}'.format(l, m)
t = self['{0:s}/knots'.format(ampPath)]
amp = self['{0:s}/data'.format(ampPath)]
ampIUS = IUS(t, amp, k=5)
return ampIUS
def getPhaseIUS(self, l, m):
"""Load phase_lm mode from the hdf5 NR file as a spline
Parameters
----------
l,m: int
load mode of degree l and order m. This corresponds to the hdf5
group `phase_l{l}_m{m}`.
Returns
-------
phase_lm: scipy.interpolate.InterpolatedUnivariateSpline
Return `phase_lm` as an spline using 5th degree interpolants
"""
phasePath = 'phase_l{0:d}_m{1:d}'.format(l, m)
t = self['{0:s}/knots'.format(phasePath)]
phase = self['{0:s}/data'.format(phasePath)]
phaseIUS = IUS(t, phase, k=5)
return phaseIUS
def att(self, att):
"""Get value of attribute from hdf5 NR file
Convinience method to functional access attributes of the hdf5 NR file
Parameters
----------
att: str
Name of the attribute to get the value of
Returns
-------
value: type(self.attrs[att])
Value of the attribute with name `att`. The type will match that of
the stored attribute.
"""
try:
return self.attrs[att]
except:
return 0
# Copyright (C) 2016 Edward Fauchon-Jones
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from h5py import File
import os
import glob
from .Sim import Sim
from . import LVCNR_DATADIR
from operator import concat
class Sims:
"""Collection of simulations from the LVC-NR Waveform Repository
Instancing this obejct without the option `paths` parameter will provide
access to all hdf5 NR files within the LVC-NR Waveform Repository.
Parameters
----------
paths: list of str, optional
Optional list of paths to hdf5 NR files.
"""
def __init__(self, paths=None):
if paths is None:
self.paths = self.getAllPaths()
elif all(isinstance(s, basestring) for s in paths):
self.paths = paths
else:
self.paths = []
def getAllPaths(self):
"""Find all paths of hdf5 NR files in the LVC-NR Waveform Repository
Returns
-------
simualtions: list of str
All hdf5 NR file paths
"""
# Find all valid (no `.` prefix) subdirectories of LVCNR_DATADIR
children = glob.glob('{0:s}/*/'.format(LVCNR_DATADIR))
descendants = [[[y[0], y[2]] for y in os.walk(x)] for x in children]
descendants = reduce(concat, descendants)
# Find all hdf5 files
paths = [[os.path.join(d[0], f) for f in d[1]] for d in descendants]
paths = reduce(concat, paths)
simPaths = [path for path in paths if path.endswith('.h5')]
# Remove RAW Cardiff data (temporary hack)
simPaths = [p for p in simPaths if 'Psi4ModeDecomp' not in p]
# Instance Sim object for each simulation
# sims = [Sim(simPath, 'r') for simPath in simPaths]
return simPaths # sims
def __len__(self):
"""List like length of contained simulations
Returns
-------
length: int
Length of `self.paths`
"""
return len(self.paths)
def __getitem__(self, index):
"""List like access to the contained simulations
Parameters
----------
index: int
Index of simulation to return.
Returns
-------
simualtion: lvcnrpy.Sim.Sim
The requeted simulation object.
"""
if isinstance(index, slice):
return Sims(self.paths[index])
else:
return Sim(self.paths[index], 'r')
# Copyright (C) 2016 Edward Fauchon-Jones
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
LVCNR_DATADIR = '{0:s}'.format(os.environ['LVCNR_DATADIR'])
setup.py 0 → 100644
#!/usr/bin/env python
#
# Copyright (C) 2016 Edward Fauchon-Jones
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from distutils.core import setup
from setuptools import find_packages
setup(name='lvcnrpy',
version='0.1.0',
description='Thin Python API to interact with the LVC NR catalogue',
author='Edward Fauchon-Jones',
author_email='edward.fauchon-jones@ligo.org',
packages=find_packages(),
url='https://git.ligo.org/edward.fauchon-jones/lvcnrpy',
download_url='https://git.ligo.org/edward.fauchon-jones/lvcnrpy',
install_requires=[
'h5py',
'numpy',
'scipy',
'matplotlib',
'pytest',
'colorama'],
scripts=['bin/lvcnrcheck'],
license='GPLv3')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment