Skip to content

Commit

Permalink
Merge 69bbd6c into 556fd9a
Browse files Browse the repository at this point in the history
  • Loading branch information
profxj committed Dec 23, 2015
2 parents 556fd9a + 69bbd6c commit de169d9
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 17 deletions.
13 changes: 12 additions & 1 deletion linetools/isgm/tests/test_init_abscomp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import pytest
from astropy import units as u
import numpy as np
import pytest

from linetools.isgm.abscomponent import AbsComponent
from linetools.spectralline import AbsLine

#import pdb
import pdb
#pdb.set_trace()
# Set of Input lines

Expand All @@ -21,6 +22,15 @@ def test_init():
# Test
assert abscomp.Zion[0] == 14
np.testing.assert_allclose(abscomp.zcomp,1.0)
print(abscomp)

def test_init_failures():
with pytest.raises(IOError):
AbsComponent.from_abslines('blah')
with pytest.raises(IOError):
AbsComponent.from_abslines(['blah'])
with pytest.raises(IOError):
AbsComponent.from_component('blah')

def test_init_single_absline():
# Single AbsLine
Expand All @@ -31,6 +41,7 @@ def test_init_single_absline():
# Test
assert abscomp.Zion[0] == 1
np.testing.assert_allclose(abscomp.zcomp,2.92939)
print(abscomp)

def test_init_multi_absline():
# AbsLine(s)
Expand Down
33 changes: 31 additions & 2 deletions linetools/isgm/tests/test_use_abscomp.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#pdb.set_trace()
# Set of Input lines

def mk_comp(ctype,vlim=[-300.,300]*u.km/u.s,add_spec=False):
def mk_comp(ctype,vlim=[-300.,300]*u.km/u.s,add_spec=False, use_rand=True):
# Read a spectrum Spec
if add_spec:
xspec = lsio.readspec(lt_path+'/spectra/tests/files/UM184_nF.fits')
Expand All @@ -41,7 +41,11 @@ def mk_comp(ctype,vlim=[-300.,300]*u.km/u.s,add_spec=False):
for trans in all_trans:
iline = AbsLine(trans)
iline.attrib['z'] = 2.92939
iline.attrib['logN'] = 13.3 + np.random.rand()
if use_rand:
rnd = np.random.rand()
else:
rnd = 0.
iline.attrib['logN'] = 13.3 + rnd
iline.attrib['sig_logN'] = 0.15
iline.attrib['flag_N'] = 1
iline.analy['spec'] = xspec
Expand All @@ -65,6 +69,30 @@ def test_synthesize_colm():
abscomp.synthesize_colm(redo_aodm=True)
# Test
np.testing.assert_allclose(abscomp.logN,13.594447075294818)
# Reset flags (for testing)
abscomp2,_ = mk_comp('SiII', vlim=[-250,80.]*u.km/u.s, add_spec=True, use_rand=False)
for iline in abscomp2._abslines:
if iline.data['name'] == 'SiII 1260':
iline.attrib['flag_N'] = 2
elif iline.data['name'] == 'SiII 1808':
iline.attrib['flag_N'] = 3
else:
iline.attrib['flag_N'] = 1
abscomp2.synthesize_colm()
# Test
np.testing.assert_allclose(abscomp2.logN, 13.3)
# Another
abscomp3,_ = mk_comp('SiII', vlim=[-250,80.]*u.km/u.s, add_spec=True, use_rand=False)
for iline in abscomp3._abslines:
if iline.data['name'] == 'SiII 1260':
iline.attrib['flag_N'] = 3
elif iline.data['name'] == 'SiII 1808':
iline.attrib['flag_N'] = 2
else:
iline.attrib['flag_N'] = 1
abscomp3.synthesize_colm()
# Test
np.testing.assert_allclose(abscomp3.logN, 13.3)

def test_build_components_from_lines():
# Lines
Expand Down Expand Up @@ -94,6 +122,7 @@ def test_synthesize_components():
synth_SiII = ltiu.synthesize_components([SiIIcomp1,SiIIcomp2])
np.testing.assert_allclose(synth_SiII.logN,13.862456155250918)
np.testing.assert_allclose(synth_SiII.sig_logN,0.010146948602759272)

"""
def test_stack_plot():
# AbsLine(s)
Expand Down
4 changes: 0 additions & 4 deletions linetools/isgm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@

import pdb
import numpy as np
from collections import OrderedDict

from astropy import constants as const
from astropy import units as u
from astropy.coordinates import SkyCoord
from astropy.table import QTable
from astropy.io import ascii

from linetools.spectralline import AbsLine
from linetools.analysis import absline as ltaa
from linetools.isgm.abscomponent import AbsComponent

Expand Down
9 changes: 9 additions & 0 deletions linetools/spectra/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ def readspec(specfil, inflg=None, efil=None, verbose=False, flux_tags=None,
else:
wave = hdulist['WAVELENGTH'].data * u.AA
fx = hdulist['FLUX'].data
# Sort
srt = np.argsort(wave)
wave = wave[srt]
fx = fx[srt]
xspec1d = XSpectrum1D.from_array(wave, u.Quantity(fx))

# Error array
Expand Down Expand Up @@ -252,7 +256,12 @@ def readspec(specfil, inflg=None, efil=None, verbose=False, flux_tags=None,
uwave = u.Quantity(wave, unit=u.AA)
else:
uwave = u.Quantity(wave)
# Sort
srt = np.argsort(uwave)
uwave = uwave[srt]
fx = fx[srt]
if sig is not None:
sig = sig[srt]
xspec1d = XSpectrum1D.from_array(uwave, u.Quantity(fx),
uncertainty=StdDevUncertainty(sig))
else:
Expand Down
27 changes: 18 additions & 9 deletions linetools/spectra/xspectrum1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def from_spec1d(cls, spec1d):
return slf

@classmethod
def from_tuple(cls,ituple):
def from_tuple(cls, ituple):
"""Make an XSpectrum1D from a tuple of arrays.
Parameters
Expand All @@ -109,16 +109,25 @@ def from_tuple(cls,ituple):
if wv_unit is None:
wv_unit = u.AA
uwave = u.Quantity(ituple[0], unit=wv_unit)

# Sort
ltuple = list(ituple)
srt = np.argsort(uwave)
uwave = uwave[srt]
for ii in range(1, len(ituple)):
ltuple[ii] = ituple[ii][srt]


# Generate
if len(ituple) == 2: # wave, flux
spec = cls.from_array(uwave, u.Quantity(ituple[1]))
elif len(ituple) == 3:
spec = cls.from_array(uwave, u.Quantity(ituple[1]),
uncertainty=StdDevUncertainty(ituple[2]))
if len(ltuple) == 2: # wave, flux
spec = cls.from_array(uwave, u.Quantity(ltuple[1]))
elif len(ltuple) == 3:
spec = cls.from_array(uwave, u.Quantity(ltuple[1]),
uncertainty=StdDevUncertainty(ltuple[2]))
else:
spec = cls.from_array(uwave, u.Quantity(ituple[1]),
uncertainty=StdDevUncertainty(ituple[2]))
spec.co = ituple[3]
spec = cls.from_array(uwave, u.Quantity(ltuple[1]),
uncertainty=StdDevUncertainty(ltuple[2]))
spec.co = ltuple[3]

spec.filename = 'none'
# Return
Expand Down
23 changes: 22 additions & 1 deletion linetools/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
from __future__ import print_function, absolute_import, division, unicode_literals

from ..utils import between
from ..utils import between, v_from_z, savejson, loadjson, z_from_v
import numpy as np
from astropy import units as u

def test_between():
x = [1,2,3,4,5]
c0 = between(x, 2.5, 3.5)
assert np.all(c0 == np.array([False, False, True, False, False]))

def test_zfromv():
z = z_from_v(2., 1000.)
#
np.testing.assert_allclose(z, 2.0100236684175417)

def test_vfromz():
v = v_from_z(2., 2.1)
#
assert v.unit == u.km/u.s
np.testing.assert_allclose(v.value, -9826.62006340679)

def test_save_load_json():
tmp_dict = dict(a=1, b=2, c='adsf')
# Write
savejson('tmp.json', tmp_dict, overwrite=True)
# Load
new_dict = loadjson('tmp.json')
assert new_dict['a'] == 1

0 comments on commit de169d9

Please sign in to comment.