def test_atom_data_chianti_ions_subset(memory_session): nist = DataSource.as_unique(memory_session, short_name="nist-asd") ch = DataSource.as_unique(memory_session, short_name="chianti_v8.0.2") ku = DataSource.as_unique(memory_session, short_name="ku_latest") with pytest.raises(ValueError): atom_data = AtomData(memory_session, selected_atoms="He, Be, B, N VI", chianti_ions="He 1; N 5; Si 1")
def test_atom_data_init(memory_session): nist = DataSource.as_unique(memory_session, short_name="nist-asd") ch = DataSource.as_unique(memory_session, short_name="chianti_v8.0.2") ku = DataSource.as_unique(memory_session, short_name="ku_latest") atom_data = AtomData(memory_session, selected_atoms="He, Be, B, N", chianti_ions="He 1; N 5") assert set(atom_data.selected_atomic_numbers) == set([2, 4, 5, 7]) assert set(atom_data.chianti_ions) == set([(2, 1), (7, 5)])
def test_atom_data_init(memory_session): nist = DataSource.as_unique(memory_session, short_name="nist-asd") ch = DataSource.as_unique(memory_session, short_name="chianti_v8.0.2") ku = DataSource.as_unique(memory_session, short_name="ku_latest") atom_data = AtomData(memory_session, selected_atoms="He, Be, B, N", chianti_ions="He 1; N 5") assert set(atom_data.selected_atomic_numbers) == set([2, 4, 5, 7]) assert set(atom_data.chianti_ions) == set([(2,1), (7,5)])
def test_atomic_weights_query(foo_session, atomic_number, ds_short_name, expected_weight): data_source = DataSource.as_unique(foo_session, short_name=ds_short_name) atom, atom_weight = foo_session.query(Atom, AtomWeight).\ filter(Atom.atomic_number == atomic_number).\ join(Atom.weights).\ filter(AtomWeight.data_source == data_source).one() assert_quantity_allclose([atom_weight.quantity], expected_weight)
def test_atom_merge_new_quantity(foo_session, H): cr = DataSource.as_unique(foo_session, short_name="cr") H.merge_quantity(foo_session, AtomicWeight(data_source=cr, quantity=1.00754*u.u, std_dev=3e-5,)) foo_session.commit() res = foo_session.query(AtomicWeight).filter(and_(AtomicWeight.atom==H, AtomicWeight.data_source==cr)).one() assert_almost_equal(res.quantity.value, 1.00754)
def test_atomic_weights_atom_relationship(foo_session): nist = DataSource.as_unique(foo_session, short_name="nist") q = foo_session.query(Atom, AtomicWeight).\ join(Atom.quantities.of_type(AtomicWeight)).\ filter(AtomicWeight.data_source == nist).first() assert_almost_equal(q.AtomicWeight.value, 1.00784)
def __init__(self, session, ions=None, ds_short_name=None): if ds_short_name is None: ds_short_name = '{}_v{}'.format(self.ds_prefix, masterlist_version) self.session = session self.ion_readers = list() self.ions = list() if ions is not None: try: ions = parse_selected_species(ions) except ParseException: raise ValueError( 'Input is not a valid species string {}'.format(ions)) self.ions = [convert_species_tuple2chianti_str(_) for _ in ions] else: self.ions = masterlist_ions for ion in self.ions: if ion in self.masterlist_ions: self.ion_readers.append(ChiantiIonReader(ion)) else: logger.info("Ion {0} is not available.".format(ion)) self.data_source = DataSource.as_unique(self.session, short_name=ds_short_name) # To get the id if a new data source was created if self.data_source.data_source_id is None: self.session.flush()
def test_line_quantities_query(foo_session, atomic_number, ion_charge, ds_short_name, lower_level_index, upper_level_index, expected_wavelength, expected_a_value, expected_gf_value): data_source = DataSource.as_unique(foo_session, short_name=ds_short_name) ion = Ion.as_unique(foo_session, atomic_number=atomic_number, ion_charge=ion_charge) lower_level = foo_session.query(Level).\ filter(and_(Level.data_source==data_source, Level.ion == ion, Level.level_index == lower_level_index)).one() upper_level = foo_session.query(Level). \ filter(and_(Level.data_source == data_source, Level.ion == ion, Level.level_index == upper_level_index)).one() line = foo_session.query(Line).\ filter(and_(Line.data_source == data_source, Line.lower_level == lower_level, Line.upper_level == upper_level)).one() wavelength = line.wavelengths[0].quantity a_value = line.a_values[0].quantity gf_value = line.gf_values[0].quantity assert_quantity_allclose(wavelength, expected_wavelength) assert_quantity_allclose(a_value, expected_a_value) assert_quantity_allclose(gf_value, expected_gf_value)
def __init__(self, session, data_fn, ds_name='knox_long'): self.session = session self.data_fn = data_fn self.data_source = DataSource.as_unique(self.session, short_name=ds_name) if self.data_source.data_source_id is None: self.session.flush()
def __init__(self, session, ions=None, ds_short_name="chianti_v8.0.2"): self.session = session # ToDo write a parser for Spectral Notation self.ion_readers = list() self.ions = list() if ions is not None: try: ions = parse_selected_species(ions) except ParseException: raise ValueError( 'Input is not a valid species string {}'.format(ions)) self.ions = [convert_species_tuple2chianti_str(_) for _ in ions] else: self.ions = masterlist_ions for ion in self.ions: if ion in self.masterlist_ions: self.ion_readers.append(ChiantiIonReader(ion)) else: print("Ion {0} is not available".format(ion)) self.data_source = DataSource.as_unique(self.session, short_name=ds_short_name) if self.data_source.data_source_id is None: # To get the id if a new data source was created self.session.flush()
def test_atomic_weights_convert_to(foo_session, atomic_number, ds_short_name, expected_weight_value): data_source = DataSource.as_unique(foo_session, short_name=ds_short_name) atom, atom_weight_value = foo_session.query(Atom, AtomWeight.quantity.to(u.solMass).value). \ filter(Atom.atomic_number == atomic_number). \ join(Atom.weights). \ filter(AtomWeight.data_source == data_source).one() assert_almost_equal(atom_weight_value, expected_weight_value)
def test_gfall_ingester_ingest_lines(memory_session, gfall_ingester, atomic_number, ion_charge, level_index_lower, level_index_upper, exp_wavelength, exp_gf_value): gfall_ingester.ingest(levels=True, lines=True) ion = Ion.as_unique(memory_session, atomic_number=atomic_number, ion_charge=ion_charge) data_source = DataSource.as_unique(memory_session, short_name="ku_latest") lower_level = memory_session.query(Level).\ filter(and_(Level.data_source==data_source, Level.ion==ion, Level.level_index==level_index_lower)).one() upper_level = memory_session.query(Level). \ filter(and_(Level.data_source == data_source, Level.ion == ion, Level.level_index == level_index_upper)).one() line = memory_session.query(Line).\ filter(and_(Line.data_source==data_source, Line.lower_level==lower_level, Line.upper_level==upper_level)).one() wavelength = line.wavelengths[0].quantity gf_value = line.gf_values[0].quantity assert_quantity_allclose(wavelength, exp_wavelength) assert_quantity_allclose(gf_value, exp_gf_value)
def test_atomic_quantity_convert_to(foo_session): H = foo_session.query(Atom).filter(Atom.atomic_number==1).one() nist = DataSource.as_unique(foo_session, short_name="nist") aw = foo_session.query(AtomicWeight).filter(and_(AtomicWeight.atom==H, AtomicWeight.data_source==nist)).one() aw.to(u.ng) assert_almost_equal(aw.value, 1.6735573234079996e-15)
def __init__(self, session, fname=None, ions=None, unique_level_identifier=None, ds_short_name="ku_latest"): self.session = session if fname is None: self.fname = GFALL_URL else: self.fname = fname self.gfall_reader = GFALLReader(ions, self.fname, unique_level_identifier) if ions is not None: try: ions = parse_selected_species(ions) except ParseException: raise ValueError( 'Input is not a valid species string {}'.format(ions)) ions = pd.DataFrame.from_records( ions, columns=["atomic_number", "ion_charge"]) self.ions = ions.set_index(['atomic_number', 'ion_charge']) else: self.ions = None self.data_source = DataSource.as_unique(self.session, short_name=ds_short_name) if self.data_source.data_source_id is None: # To get the id if a new data source was created self.session.flush()
def test_atomic_weights_unique_constraint(foo_session): nist = DataSource.as_unique(foo_session, short_name="nist") H = foo_session.query(Atom).filter(Atom.atomic_number==1).one() H.quantities.append( AtomicWeight(data_source=nist, value=666) ) with pytest.raises(IntegrityError): foo_session.commit()
def test_weightscomp_ingest_nonexisting_atomic_weights(atomic_number, nom_val, std_dev, weightscomp_ingester, test_session): weightscomp_ingester.ingest(test_session) nist = DataSource.as_unique(test_session, short_name="nist") aw = test_session.query(AtomicWeight).\ filter(AtomicWeight.atomic_number==atomic_number).\ filter(AtomicWeight.data_source==nist).one() assert_almost_equal(aw.value, nom_val) assert_almost_equal(aw.std_dev, std_dev)
def test_atom_merge_new_quantity(foo_session): H = foo_session.query(Atom).filter(Atom.atomic_number==1).one() cr = DataSource.as_unique(foo_session, short_name="cr") H.merge_quantity(foo_session, AtomicWeight(data_source=cr, unit=u.u, value=1.00754, std_dev=3e-5,)) foo_session.commit() q = foo_session.query(AtomicWeight).filter(and_(AtomicWeight.atom==H, AtomicWeight.data_source==cr)).one() assert_almost_equal(q.value, 1.00754)
def test_atom_merge_existing_quantity(foo_session): H = foo_session.query(Atom).filter(Atom.atomic_number==1).one() nist = DataSource.as_unique(foo_session, short_name="nist") H.merge_quantity(foo_session, AtomicWeight(data_source=nist, unit=u.u, value=1.00654, std_dev=4e-5,)) foo_session.commit() q = foo_session.query(AtomicWeight).filter(and_(AtomicWeight.atom==H, AtomicWeight.data_source==nist)).one() assert_almost_equal(q.value, 1.00654)
def test_level_g_hybrid_attribute_expression(foo_session, atomic_number, ion_charge, ds_short_name, level_index, exp_g): data_source = DataSource.as_unique(foo_session, short_name=ds_short_name) ion = Ion.as_unique(foo_session, atomic_number=atomic_number, ion_charge=ion_charge) g = foo_session.query(Level.g). \ filter(and_(Level.data_source == data_source, Level.ion == ion, Level.level_index == level_index)).scalar() assert g == exp_g
def test_ionization_energies_query(foo_session, atomic_number, ion_charge, ds_short_name, method, expected_ionization_energy): data_source = DataSource.as_unique(foo_session, short_name=ds_short_name) ion, ionization_energy = foo_session.query(Ion, IonizationEnergy).\ filter(and_(Ion.atomic_number == atomic_number, Ion.ion_charge == ion_charge)).\ join(Ion.ionization_energies).\ filter(and_(IonizationEnergy.data_source == data_source, IonizationEnergy.method == method)).one() assert_quantity_allclose([ionization_energy.quantity], expected_ionization_energy)
def __init__(self, session, ds_short_name, parser, downloader): self.parser = parser self.downloader = downloader self.session = session self.data_source = DataSource.as_unique(self.session, short_name=ds_short_name) if self.data_source.data_source_id is None: # To get the id if a new data source was created self.session.flush() if not self.requirements_satisfied(): raise IngesterError('Requirements for ingest are not satisfied!')
def ingest(self, session): """ *Only* ingests atomic weights *for now* """ print "Ingesting atomic weights" atomic_df = self.parser.prepare_atomic_dataframe() atomic_df = atomic_df[pd.notnull(atomic_df[AW_VAL_COL])] data_source = DataSource.as_unique(session, short_name=self.ds_short_name) for atom_num, row in atomic_df.iterrows(): atom = session.query(Atom).filter(Atom.atomic_number==atom_num).one() atom.merge_quantity(session, AtomicWeight(data_source=data_source, value=row[AW_VAL_COL], std_dev=row[AW_SD_COL], unit=u.u))
def test_gfall_ingester_ingest_levels(memory_session, gfall_ingester, atomic_number, ion_charge, level_index, exp_energy, exp_j, exp_method): gfall_ingester.ingest(levels=True, lines=False) ion = Ion.as_unique(memory_session, atomic_number=atomic_number, ion_charge=ion_charge) data_source = DataSource.as_unique(memory_session, short_name="ku_latest") level, energy = memory_session.query(Level, LevelEnergy).\ filter(and_(Level.ion==ion, Level.level_index==level_index), Level.data_source==data_source).\ join(Level.energies).one() assert_almost_equal(level.J, exp_j) assert_quantity_allclose(energy.quantity, exp_energy.to(u.eV, equivalencies=u.spectral())) assert energy.method == exp_method
def __init__(self, session, fname=None, ds_name='knox_long'): self.session = session if fname is None: self.fname = ZETA_DATA_URL else: self.fname = fname self.data_source = DataSource.as_unique(self.session, short_name=ds_name) if self.data_source.data_source_id is None: self.session.flush()
def test_ionization_energies_query(foo_session, atomic_number, ion_charge, ds_short_name, level_index, method, expected_level_energy_value): data_source = DataSource.as_unique(foo_session, short_name=ds_short_name) ion = Ion.as_unique(foo_session, atomic_number=atomic_number, ion_charge=ion_charge) level, level_energy_value = foo_session.query(Level, LevelEnergy.quantity.to(u.Unit("cm-1"), equivalencies=u.spectral()).value). \ filter(and_(Level.ion == ion, Level.data_source == data_source), Level.level_index == level_index). \ join(Level.energies). \ filter(and_(LevelEnergy.data_source == data_source, LevelEnergy.method == method)).one() assert_almost_equal(level_energy_value, expected_level_energy_value)
def test_level_query(foo_session, atomic_number, ion_charge, ds_short_name, level_index, configuration, term, L, J, spin_multiplicity, parity): data_source = DataSource.as_unique(foo_session, short_name=ds_short_name) ion = Ion.as_unique(foo_session, atomic_number=atomic_number, ion_charge=ion_charge) level = foo_session.query(Level).\ filter(and_(Level.data_source == data_source, Level.ion == ion, Level.level_index == level_index)).one() assert level.configuration == configuration assert level.term == term assert level.L == L assert_almost_equal(level.J, J) assert level.spin_multiplicity == spin_multiplicity assert level.parity == parity
def __init__(self, session, fname, ions=None, ds_short_name="ku_latest"): self.session = session self.gfall_reader = GFALLReader(fname) if ions is not None: try: ions = parse_selected_species(ions) except ParseException: raise ValueError('Input is not a valid species string {}'.format(ions)) ions = pd.DataFrame.from_records(ions, columns=["atomic_number", "ion_charge"]) self.ions = ions.set_index(['atomic_number', 'ion_charge']) else: self.ions = None self.data_source = DataSource.as_unique(self.session, short_name=ds_short_name) if self.data_source.data_source_id is None: # To get the id if a new data source was created self.session.flush()
def test_line_wavelength_medium(foo_session, atomic_number, ion_charge, ds_short_name, lower_level_index, upper_level_index, expected_medium): data_source = DataSource.as_unique(foo_session, short_name=ds_short_name) ion = Ion.as_unique(foo_session, atomic_number=atomic_number, ion_charge=ion_charge) lower_level = foo_session.query(Level). \ filter(and_(Level.data_source == data_source, Level.ion == ion, Level.level_index == lower_level_index)).one() upper_level = foo_session.query(Level). \ filter(and_(Level.data_source == data_source, Level.ion == ion, Level.level_index == upper_level_index)).one() line = foo_session.query(Line). \ filter(and_(Line.data_source == data_source, Line.lower_level == lower_level, Line.upper_level == upper_level)).one() wavelength = line.wavelengths[0] assert wavelength.medium == expected_medium
def test_weithscomp_ingest_existing_atomic_weights(atomic_number, nom_val, std_dev, weightscomp_ingester, test_session): nist = DataSource.as_unique(test_session, short_name="nist") atom = test_session.query(Atom).filter(Atom.atomic_number==atomic_number).one() #import pdb; pdb.set_trace() atom.quantities = [ AtomicWeight(data_source=nist, value=9.9999, unit=u.u), ] test_session.commit() weightscomp_ingester.ingest(test_session) q = test_session.query(Atom, AtomicWeight).\ join(Atom.quantities.of_type(AtomicWeight)).\ filter(AtomicWeight.data_source==nist).\ filter(Atom.atomic_number==atomic_number).one() assert q.AtomicWeight.atomic_number == atomic_number assert_almost_equal(q.AtomicWeight.value, nom_val) assert_almost_equal(q.AtomicWeight.std_dev, std_dev)
def test_gfall_ingester_ingest_lines_wavelength_medium(memory_session, gfall_ingester, atomic_number, ion_charge, level_index_lower, level_index_upper, exp_wavelength, exp_medium): gfall_ingester.ingest(levels=True, lines=True) ion = Ion.as_unique(memory_session, atomic_number=atomic_number, ion_charge=ion_charge) data_source = DataSource.as_unique(memory_session, short_name="ku_latest") lower_level = memory_session.query(Level). \ filter(and_(Level.data_source == data_source, Level.ion == ion, Level.level_index == level_index_lower)).one() upper_level = memory_session.query(Level). \ filter(and_(Level.data_source == data_source, Level.ion == ion, Level.level_index == level_index_upper)).one() line = memory_session.query(Line). \ filter(and_(Line.data_source == data_source, Line.lower_level == lower_level, Line.upper_level == upper_level)).one() wavelength = line.wavelengths[0] assert_quantity_allclose(wavelength.quantity, exp_wavelength) assert wavelength.medium == exp_medium
def test_e_collision_quantities_query(foo_session, atomic_number, ion_charge, ds_short_name, lower_level_index, upper_level_index, expected_energy, expected_temp_strengths): data_source = DataSource.as_unique(foo_session, short_name=ds_short_name) ion = Ion.as_unique(foo_session, atomic_number=atomic_number, ion_charge=ion_charge) lower_level = foo_session.query(Level). \ filter(and_(Level.data_source == data_source, Level.ion == ion, Level.level_index == lower_level_index)).one() upper_level = foo_session.query(Level). \ filter(and_(Level.data_source == data_source, Level.ion == ion, Level.level_index == upper_level_index)).one() e_col = foo_session.query(ECollision). \ filter(and_(ECollision.data_source == data_source, ECollision.lower_level == lower_level, ECollision.upper_level == upper_level)).one() energy = e_col.energies[0].quantity assert_quantity_allclose(energy, expected_energy) for temp_strength, expected_temp_strength in zip(e_col.temp_strengths_tuple, expected_temp_strengths): assert_allclose(temp_strength, expected_temp_strength)
def ingest(self, session): data_source = DataSource.as_unique(session, short_name=self.ds_short_name) return "Ingested"
def test_weightscomp_ingest_default_count(weightscomp_ingester, test_session): weightscomp_ingester.download() weightscomp_ingester.ingest(test_session) nist = DataSource.as_unique(test_session, short_name="nist") assert test_session.query(AtomicWeight).\ filter(AtomicWeight.data_source==nist).count() == 94
def nist(foo_session): return DataSource.as_unique(foo_session, short_name="nist")
def test_data_source_as_unique(memory_session): nist = DataSource.as_unique(memory_session, short_name="nist", name="National Institute of Standards and Technology") nist2 = DataSource.as_unique(memory_session, short_name="nist") assert nist is nist2
def foo_engine(): engine = create_engine(foo_db_url) Base.metadata.drop_all(engine) Base.metadata.create_all(engine) session = Session(bind=engine) # atoms h = Atom(atomic_number=1, symbol='H') ne = Atom(atomic_number=10, symbol='Ne') # data sources nist = DataSource(short_name='nist') ku = DataSource(short_name='ku') ch = DataSource(short_name="chianti") # atomic weights h.weights = [ AtomWeight(quantity=1.00784 * u.u, data_source=nist, uncert=4e-3), AtomWeight(quantity=1.00811 * u.u, data_source=ku, uncert=4e-3), ] # ions h0 = Ion(atomic_number=1, ion_charge=0, ionization_energies=[ IonizationEnergy(quantity=13.5984 * u.eV, data_source=nist, method="th") ]) ne1 = Ion(atomic_number=10, ion_charge=1, ionization_energies=[ IonizationEnergy(quantity=40.96296 * u.eV, data_source=nist, method="th"), IonizationEnergy(quantity=40.97 * u.eV, data_source=nist, method="m") ]) ne2 = Ion(atomic_number=10, ion_charge=2, ionization_energies=[ IonizationEnergy(quantity=48.839 * u.eV, data_source=nist, method="th") ]) # levels ne2_lvl0_ku = Level(ion=ne2, data_source=ku, level_index=0, configuration="2s2.2p5", term="2P1.5", L="P", J=1.5, spin_multiplicity=2, parity=1, energies=[ LevelEnergy(quantity=0, data_source=ku, method="m"), LevelEnergy(quantity=0, data_source=ku, method="th") ]) ne2_lvl1_ku = Level(ion=ne2, data_source=ku, level_index=1, configuration="2s2.2p5", term="2P0.5", L="P", J=0.5, spin_multiplicity=2, parity=1, energies=[ LevelEnergy(quantity=780.4 * u.Unit("cm-1"), data_source=ku, method="m"), LevelEnergy(quantity=780.0 * u.Unit("cm-1"), data_source=ku, method="th") ]) ne2_lvl2_ku = Level(ion=ne2, data_source=ku, level_index=2, configuration="2s2.2p5", term="2D2.5", L="D", J=2.5, spin_multiplicity=2, parity=0, energies=[ LevelEnergy(quantity=1366.3 * u.Unit("cm-1"), data_source=ku, method="m") ]) ne2_lvl1_ch = Level(ion=ne2, data_source=ch, level_index=1, configuration="2s2.2p5", term="2P0.5", L="P", J=0.5, spin_multiplicity=2, parity=1, energies=[ LevelEnergy(quantity=780.2 * u.Unit("cm-1"), data_source=ch, method="m") ]) # lines ne2_line0_ku = Line( lower_level=ne2_lvl0_ku, upper_level=ne2_lvl1_ku, data_source=ku, wavelengths=[LineWavelength(quantity=183.571 * u.AA, data_source=ku)], a_values=[ LineAValue(quantity=5.971e-03 * u.Unit("s-1"), data_source=ku) ], gf_values=[LineGFValue(quantity=8.792e-01, data_source=ku)]) ne2_line1_ku = Line( lower_level=ne2_lvl0_ku, upper_level=ne2_lvl2_ku, data_source=ku, wavelengths=[ LineWavelength(quantity=18.4210 * u.nm, medium=1, data_source=ku) ], a_values=[ LineAValue(quantity=5.587e-03 * u.Unit("s-1"), data_source=ku) ], gf_values=[LineGFValue(quantity=8.238e-01, data_source=ku)]) # electron collisions ne2_e_col0_ku = ECollision( lower_level=ne2_lvl0_ku, upper_level=ne2_lvl1_ku, data_source=ku, bt92_ttype=2, bt92_cups=11.16, energies=[ ECollisionEnergy(quantity=0.007108 * u.rydberg, data_source=ku) ], temp_strengths=[(ECollisionTempStrength(temp=0.0, strength=0.255)), (ECollisionTempStrength(temp=0.07394, strength=0.266))]) session.add_all([ h, ne, nist, ku, ch, h0, ne1, ne2_lvl1_ch, ne2_lvl0_ku, ne2_lvl1_ku, ne2_lvl2_ku, ne2_line0_ku, ne2_line1_ku, ne2_e_col0_ku ]) session.commit() session.close() return engine
def test_data_source_as_unique(foo_session): nist = DataSource.as_unique(foo_session, short_name="nist", name="National Institute of Standards and Technology") nist2 = DataSource.as_unique(foo_session, short_name="nist", name="National Institute of Standards and Technology") assert nist is nist2
def test_data_source_unique_constraint(foo_session): nist_duplicate = DataSource(short_name="nist") foo_session.add(nist_duplicate) with pytest.raises(IntegrityError): foo_session.commit()