diff --git a/README.md b/README.md index 3f791a7..daedcc1 100644 --- a/README.md +++ b/README.md @@ -51,12 +51,15 @@ pip install --user --upgrade -r requirements.txt # witzit scripts * `witzit-load` --- Load and text display data from SciAps or Olympus XRF. +* `witzit-plot` --- Plot a sample from a SciAps X-555 or Olympus Vanta-M. +* `witzit-plot2png` --- Plot a sample from a SciAps X-555 or Olympus Vanta-M + and save to PNG. Development is most easily done under Jupyter with Tensorboard for training models. These files are in the `notebooks/` directory. -* `witzit-plot-x555.ipynb` --- witzit Jupyter notebook, plotting application for SciAps X-555 XRF. -* `witzit-plot-vanta.ipynb` --- witzit Jupyter notebook, plotting application for Olympus Vanta XRF. +* `witzit-plot.ipynb` --- witzit Jupyter notebook, plotting application for + SciAps X-555 or Olympus Vanta-M. * `witzit-predict.ipynb` --- witzit Jupyter notebook, prediction application. * `witzit-train.ipynb` --- witzit Jupyter notebook, training application. diff --git a/witzit-plot b/witzit-plot new file mode 100755 index 0000000..7957345 --- /dev/null +++ b/witzit-plot @@ -0,0 +1,87 @@ +#!/usr/bin/python3 +# +# witzit-plot +# +# Copyright (C) 2022, Jeff Moe +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# witzit-plot +# Plot a sample from a SciAps X-555 or Olympus Vanta-M. +# +# Usage: +# witzit-plot [data filename] +# +# Examples: +# witzit-plot examples/olympus-vanta.csv +# witzit-plot examples/sciaps-x555.mca + +import numpy as np +import pandas as pd +import seaborn as sns +import matplotlib.pyplot as plt +import locale +import sys +import re + +locale.setlocale(locale.LC_ALL, "en_US.utf8") +plt.rcParams['axes.formatter.use_locale'] = True + +# Determine what type of file to load to dataframe +datafile=(sys.argv[1]) + +with open(datafile) as f: + firstline = f.readline().rstrip() + +if firstline == 'File Version = 2': + print('SciAps X-555 XRF MCA') + template = pd.read_csv('template/sciaps-x555-ev.csv', header=0, + skiprows=0, + usecols = [0]) + mca = pd.read_csv(datafile, skiprows=21, header=0, + usecols = [0]) + df=template.join(mca) + df.rename(columns = {'2048':'Counts'}, inplace = True) + +elif re.match(re.compile('Date,*'), firstline): + print('Olympus Vanta-M XRF') + df = pd.read_csv(datafile, header=0, + skiprows=39, + names=('energy (eV)', 'Counts'), + usecols = [0, 1]) + df['energy (eV)'] = df['energy (eV)'] * 1000 + +elif re.match(re.compile('"Date",*'), firstline): + print('Possibly SciAps X-555 XRF CSV, not processed. Use .mca file instead of .csv.') + exit() + +else: + print('Unknown file type.') + exit() + +# Plot +sns.set_theme() +plt.figure(facecolor='xkcd:off white', figsize=(15, 8)) +g=sns.lineplot(x="energy (eV)", y="Counts", data=df, linestyle='-', linewidth=1, color='xkcd:pale orange') +g.set_title('XRF Spectrum'.format('seaborn'), color='xkcd:cornflower blue', fontsize='large', fontweight='bold') +g.set_xlabel('energy (eV)', color='xkcd:goldenrod', fontsize='large', fontweight='bold') +g.set_ylabel('Counts', color='xkcd:cerulean', fontsize='large', fontweight='bold') +g.patch.set_alpha(1.0) +g.set_facecolor('xkcd:powder blue') +plt.xlim(0,50000) +plt.ylim(0,100000) +plt.tight_layout() +plt.show() +plt.close() + diff --git a/witzit-plot2png b/witzit-plot2png new file mode 100755 index 0000000..abd73fa --- /dev/null +++ b/witzit-plot2png @@ -0,0 +1,88 @@ +#!/usr/bin/python3 +# +# witzit-plot2png +# +# Copyright (C) 2022, Jeff Moe +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# witzit-plot2png +# Plot a sample from a SciAps LIBS or XRF and save to PNG. +# +# Usage: +# witzit-plot2png [data filename] [PNG output filename] +# +# Examples: +# witzit-plot2png examples/olympus-vanta.csv tmp/spectrum.png +# witzit-plot2png examples/sciaps-x555.mca tmp/x555.png + +import numpy as np +import pandas as pd +import seaborn as sns +import matplotlib.pyplot as plt +import locale +import sys +import re + +locale.setlocale(locale.LC_ALL, "en_US.utf8") +plt.rcParams['axes.formatter.use_locale'] = True + +# Determine what type of file to load to dataframe +datafile=(sys.argv[1]) + +with open(datafile) as f: + firstline = f.readline().rstrip() + +if firstline == 'File Version = 2': + print('SciAps X-555 XRF MCA') + template = pd.read_csv('template/sciaps-x555-ev.csv', header=0, + skiprows=0, + usecols = [0]) + mca = pd.read_csv(datafile, skiprows=21, header=0, + usecols = [0]) + df=template.join(mca) + df.rename(columns = {'2048':'Counts'}, inplace = True) + +elif re.match(re.compile('Date,*'), firstline): + print('Olympus Vanta-M XRF') + df = pd.read_csv(datafile, header=0, + skiprows=39, + names=('energy (eV)', 'Counts'), + usecols = [0, 1]) + df['energy (eV)'] = df['energy (eV)'] * 1000 + +elif re.match(re.compile('"Date",*'), firstline): + print('Possibly SciAps X-555 XRF CSV, not processed. Use .mca file instead of .csv.') + exit() + +else: + print('Unknown file type.') + exit() + +# Plot +sns.set_theme() +plt.figure(facecolor='xkcd:off white', figsize=(15, 8)) +g=sns.lineplot(x="energy (eV)", y="Counts", data=df, linestyle='-', linewidth=1, color='xkcd:pale orange') +g.set_title('XRF Spectrum'.format('seaborn'), color='xkcd:cornflower blue', fontsize='large', fontweight='bold') +g.set_xlabel('energy (eV)', color='xkcd:goldenrod', fontsize='large', fontweight='bold') +g.set_ylabel('Counts', color='xkcd:cerulean', fontsize='large', fontweight='bold') +g.patch.set_alpha(1.0) +g.set_facecolor('xkcd:powder blue') +plt.xlim(0,50000) +plt.ylim(0,100000) +plt.tight_layout() +plt.savefig(sys.argv[2], dpi=72, transparent=False, facecolor='xkcd:off white', edgecolor=g.get_facecolor()) +plt.show() +plt.close() +