From 6e61ceab9c01ec7ed415d2778e4650de66e2a1f0 Mon Sep 17 00:00:00 2001 From: dgorthi Date: Mon, 5 Dec 2016 21:07:44 -0800 Subject: [PATCH 1/3] added SFH tests --- fsps/test_sps.py | 114 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/fsps/test_sps.py b/fsps/test_sps.py index 4f9dba4c..c36f8a8d 100644 --- a/fsps/test_sps.py +++ b/fsps/test_sps.py @@ -104,3 +104,117 @@ def test_sps_ssps(reference_in=None, reference_out=None): ref.close() return [fig] + +def test_csp_sfh(reference_in=None, reference_out=None): + """This function tests the SFH parameters- sfstart, sftrunc, tau, + tburst, fburst and constant; by generating the magnitude of each + CSP under three different filters- Galex-FUV, SDSS-r, WISE-1. As + a fourth comparision, the evolution of the stellar mass for each + sfh is also shown. + + :param reference_in: (optional) + The name (and path) to a file to use as a refernce for the quantities + being calculated. The currently calculated quantities will be compared + to the ones in this file. + + :param reference_out: (optional) + Name of hdf5 file to output containing the calculated quantities + + :returns figs: + List of matplotlib.pyplot figure objects. + + """ + filters = ['wise_w1', 'sdss_r', 'galex_fuv'] + propname = ['WISE W1', 'SDSS R', 'GALEX FUV','MASS'] + ages = pop.ssp_ages + + prop = {};color= {} + + # 1. Constant SFH + color['const'] = '#e41a1c' + _reset_default_params() + pop.params['sfh'] = 1 + pop.params['const'] = 1.0 + prop['const'] = np.concatenate((pop.get_mags(bands=filters), + pop.stellar_mass.reshape(np.size(ages),1)), + axis=1) + # 2. Constant SFH between 10Myr and 10Gyr + color['tophat'] = '#377eb8' + _reset_default_params() + pop.params['sfh'] = 1 + pop.params['const'] = 1.0 + pop.params['sf_start'] = 1e-3 + pop.params['sf_trunc'] = 10.0 + prop['tophat'] = np.concatenate((pop.get_mags(bands=filters), + pop.stellar_mass.reshape(np.size(ages),1)), + axis=1) + # 3. Constant SFH + Burst at log-age 10.01 + color['cburst'] = '#4daf4a' + _reset_default_params() + pop.params['sfh'] = 1 + pop.params['const'] = 0.5 + pop.params['tburst'] = 10.01 + pop.params['fburst'] = 0.5 + prop['cburst'] = np.concatenate((pop.get_mags(bands=filters), + pop.stellar_mass.reshape(np.size(ages),1)), + axis=1) + # 4. Exponential SFH with tau= 1Gyr + color['exp'] = '#984ea3' + _reset_default_params() + pop.params['sfh'] = 1 + pop.params['tau'] = 1.0 + prop['exp'] = np.concatenate((pop.get_mags(bands=filters), + pop.stellar_mass.reshape(np.size(ages),1)), + axis=1) + # 5. Delayed Exponential SFH with tau= 1Gyr + color['dexp'] = '#ff7f00' + _reset_default_params() + pop.params['sfh'] = 4 + pop.params['tau'] = 1.0 + prop['dexp'] = np.concatenate((pop.get_mags(bands=filters), + pop.stellar_mass.reshape(np.size(ages),1)), + axis=1) + + # Store the magnitudes for three filters in linear units for + # meaningful comparision later. + current = {} + for k,v in prop.items(): + current[k] = np.concatenate((np.power(10,prop[k][:,0:np.size(filters)]), + (prop[k][:,np.size(filters)]).reshape(np.size(ages),1)), + axis=1) + + nrows=2;ncols=2 + fig, ax = pl.subplots(nrows=nrows, ncols=ncols,sharex=True) + ax[0][0].set_ylim((25,0)) + ax[0][1].set_ylim((25,0)) + ax[1][0].set_ylim((25,0)) + ax[1][1].set_yscale('log') + + for row in range(nrows): + for col in range(ncols): + ax[row][col].set_ylabel(propname[(nrows*row+col)]) + ax[row][col].set_xlabel('Log Age[yrs]') + for sfh in prop.keys(): + ax[row][col].plot(ages, prop[sfh][:,(nrows*row+col)],c=color[sfh],label=sfh,linewidth=2) + + ax[row][col].legend(loc=4) + + # Here we compare to an old set of values stored in an hdf5 file + if reference_in is not None: + ref = h5py.File(reference_in, 'r') + diffstring = compare_reference(ref, current) + print(diffstring) + ref.close() + + # Here we write out an hdf5 file + if reference_out is not None: + ref = h5py.File(reference_out, 'w') + for k, v in current.items(): + d = ref.create_dataset(k, data=v) + # now we add useful version things + ref.attrs['libraries'] = json.dumps(libs) + ref.attrs['default_params'] = json.dumps(default_params) + ref.attrs['git_history'] = json.dumps(githashes) + ref.close() + + return [fig] From ec9a05a15ab1c8e708903f02a4bb72e87805b769 Mon Sep 17 00:00:00 2001 From: dgorthi Date: Mon, 5 Dec 2016 22:26:16 -0800 Subject: [PATCH 2/3] added SFH tests --- fsps/test_sps.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fsps/test_sps.py b/fsps/test_sps.py index c36f8a8d..801f35c3 100644 --- a/fsps/test_sps.py +++ b/fsps/test_sps.py @@ -184,7 +184,7 @@ def test_csp_sfh(reference_in=None, reference_out=None): axis=1) nrows=2;ncols=2 - fig, ax = pl.subplots(nrows=nrows, ncols=ncols,sharex=True) + fig, ax = pl.subplots(nrows=nrows, ncols=ncols,sharex=True,figsize=(15,15)) ax[0][0].set_ylim((25,0)) ax[0][1].set_ylim((25,0)) ax[1][0].set_ylim((25,0)) @@ -192,12 +192,12 @@ def test_csp_sfh(reference_in=None, reference_out=None): for row in range(nrows): for col in range(ncols): - ax[row][col].set_ylabel(propname[(nrows*row+col)]) - ax[row][col].set_xlabel('Log Age[yrs]') + ax[row][col].set_ylabel(propname[(nrows*row+col)],fontsize=18) + ax[row][col].set_xlabel('Log Age[yrs]',fontsize=18) for sfh in prop.keys(): ax[row][col].plot(ages, prop[sfh][:,(nrows*row+col)],c=color[sfh],label=sfh,linewidth=2) - ax[row][col].legend(loc=4) + ax[row][col].legend(loc=4,fontsize=18) # Here we compare to an old set of values stored in an hdf5 file if reference_in is not None: From e48eab88a065aca1adc792f83cefd7d1b687c8d4 Mon Sep 17 00:00:00 2001 From: dgorthi Date: Tue, 6 Dec 2016 14:28:32 -0800 Subject: [PATCH 3/3] changes to compare_reference --- fsps/test_sps.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fsps/test_sps.py b/fsps/test_sps.py index 801f35c3..04ea0733 100644 --- a/fsps/test_sps.py +++ b/fsps/test_sps.py @@ -39,15 +39,15 @@ def compare_reference(ref_in, current): stellar_mass, etc.) """ ref_libs = json.loads(ref_in.attrs['libraries']) - assert ref_libs == libs + #assert ref_libs == libs diffstring = ("The maximum difference from the reference quantities " "is {:4.5f} percent in the quantity {}") maxdiff, maxdq = 0, 'ANY' for k, v in current.items(): - diff = np.atleast_1d(v /ref_in[v] - 1) # percent difference + diff = np.atleast_1d(v /ref_in[k] - 1).flatten() # percent difference maxdind = np.argmax(np.abs(diff)) - if diff[maxdind] > maxdiff: + if np.abs(diff[maxdind]) > maxdiff: maxdiff = diff[maxdind] maxdq = k