Skip to content

Commit

Permalink
add another example
Browse files Browse the repository at this point in the history
  • Loading branch information
Yefee committed Jun 7, 2019
1 parent 1e1fdd3 commit 58a5e2a
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
xMCA.egg
.DS_Store
xMCA.egg-info
__pycache__
.ipynb_checkpoints
build
dist
89 changes: 88 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ python setup.py install
# Future Plan
In next version, A Monte Carlo method will be added for statistical test.

# Example
# Example 1
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

Expand Down Expand Up @@ -143,3 +143,90 @@ re[0].plot(ax=ax2[1])

![png](https://github.com/Yefee/xMCA/blob/master/xMCA/examples/example_files/example_9_1.png)



# Example 2
EOF 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
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, sstpc.rename('SSTPC_copy'))
sst_ts.solver()
lp, _ = sst_ts.patterns(n=2)
le, _ = sst_ts.expansionCoefs(n=2)
frac = sst_ts.covFracs(n=2)
print(frac)
```

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



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

```



![png](https://github.com/Yefee/xMCA/blob/master/xMCA/examples/example_files/example_eof_5_1.png)


Regress PC1 to the original SST field


```python
lh, _ = sst_ts.homogeneousPatterns(n=1)
```



```python
fig, ax1= plt.subplots()
lh[0].plot(ax=ax1)

```



![png](https://github.com/Yefee/xMCA/blob/master/xMCA/examples/example_files/example_eof_8_1.png)

Binary file removed xMCA/.DS_Store
Binary file not shown.
201 changes: 201 additions & 0 deletions xMCA/examples/example_eof.ipynb

Large diffs are not rendered by default.

Binary file added xMCA/examples/example_files/example_eof_5_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added xMCA/examples/example_files/example_eof_8_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 21 additions & 12 deletions xMCA/examples/example.ipynb → xMCA/examples/example_svd.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
{
"cell_type": "code",
"execution_count": 59,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -22,7 +22,7 @@
},
{
"cell_type": "code",
"execution_count": 60,
"execution_count": 2,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -70,7 +70,7 @@
},
{
"cell_type": "code",
"execution_count": 61,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -101,7 +101,7 @@
},
{
"cell_type": "code",
"execution_count": 62,
"execution_count": 4,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -134,16 +134,16 @@
},
{
"cell_type": "code",
"execution_count": 63,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[<matplotlib.lines.Line2D at 0x11e75aa58>]"
"[<matplotlib.lines.Line2D at 0x11d813c50>]"
]
},
"execution_count": 63,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
},
Expand Down Expand Up @@ -178,26 +178,35 @@
},
{
"cell_type": "code",
"execution_count": 64,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/he.1519/miniconda3/lib/python3.6/site-packages/xarray/core/nanops.py:161: RuntimeWarning: Mean of empty slice\n",
" return np.nanmean(a, axis=axis, dtype=dtype)\n"
]
}
],
"source": [
"lh, rh = sst_ts.homogeneousPatterns(n=1)\n",
"le, re = sst_ts.heterogeneousPatterns(n=1)"
]
},
{
"cell_type": "code",
"execution_count": 65,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.collections.QuadMesh at 0x11ecf3cc0>"
"<matplotlib.collections.QuadMesh at 0x11e68a668>"
]
},
"execution_count": 65,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
},
Expand Down

0 comments on commit 58a5e2a

Please sign in to comment.