From c0134d24335725f03553dbf50d00a8be435b58a8 Mon Sep 17 00:00:00 2001 From: Hendrix Demers Date: Thu, 30 Apr 2020 18:49:08 -0400 Subject: [PATCH 1/3] Ignore virtual environment files --- .gitignore | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.gitignore b/.gitignore index fed079a..548c81a 100644 --- a/.gitignore +++ b/.gitignore @@ -97,6 +97,15 @@ docs/_build/ # PyBuilder target/ +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + #####=== Jython ===##### *.pyc From 3021322ac9ba2f1f29b213cc2617ff29d0d479e3 Mon Sep 17 00:00:00 2001 From: Hendrix Demers Date: Thu, 30 Apr 2020 21:28:18 -0400 Subject: [PATCH 2/3] Get transitions for non zero relative weight Print relative weight with print_element_xray_transitions. Get transitions for non zero relative weight in element_xray_transitions even if probability is zero. This to get x-ray transition for lights element like Li. Implement a naive implementation, it could be improved. --- pyxray/base.py | 9 +++++++-- pyxray/sql/data.py | 37 ++++++++++++++++++++++++++++++++++--- tests/sql/test_data.py | 16 ++++++++++++++++ 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/pyxray/base.py b/pyxray/base.py index c8c1108..dd68653 100644 --- a/pyxray/base.py +++ b/pyxray/base.py @@ -241,7 +241,7 @@ def print_element_xray_transitions(self, element, file=sys.stdout, tabulate_kwar :arg file: file for output, default to standard out """ - header = ['IUPAC', 'Siegbahn', 'Energy (eV)', 'Probability'] + header = ['IUPAC', 'Siegbahn', 'Energy (eV)', 'Probability', 'Relative weight'] rows = [] for xray_transition in self.element_xray_transitions(element): @@ -265,7 +265,12 @@ def print_element_xray_transitions(self, element, file=sys.stdout, tabulate_kwar except: probability = '' - rows.append([iupac, siegbahn, energy_eV, probability]) + try: + relative_weight = self.xray_transition_relative_weight(element, xray_transition) + except: + relative_weight = '' + + rows.append([iupac, siegbahn, energy_eV, probability, relative_weight]) rows.sort(key=operator.itemgetter(2)) diff --git a/pyxray/sql/data.py b/pyxray/sql/data.py index 8ab8184..a8f320a 100644 --- a/pyxray/sql/data.py +++ b/pyxray/sql/data.py @@ -6,6 +6,7 @@ # Third party modules. import sqlalchemy.sql +from sqlalchemy import or_ # Local modules. from pyxray.base import _DatabaseMixin, NotFound @@ -367,9 +368,39 @@ def element_xray_transitions(self, element, xray_transition=None, reference=None self._update_xray_transition(builder, table_probability, xray_transition, search=True) transitions = [] - for src_n, src_l, src_j_n, dst_n, dst_l, dst_j_n in self._execute_many(builder): - transition = descriptor.XrayTransition(src_n, src_l, src_j_n, dst_n, dst_l, dst_j_n) - transitions.append(transition) + try: + for src_n, src_l, src_j_n, dst_n, dst_l, dst_j_n in self._execute_many(builder): + transition = descriptor.XrayTransition(src_n, src_l, src_j_n, dst_n, dst_l, dst_j_n) + transitions.append(transition) + except NotFound: + logger.info("No transition found for {}".format(element)) + + if len(transitions) == 0: + table_relative_weight = self.require_table(property.XrayTransitionRelativeWeight) + builder = StatementBuilder() + builder.add_column(table_xray.c['source_principal_quantum_number']) + builder.add_column(table_xray.c['source_azimuthal_quantum_number']) + builder.add_column(table_xray.c['source_total_angular_momentum_nominator']) + builder.add_column(table_xray.c['destination_principal_quantum_number']) + builder.add_column(table_xray.c['destination_azimuthal_quantum_number']) + builder.add_column(table_xray.c['destination_total_angular_momentum_nominator']) + builder.add_join(table_relative_weight, table_xray, table_relative_weight.c['xray_transition_id'] == table_xray.c['id']) + builder.add_clause(table_relative_weight.c['value'] > 0.0) + self._update_element(builder, table_relative_weight, element) + self._update_reference(builder, table_relative_weight, reference) + if xray_transition is not None: + self._update_xray_transition(builder, table_relative_weight, xray_transition, search=True) + + transitions = [] + try: + for src_n, src_l, src_j_n, dst_n, dst_l, dst_j_n in self._execute_many(builder): + transition = descriptor.XrayTransition(src_n, src_l, src_j_n, dst_n, dst_l, dst_j_n) + transitions.append(transition) + except NotFound: + logger.info("No transition found for {}".format(element)) + + if len(transitions) == 0: + raise NotFound return tuple(transitions) diff --git a/tests/sql/test_data.py b/tests/sql/test_data.py index 4a53e04..3928b26 100644 --- a/tests/sql/test_data.py +++ b/tests/sql/test_data.py @@ -13,6 +13,7 @@ # Local modules. import pyxray.descriptor as descriptor from pyxray.sql.data import SqlDatabase, NotFound +import pyxray.data # Globals and constants variables. K = descriptor.AtomicSubshell(1, 0, 1) @@ -23,6 +24,10 @@ def database(builder): return SqlDatabase(builder.engine) +@pytest.fixture +def database_real(tmp_path): + return pyxray.data.database + def test_add_preferred_reference(database): database.clear_preferred_references() database.add_preferred_reference('lee1966') @@ -131,6 +136,17 @@ def test_element_xray_transitions_with_xray_transition(database, xray_transition transitions = database.element_xray_transitions(118, xray_transition) assert len(transitions) == expected +@pytest.mark.parametrize('element, expected', [ + (13, 14), + (6, 2), + (5, 3), + (4, 3), + (3, 2), +]) +def test_element_xray_transitions(database_real, element, expected): + transitions = database_real.element_xray_transitions(element) + assert len(transitions) == expected + @pytest.mark.parametrize('element,reference', [(118, 'unknown'), (1, None)]) def test_element_xray_transitions_notfound(database, element, reference): with pytest.raises(NotFound): From 33c869431c5ef92faf18b6e1f765755eff5f05bb Mon Sep 17 00:00:00 2001 From: Hendrix Demers Date: Fri, 1 May 2020 17:05:47 -0400 Subject: [PATCH 3/3] Remove unused import --- pyxray/sql/data.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pyxray/sql/data.py b/pyxray/sql/data.py index a8f320a..8f7e8a4 100644 --- a/pyxray/sql/data.py +++ b/pyxray/sql/data.py @@ -6,7 +6,6 @@ # Third party modules. import sqlalchemy.sql -from sqlalchemy import or_ # Local modules. from pyxray.base import _DatabaseMixin, NotFound