Skip to content

Commit

Permalink
Merge pull request #156 from pycroscopy/main
Browse files Browse the repository at this point in the history
Getting the recent changes
  • Loading branch information
saimani5 authored Oct 3, 2022
2 parents e065c1b + 60f9b58 commit 0c6fba4
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 51 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: build

env:
PYTHON_MAIN_VERSION: 3.8
PYTHON_MAIN_VERSION: 3.9

on:
pull_request:
Expand All @@ -19,7 +19,7 @@ jobs:
strategy:
max-parallel: 5
matrix:
python-version: [3.8, 3.7, 3.6, 3.9]
python-version: [3.8, 3.9, "3.10", 3.7, 3.6]

steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -69,15 +69,15 @@ jobs:
verbose: true

- name: Documentation build
if: ${{ matrix.python-version == env.PYTHON_MAIN_VERSION && github.ref == 'refs/heads/master'}}
if: ${{ matrix.python-version == env.PYTHON_MAIN_VERSION && github.ref == 'refs/heads/main'}}
run: |
pip install sphinx>=3.1.1 sphinx-gallery sphinx-rtd-theme>=0.5.0 sphinx-autodoc-typehints numpydoc wget pysptools cvxopt scipy nbsphinx
sudo apt-get install pandoc
sphinx-build -b html -aET docs/source docs/_build/html
touch docs/_build/html/.nojekyll
- name: Deploy to GitHub Pages
if: ${{ matrix.python-version == env.PYTHON_MAIN_VERSION && github.ref == 'refs/heads/master'}}
if: ${{ matrix.python-version == env.PYTHON_MAIN_VERSION && github.ref == 'refs/heads/main'}}
uses: crazy-max/ghaction-github-pages@v2
with:
target_branch: gh-pages
Expand Down
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ sidpy
.. image:: https://img.shields.io/pypi/v/sidpy.svg
:target: https://pypi.org/project/sidpy/
:alt: PyPI

.. image:: https://img.shields.io/conda/vn/conda-forge/sidpy.svg
:target: https://github.com/conda-forge/sidpy-feedstock
:alt: conda-forge

.. image:: https://codecov.io/gh/pycroscopy/sidpy/branch/master/graph/badge.svg?token=BCFR4FR6AL
:target: https://codecov.io/gh/pycroscopy/sidpy
Expand Down
4 changes: 2 additions & 2 deletions sidpy/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = '0.0.8'
time = '2022-01-07 17:08:00'
version = '0.10'
time = '2022-04-29 17:08:00'
45 changes: 37 additions & 8 deletions sidpy/proc/fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
@author: Rama Vasudevan, Mani Valleti
"""

from xml.dom import NotFoundErr
from dask.distributed import Client
import numpy as np
import dask
import inspect
from ..sid import Dimension, Dataset
from ..sid.dimension import DimensionType
from ..viz.dataset_viz import SpectralImageFitVisualizer
from ..sid.dataset import DataType

try:
from scipy.optimize import curve_fit
Expand Down Expand Up @@ -47,7 +50,7 @@ def __init__(self, sidpy_dataset, fit_fn, xvec=None, ind_dims=None, guess_fn=Non
If NOT provided, it is assumed that all the non-spectral dimensions are independent dimensions.
guess_fn: (function) (optional) This optional function should be utilized to generate priors for the full fit
It takes the same arguments as the fitting function and should return the same type of results array.
It takes (xvec,yvec) as inputs and should return the fit parameters.
If the guess_fn is NOT provided, then the user MUST input the num_fit_parms.
num_fit_parms: (int) Number of fitting parameters. This is needed IF the guess function is not provided to set
Expand Down Expand Up @@ -170,6 +173,7 @@ def __init__(self, sidpy_dataset, fit_fn, xvec=None, ind_dims=None, guess_fn=Non
self.return_std = return_std
self.return_cov = return_cov
self.return_fit = return_fit
self.fitted_dset = None

self.mean_fit_results = []
if self.return_cov:
Expand Down Expand Up @@ -251,7 +255,7 @@ def do_fit(self, **kwargs):
p0 = self.prior[ind, :]

lazy_result = dask.delayed(SidFitter.default_curve_fit)(self.fit_fn, self.dep_vec,
self.folded_dataset[ind, :],
self.folded_dataset[ind, :],self.num_fit_parms,
return_cov=(self.return_cov or self.return_std),
p0=p0, **kwargs)
fit_results.append(lazy_result)
Expand All @@ -263,7 +267,7 @@ def do_fit(self, **kwargs):
self.get_km_priors()
for ind in range(self.num_computations):
lazy_result = dask.delayed(SidFitter.default_curve_fit)(self.fit_fn, self.dep_vec,
self.folded_dataset[ind, :],
self.folded_dataset[ind, :], self.num_fit_parms,
return_cov=(self.return_cov or self.return_std),
p0=self.km_priors[self.km_labels[ind]],
**kwargs)
Expand Down Expand Up @@ -394,7 +398,7 @@ def get_fitted_dataset(self):
fitted_sid_dset_folded = fitted_dset_fold.like_data(np_folded_arr, title=fitted_dset_fold.title)
fitted_sid_dset = fitted_sid_dset_folded.unfold()
fitted_sid_dset.original_metadata = self.dataset.original_metadata.copy()

self.fitted_dset = fitted_sid_dset
return fitted_sid_dset

def get_km_priors(self):
Expand Down Expand Up @@ -422,23 +426,48 @@ def get_km_priors(self):
else:
p0 = np.random.normal(loc=0.5, scale=0.1, size=self.num_fit_parms)

km_priors.append(SidFitter.default_curve_fit(self.fit_fn, self.dep_vec, cen,
km_priors.append(SidFitter.default_curve_fit(self.fit_fn, self.dep_vec, cen, self.num_fit_parms,
return_cov=False,
p0=p0, maxfev=10000))
self.km_priors = np.array(km_priors)
self.num_fit_parms = self.km_priors.shape[-1]

def visualize_fit_results(self, figure = None, horizontal = True):
'''
Calls the interactive visualizer for comparing raw and fit datasets.
Inputs:
- figure: (Optional, default None) - handle to existing figure
- horiziontal: (Optional, default True) - whether spectrum should be plotted horizontally
'''
dset_type = self.dataset.data_type
supported_types = ['SPECTRAL_IMAGE']
if self.fitted_dset == None:
raise NotFoundErr("No fitted dataset found. Re-run with return_fit=True to use this feature")
if dset_type == DataType.SPECTRAL_IMAGE:
visualizer = SpectralImageFitVisualizer(self.dataset, self.fitted_dset,
figure=figure, horizontal=horizontal)
else:
raise NotImplementedError("Data type is {} but currently we only support types {}".format(dset_type, supported_types))

return visualizer


@staticmethod
def default_curve_fit(fit_fn, xvec, yvec, return_cov=True, **kwargs):
def default_curve_fit(fit_fn, xvec, yvec, num_fit_parms, return_cov=True, **kwargs):
xvec = np.array(xvec)
yvec = np.array(yvec)
yvec = yvec.ravel()
xvec = xvec.ravel()
if curve_fit is None:
raise ModuleNotFoundError("scipy is not installed")
else:
popt, pcov = curve_fit(fit_fn, xvec, yvec, **kwargs)

try:
popt, pcov = curve_fit(fit_fn, xvec, yvec, **kwargs)
except:
popt = np.zeros(num_fit_parms)
pcov = np.zeros((num_fit_parms, num_fit_parms))
if return_cov:
return popt, pcov
else:
Expand Down
3 changes: 2 additions & 1 deletion sidpy/sid/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class DimensionType(Enum):
RECIPROCAL = 2
SPECTRAL = 3
TEMPORAL = 4
CHANNEL = 5


class Dimension(np.ndarray):
Expand All @@ -53,7 +54,7 @@ def __new__(cls, values, name='none', quantity='generic', units='generic',
set of values will be generated if an integer is provided instead
of an array.
dimension_type : str or sidpy.sid.dimension.DimensionType
For example: 'spectral', 'spatial', 'reciprocal', or 'UNKNOWN'
For example: 'spectral', 'spatial', 'reciprocal', 'channel', or 'UNKNOWN',
'time', 'frame', 'reciprocal'
This will determine how the data are visualized. 'spatial' are
image dimensions. 'spectral' indicate spectroscopy data dimensions.
Expand Down
Loading

0 comments on commit 0c6fba4

Please sign in to comment.