def test_programmatic(): # Test a single inversion in non-iterator mode mod = hazel.Model(working_mode='synthesis') mod.add_spectral({'Name': 'spec1', 'Wavelength': [10826, 10833, 150], 'topology': 'ch1', 'LOS': [0.0,0.0,90.0], 'Boundary condition': [1.0,0.0,0.0,0.0]}) mod.add_chromosphere({'Name': 'ch1', 'Spectral region': 'spec1', 'Height': 3.0, 'Line': '10830', 'Wavelength': [10826, 10833]}) mod.setup() mod.atmospheres['ch1'].set_parameters([0.0,0.0,100.0,1.0,0.0,8.0,1.0,0.0],1.0) mod.synthesize() assert mod.spectrum['spec1'].stokes.shape == (4,150), "incorrect dimensions in synthesis"
def test_single_syn(): # Test a single inversion in non-iterator mode mod = hazel.Model('test/configurations/conf_single.ini', working_mode='synthesis', verbose=3) mod.open_output() mod.synthesize() mod.write_output() mod.close_output() assert mod.spectrum['spec1'].stokes.shape == ( 4, 150), "incorrect dimensions in synthesis"
def test_nonmpi_syn1d(): # Test iterator with a single observation in synthesis iterator = hazel.Iterator(use_mpi=False) rank = iterator.get_rank() mod = hazel.Model('test/configurations/conf_nonmpi_syn1d.ini', working_mode='synthesis', verbose=2) iterator.use_model(model=mod) iterator.run_all_pixels() if (rank == 0): f = h5py.File('output.h5', 'r') assert f['spec1']['stokes'].shape == (1,1,1,4,150), "incorrect dimensions in inversion" f.close()
def test_single_inv(): # Test a single inversion in non-iterator mode without randomization mod = hazel.Model('test/configurations/conf_single.ini', working_mode='inversion', verbose=2) mod.read_observation() mod.open_output() mod.invert() mod.write_output() mod.close_output() f = h5py.File('output.h5') assert f['spec1']['stokes'].shape == (1,1,2,4,150), "incorrect dimensions in inversion" f.close()
import numpy as np import matplotlib.pyplot as pl import hazel import h5py from ipdb import set_trace as stop iterator = hazel.Iterator(use_mpi=True) rank = iterator.get_rank() mod = hazel.Model('configurations/conf_mpi_invh5.ini', working_mode='inversion', rank=rank, randomization=2) iterator.use_model(model=mod) iterator.run_all_pixels()
import numpy as np import matplotlib.pyplot as pl import hazel import h5py from scipy.optimize import minimize import gc # Test a single inversion in non-iterator mode mod = hazel.Model('conf_single.ini', working_mode='inversion', verbose=2) mod.read_observation() mod.open_output() mod.invert() mod.write_output() mod.close_output() final = np.loadtxt('photospheres/model_photosphere.1d', skiprows=4) start = np.loadtxt('photospheres/model_photosphere_200.1d', skiprows=4) f = h5py.File('output.h5') pl.plot(f['ph1']['T'][0, 0, :], label='inverted') pl.plot(final[:, 1], label='target') pl.plot(start[:, 1], 'x', label='initial') f.close() pl.legend() mod = hazel.Model('conf_single.ini', working_mode='inversion', verbose=2) mod.read_observation() mod.open_output() mod.invert_external(minimize, method='Nelder-Mead') mod.write_output() mod.close_output()
import numpy as np import matplotlib.pyplot as pl import hazel # Test a single inversion in non-iterator mode mod = hazel.Model('configurations/conf_single.ini', working_mode='synthesis', verbose=3) mod.open_output() mod.synthesize() mod.write_output() mod.close_output() f, ax = pl.subplots() ax.plot(mod.spectrum['spec1'].stokes[0, :]) pl.show()
for i, l in enumerate(tmp): t = np.array(l.split()).astype('float64') stokes[:,i] = t[0:4] noise[:,i] = t[4:] mu = np.cos(los[0] * np.pi / 180.0) return stokes, noise, los, mu, boundary if __name__ == '__main__': #------------------------------------------- # Synthesize a sample profile with some default configuration given in conf_syn.ini # The observed files will be used later as observations for the inversions #------------------------------------------- mod = hazel.Model('configurations/conf_syn.ini') mod.synthesize() noise = 1e-4 * np.ones((150,4)) spec = mod.spectrum['spec1'].stokes.T + 1e-4 * np.random.randn(150,4) # Generate wavelength axis np.savetxt('observations/10830.wavelength', mod.spectrum['spec1'].wavelength_axis, header='lambda') # Generate output Stokes parameters f = open('observations/10830_stokes.1d', 'w') f.write('# LOS theta_LOS, phi_LOS, gamma_LOS\n') f.write('0.0 0.0 90.0\n') f.write('\n') f.write('# Boundary condition I/Ic(mu=1), Q/Ic(mu=1), U/Ic(mu=1), V/Ic(mu=1)\n')
for i, l in enumerate(tmp): t = np.array(l.split()).astype('float64') stokes[:,i] = t[0:4] noise[:,i] = t[4:] mu = np.cos(los[0] * np.pi / 180.0) return stokes, noise, los, mu, boundary if __name__ == '__main__': #------------------------------------------- # Synthesize a sample profile with some default configuration given in conf_syn.ini # The observed files will be used later as observations for the inversions #------------------------------------------- mod = hazel.Model('conf_syn.ini') mod.synthesize() noise = 1e-4 * np.ones((150,4)) spec = mod.spectrum['spec1'].stokes.T + 1e-4 * np.random.randn(150,4) # Generate wavelength axis np.savetxt('observations/10830.wavelength', mod.spectrum['spec1'].wavelength_axis, header='lambda') # Generate output Stokes parameters f = open('observations/10830_stokes.1d', 'w') f.write('# LOS theta_LOS, phi_LOS, gamma_LOS\n') f.write('0.0 0.0 90.0\n') f.write('\n') f.write('# Boundary condition I/Ic(mu=1), Q/Ic(mu=1), U/Ic(mu=1), V/Ic(mu=1)\n')
import numpy as np import matplotlib.pyplot as pl import hazel print(hazel.__version__) label = ['I', 'Q', 'U', 'V'] mod = hazel.Model(working_mode='synthesis') mod.add_spectral({ 'Name': 'spec1', 'Wavelength': [10825, 10835, 150], 'topology': 'ph1->ch1->te1', 'LOS': [0.0, 0.0, 90.0], 'Boundary condition': [1.0, 0.0, 0.0, 0.0] }) mod.add_chromosphere({ 'Name': 'ch1', 'Spectral region': 'spec1', 'Height': 3.0, 'Line': '10830', 'Wavelength': [10826, 10835] }) mod.add_photosphere({ 'Name': 'ph1', 'Spectral region': 'spec1', 'Spectral lines': [300], 'Wavelength': [10825, 10835], 'Reference atmospheric model': '../../models/photospheres/model_photosphere.1d'
import numpy as np import matplotlib.pyplot as pl import hazel import h5py from ipdb import set_trace as stop # Test iterator with a single observation in synthesis iterator = hazel.Iterator(use_mpi=False) rank = iterator.get_rank() mod = hazel.Model('configurations/conf_nonmpi_syn1d.ini', working_mode='synthesis', verbose=2) iterator.use_model(model=mod) iterator.run_all_pixels() if (rank == 0): fig, ax = pl.subplots(nrows=2, ncols=2) ax = ax.flatten() f = h5py.File('output.h5', 'r') for i in range(4): ax[i].plot(f['spec1']['stokes'][0,0,0,i,:]) pl.show() f.close()
import numpy as np import matplotlib.pyplot as pl import hazel import h5py from ipdb import set_trace as stop # Test iterator with a single observation in synthesis iterator = hazel.iterator(use_mpi=False) rank = iterator.get_rank() mod = hazel.Model('conf_nonmpi_inv1d.ini', working_mode='inversion', verbose=2) iterator.use_model(model=mod) iterator.run_all_pixels() if (rank == 0): fig, ax = pl.subplots(nrows=2, ncols=2) ax = ax.flatten() f = h5py.File('output.h5', 'r') for i in range(4): ax[i].plot(f['spec1']['stokes'][0, 0, i, :]) pl.show() pl.pause(0.001) input("Press [enter] to continue.")
import hazel import h5py import sys conf_file = sys.argv[1] iterator = hazel.Iterator(use_mpi=True) mod = hazel.Model(conf_file, rank=iterator.get_rank(), working_mode='inversion') iterator.use_model(model=mod) iterator.run_all_pixels()
import hazel iterator = hazel.Iterator(use_mpi=True) mod = hazel.Model('conf_synth_parallel.ini', working_mode='synthesis') iterator.use_model(model=mod) iterator.run_all_pixels()
import numpy as np import matplotlib.pyplot as pl import hazel import h5py from ipdb import set_trace as stop iterator = hazel.iterator(use_mpi=True) rank = iterator.get_rank() if (rank == 0): mod = hazel.Model('conf_mpi_synh5.ini', working_mode='synthesis') iterator.use_model(model=mod) else: iterator.use_model() iterator.run_all_pixels()
np.savetxt(f, tmp.T) f.close() # ###################### # # And now we do the inversion using the appropriate configuration file #mod = hazel.Model('conf_firs.ini', working_mode='inversion', verbose=3, randomization=5) #mod.read_observation() #mod.open_output() #mod.invert() #mod.write_output() #mod.close_output() iterator = hazel.Iterator(use_mpi=False) mod = hazel.Model('conf_firs.ini', working_mode='inversion', verbose=3, rank=iterator.get_rank(), randomization=5) iterator.use_model(model=mod) iterator.run_all_pixels() # Do some plots # Open the file f = h5py.File('output_onepixel.h5', 'r') # Check the sizes of the output npix, nrand, nstokes, nlambda = f['spec1']['stokes'].shape print('(npix,nrand,nstokes,nlambda) -> {0}'.format(f['spec1']['stokes'].shape)) fig, ax = pl.subplots(nrows=2, ncols=2, figsize=(10, 10))
import hazel import numpy as np import matplotlib.pyplot as pl # Define LOS theta angle theta_los = 25.0 # Atmosphere with vertical/cartesian mod = hazel.Model(working_mode='synthesis') mod.add_spectral({'Name': 'spec1', 'Wavelength': [10826, 10833, 150], 'topology': 'ch1', 'LOS': [theta_los,0.0,90.0], 'Boundary condition': [1.0,0.0,0.0,0.0]}) mod.add_chromosphere({'Name': 'ch1', 'Spectral region': 'spec1', 'Height': 3.0, 'Line': '10830', 'Wavelength': [10826, 10833], 'Reference frame' : 'vertical', 'Coordinates for magnetic field vector': 'cartesian'}) mod.setup() # Atmosphere with vertical/spherical mod2 = hazel.Model(working_mode='synthesis') mod2.add_spectral({'Name': 'spec1', 'Wavelength': [10826, 10833, 150], 'topology': 'ch1', 'LOS': [theta_los,0.0,90.0], 'Boundary condition': [1.0,0.0,0.0,0.0]}) mod2.add_chromosphere({'Name': 'ch1', 'Spectral region': 'spec1', 'Height': 3.0, 'Line': '10830', 'Wavelength': [10826, 10833], 'Reference frame' : 'vertical', 'Coordinates for magnetic field vector': 'spherical'}) mod2.setup() # Atmosphere with line-of-sight/cartesian mod3 = hazel.Model(working_mode='synthesis') mod3.add_spectral({'Name': 'spec1', 'Wavelength': [10826, 10833, 150], 'topology': 'ch1', 'LOS': [theta_los,0.0,90.0], 'Boundary condition': [1.0,0.0,0.0,0.0]}) mod3.add_chromosphere({'Name': 'ch1', 'Spectral region': 'spec1', 'Height': 3.0, 'Line': '10830', 'Wavelength': [10826, 10833], 'Reference frame' : 'line-of-sight', 'Coordinates for magnetic field vector': 'cartesian'}) mod3.setup()
import numpy as np import matplotlib.pyplot as pl import hazel iterator = hazel.Iterator(use_mpi=True) rank = iterator.get_rank() mod = hazel.Model('configurations/conf_mpi_invh5_mask.ini', working_mode='inversion', rank=rank, verbose=3) iterator.use_model(model=mod) iterator.run_all_pixels()
import numpy as np import matplotlib.pyplot as pl import hazel import h5py from ipdb import set_trace as stop iterator = hazel.Iterator(use_mpi=True) rank = iterator.get_rank() mod = hazel.Model('configurations/conf_mpi_synh5.ini', working_mode='synthesis', rank=rank) iterator.use_model(model=mod) iterator.run_all_pixels()
f.write( b'# Boundary condition I/Ic(mu=1), Q/Ic(mu=1), U/Ic(mu=1), V/Ic(mu=1)\n') f.write(b'1.0 0.0 0.0 0.0\n') f.write(b'\n') f.write(b'# SI SQ SU SV sigmaI sigmaQ sigmaU sigmaV\n') tmp = np.vstack([ stokes[x_coordinate, y_coordinate, :, l_left:l_right] / norm, noise * np.ones((4, n_wvl)) ]) np.savetxt(f, tmp.T) f.close() # ###################### # # And now we do the inversion using the appropriate configuration file mod = hazel.Model('conf.ini', working_mode='inversion', verbose=3, randomization=1) mod.read_observation() mod.open_output() mod.invert() mod.write_output() mod.close_output() # Do some plots # Open the file f = h5py.File('output_onepixel.h5', 'r') # Check the sizes of the output npix, nrand, ncycle, nstokes, nlambda = f['spec1']['stokes'].shape print('(npix,nrand,ncycle,nstokes,nlambda) -> {0}'.format(
import numpy as np import matplotlib.pyplot as pl import hazel import h5py from ipdb import set_trace as stop iterator = hazel.iterator(use_mpi=True) rank = iterator.get_rank() if (rank == 0): mod = hazel.Model('conf_mpi_invh5.ini', working_mode='inversion') iterator.use_model(model=mod) else: iterator.use_model() iterator.run_all_pixels()
import numpy as np import matplotlib.pyplot as pl import hazel import h5py from ipdb import set_trace as stop print(hazel.__version__) label = ['I', 'Q', 'U', 'V'] # Instantiate the model mod_syn = hazel.Model(working_mode='synthesis') # Add spectral region mod_syn.add_spectral({'Name': 'spec1', 'Wavelength': [10826, 10833, 150], 'topology': 'ch1', 'LOS': [0.0,0.0,90.0], 'Boundary condition': [1.0,0.0,0.0,0.0]}) # Add chromosphere mod_syn.add_chromosphere({'Name': 'ch1', 'Spectral region': 'spec1', 'Height': 3.0, 'Line': '10830', 'Wavelength': [10826, 10833]}) # Finalize setup mod_syn.setup() # Define parameters for the chromosphere tau = 0.5 v = 10.0 deltav = 8.0 beta = 1.0 a = 0.0 Bx = 100.0 By = 100.0
import numpy as np import matplotlib.pyplot as pl import hazel # Test a single inversion in non-iterator mode mod = hazel.Model('conf_single.ini', working_mode='synthesis') mod.open_output() mod.synthesize() mod.write_output() mod.close_output() f, ax = pl.subplots() ax.plot(mod.spectrum['spec1'].stokes[0, :]) pl.show()
import numpy as np import matplotlib.pyplot as pl import hazel import h5py # Test a single inversion in non-iterator mode without randomization mod = hazel.Model('configurations/conf_inv_D3_10830.ini', working_mode='inversion', verbose=4) mod.read_observation() mod.open_output() mod.invert() mod.write_output() mod.close_output() fig, ax = pl.subplots(nrows=1, ncols=2) f = h5py.File('output.h5', 'r') obs1 = np.loadtxt('observations/multi_10830_stokes.1d', skiprows=7) obs2 = np.loadtxt('observations/multi_D3_stokes.1d', skiprows=7) ax[0].plot(obs1[:, 0]) ax[0].plot(f['spec1']['stokes'][0, 0, 0, 0, :]) ax[1].plot(obs2[:, 0]) ax[1].plot(f['spec2']['stokes'][0, 0, 0, 0, :]) pl.show()
import numpy as np import matplotlib.pyplot as pl import hazel import h5py tmp = hazel.tools.File_photosphere(mode='single') tmp.set_default(n_pixel=1) tmp.save('photospheres/init') tmp = hazel.tools.File_chromosphere(mode='single') tmp.set_default(n_pixel=1) tmp.save('chromospheres/init') mod = hazel.Model('conf_spot.ini', working_mode='inversion', verbose=3) mod.read_observation() mod.open_output() mod.invert() mod.write_output() mod.close_output() f = h5py.File('output.h5', 'r') stokes = np.loadtxt('10830_spot.1d', skiprows=7).T print('(npix,nrand,ncycle,nstokes,nlambda) -> {0}'.format(f['spec1']['stokes'].shape)) fig, ax = pl.subplots(nrows=2, ncols=2, figsize=(10,10)) ax = ax.flatten() for i in range(4): ax[i].plot(f['spec1']['wavelength'][:] - 10830, stokes[i,:]) for j in range(2):
import numpy as np import matplotlib.pyplot as pl import hazel import h5py # Test iterator with a single observation in synthesis iterator = hazel.Iterator(use_mpi=False) rank = iterator.get_rank() mod = hazel.Model('configurations/conf_nonmpi_inv1d.ini', working_mode='inversion', verbose=4, randomization=2) iterator.use_model(model=mod) iterator.run_all_pixels() if (rank == 0): fig, ax = pl.subplots(nrows=2, ncols=2) ax = ax.flatten() f = h5py.File('output.h5', 'r') obs = np.loadtxt('observations/10830_stokes.1d', skiprows=7) for i in range(4): ax[i].plot(obs[:, i]) ax[i].plot(f['spec1']['stokes'][0, 0, 0, i, :]) ax[i].plot(f['spec1']['stokes'][0, 1, 0, i, :]) pl.show() pl.pause(0.001) f.close()
tmp = hazel.tools.File_observation(mode='single') tmp.set_size(n_lambda=250, n_pixel=1) tmp.obs['stokes'] = np.array([np.transpose(stokes)]) tmp.obs['sigma'] = np.array([ np.std(stokes[0, 0:15]) * np.ones((250, 1)), np.std(stokes[1, 0:15]) * np.ones((250, 1)), np.std(stokes[2, 0:15]) * np.ones((250, 1)), np.std(stokes[3, 0:15]) * np.ones((250, 1)) ]).transpose() tmp.obs['los'][:] = np.full((1, 3), [90, 0, 90]) tmp.obs['boundary'][:] = np.full((1, 250, 4), [0, 0, 0, 0]) tmp.save('data/tmp_r') # CHROMOSPHERE tmp = hazel.tools.File_chromosphere(mode='single') tmp.set_default(n_pixel=1, default='offlimb') tmp.save('configurations/model_chromosphere_r') # REMOVE USELESS FILES os.remove('data/tmp_r.wavelength') os.remove('data/tmp_r.weights') # WITHOUT RANDOMIZATION mod = hazel.Model('configurations/1d_r.ini', working_mode='inversion', verbose=3) mod.read_observation() mod.open_output() mod.invert() mod.write_output() mod.close_output()
def test_coordinates(): """ Test for magnetic fields given in spherical/cartesian geometries and vertical/line-of-sight reference frames """ # Define LOS theta angle theta_los = 25.0 # Atmosphere with vertical/cartesian mod = hazel.Model(working_mode='synthesis') mod.add_spectral({ 'Name': 'spec1', 'Wavelength': [10826, 10833, 150], 'topology': 'ch1', 'LOS': [theta_los, 0.0, 90.0], 'Boundary condition': [1.0, 0.0, 0.0, 0.0] }) mod.add_chromosphere({ 'Name': 'ch1', 'Spectral region': 'spec1', 'Height': 3.0, 'Line': '10830', 'Wavelength': [10826, 10833], 'Reference frame': 'vertical', 'Coordinates for magnetic field vector': 'cartesian' }) mod.setup() # Atmosphere with vertical/spherical mod2 = hazel.Model(working_mode='synthesis') mod2.add_spectral({ 'Name': 'spec1', 'Wavelength': [10826, 10833, 150], 'topology': 'ch1', 'LOS': [theta_los, 0.0, 90.0], 'Boundary condition': [1.0, 0.0, 0.0, 0.0] }) mod2.add_chromosphere({ 'Name': 'ch1', 'Spectral region': 'spec1', 'Height': 3.0, 'Line': '10830', 'Wavelength': [10826, 10833], 'Reference frame': 'vertical', 'Coordinates for magnetic field vector': 'spherical' }) mod2.setup() # Atmosphere with line-of-sight/cartesian mod3 = hazel.Model(working_mode='synthesis') mod3.add_spectral({ 'Name': 'spec1', 'Wavelength': [10826, 10833, 150], 'topology': 'ch1', 'LOS': [theta_los, 0.0, 90.0], 'Boundary condition': [1.0, 0.0, 0.0, 0.0] }) mod3.add_chromosphere({ 'Name': 'ch1', 'Spectral region': 'spec1', 'Height': 3.0, 'Line': '10830', 'Wavelength': [10826, 10833], 'Reference frame': 'line-of-sight', 'Coordinates for magnetic field vector': 'cartesian' }) mod3.setup() # Atmosphere with line-of-sight/spherical mod4 = hazel.Model(working_mode='synthesis') mod4.add_spectral({ 'Name': 'spec1', 'Wavelength': [10826, 10833, 150], 'topology': 'ch1', 'LOS': [theta_los, 0.0, 90.0], 'Boundary condition': [1.0, 0.0, 0.0, 0.0] }) mod4.add_chromosphere({ 'Name': 'ch1', 'Spectral region': 'spec1', 'Height': 3.0, 'Line': '10830', 'Wavelength': [10826, 10833], 'Reference frame': 'line-of-sight', 'Coordinates for magnetic field vector': 'spherical' }) mod4.setup() # Magnetic field points along the LOS B = 0.5 thB = 25.0 phiB = 0.0 mod2.atmospheres['ch1'].set_parameters( [B, thB, phiB, 1.0, 0.0, 8.0, 1.0, 0.0], 1.0) mod2.synthesize() # Transform to cartesian and check that they are the same Bx = B * np.sin(thB * np.pi / 180) * np.cos(phiB * np.pi / 180) By = B * np.sin(thB * np.pi / 180) * np.sin(phiB * np.pi / 180) Bz = B * np.cos(thB * np.pi / 180) mod.atmospheres['ch1'].set_parameters( [Bx, By, Bz, 1.0, 0.0, 8.0, 1.0, 0.0], 1.0) mod.synthesize() # Compute now in LOS/spherical and check that they are the same # We use thetaB=0 in this case because LOS is pointing along the field. mod4.atmospheres['ch1'].set_parameters( [B, 0.0, phiB, 1.0, 0.0, 8.0, 1.0, 0.0], 1.0) mod4.synthesize() np.testing.assert_allclose(mod.spectrum['spec1'].stokes, mod2.spectrum['spec1'].stokes, rtol=1e-5, atol=0) np.testing.assert_allclose(mod.spectrum['spec1'].stokes, mod4.spectrum['spec1'].stokes, rtol=1e-5, atol=0)
I, Q, U, V = np.array(fits.open('data/5876_m1_20100316.fits')[0].data) I_I_max = np.divide(I, np.transpose([np.max(I, axis=1)])) Q_I_max = np.divide(Q, np.transpose([np.max(I, axis=1)])) U_I_max = np.divide(U, np.transpose([np.max(I, axis=1)])) V_I_max = np.divide(V, np.transpose([np.max(I, axis=1)])) I_mean = np.mean(np.transpose(I_I_max[6:54]), axis=1)[300:550] Q_mean = np.mean(np.transpose(Q_I_max[6:54]), axis=1)[300:550] U_mean = np.mean(np.transpose(U_I_max[6:54]), axis=1)[300:550] V_mean = np.mean(np.transpose(V_I_max[6:54]), axis=1)[300:550] stokes = np.array([I_mean, Q_mean, U_mean, V_mean]) mod = hazel.Model(working_mode='synthesis', verbose=True) dict = { 'Name': 'spec1', 'Wavelength': [5874.699, 5876.807, 250], 'topology': 'ch1', 'LOS': [90.0, 0.0, 90.0], 'Boundary condition': [0.0, 0.0, 0.0, 0.0] } mod.add_spectral(dict) mod.add_chromosphere({ 'Name': 'ch1', 'Spectral region': 'spec1', 'Height': 29.5, 'Line': '5876',