Skip to content

Commit

Permalink
Merge pull request #177 from lbl-anp/release-0.2.2
Browse files Browse the repository at this point in the history
Release 0.2.2
  • Loading branch information
markbandstra committed Feb 20, 2019
2 parents be603fa + 007841b commit aee527b
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 97 deletions.
46 changes: 42 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,51 @@ Contributions to `becquerel` are welcome and encouraged, whether it is
reporting bugs, requesting features, or contributing code.
Please follow these guidelines when contributing to this project.

### Reporting issues
### Developer Instructions

When reporting issues with `becquerel`, please provide a minimum working example to help identify the problem and tag the issue as a `bug`.
```
pip install -r requirements.txt
pip install -r requirements-dev.txt
python setup.py develop
```

### Feature requests
(It is more convenient to use `develop` so that the code is soft-linked
from the installation directory, and the installed package will always use
the current version of code.)

For a feature request, please create an issue and label it as a `new feature`.
### Running the tests

(Requires `requirements-dev.txt` to be installed)
To run the tests using `pytest`, from the root directory of the repo:

```
pytest
```

(`python setup.py test` is still supported also.)
By default, a code coverage report is printed to the terminal.
Tests marked `webtest` or `plottest` are by default skipped for the sake of
speed. To run all tests, clear the pre-configured markers option:

```
pytest -m ""
```

To produce an HTML code coverage report in directory `htmlcov`
with line-by-line highlighting:

```
pytest --cov-report html:htmlcov
```

### Code Style Guide

Use [google standards](https://google.github.io/styleguide/pyguide.html)

### Linter

* Use `flake8` in your IDE
* Use `pylint` from command line (as in style guide)

### Checklist for code contributions:
- [ ] Branch off of `develop` and name the branch `feature-XX` or `issue-XX`
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

*** Copyright Notice ***

Becquerel v. 0.2.1, Copyright (c) 2017, The Regents of the University of California (UC), through Lawrence Berkeley National Laboratory, and the UC Berkeley campus (subject to receipt of any required approvals from the U.S. Dept. of Energy). All rights reserved.
Becquerel v. 0.2.2, Copyright (c) 2017, The Regents of the University of California (UC), through Lawrence Berkeley National Laboratory, and the UC Berkeley campus (subject to receipt of any required approvals from the U.S. Dept. of Energy). All rights reserved.
If you have questions about your rights to use or distribute this software, please contact Berkeley Lab's Innovation & Partnerships Office at IPO@lbl.gov.

NOTICE. This Software was developed under funding from the U.S. Department of Energy and the U.S. Government consequently retains certain rights. As such, the U.S. Government has been granted for itself and others acting on its behalf a paid-up, nonexclusive, irrevocable, worldwide license in the Software to reproduce, distribute copies to the public, prepare derivative works, and perform publicly and display publicly, and to permit other to do so.
Expand Down
89 changes: 23 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,95 +11,52 @@ and pandas. It is intended to be general-purpose enough that it can be useful
to anyone from an undergraduate taking a laboratory course to the advanced
researcher.

## Installation instructions

### As a user

```
pip install -r requirements.txt
python setup.py install --user
```

Before reinstalling, be sure to either remove the ```build``` directory
or run:

```
python setup.py clean --all
```

### As a developer
## Installation

```
pip install -r requirements-dev.txt
python setup.py develop
pip install becquerel
```

(It is more convenient to use `develop` so that the code is soft-linked
from the installation directory, and the installed package will always use
the current version of code.)

## Running the tests
## Features in development (contributions welcome!)

(Requires `requirements-dev.txt` to be installed)
To run the tests using `pytest`, from the root directory of the repo:
* Reading additional `Spectrum` file types (N42, CHN, CSV)
* Writing `Spectrum` objects to various standard formats
* Fitting spectral features (e.g., gaussian lines with different background models)

```
pytest
```
If you are interested in contributing or are want to install the package from
source, please see the instructions in [`CONTRIBUTING.md`](./CONTRIBUTING.md).

(`python setup.py test` is still supported also.)
By default, a code coverage report is printed to the terminal.
Tests marked `webtest` or `plottest` are by default skipped for the sake of
speed. To run all tests, clear the pre-configured markers option:
## Reporting issues

```
pytest -m ""
```
When reporting issues with `becquerel`, please provide a minimum working example to help identify the problem and tag the issue as a `bug`.

To produce an HTML code coverage report in directory `htmlcov`
with line-by-line highlighting:
## Feature requests

```
pytest --cov-report html:htmlcov
```
For a feature request, please create an issue and label it as a `new feature`.

## Dependencies

External dependencies are listed in `requirements.txt` and can be installed
with `pip` (see [Installation instructions][0]) or manually. The dependencies
`beautifulsoup4`, `lxml` and `html5lib` are necessary for [`pandas`][1].
Developers additionally need [`pytest`][2] and are encouraged to use
[`pylint`][3], [`pycodestyle`][4], [`pydocstyle`][5] and [`yapf`][6] for
proper code formatting.
External dependencies are listed in `requirements.txt` and will be installed
automatically with the standard `pip` installation. They can also be installed
manually with the package manager of your choice (`pip`, `conda`, etc).
The dependencies `beautifulsoup4`, `lxml` and `html5lib` are necessary for
[`pandas`][1].

Developers require additional requirements which are listed in
`requirements-dev.txt`. We use [`pytest`][2] for unit testing and encourage
contributors to use [`pylint`][3], [`pycodestyle`][4], [`pydocstyle`][5] and
[`yapf`][6] for proper code formatting.

[0]: #installation-instructions
[1]: https://pandas.pydata.org/pandas-docs/stable/install.html#dependencies
[2]: https://docs.pytest.org
[3]: https://pylint.readthedocs.io
[4]: http://pycodestyle.pycqa.org
[5]: http://www.pydocstyle.org
[6]: https://github.com/google/yapf

## Code Style Guide

Use [google standards](https://google.github.io/styleguide/pyguide.html)

## Linter

* Use `flake8` in your IDE
* Use `pylint` from command line (as in style guide)

## Features in development (contributions welcome!)

* Reading additional `Spectrum` file types (N42, CHN, CSV)
* Writing `Spectrum` objects to various standard formats
* Fitting spectral features (e.g., gaussian lines with different background models)

If you are interested in contributing, please see the guidelines in [`CONTRIBUTING.md`](./CONTRIBUTING.md).

## Copyright Notice

Becquerel v. 0.2.1, Copyright (c) 2017, The Regents of the University of
Becquerel v. 0.2.2, Copyright (c) 2017, The Regents of the University of
California (UC), through Lawrence Berkeley National Laboratory, and the UC
Berkeley campus (subject to receipt of any required approvals from the U.S.
Dept. of Energy). All rights reserved. If you have questions about your rights
Expand Down
6 changes: 2 additions & 4 deletions becquerel/core/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,8 @@ def from_file(cls, infilename):
spect_file_obj = _get_file_object(infilename)

kwargs = {'counts': spect_file_obj.data,
'input_file_object': spect_file_obj}

if spect_file_obj.cal_coeff:
kwargs['bin_edges_kev'] = spect_file_obj.energy_bin_edges
'input_file_object': spect_file_obj,
'bin_edges_kev': spect_file_obj.bin_edges_kev}

# TODO Get more attributes from self.infileobj

Expand Down
3 changes: 2 additions & 1 deletion becquerel/parsers/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ Other data available include:

* `spec.livetime`
* `spec.realtime`
* `spec.bin_edges_kev`
* `spec.energy_bin_widths`

* `spec.energy_bin_edges` (deprecated)

Some parsers extract more information than others either because of their
stage of development or because the file format may not carry certain
Expand Down
2 changes: 2 additions & 0 deletions becquerel/parsers/cnf_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ class CnfFile(SpectrumFile):
spec.data [counts]
spec.channels
spec.energies
spec.bin_edges_kev
spec.energy_bin_widths
spec.energy_bin_edges (deprecated)
"""

Expand Down
2 changes: 2 additions & 0 deletions becquerel/parsers/spc_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class SpcFile(SpectrumFile):
spec.data [counts]
spec.channels
spec.energies
spec.bin_edges_kev
spec.energy_bin_widths
spec.energy_bin_edges (deprecated)
ORTEC's SPC file format is divided into records of 128 bytes each. The
specifications for what each record should contain can be found on pages
Expand Down
2 changes: 2 additions & 0 deletions becquerel/parsers/spe_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class SpeFile(SpectrumFile):
spec.data [counts]
spec.channels
spec.energies
spec.bin_edges_kev
spec.energy_bin_widths
spec.energy_bin_edges (deprecated)
ORTEC's SPE file format is given on page 73 of this document:
http://www.ortec-online.com/download/ortec-software-file-structure-manual.pdf
Expand Down
25 changes: 19 additions & 6 deletions becquerel/parsers/spectrum_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class SpectrumFile(object):
spec.data [counts]
spec.channels
spec.energies
spec.bin_edges_kev
spec.energy_bin_widths
spec.energy_bin_edges (deprecated)
"""

Expand All @@ -56,8 +58,7 @@ def __init__(self, filename):
self.cal_coeff = []
# arrays to be calculated using calibration
self.energies = np.array([], dtype=np.float)
self.energy_bin_widths = np.array([], dtype=np.float)
self.energy_bin_edges = np.array([], dtype=np.float)
self.bin_edges_kev = None

def __str__(self):
"""String form of the spectrum."""
Expand Down Expand Up @@ -100,6 +101,17 @@ def __str__(self):
s += ' [length {}]\n'.format(len(self.data))
return s

@property
def energy_bin_edges(self):
warnings.warn('The use of energy_bin_edges is deprecated, ' +
'use bin_edges_kev instead', DeprecationWarning)
return self.bin_edges_kev

@property
def energy_bin_widths(self):
"""Retrieve the calibrated width of all the bins."""
return self.bin_width(self.channels)

def read(self, verbose=False):
"""Read in the file."""
raise NotImplementedError('read method not implemented')
Expand All @@ -111,16 +123,17 @@ def write(self, filename):
def apply_calibration(self):
"""Calculate energies corresponding to channels."""
self.energies = self.channel_to_energy(self.channels)
self.energy_bin_widths = self.bin_width(self.channels)
n_edges = len(self.energies) + 1
channel_edges = np.linspace(-0.5, self.channels[-1] + 0.5, num=n_edges)
self.energy_bin_edges = self.channel_to_energy(channel_edges)
self.bin_edges_kev = self.channel_to_energy(channel_edges)

# check that calibration makes sense, remove calibration if not
if np.any(np.diff(self.energies) <= 0):
warnings.warn(
'Ignoring calibration; energies not monotonically increasing')
self.cal_coeff = []
'Spectrum will be initated without an energy calibration;' +
'invalid calibration, energies not monotonically increasing.',
SpectrumFileParsingWarning)
self.bin_edges_kev = None

def channel_to_energy(self, channel):
"""Apply energy calibration to the given channel(s)."""
Expand Down
34 changes: 29 additions & 5 deletions becquerel/tools/materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,20 @@ def fetch_element_data():
"""
req = _get_request(_URL_TABLE1)
# remove extra columns in Hydrogen row and extra empty rows
text = req.text
text = text.replace('<TD ROWSPAN="92">&nbsp;</TD>', '')
# rename first two header columns
text = text.replace(
'<TH scope="col" COLSPAN="2"><I>Z</I></TH>',
'<TH scope="col">Z</TH><TH scope="col">Symbol</TH>')
# remove row that makes the table too wide
text = text.replace('<TD COLSPAN="10"><HR SIZE="1" NOSHADE></TD>', '')
# remove extra header columns
text = text.replace('<TD COLSPAN="2">', '<TD>')
text = text.replace('<TD COLSPAN="4">', '<TD>')
text = text.replace('TD>&nbsp;</TD>', '')
# remove extra columns in Hydrogen row
text = text.replace('<TD ROWSPAN="92">&nbsp;</TD>', '')
# remove open <TR> at the end of the table
text = text.replace('</TD></TR><TR>', '</TD></TR>')
# read HTML table into pandas DataFrame
tables = pd.read_html(text, header=0, skiprows=[1, 2])
Expand All @@ -85,7 +95,11 @@ def fetch_element_data():
if len(df) != MAX_Z:
raise NISTMaterialsRequestError(
'{} elements expected, but found {}'.format(MAX_Z, len(df)))
# set column names
if len(df.columns) != 6:
raise NISTMaterialsRequestError(
'10 columns expected, but found {} ({})'.format(
len(df.columns), df.columns))
# rename columns
df.columns = ['Z', 'Symbol', 'Element', 'Z_over_A', 'I_eV', 'Density']
return df

Expand Down Expand Up @@ -145,10 +159,16 @@ def fetch_compound_data():
"""
req = _get_request(_URL_TABLE2)
# remove extra columns and replace <BR> symbols in composition lists
text = req.text
# remove extra header columns
text = text.replace('<TD ROWSPAN="2">&nbsp;</TD>', '')
# remove extra rows in header row
text = text.replace(' ROWSPAN="2"', '')
# remove extra columns in third header row
text = text.replace('<TD COLSPAN="9"><HR SIZE="1" NOSHADE></TD>', '')
# remove extra columns in the first material row
text = text.replace('<TD ROWSPAN="50"> &nbsp; </TD>', '')
# replace <BR> symbols in composition lists with semicolons
text = text.replace('<BR>', ';')
# read HTML table into pandas DataFrame
tables = pd.read_html(text, header=0, skiprows=[1, 2])
Expand All @@ -159,7 +179,11 @@ def fetch_compound_data():
if len(df) != N_COMPOUNDS:
raise NISTMaterialsRequestError(
'{} compounds expected, but found {}'.format(N_COMPOUNDS, len(df)))
# set column names
if len(df.columns) != 5:
raise NISTMaterialsRequestError(
'5 columns expected, but found {} ({})'.format(
len(df.columns), df.columns))
# rename columns
df.columns = ['Material', 'Z_over_A', 'I_eV', 'Density', 'Composition_Z']
# clean up Z composition
df['Composition_Z'] = [
Expand Down
19 changes: 10 additions & 9 deletions examples/overview.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

MAJOR = 0
MINOR = 2
MICRO = 1
MICRO = 2
VERSION = '{}.{}.{}'.format(MAJOR, MINOR, MICRO)

DESCRIPTION = __doc__.split('\n')[0].split(': ')[-1]
Expand Down

0 comments on commit aee527b

Please sign in to comment.