Giter Club home page Giter Club logo

pyxray's Introduction

pyxray

image

GitHub Workflow Status

image

Documentation Status

pyxray is a Python library that defines basic object to specify atomic subshells and X-ray transitions. The objects also provide critical information as the energy, existence and different notations of the X-ray transitions.

pyxray supports 3.6+ (no Python 2.x support).

Installation

Easiest way to install using pip:

pip install pyxray

For development installation from the git repository:

git clone [email protected]/openmicroanalysis/pyxray.git
cd pyxray
pip install -e .
pre-commit install

See development section below

Methods

All methods below are accessed by importing pyxray:

import pyxray

Variables of the methods are defined as follows

  • element:

    either

    • Element object
    • atomic number
    • symbol (case insensitive)
    • name (in any language, case insensitive)
    • object with attribute atomic_number or z
  • atomic_shell:

    either

    • AtomicShell object
    • principal quantum number
    • any notation (case insensitive)
  • atomic_subshell:

    either

    • AtomicSubshell object
    • a tuple of principal quantum number, azimuthal quantum number and total angular momentum nominator (e.g. (1, 0, 1) for the atomic subshell 1s^{0.5}
    • any notation (case insensitive)
  • xray_transition:

    either

    • XrayTransition object
    • a tuple of source and destination subshells
    • any notation (case insensitive)
  • language:

    language code (e.g. en, fr, de)

  • notation:

    name of a notation (case insensitive), iupac, siegbahn and orbital are usually supported

  • encoding:

    type of encoding, either ascii, utf16, html or latex

  • reference:

    reference to use to retrieve this value, either

    • Reference object
    • BibTeX key of a reference
    • None, the newest reference will be returned

Element properties

Properties associated with an element, defined as the ground state of an atom where the number of protons equal the number of electrons.

  • pyxray.element(element)

    Returns element descriptor.

  • pyxray.element_atomic_number(element)

    Returns atomic number of an element.

    Examples:

    pyxray.element_atomic_number('fe') #=> 26
    pyxray.element_atomic_number('Fe') #=> 26
    pyxray.element_atomic_number('iron') #=> 26
    pyxray.element_atomic_number('eisen') #=> 26
  • pyxray.element_symbol(element, reference=None)

    Returns symbol of an element.

  • pyxray.element_name(element, language='en', reference=None)

    Returns full name of an element, in the language specified.

  • pyxray.element_atomic_weight(element, reference=None)

    Returns atomic weight of an element. The atomic weight is defined by the CIAAW as it is the ratio of the average atomic mass of an element over 1/12 of the mass of the carbon-12 atom.

  • pyxray.element_mass_density_kg_per_m3(element, reference=None)

    Returns mass density (in kg/m3) of an element.

  • pyxray.element_mass_density_g_per_cm3(element, reference=None)

    Returns mass density (in g/cm3) of an element.

  • pyxray.element_xray_transition(element, reference=None)

    Returns X-ray transition descriptor if x-ray transition has a probability greater than 0 for that element.

  • pyxray.element_xray_transitions(element, xray_transition_set=None, reference=None)

    Returns all X-ray transitions which have a probability greater than 0 for that element. If xray_transition_set is not None, returns all x-ray transitions for this x-ray transition set.

Atomic shell properties

Properties associated with an atomic shell, defined by its principal quantum number.

  • pyxray.atomic_shell(atomic_shell)

    Returns atomic shell descriptor.

  • pyxray.atomic_shell_notation(atomic_shell, notation, encoding='utf16', reference=None)

    Returns notation of an atomic shell.

Atomic subshell properties

Properties associated with an atomic subshell, a subdivision of atomic shells.

  • pyxray.atomic_subshell(atomic_subshell)

    Returns atomic subshell descriptor.

  • pyxray.atomic_subshell_notation(atomic_subshell, notation, encoding='utf16', reference=None)

    Returns notation of an atomic subshell.

    Examples:

    pyxray.atomic_subshell_notation('L3', 'iupac', 'latex') #=> 'L$_{3}$'
    pyxray.atomic_subshell_notation('L3', 'orbital') #-> '2p3/2'
  • pyxray.atomic_subshell_binding_energy_eV(element, atomic_subshell, reference=None)

    Returns binding energy of an element and atomic subshell (in eV).

  • pyxray.atomic_subshell_radiative_width_eV(element, atomic_subshell, reference=None)

    Returns radiative width of an element and atomic subshell (in eV).

  • pyxray.atomic_subshell_nonradiative_width_eV(element, atomic_subshell, reference=None)

    Returns nonradiative width of an element and atomic subshell (in eV).

  • pyxray.atomic_subshell_occupancy(element, atomic_subshell, reference=None)

    Returns occupancy of an element and atomic subshell.

X-ray transition properties

Properties associated with an electron transition, relaxation process of an electron between quantum states leading to X-rays emission.

  • pyxray.xray_transition(xray_transition)

    Returns X-ray transition descriptor.

  • pyxray.xray_transition_notation(xray_transition, notation, encoding='utf16', reference=None)

    Returns notation of an X-ray transition.

    Examples:

    pyxray.transition_notation('Ka1', 'iupac') #=> 'K-L3'
    pyxray.transition_notation('Ka', 'iupac') #=> 'K-L2,3'
    pyxray.transition_notation('L3-M1', 'siegbahn', 'ascii') #=> 'Ll'
  • pyxray.xray_transition_energy_eV(element, xray_transition, reference=None)

    Returns energy of an element and X-ray transition (in eV).

    Examples:

    pyxray.xray_transition_energy_eV(14, 'Ka1') #=> 1740.0263764535946
    pyxray.xray_transition_energy_eV(14, 'Ma1') #=> NotFound exception
  • pyxray.xray_transition_probability(element, xray_transition, reference=None)

    Returns probability of an element and X-ray transition.

  • pyxray.xray_transition_relative_weight(element, xray_transition, reference=None)

    Returns relative weight of an element and X-ray transition.

X-ray line

Object to represent an x-ray transition and its properties.

  • pyxray.xray_line(element, xray_transition, reference=None)

    Returns X-ray line descriptor.

xrayline = pyxray.xray_line(14, 'Ka1')
xrayline.atomic_number #=> 14
xrayline.transition #=> XrayTransition(2, 1, 3, 1, 0, 1)
xrayline.iupac #=> Si K–L3
xrayline.siegbahn #=> Si Kα1
xrayline.energy_eV #=> 1740.0
xrayline.probability #=> 0.031705199999999996
xrayline.relative_weight #=> 1.0

As any other descriptors, X-ray line objects are immutable and hashable so they can be used as keys of a dictionary.

xrayline1 = pyxray.xray_line(13, 'Ka1')
xrayline2 = pyxray.xray_line('Al', 'Ka1')
xrayline1 == xrayline2 #=> True
pyxray.xray_line(13, 'Ka1') == pyxray.xray_line(13, 'Ka') #=> False

To sort X-ray lines, use one of their properties:

from operator import attrgetter
lines = [pyxray.xray_line(14, 'Ka1'), pyxray.xray_line(13, 'Ka1'), pyxray.xray_line(14, 'Ll')]
sorted(lines, key=attrgetter('energy_eV')) #=> [XrayLine(Si L3–M1), XrayLine(Al K–L3), XrayLine(Si K–L3)]

Composition

Defines a composition of a compound.

To create a composition, use the class methods:

  • Composition.from_pure(z)
  • Composition.from_formula(formula)
  • Composition.from_mass_fractions(mass_fractions, formula=None)
  • Composition.from_atomic_fractions(atomic_fractions, formula=None)

Use the following attributes to access the composition values:

  • mass_fractions: dict where the keys are atomic numbers and the values weight fractions.
  • atomic_fractions: dict where the keys are atomic numbers and the values atomic fractions.
  • formula: chemical formula

The composition object is immutable, i.e. it cannot be modified once created. Equality can be checked. It is hashable. It can be pickled or copied.

Release notes

1.7

  • #19 Get transitions for light elements when no probability are available
  • #20 Use GitHub Actions for continuous integration. Add pre-commit hooks and black formatting.

1.6.1

  • Fix deprecation warning with new setuptools
  • Fix problem with requests caching

1.6

  • Add ordering of Element, AtomicShell, AtomicSubshell
  • Use sqlalchemy to create and interact with database
  • Add probability and relative weight properties to XrayLine
  • Add possibility to define preferred references

1.5

  • Add composition object

1.4

  • #13 Add DTSA X-ray subshell and line data
  • #14 Use dataclasses for descriptors and properties

1.3.4

  • Fix descriptors can be copied and pickled.

1.3.3

  • Fix method element_xray_transitions not to return duplicates.

1.3.2

  • Add energy to XrayLine.
  • Fix missing energy property for x-ray transition sets from JEOL database.
  • Clean up of unit tests.

1.3.1

  • Make XrayLine a descriptor and add method to create it from database.

1.2.1

  • Fix in build process.

1.2.0

  • Add XrayLine class.

Contributors

Development

pyxray stores all data for the above functions in a SQLite database. The database is constructed during the build process of the Python package (i.e. python setup.py build) using registered parsers. The provided parsers are located in the package pyxray.parser, but external parsers can be provided by registering to the entry point pyxray.parser. In short, the database is not provide in the source code, only in the distributed version. It is therefore necessary to build the SQLite database when running pyxray in development mode. Building the database will take several minutes. In short, in the pyxray folder, run

pip install -e .[develop]
python3 setup.py build

Build the documentation:

$ cd docs
$ make html

Add or modify the API documentation:

$ cd docs
$ sphinx-apidoc -o source/api -e -f -P ../pyxray
$ make html

License

The library is provided under the MIT license.

pyxray was partially developed as part of the doctorate thesis project of Philippe T. Pinard at RWTH Aachen University (Aachen, Germany) under the supervision of Dr. Silvia Richter.

Copyright (c) 2015-2016/06 Philippe Pinard and Silvia Richter

Copyright (c) 2016/06-2020 Philippe Pinard

pyxray's People

Contributors

drix00 avatar ppinard avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

pyxray's Issues

Refactor data tables from hard coded and CSV to SQL

pyxray contains three main types of data:

  • element properties (atomic number, symbol, English name, atomic weight, mass density)
  • atomic subshells (existence, energy and natural width)
  • X-ray transitions (existence, energy, IUPAC and Siegbahn nomenclature)

As these data can come from different sources, especially for numerical values (e.g. X-ray transition energy), pyxray should be able to accommodate for this reality within its implementation. It should also be extendable to include other types of data in the future.

The current version of pyxray retrieves the data from hard coded values and CSV files. This has the following limitations:

  • All data must be read and kept in memory
  • Requires the specification of arbitrary indexes for the X-ray transition and subshells
  • New sources requires a new file to be generated or modification of the existing tables

After looking at other file formats such as HDF5, the tasks required by pyxray can be addressed by a sqlite database and object relational mapping by the Python library SQLAlchemy. It has the following advantages:

  • information is structured in well-defined table
  • values can be retrieved using specific queries
  • supports multiple entries for different sources
  • all data to be kept in a single file
  • sqlite is available in the Python standard library
  • sqlite has implementations in other programming languages

The development of this refactoring is done in the branch sql.

Cannot build the database

I cannot build the database in developer mode after the commit 2d4f8f2 on a windows 10 computer with Python 3.6 (64-bit). This is the output I obtained

$ python setup.py build
running build
running build_py
No SQL database found
Building database: 0it [00:00, ?it/s]

I found "No SQL database found" in the data.py and it seem when this file is imported, it did not find the database and use the _EmptyDatabase and did not build the missing database later. I am not sure where to create the SQL database file (data/pyxray.db)

Thanks Hendrix

Extract atomic weight from NIST/CIAAW

Referring to issue #2, this issue is to populate the SQL database with atomic weight. Ideally, this should be done automatically from an available online resource.

The most official tabulation of atomic weight appears to come from the CIAAW. Unfortunately, their website does not directly provide an ASCII table of the values. NIST has an online database of atomic weight and isotopes which can be generated as an ASCII file (website, results). The information appears to come from the CIAAW/IUPAC.

The goal would therefore to write a parser to extract the atomic weight from the NIST output. I would proceed as follows:

  1. Use the Python library requests to download the NIST ASCII output
  2. Parse each isotope, storing their Atomic Number, Relative Atomic Mass and Isotopic Composition
  3. For each atomic number, multiply the relative atomic mass by the isotopic composition and sum over all isotopes.
  4. Add values to the SQL database using the ElementAtomicWeightProperty model (see populate_element_symbol_table(engine) in pyxray/sql/default.py as an example)
  5. Write some unit tests to compare the values with those appearing in the NIST periodic table

The code should go in a new function populate_element_atomic_weight_table_(engine) in pyxray/sql/default.py.

Return values of all references

For some functions and set of parameters, multiple references exist. The proposal is to add a all keyword parameter to the functions which if True (default is False) will return a list of properties, which may include properties for several references.

dependencies

Building the database fails if requests package is not installed. Is it intended that it's not part of the requirements?

(env_auto) [vm-user@localhost pyxray]$ python setup.py build
running build
running build_py
Traceback (most recent call last):
  File "setup.py", line 89, in <module>
    entry_points=ENTRY_POINTS,
  File "/home/vm-user/Workspace/pymontecarlo_test/env_auto/lib64/python3.6/site-packages/setuptools/__init__.py", line 129, in setup
    return distutils.core.setup(**attrs)
  File "/usr/lib64/python3.6/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/lib64/python3.6/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/usr/lib64/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/usr/lib64/python3.6/distutils/command/build.py", line 135, in run
    self.run_command(cmd_name)
  File "/usr/lib64/python3.6/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/lib64/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "setup.py", line 22, in run
    builder.build()
  File "/home/vm-user/Workspace/pymontecarlo_test/pyxray/pyxray/sql/build.py", line 98, in build
    parsers = self._find_parsers()
  File "/home/vm-user/Workspace/pymontecarlo_test/pyxray/pyxray/sql/build.py", line 74, in _find_parsers
    return find_parsers()
  File "/home/vm-user/Workspace/pymontecarlo_test/pyxray/pyxray/parser/parser.py", line 40, in find_parsers
    parser = entry_point.load()
  File "/home/vm-user/Workspace/pymontecarlo_test/env_auto/lib64/python3.6/site-packages/pkg_resources/__init__.py", line 2408, in load
    return self.resolve()
  File "/home/vm-user/Workspace/pymontecarlo_test/env_auto/lib64/python3.6/site-packages/pkg_resources/__init__.py", line 2414, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/home/vm-user/Workspace/pymontecarlo_test/pyxray/pyxray/parser/nist.py", line 12, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'

Restructuring

As part of issue #2 and the definition of SQL tables, here is a draft how to restructure the pyxray library.

Build

  • One should be able to build the SQL database directly from pyxray.
  • This entails that the data required should either be available as part of the pyxray source code or be downloaded from the internet.
  • pyxray package should be distributed (e.g. on PyPi) with the most recent version of the SQL database, ready to be used.
  • Building the SQL database should involve
    1. Download data from internet (optional)
    2. Parse data
    3. Cache parsed data (optional)
    4. Create SQL table if it does not already exists
    5. Populate SQL table with new entries if they do not already exist
  • Note that separating the parsing step from the SQL related steps would allow to store the data in a different file format in the future

Structure

  • A data source is defined as: values extracted from a reference representing one or more properties associated to one or more descriptors. Some examples:
  • There are therefore two main categories of object: descriptors (a reference is also a descriptor) and properties

Descriptors

Here are the descriptors required by pyxray and their definition:

  • Element: Ground state of an atom where the number of protons equal the number of electrons
    • Atomic number (z)
  • Atomic shell: Defined by the principal quantum number
    • Principal quantum number (n)
  • Atomic subshell: Subdivision of atomic shells
    • Atomic shell
    • Azimuthal quantum number (l)
    • Total angular momentum (j)
  • Transition: Relaxation process of an electron between quantum states leading to X-rays emission, Auger electrons or Coster-Kronig transitions
    • Source subshell
    • Destination subshell
    • Second destination subshell (for Auger)
  • Set of transitions: For indistinguishable transition (e.g. Ka from Ka1/Ka2)
    • transitions
  • Reference: Bibliographic reference
    • authors
    • year
    • journal
    • ...

There is a finite set for each descriptors (e.g. 118 elements, 7 atomic shells, etc.). From a programming point of view, the descriptors should therefore be:

  • immutable: cannot be modified once created
  • unique: Element(z=6) == Element(z=6) and Element(z=6) is Element(z=6)

Properties

There is no limit on the number of properties that can be defined by combining descriptors. Some examples

  • Energy of a transition of an element
  • Symbol of an element
  • Atomic weight of an element
  • Notation of a transition represented in LaTeX
  • Natural width of an atomic subshell of an element

In all cases, a reference descriptor should be provided to identify the source of the values.

Implementation

  • Classes for each descriptor, respecting the definition and requirements mentioned above
  • Methods for each property, taking descriptors as arguments, e.g.
def get_transition_energy_eV(element, transition, reference=None):
    ...
def get_element_symbol(element, reference=None):
    ...
  • The methods are responsible to retrieve the values from the SQL database
  • A set of default references for each property (when reference is None)

Exchange Class

@fingenerf wrote:

With the pyXRAY module it is possible to get a lot of information about elements, their subshells and transitions. To use these information for example for a digital periodic table it would probably be a good idea to create some kind of an element exchange class to parse all the information about one specific element in one object. This class could not just be used for the periodic table, it could be used in other modules as well.

This class would contain all the element information like the name, the weight and the density, all the information for each subshell of this element like the binding energy and radiative width and it would contain the information for each transition of this element like the transition
energy.

What we're looking for is a exchange format for pyXRAY and applications of any kind. We would like to introduce a class on element level that collects all information about an element (or at least all information from one source).

@ppinard: What do you think about that idea? Do you already plan something similar?

Partial subshell occupancy neglected

The Livermore EADL provides fractional subshell occupancies for some elements. For example, Boron has L_2 and L_3 subshells with 0.33 and 0.67 electrons.

However, in pyxray only the integral part of the value is retained:

value = int(float_(row[11:22]))

This results in e.g. Boron having empty L_2 and L_3 subshells. I'm assuming this is a bug?

Return property instead of only value

All pyxray functions currently only return the requested value. For example pyxray.xray_transition_energy_eV(...) returns the energy of a transition. A keyword argument should be added to each function to return the actual property which would include the reference.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.