pyphi-mvda 6.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyphi_mvda-6.0/LICENSE +21 -0
- pyphi_mvda-6.0/MANIFEST.in +5 -0
- pyphi_mvda-6.0/PKG-INFO +78 -0
- pyphi_mvda-6.0/README.md +45 -0
- pyphi_mvda-6.0/docs/Makefile +20 -0
- pyphi_mvda-6.0/docs/make.bat +35 -0
- pyphi_mvda-6.0/docs/source/conf.py +24 -0
- pyphi_mvda-6.0/docs/source/index.rst +10 -0
- pyphi_mvda-6.0/docs/source/pyphi.batch.rst +7 -0
- pyphi_mvda-6.0/docs/source/pyphi.calc.rst +7 -0
- pyphi_mvda-6.0/docs/source/pyphi.plots.rst +7 -0
- PLS/Automobiles PCA w MD.xlsx +0 -0
- PLS/Automobiles PLS.xlsx +0 -0
- pyphi_mvda-6.0/examples/Basic calculations PCA and PLS/Example_Script.py +69 -0
- pyphi_mvda-6.0/examples/Basic calculations PCA and PLS/Example_Script_testing_MD_by_NLP.py +45 -0
- pyphi_mvda-6.0/examples/Basic calculations PCA and PLS/plotting_scores_for_newx.py +33 -0
- pyphi_mvda-6.0/examples/Batch analysis/Batch Dryer Case Study.xlsx +0 -0
- pyphi_mvda-6.0/examples/Batch analysis/Batch Film Coating.xlsx +0 -0
- pyphi_mvda-6.0/examples/Batch analysis/basic_batch_analysis.py +206 -0
- pyphi_mvda-6.0/examples/Batch analysis/batch_dryer_case_study.py +78 -0
- TPLS/jrpls_tpls_dataset.xlsx +0 -0
- pyphi_mvda-6.0/examples/JRPLS and TPLS/jrpls_tpls_example.py +117 -0
- pyphi_mvda-6.0/examples/LPLS/lpls_dataset.xlsx +0 -0
- pyphi_mvda-6.0/examples/LPLS/lpls_example.py +27 -0
- pyphi_mvda-6.0/examples/LWPLS/NIRdata_tablets.MAT +0 -0
- pyphi_mvda-6.0/examples/LWPLS/lwpls_example.py +109 -0
- pyphi_mvda-6.0/examples/Misc/build_polynomial_regression.py +47 -0
- pyphi_mvda-6.0/examples/Misc/data.xlsx +0 -0
- pyphi_mvda-6.0/examples/Multi-block PLS/MBDataset.xlsx +0 -0
- pyphi_mvda-6.0/examples/Multi-block PLS/mbpls_example.py +43 -0
- pyphi_mvda-6.0/examples/NIR Calibration/NIR.xlsx +0 -0
- pyphi_mvda-6.0/examples/NIR Calibration/NIR_Calibration.py +56 -0
- pyphi_mvda-6.0/examples/Varimax Rotation/chem_exp_example.py +63 -0
- pyphi_mvda-6.0/examples/Varimax Rotation/chemical_experiments_dataset.xlsx +0 -0
- pyphi_mvda-6.0/examples/plscca vs opls/OPLS Test Data.xlsx +0 -0
- loadings.xlsx +0 -0
- pyphi_mvda-6.0/examples/plscca vs opls/plscca _example.py +45 -0
- pyphi_mvda-6.0/pyphi/__init__.py +5 -0
- pyphi_mvda-6.0/pyphi/batch.py +2537 -0
- pyphi_mvda-6.0/pyphi/calc.py +4626 -0
- pyphi_mvda-6.0/pyphi/plots.py +2059 -0
- pyphi_mvda-6.0/pyphi_mvda.egg-info/PKG-INFO +78 -0
- pyphi_mvda-6.0/pyphi_mvda.egg-info/SOURCES.txt +47 -0
- pyphi_mvda-6.0/pyphi_mvda.egg-info/dependency_links.txt +1 -0
- pyphi_mvda-6.0/pyphi_mvda.egg-info/requires.txt +8 -0
- pyphi_mvda-6.0/pyphi_mvda.egg-info/top_level.txt +1 -0
- pyphi_mvda-6.0/requirements.txt +8 -0
- pyphi_mvda-6.0/setup.cfg +4 -0
- pyphi_mvda-6.0/setup.py +33 -0
pyphi_mvda-6.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Salvador Garcia-Munoz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
pyphi_mvda-6.0/PKG-INFO
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyphi-mvda
|
|
3
|
+
Version: 6.0
|
|
4
|
+
Summary: A Python toolbox for multivariate analysis using PCA and PLS methods
|
|
5
|
+
Home-page: https://github.com/salvadorgarciamunoz/pyphi
|
|
6
|
+
Author: Sal Garcia
|
|
7
|
+
Author-email: sgarciam@ic.ac.uk
|
|
8
|
+
License: MIT
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.7
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: bokeh
|
|
15
|
+
Requires-Dist: matplotlib
|
|
16
|
+
Requires-Dist: numpy
|
|
17
|
+
Requires-Dist: openpyxl
|
|
18
|
+
Requires-Dist: pandas
|
|
19
|
+
Requires-Dist: pyomo
|
|
20
|
+
Requires-Dist: scipy
|
|
21
|
+
Requires-Dist: statsmodels
|
|
22
|
+
Dynamic: author
|
|
23
|
+
Dynamic: author-email
|
|
24
|
+
Dynamic: classifier
|
|
25
|
+
Dynamic: description
|
|
26
|
+
Dynamic: description-content-type
|
|
27
|
+
Dynamic: home-page
|
|
28
|
+
Dynamic: license
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
Dynamic: requires-dist
|
|
31
|
+
Dynamic: requires-python
|
|
32
|
+
Dynamic: summary
|
|
33
|
+
|
|
34
|
+
# Modules
|
|
35
|
+
## pyphi Release 6.0
|
|
36
|
+
Phi toolbox for multivariate analysis by Sal Garcia (salvadorgarciamunoz@gmail.com, sgarciam@ic.ac.uk)
|
|
37
|
+
|
|
38
|
+
Documentation: https://salvadorgarciamunoz.github.io/pyphi/index.html
|
|
39
|
+
|
|
40
|
+
## pyphi_plots
|
|
41
|
+
A variety of plotting tools for models created with pyphi.
|
|
42
|
+
|
|
43
|
+
## pyphi_batch
|
|
44
|
+
Batch analysis toolbox to perform batch alightment and Multi-way models for batch data
|
|
45
|
+
|
|
46
|
+
# Getting Started
|
|
47
|
+
Pyphi requires the python packages listed in the ```requirements.txt``` file.
|
|
48
|
+
|
|
49
|
+
## Installation
|
|
50
|
+
1) Ensure you have Python 3 installed and accessible via your terminal ("python" command).
|
|
51
|
+
2) Download this repository via ```git clone``` or manually using the download zip button at the top of the page.
|
|
52
|
+
3) Install the pyphi, pyphi_plots and pyphi_batch modules by opening a terminal window, navigating to the root of this repository, and typing
|
|
53
|
+
```pip install -r requirements.txt```.
|
|
54
|
+
|
|
55
|
+
To confirm you have a working installation, navigate to the ```Examples``` folder and copy the ```Example_Script.py``` to the directory of your choice. Run ```python Example_Script.py```, verifying there are no errors logged to the console.
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
## Optional External Dependencies
|
|
59
|
+
- IPOPT as an executable in your system path or GAMS python module or GAMS executable in yoru system path.
|
|
60
|
+
- Windows: ```conda install -c conda-forge IPOPT=3.11.1``` or download from [IPOPT releases page](https://github.com/coin-or/Ipopt/releases), extract and add the IPOPT\bin folder to your system path or add all files to your working directory.
|
|
61
|
+
- Mac/Linux: ```conda install -c conda-forge IPOPT```, download from [IPOPT releases page](https://github.com/coin-or/Ipopt/releases), or [Compile using coinbrew](https://coin-or.github.io/Ipopt/INSTALL.html#COINBREW).
|
|
62
|
+
|
|
63
|
+
- if GAMS is installed, pyphi will run ipopt via GAMS, make sure the GAMS executables are reachable through the system PATH
|
|
64
|
+
|
|
65
|
+
- If IPOPT is not detected, pyphi will submit the pyomo models to the NEOS server to solve them remotely.
|
|
66
|
+
- To use the NEOS server, the environment variable "NEOS_EMAIL" must be assigned a valid email. This can be done outside of python using set/set/export or use ```import os
|
|
67
|
+
os.environ["NEOS_EMAIL"] = youremail@domain.com```
|
|
68
|
+
in your code.
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
Run the script '''Example_Script_testing_MD_by_NLP.py''' to verify that pyphi can execute IPOPT
|
|
72
|
+
|
|
73
|
+
Adding a folder to your system path:
|
|
74
|
+
- Windows: temporary ```set PATH=C:\Path\To\ipopt\bin;%PATH%``` or persistent ```setx PATH=C:\Path\To\ipopt\bin;%PATH%```.
|
|
75
|
+
- Mac/Linux: ```export PATH=/path/to/ipopt:$PATH```, add to .profile/.*rc file to make persistent.
|
|
76
|
+
- Both via Conda: after activating your environment, use ```conda env config vars set``` and your OS-specific set or export command.
|
|
77
|
+
|
|
78
|
+
This is Release 4.0 the lastest added in terms of computation is the calculation of the Covariant scores and loadings (equivalent to what you get with OPLS) with the '''cca''' flag in the PLS routing
|
pyphi_mvda-6.0/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Modules
|
|
2
|
+
## pyphi Release 6.0
|
|
3
|
+
Phi toolbox for multivariate analysis by Sal Garcia (salvadorgarciamunoz@gmail.com, sgarciam@ic.ac.uk)
|
|
4
|
+
|
|
5
|
+
Documentation: https://salvadorgarciamunoz.github.io/pyphi/index.html
|
|
6
|
+
|
|
7
|
+
## pyphi_plots
|
|
8
|
+
A variety of plotting tools for models created with pyphi.
|
|
9
|
+
|
|
10
|
+
## pyphi_batch
|
|
11
|
+
Batch analysis toolbox to perform batch alightment and Multi-way models for batch data
|
|
12
|
+
|
|
13
|
+
# Getting Started
|
|
14
|
+
Pyphi requires the python packages listed in the ```requirements.txt``` file.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
1) Ensure you have Python 3 installed and accessible via your terminal ("python" command).
|
|
18
|
+
2) Download this repository via ```git clone``` or manually using the download zip button at the top of the page.
|
|
19
|
+
3) Install the pyphi, pyphi_plots and pyphi_batch modules by opening a terminal window, navigating to the root of this repository, and typing
|
|
20
|
+
```pip install -r requirements.txt```.
|
|
21
|
+
|
|
22
|
+
To confirm you have a working installation, navigate to the ```Examples``` folder and copy the ```Example_Script.py``` to the directory of your choice. Run ```python Example_Script.py```, verifying there are no errors logged to the console.
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
## Optional External Dependencies
|
|
26
|
+
- IPOPT as an executable in your system path or GAMS python module or GAMS executable in yoru system path.
|
|
27
|
+
- Windows: ```conda install -c conda-forge IPOPT=3.11.1``` or download from [IPOPT releases page](https://github.com/coin-or/Ipopt/releases), extract and add the IPOPT\bin folder to your system path or add all files to your working directory.
|
|
28
|
+
- Mac/Linux: ```conda install -c conda-forge IPOPT```, download from [IPOPT releases page](https://github.com/coin-or/Ipopt/releases), or [Compile using coinbrew](https://coin-or.github.io/Ipopt/INSTALL.html#COINBREW).
|
|
29
|
+
|
|
30
|
+
- if GAMS is installed, pyphi will run ipopt via GAMS, make sure the GAMS executables are reachable through the system PATH
|
|
31
|
+
|
|
32
|
+
- If IPOPT is not detected, pyphi will submit the pyomo models to the NEOS server to solve them remotely.
|
|
33
|
+
- To use the NEOS server, the environment variable "NEOS_EMAIL" must be assigned a valid email. This can be done outside of python using set/set/export or use ```import os
|
|
34
|
+
os.environ["NEOS_EMAIL"] = youremail@domain.com```
|
|
35
|
+
in your code.
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
Run the script '''Example_Script_testing_MD_by_NLP.py''' to verify that pyphi can execute IPOPT
|
|
39
|
+
|
|
40
|
+
Adding a folder to your system path:
|
|
41
|
+
- Windows: temporary ```set PATH=C:\Path\To\ipopt\bin;%PATH%``` or persistent ```setx PATH=C:\Path\To\ipopt\bin;%PATH%```.
|
|
42
|
+
- Mac/Linux: ```export PATH=/path/to/ipopt:$PATH```, add to .profile/.*rc file to make persistent.
|
|
43
|
+
- Both via Conda: after activating your environment, use ```conda env config vars set``` and your OS-specific set or export command.
|
|
44
|
+
|
|
45
|
+
This is Release 4.0 the lastest added in terms of computation is the calculation of the Covariant scores and loadings (equivalent to what you get with OPLS) with the '''cca''' flag in the PLS routing
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Minimal makefile for Sphinx documentation
|
|
2
|
+
#
|
|
3
|
+
|
|
4
|
+
# You can set these variables from the command line, and also
|
|
5
|
+
# from the environment for the first two.
|
|
6
|
+
SPHINXOPTS ?=
|
|
7
|
+
SPHINXBUILD ?= sphinx-build
|
|
8
|
+
SOURCEDIR = source
|
|
9
|
+
BUILDDIR = build
|
|
10
|
+
|
|
11
|
+
# Put it first so that "make" without argument is like "make help".
|
|
12
|
+
help:
|
|
13
|
+
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
|
14
|
+
|
|
15
|
+
.PHONY: help Makefile
|
|
16
|
+
|
|
17
|
+
# Catch-all target: route all unknown targets to Sphinx using the new
|
|
18
|
+
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
|
|
19
|
+
%: Makefile
|
|
20
|
+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
@ECHO OFF
|
|
2
|
+
|
|
3
|
+
pushd %~dp0
|
|
4
|
+
|
|
5
|
+
REM Command file for Sphinx documentation
|
|
6
|
+
|
|
7
|
+
if "%SPHINXBUILD%" == "" (
|
|
8
|
+
set SPHINXBUILD=sphinx-build
|
|
9
|
+
)
|
|
10
|
+
set SOURCEDIR=source
|
|
11
|
+
set BUILDDIR=build
|
|
12
|
+
|
|
13
|
+
%SPHINXBUILD% >NUL 2>NUL
|
|
14
|
+
if errorlevel 9009 (
|
|
15
|
+
echo.
|
|
16
|
+
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
|
|
17
|
+
echo.installed, then set the SPHINXBUILD environment variable to point
|
|
18
|
+
echo.to the full path of the 'sphinx-build' executable. Alternatively you
|
|
19
|
+
echo.may add the Sphinx directory to PATH.
|
|
20
|
+
echo.
|
|
21
|
+
echo.If you don't have Sphinx installed, grab it from
|
|
22
|
+
echo.https://www.sphinx-doc.org/
|
|
23
|
+
exit /b 1
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
if "%1" == "" goto help
|
|
27
|
+
|
|
28
|
+
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
|
|
29
|
+
goto end
|
|
30
|
+
|
|
31
|
+
:help
|
|
32
|
+
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
|
|
33
|
+
|
|
34
|
+
:end
|
|
35
|
+
popd
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import os, sys
|
|
2
|
+
sys.path.insert(0, os.path.abspath('../..'))
|
|
3
|
+
|
|
4
|
+
project = 'pyphi'
|
|
5
|
+
copyright = '2026, Salvador Garcia Munoz'
|
|
6
|
+
author = 'Salvador Garcia Munoz'
|
|
7
|
+
release = '6.0'
|
|
8
|
+
|
|
9
|
+
extensions = [
|
|
10
|
+
'sphinx.ext.autodoc',
|
|
11
|
+
'sphinx.ext.napoleon',
|
|
12
|
+
'sphinx.ext.viewcode',
|
|
13
|
+
'sphinx.ext.autosummary',
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
napoleon_google_docstring = True
|
|
17
|
+
napoleon_numpy_docstring = False
|
|
18
|
+
autodoc_member_order = 'bysource'
|
|
19
|
+
|
|
20
|
+
templates_path = ['_templates']
|
|
21
|
+
exclude_patterns = []
|
|
22
|
+
|
|
23
|
+
html_theme = 'alabaster'
|
|
24
|
+
html_static_path = ['_static']
|
|
Binary file
|
PLS/Automobiles PLS.xlsx
ADDED
|
Binary file
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
Example script to build a PCA and a PLS model
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import pandas as pd
|
|
8
|
+
import numpy as np
|
|
9
|
+
import pyphi.calc as phi
|
|
10
|
+
import pyphi.plots as pp
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# Load the data from Excel
|
|
14
|
+
Cars_Features = pd.read_excel('Automobiles PLS.xlsx', 'Features', index_col=None, na_values=np.nan,engine='openpyxl')
|
|
15
|
+
Cars_Performance = pd.read_excel('Automobiles PLS.xlsx', 'Performance', index_col=None, na_values=np.nan,engine='openpyxl')
|
|
16
|
+
Cars_CLASSID = pd.read_excel('Automobiles PLS.xlsx', 'CLASSID', index_col=None, na_values=np.nan,engine='openpyxl')
|
|
17
|
+
|
|
18
|
+
# Build a PCA model with 3 PC's, cross validating by elements removing 5% of the data per round
|
|
19
|
+
pcaobj=phi.pca(Cars_Features,3,cross_val=5)
|
|
20
|
+
|
|
21
|
+
#Make some plots
|
|
22
|
+
# Captured variance per component per variable
|
|
23
|
+
pp.r2pv(pcaobj)
|
|
24
|
+
#t1 Line plot color coded by Car Origin
|
|
25
|
+
pp.score_line(pcaobj,1,CLASSID=Cars_CLASSID,colorby='Origin',add_ci=True,add_labels=True)
|
|
26
|
+
#Loadings map for 1 and 2nd PC
|
|
27
|
+
pp.loadings_map(pcaobj,[1,2])
|
|
28
|
+
#t1 vs t2 color coded by car origin
|
|
29
|
+
pp.score_scatter(pcaobj,[1,2],CLASSID=Cars_CLASSID,colorby='Origin')
|
|
30
|
+
#t1 vs t2 color coded by cylinder (where Cylinder is a discrete class)
|
|
31
|
+
pp.score_scatter(pcaobj,[1,2],CLASSID=Cars_CLASSID,colorby='Cylinders')
|
|
32
|
+
#t1 vs t2 color coded by Car Wegith using 4 bins to group
|
|
33
|
+
pp.score_scatter(pcaobj,[1,2],CLASSID=Cars_Features,colorby='Horsepower',nbins=4,
|
|
34
|
+
addtitle='Color coded by Car Weight')
|
|
35
|
+
|
|
36
|
+
#Bar plots of loadings
|
|
37
|
+
pp.loadings(pcaobj)
|
|
38
|
+
#Bar plots of loadings weighted by the R2 per variable per component
|
|
39
|
+
pp.weighted_loadings(pcaobj)
|
|
40
|
+
#Hotellings T2 and SPE and Outlier plot, adding a t1-t2 plot at the end
|
|
41
|
+
pp.diagnostics(pcaobj, score_plot_xydim=[1,2])
|
|
42
|
+
#Plot contributions from mean to Car1
|
|
43
|
+
pp.contributions_plot(pcaobj,Cars_Features,'scores',to_obs=['Car1'])
|
|
44
|
+
#Plot contributions from Car1 to Car4
|
|
45
|
+
pp.contributions_plot(pcaobj,Cars_Features,'scores',to_obs=['Car1'],from_obs=['Car4'])
|
|
46
|
+
|
|
47
|
+
# Build a PLS model with 3 PC's, cross validating by elements
|
|
48
|
+
# removing 5% of the data per round
|
|
49
|
+
plsobj=phi.pls(Cars_Features,Cars_Performance,3,cross_val=5)
|
|
50
|
+
# Build a PLS model with 3 PC's, cross validating by elements
|
|
51
|
+
# removing 5% of the data per round adding crossval of X Space
|
|
52
|
+
plsobj=phi.pls(Cars_Features,Cars_Performance,3,cross_val=5,cross_val_X=True)
|
|
53
|
+
|
|
54
|
+
#Make some plots
|
|
55
|
+
pp.r2pv(plsobj)
|
|
56
|
+
pp.score_scatter(plsobj,[1,2],CLASSID=Cars_CLASSID,colorby='Cylinders')
|
|
57
|
+
pp.score_scatter(plsobj,[1,2],CLASSID=Cars_CLASSID,colorby='Origin',add_ci=True)
|
|
58
|
+
pp.loadings(plsobj)
|
|
59
|
+
pp.weighted_loadings(plsobj)
|
|
60
|
+
pp.diagnostics(plsobj, score_plot_xydim=[1,2])
|
|
61
|
+
pp.contributions_plot(plsobj,Cars_Features,'scores',to_obs=['Car1'])
|
|
62
|
+
pp.contributions_plot(plsobj,Cars_Features,'scores',to_obs=['Car1'],from_obs=['Car4'])
|
|
63
|
+
pp.vip(plsobj)
|
|
64
|
+
pp.loadings_map(plsobj,[1,2])
|
|
65
|
+
pp.predvsobs(plsobj,Cars_Features,Cars_Performance)
|
|
66
|
+
pp.predvsobs(plsobj,Cars_Features,Cars_Performance,CLASSID=Cars_CLASSID,colorby='Origin')
|
|
67
|
+
pp.predvsobs(plsobj,Cars_Features,Cars_Performance,CLASSID=Cars_CLASSID,colorby='Origin',x_space=True)
|
|
68
|
+
|
|
69
|
+
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
Example script to build a PCA and a PLS model testgin the NLP algortithms
|
|
5
|
+
to handle missing data
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import pandas as pd
|
|
9
|
+
import numpy as np
|
|
10
|
+
import pyphi.calc as phi
|
|
11
|
+
import pyphi.plots as pp
|
|
12
|
+
|
|
13
|
+
# Load the data from Excel
|
|
14
|
+
Cars_Features_MD = pd.read_excel('Automobiles PCA w MD.xlsx', 'Features', index_col=None, na_values=np.nan,engine='openpyxl')
|
|
15
|
+
Cars_Performance = pd.read_excel('Automobiles PLS.xlsx', 'Performance', index_col=None, na_values=np.nan,engine='openpyxl')
|
|
16
|
+
Cars_Features = pd.read_excel('Automobiles PLS.xlsx', 'Features', index_col=None, na_values=np.nan,engine='openpyxl')
|
|
17
|
+
Cars_CLASSID = pd.read_excel('Automobiles PCA w MD.xlsx', 'CLASSID', index_col=None, na_values=np.nan,engine='openpyxl')
|
|
18
|
+
|
|
19
|
+
# Build a PCA model with 3 PC's using Non-Linear Programing algorithm to handle the missing data
|
|
20
|
+
pcaobj1=phi.pca(Cars_Features_MD,3,md_algorithm='nlp')
|
|
21
|
+
print(pcaobj1['P'])
|
|
22
|
+
pp.score_scatter(pcaobj1,[1,2],CLASSID=Cars_CLASSID,colorby='Cylinders')
|
|
23
|
+
pp.loadings(pcaobj1)
|
|
24
|
+
|
|
25
|
+
pcaobj2=phi.pca(Cars_Features_MD,3)
|
|
26
|
+
print(pcaobj2['P'])
|
|
27
|
+
pp.score_scatter(pcaobj2,[1,2],CLASSID=Cars_CLASSID,colorby='Cylinders')
|
|
28
|
+
pp.loadings(pcaobj2)
|
|
29
|
+
|
|
30
|
+
pcaobj3=phi.pca(Cars_Features,3)
|
|
31
|
+
print(pcaobj3['P'])
|
|
32
|
+
pp.score_scatter(pcaobj3,[1,2],CLASSID=Cars_CLASSID,colorby='Cylinders')
|
|
33
|
+
pp.loadings(pcaobj3)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
plsobj=phi.pls(Cars_Features,Cars_Performance,3)
|
|
37
|
+
pp.r2pv(plsobj)
|
|
38
|
+
pp.score_scatter(plsobj,[1,2],CLASSID=Cars_CLASSID,colorby='Origin',add_ci=True)
|
|
39
|
+
pp.loadings(plsobj)
|
|
40
|
+
|
|
41
|
+
plsobj_nlp=phi.pls(Cars_Features_MD,Cars_Performance,3,md_algorithm='nlp')
|
|
42
|
+
pp.r2pv(plsobj_nlp)
|
|
43
|
+
pp.score_scatter(plsobj_nlp,[1,2],CLASSID=Cars_CLASSID,colorby='Origin',add_ci=True)
|
|
44
|
+
pp.loadings(plsobj_nlp)
|
|
45
|
+
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
Created on Thu Jan 18 08:37:36 2024
|
|
4
|
+
Example on how to plot scores for observations not in the model
|
|
5
|
+
@author: salva
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import pandas as pd
|
|
9
|
+
import numpy as np
|
|
10
|
+
import pyphi.calc as phi
|
|
11
|
+
import pyphi.plots as pp
|
|
12
|
+
|
|
13
|
+
# Load the data from Excel
|
|
14
|
+
Cars_Features_MD = pd.read_excel('Automobiles PCA w MD.xlsx', 'Features', index_col=None, na_values=np.nan,engine='openpyxl')
|
|
15
|
+
Cars_Performance = pd.read_excel('Automobiles PLS.xlsx', 'Performance', index_col=None, na_values=np.nan,engine='openpyxl')
|
|
16
|
+
Cars_Features = pd.read_excel('Automobiles PLS.xlsx', 'Features', index_col=None, na_values=np.nan,engine='openpyxl')
|
|
17
|
+
Cars_CLASSID = pd.read_excel('Automobiles PCA w MD.xlsx', 'CLASSID', index_col=None, na_values=np.nan,engine='openpyxl')
|
|
18
|
+
|
|
19
|
+
Cars_Features_new = Cars_Features.iloc[:100,:]
|
|
20
|
+
Cars_Features = Cars_Features.iloc[100:,:]
|
|
21
|
+
Cars_CLASSID_new = Cars_CLASSID.iloc[:100,:]
|
|
22
|
+
Cars_CLASSID = Cars_CLASSID.iloc[100:,:]
|
|
23
|
+
#Cars_CLASSID.reset_index(inplace=True,drop=True)
|
|
24
|
+
# Build a PCA model with 3 PC's,
|
|
25
|
+
pcaobj=phi.pca(Cars_Features,3)
|
|
26
|
+
|
|
27
|
+
pp.score_scatter(pcaobj,[1,2],CLASSID=Cars_CLASSID,colorby='Cylinders')
|
|
28
|
+
pp.score_scatter(pcaobj,[1,2],Xnew=Cars_Features_new, CLASSID=Cars_CLASSID_new,colorby='Cylinders',addtitle='For New Observations')
|
|
29
|
+
pp.score_scatter(pcaobj,[1,2],Xnew=Cars_Features_new, include_model=True,addtitle='For New Observations w/ Model')
|
|
30
|
+
|
|
31
|
+
pp.score_scatter(pcaobj,[1,2],Xnew=Cars_Features_new, CLASSID=Cars_CLASSID_new,
|
|
32
|
+
colorby='Cylinders',include_model=True,
|
|
33
|
+
addtitle='For New Observations w Classifiers and Model')
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
Created on Mon Apr 11 20:53:21 2022
|
|
4
|
+
|
|
5
|
+
@author: salva
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import pandas as pd
|
|
9
|
+
import numpy as np
|
|
10
|
+
import pyphi.batch as phibatch
|
|
11
|
+
import pyphi.plots as pp
|
|
12
|
+
import matplotlib.pyplot as plt
|
|
13
|
+
|
|
14
|
+
bdata=pd.read_excel('Batch Film Coating.xlsx')
|
|
15
|
+
|
|
16
|
+
#Plot variables for all batches
|
|
17
|
+
#plot all variables
|
|
18
|
+
phibatch.plot_var_all_batches(bdata)
|
|
19
|
+
#plot a variable
|
|
20
|
+
phibatch.plot_var_all_batches(bdata,which_var='INLET_AIR_TEMP')
|
|
21
|
+
#plot some variables
|
|
22
|
+
phibatch.plot_var_all_batches(bdata,which_var=['INLET_AIR_TEMP','EXHAUST_AIR_TEMP'])
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
#plot a variable for a single batch or groups of batches or contrast trajectories
|
|
26
|
+
#Plot single variable for single batch
|
|
27
|
+
phibatch.plot_batch(bdata, which_batch='B1805', which_var='INLET_AIR_TEMP')
|
|
28
|
+
|
|
29
|
+
#Plot single variable for single batch and for contrast add the same variable for the rest of the set
|
|
30
|
+
phibatch.plot_batch(bdata, which_batch='B1805', which_var='INLET_AIR_TEMP',include_set=True)
|
|
31
|
+
|
|
32
|
+
#Plot single variable for single batch and for contrast add the same variable for the rest of the set
|
|
33
|
+
#and include the mean trajectory calculated from the rest of the set (excluding the one being plotted)
|
|
34
|
+
phibatch.plot_batch(bdata, which_batch='B1805', which_var='INLET_AIR_TEMP',include_set=True,include_mean_exc=True)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
#%% Simplistic alignment simply taking the same number of samples per batch
|
|
38
|
+
bdata_aligned=phibatch.simple_align(bdata,250)
|
|
39
|
+
phibatch.plot_var_all_batches(bdata_aligned,
|
|
40
|
+
plot_title='With simple alignment')
|
|
41
|
+
#%%
|
|
42
|
+
#Count how many samples each batch has per phase
|
|
43
|
+
phibatch.phase_sampling_dist(bdata)
|
|
44
|
+
|
|
45
|
+
#%% Better alignment taking advantage of the phase information
|
|
46
|
+
samples_per_phase={'STARTUP':3, 'HEATING':20,'SPRAYING':40,
|
|
47
|
+
'DRYING':40,'DISCHARGING':5}
|
|
48
|
+
|
|
49
|
+
bdata_aligned_phase=phibatch.phase_simple_align(bdata,samples_per_phase)
|
|
50
|
+
phibatch.plot_var_all_batches(bdata_aligned_phase,
|
|
51
|
+
plot_title='Batch data synchronized by phase',
|
|
52
|
+
phase_samples=samples_per_phase)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
#%% Alignment using the temperature during heating as an Indicator Variable
|
|
57
|
+
# this in this example the sampler will take 35 samples from the inital value of Inlet Temp
|
|
58
|
+
# until it reaches 67 C
|
|
59
|
+
samples_per_phase={'STARTUP':3, 'HEATING':['INLET_AIR_TEMP',35,67],'SPRAYING':40,
|
|
60
|
+
'DRYING':40,'DISCHARGING':5}
|
|
61
|
+
|
|
62
|
+
bdata_aligned_phase=phibatch.phase_iv_align(bdata,samples_per_phase)
|
|
63
|
+
phibatch.plot_var_all_batches(bdata_aligned_phase,
|
|
64
|
+
plot_title='Batch data synchronized by phase',
|
|
65
|
+
phase_samples=samples_per_phase)
|
|
66
|
+
|
|
67
|
+
#%% Staying with non IV alignment for the sake of the example
|
|
68
|
+
|
|
69
|
+
samples_per_phase={'STARTUP':3, 'HEATING':20,'SPRAYING':40,
|
|
70
|
+
'DRYING':40,'DISCHARGING':5}
|
|
71
|
+
bdata_aligned_phase=phibatch.phase_simple_align(bdata,samples_per_phase)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
#%% Build a model with all batches and use scores to understand spread
|
|
77
|
+
mpca_obj=phibatch.mpca(bdata_aligned_phase,2, phase_samples=samples_per_phase)
|
|
78
|
+
pp.score_scatter(mpca_obj,[1,2],add_labels=True,marker_size=10)
|
|
79
|
+
pp.diagnostics(mpca_obj)
|
|
80
|
+
phibatch.r2pv(mpca_obj,which_var='TOTAL_SPRAY_USED')
|
|
81
|
+
pp.r2pv(mpca_obj,plotwidth=1500)
|
|
82
|
+
|
|
83
|
+
#Suggestion: Use the "Indicator Variable" alignment approach and see how the
|
|
84
|
+
#score space changes
|
|
85
|
+
|
|
86
|
+
#%%
|
|
87
|
+
# Calculate the contributions to the sores for some batches on the outskits of the score space
|
|
88
|
+
#Contributions can be summarized by the summation of the abs value wrt time
|
|
89
|
+
phibatch.contributions(mpca_obj, bdata_aligned_phase, 'scores',to_obs=['B1905'],plot_title='Cont. to B1905')
|
|
90
|
+
|
|
91
|
+
#This also displays the dynamics of Contributions
|
|
92
|
+
phibatch.contributions(mpca_obj, bdata_aligned_phase, 'scores',to_obs=['B1905'],dyn_conts=True,plot_title='Cont. to B1905')
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
#%% Plotting some of the raw variables identifued as large contributors
|
|
96
|
+
phibatch.plot_batch(bdata_aligned_phase,
|
|
97
|
+
which_batch=['B1905'],
|
|
98
|
+
which_var=['INLET_AIR_TEMP','EXHAUST_AIR_TEMP','INLET_AIR'],
|
|
99
|
+
include_mean_exc=True,
|
|
100
|
+
include_set=True,
|
|
101
|
+
phase_samples=samples_per_phase)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
#%% Take out unusual batches and fit model to normal data use scores and loadings to understand variations
|
|
105
|
+
#normal batches with no abnormal batches
|
|
106
|
+
noc_batch_data=bdata_aligned_phase[np.logical_and(bdata_aligned_phase['BATCH NUMBER']!='B1905',
|
|
107
|
+
bdata_aligned_phase['BATCH NUMBER']!='B1805')]
|
|
108
|
+
|
|
109
|
+
#batches with deviations
|
|
110
|
+
dev_batch_data=bdata_aligned_phase[np.logical_or(bdata_aligned_phase['BATCH NUMBER']=='B1905',
|
|
111
|
+
bdata_aligned_phase['BATCH NUMBER']=='B1805')]
|
|
112
|
+
|
|
113
|
+
#Build a Multi-way PCA model with 2PC's and display some basic diagnostics
|
|
114
|
+
mpca_obj=phibatch.mpca(noc_batch_data,2, phase_samples=samples_per_phase,cross_val=5)
|
|
115
|
+
pp.score_scatter(mpca_obj,[1,2],add_labels=True)
|
|
116
|
+
pp.diagnostics(mpca_obj)
|
|
117
|
+
#%% Make some diagnostic plots to understand the data and the model
|
|
118
|
+
phibatch.r2pv(mpca_obj)
|
|
119
|
+
phibatch.loadings_abs_integral(mpca_obj)
|
|
120
|
+
phibatch.loadings(mpca_obj,2,which_var=['INLET_AIR_TEMP','EXHAUST_AIR_TEMP','TOTAL_SPRAY_USED'],
|
|
121
|
+
r2_weighted=True)
|
|
122
|
+
#%% PLot the raw variables
|
|
123
|
+
phibatch.plot_batch(bdata_aligned_phase,
|
|
124
|
+
which_batch=['B1910','B2110'],
|
|
125
|
+
which_var=['TOTAL_SPRAY_USED'],
|
|
126
|
+
include_mean_exc=True,
|
|
127
|
+
include_set=True,
|
|
128
|
+
phase_samples=samples_per_phase,single_plot=True)
|
|
129
|
+
|
|
130
|
+
batch_list=['B1910','B1205','B2510','B2210','B1810','B2110']
|
|
131
|
+
phibatch.plot_batch(bdata_aligned_phase,
|
|
132
|
+
which_batch=batch_list,
|
|
133
|
+
which_var=['EXHAUST_AIR_TEMP'],
|
|
134
|
+
include_mean_exc=True,
|
|
135
|
+
include_set=True,
|
|
136
|
+
phase_samples=samples_per_phase,single_plot=True)
|
|
137
|
+
|
|
138
|
+
phibatch.plot_batch(bdata_aligned_phase,
|
|
139
|
+
which_batch=batch_list,
|
|
140
|
+
which_var=['INLET_AIR_TEMP'],
|
|
141
|
+
include_mean_exc=True,
|
|
142
|
+
include_set=True,
|
|
143
|
+
phase_samples=samples_per_phase,single_plot=True)
|
|
144
|
+
|
|
145
|
+
#%% Prepare the model for monitorig
|
|
146
|
+
|
|
147
|
+
phibatch.monitor(mpca_obj, noc_batch_data)
|
|
148
|
+
#%%Monitor all normal batches and show diagnostics
|
|
149
|
+
all_batches=np.unique(noc_batch_data['BATCH NUMBER'].values.tolist())
|
|
150
|
+
mon_all_batches=phibatch.monitor(mpca_obj,noc_batch_data,
|
|
151
|
+
which_batch=all_batches)
|
|
152
|
+
|
|
153
|
+
#%% Monitor batch 1905 and diagnose
|
|
154
|
+
|
|
155
|
+
mon_1905 = phibatch.monitor(mpca_obj,dev_batch_data,which_batch=['B1905'])
|
|
156
|
+
|
|
157
|
+
#contribution to instantaneous SPE at sample 17
|
|
158
|
+
sam_num = 5
|
|
159
|
+
plt.figure()
|
|
160
|
+
plt.bar(mon_1905['cont_spei'].columns ,mon_1905['cont_spei'].iloc[sam_num-1])
|
|
161
|
+
plt.xticks(rotation=90)
|
|
162
|
+
plt.ylabel('Contributions to i-SPE')
|
|
163
|
+
plt.title('Contributions to instantaneous up to sample #'+str(sam_num))
|
|
164
|
+
plt.tight_layout()
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
phibatch.plot_batch(bdata_aligned_phase,
|
|
168
|
+
which_batch=['B1905'],
|
|
169
|
+
which_var=['INLET_AIR_TEMP','EXHAUST_AIR_TEMP','INLET_AIR'],
|
|
170
|
+
include_mean_exc=True,
|
|
171
|
+
include_set=True,
|
|
172
|
+
phase_samples=samples_per_phase)
|
|
173
|
+
#%% Prepare forecasting for batch B2510
|
|
174
|
+
batch2forecast='B2510'
|
|
175
|
+
mon_data=phibatch.monitor(mpca_obj,noc_batch_data,
|
|
176
|
+
which_batch=batch2forecast)
|
|
177
|
+
#%% Plot forecast for the inlet_temp at various points in time
|
|
178
|
+
var='INLET_AIR_TEMP'
|
|
179
|
+
point_in_time_=[5,20,50,80]
|
|
180
|
+
for point_in_time in point_in_time_:
|
|
181
|
+
forecast=mon_data['forecast']
|
|
182
|
+
mdata=noc_batch_data[noc_batch_data['BATCH NUMBER']==batch2forecast]
|
|
183
|
+
x_axis=np.arange(mdata.shape[0])+1
|
|
184
|
+
plt.figure()
|
|
185
|
+
f=forecast[point_in_time-1]
|
|
186
|
+
x_axis_=np.arange( len(mdata[var][:point_in_time-1]))+1
|
|
187
|
+
plt.plot(x_axis_,mdata[var][:point_in_time-1],'o',label='Measured')
|
|
188
|
+
aux=[np.nan]*point_in_time
|
|
189
|
+
aux.extend(f[var].values[point_in_time:].tolist())
|
|
190
|
+
plt.plot(x_axis,np.array(aux),label='Forecast')
|
|
191
|
+
x_axis_=np.arange(len(mdata[var][point_in_time:] ))+point_in_time+1
|
|
192
|
+
plt.plot(x_axis_,mdata[var][point_in_time:],'o',label='Known trajectory',alpha=0.3)
|
|
193
|
+
plt.xlabel('sample')
|
|
194
|
+
plt.ylabel(var)
|
|
195
|
+
plt.legend()
|
|
196
|
+
plt.title('Forecast for '+var+' at sample '+str(point_in_time)+' for '+batch2forecast )
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|