Skip to content

Commit

Permalink
first commit for xMCA
Browse files Browse the repository at this point in the history
  • Loading branch information
He authored and He committed Jun 5, 2019
1 parent 4dd63ab commit e312ab9
Show file tree
Hide file tree
Showing 15 changed files with 1,209 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
155 changes: 155 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@

# About xMCA
xMCA is Maximum Covariance Analysis (sometimes also called SVD)in xarray.


# How to install
### via git
```
git clone https://github.com/Yefee/xMCA.git
cd xcesm
python setup.py install
```


# Example
MCA analysis for US surface air temperature and SST over the Pacific
This example is taken from https://atmos.washington.edu/~breth/classes/AS552/matlab/lect/html/MCA_PSSTA_USTA.html


```python
from xMCA import xMCA
import xarray as xr
import matplotlib.pyplot as plt
%matplotlib inline
```


```python
usta = xr.open_dataarray('data/USTA.nc').transpose(*['time', 'lat', 'lon'])
usta.name = 'USTA'
print(usta)
```

<xarray.DataArray 'USTA' (time: 396, lat: 5, lon: 12)>
array([[[-0.450303, -0.734848, ..., -4.270303, -2.69697 ],
[ 1.066061, 2.691515, ..., -4.947273, -3.330303],
...,
[ nan, -0.342424, ..., nan, nan],
[ nan, nan, ..., nan, nan]],

[[ 1.524545, 1.370606, ..., -1.430303, 0.048485],
[ 1.366364, 2.497273, ..., -0.593939, -0.079697],
...,
[ nan, 0.695455, ..., nan, nan],
[ nan, nan, ..., nan, nan]],

...,

[[ 1.077879, 0.630303, ..., -1.262727, -1.496364],
[ 1.020606, 0.114848, ..., -0.786667, -0.573939],
...,
[ nan, 1.65 , ..., nan, nan],
[ nan, nan, ..., nan, nan]],

[[ 1.768182, 2.807879, ..., 0.885758, 0.618182],
[ 1.555152, 3.435152, ..., -0.416667, 0.185152],
...,
[ nan, 0.012121, ..., nan, nan],
[ nan, nan, ..., nan, nan]]])
Coordinates:
* lat (lat) float64 47.5 42.5 37.5 32.5 27.5
* lon (lon) float64 -122.5 -117.5 -112.5 -107.5 ... -77.5 -72.5 -67.5
* time (time) int64 0 1 2 3 4 5 6 7 8 ... 388 389 390 391 392 393 394 395



```python
sstpc = xr.open_dataarray('data/SSTPac.nc').transpose(*['time', 'lat', 'lon'])
sstpc.name = 'SSTPC'
print(sstpc)
```

<xarray.DataArray 'SSTPC' (time: 396, lat: 30, lon: 84)>
[997920 values with dtype=float64]
Coordinates:
* lat (lat) int16 -29 -27 -25 -23 -21 -19 -17 ... 17 19 21 23 25 27 29
* lon (lon) uint16 124 126 128 130 132 134 ... 280 282 284 286 288 290
* time (time) int64 0 1 2 3 4 5 6 7 8 ... 388 389 390 391 392 393 394 395


Decompsition and retrieve the first and second loadings and expansion coefficeints


```python
'''
decomposition, time should be in the first axis
lp is for SSTPC
rp is for USTA
'''

sst_ts = xMCA(sstpc, usta)
sst_ts.solver()
lp, rp = sst_ts.patterns(n=2)
le, re = sst_ts.expansionCoefs(n=2)
frac = sst_ts.covFracs(n=2)
print(frac)
```

<xarray.DataArray 'frac' (n: 2)>
array([0.407522, 0.391429])
Coordinates:
* n (n) int64 0 1
Attributes:
long_name: Fractions explained of the covariance matrix between SSTPC an...



```python
fig, (ax1, ax2) = plt.subplots(2, 2, figsize=(12, 5))
lp[0].plot(ax=ax1[0])
le[0].plot(ax=ax1[1])

rp[0].plot(ax=ax2[0])
re[0].plot(ax=ax2[1])
```




[<matplotlib.lines.Line2D at 0x11e75aa58>]




![png](example_files/example_6_1.png)


Homogeneous and heterogeneous regression


```python
lh, rh = sst_ts.homogeneousPatterns(n=1)
le, re = sst_ts.heterogeneousPatterns(n=1)
```


```python
fig, (ax1, ax2) = plt.subplots(2, 2, figsize=(12, 5))
lh[0].plot(ax=ax1[0])
rh[0].plot(ax=ax1[1])

le[0].plot(ax=ax2[0])
re[0].plot(ax=ax2[1])
```




<matplotlib.collections.QuadMesh at 0x11ecf3cc0>




![png](example_files/example_9_1.png)

23 changes: 23 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from setuptools import setup, find_packages

setup(name='xMCA',
version='0.1',
description='Maximum Covariance Analysis in xarray.',
url='https://github.com/Yefee/xMCA',
author='Chengfei He',
author_email='che43@wisc.edu',
include_package_data=True,
classifiers=[
'Development Status :: 3 - Alpha',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
],
keywords='statistical analysis MCA SVD xarray',
license='MIT',
# packages=['xcesm','config'],
packages=find_packages(),
package_data={'xMCA': ['examples/data/*.nc']},
install_requires=['xarray', 'numpy'],
zip_safe=False)
print(find_packages())
Binary file added xMCA/.DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions xMCA/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from .core.xMCA import xMCA
Binary file added xMCA/core/.DS_Store
Binary file not shown.
Binary file added xMCA/core/__pycache__/xMCA.cpython-36.pyc
Binary file not shown.
Loading

0 comments on commit e312ab9

Please sign in to comment.