https://github.com/nicrie/xmca

Maximum Covariance Analysis in Python

https://github.com/nicrie/xmca

Science Score: 23.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
  • .zenodo.json file
  • DOI references
    Found 12 DOI reference(s) in README
  • Academic publication links
    Links to: arxiv.org, sciencedirect.com, wiley.com, zenodo.org
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (14.8%) to scientific vocabulary

Keywords

complex correlation eof maximum maximum-covariance-analysis mca numpy pca principal-component-analysis promax python rotation varimax xarray
Last synced: 6 months ago · JSON representation

Repository

Maximum Covariance Analysis in Python

Basic Info
  • Host: GitHub
  • Owner: nicrie
  • License: mit
  • Language: Python
  • Default Branch: main
  • Homepage:
  • Size: 6.89 MB
Statistics
  • Stars: 57
  • Watchers: 2
  • Forks: 16
  • Open Issues: 8
  • Releases: 17
Topics
complex correlation eof maximum maximum-covariance-analysis mca numpy pca principal-component-analysis promax python rotation varimax xarray
Created over 5 years ago · Last pushed over 2 years ago
Metadata Files
Readme License

README.md

Warning: This package is no longer actively maintained. Please use xeofs instead.

xMCA | Maximum Covariance Analysis in Python

version GitHub Workflow Status Documentation Status codecov.io downloads DOI

The aim of this package is to provide a flexible tool for the climate science community to perform Maximum Covariance Analysis (MCA) in a simple and consistent way. Given the huge popularity of xarray in the climate science community, xmca supports xarray.DataArray as well as numpy.ndarray as input formats.

Example Figure Mode 2 of complex rotated Maximum Covariance Analysis showing the shared dynamics of SST and continental precipitation associated to ENSO between 1980 and 2020.

:beginner: What is MCA?

MCA maximises the temporal covariance between two different data fields and is closely related to Principal Component Analysis (PCA) / Empirical Orthogonal Function analysis (EOF analysis). While EOF analysis maximises the variance within a single data field, MCA allows to extract the dominant co-varying patterns between two different data fields. When the two input fields are the same, MCA reduces to standard EOF analysis.

For the mathematical understanding please have a look at e.g. Bretherton et al. or the lecture material written by C. Bretherton.

:star: New in release 1.4.x

  • Much faster and more memory-efficient algorithm
  • Significance testing of individual modes via
  • Period parameter of solve method provides more flexibility to exponential extension, making complex MCA more stable
  • Fixed missing coslat weighting when saving a model (Issue 25)

:pushpin: Core Features

| | Standard | Rotated | Complex | Complex Rotated | |-------------- |---------- |---------- |--------- |------------------ | | EOF analysis |:heavycheckmark:|:heavycheckmark:|:heavycheckmark:|:heavycheckmark:| | MCA |:heavycheckmark:|:heavycheckmark:|:heavycheckmark:|:heavycheckmark:|

* click on check marks for reference \ ** Complex rotated MCA is also available as a pre-print on arXiv.

:wrench: Installation

Installation is simply done via

pip install xmca

If you have problems during the installation please consult the documentation or raise an issue here on Github.

:newspaper: Documentation

A tutorial to get you started as well as the full API can be found in the documentation.

:zap: Quickstart

Import the package

py from xmca.array import MCA # use with np.ndarray from xmca.xarray import xMCA # use with xr.DataArray

As an example, we take North American surface temperatures shipped with xarray. Note: only works with xr.DataArray, not xr.Dataset.

```py import xarray as xr # only needed to obtain test data

# split data arbitrarily into west and east coast
data = xr.tutorial.open_dataset('air_temperature').air
west = data.sel(lon=slice(200, 260))
east = data.sel(lon=slice(260, 360))

```

PCA / EOF analysis

Construct a model with only one field and solve it to perform standard PCA / EOF analysis. ```py pca = xMCA(west) # PCA of west coast pca.solve(complexify=False) # True for complex PCA

svals = pca.singular_values()     # singular vales = eigenvalues for PCA
expvar      = pca.explained_variance()  # explained variance
pcs         = pca.pcs()                 # Principal component scores (PCs)
eofs        = pca.eofs()                # spatial patterns (EOFs)

```

Obtaining a Varimax/Promax-rotated solution can be achieved by rotating the model choosing the number of EOFs to be rotated (n_rot) as well as the Promax parameter (power). Here, power=1 equals a Varimax-rotated solution. ```py pca.rotate(n_rot=10, power=1)

expvar_rot  = pca.explained_variance()  # explained variance
pcs_rot     = pca.pcs()                 # Principal component scores (PCs)
eofs_rot    = pca.eofs()                # spatial patterns (EOFs)

```

MCA

Same as for PCA / EOF analysis, but with two input fields instead of one. ```py
mca = xMCA(west, east) # MCA of field A and B mca.solve(complexify=False) # True for complex MCA

eigenvalues = mca.singular_values()     # singular vales
pcs = mca.pcs()                         # expansion coefficient (PCs)
eofs = mca.eofs()                       # spatial patterns (EOFs)

```

Significance analysis

A simple way of estimating the significance of the obtained modes is by running Monte Carlo simulations based on uncorrelated Gaussian white noise known as Rule N (Overland and Preisendorfer 1982). Here we create 200 of such synthetic data sets and compare the synthetic with the real singular spectrum to assess significance.

```py
surr = mca.rule_n(200) median = surr.median('run') q99 = surr.quantile(.99, dim='run') q01 = surr.quantile(.01, dim='run')

cutoff = np.sum((svals - q99 > 0)).values  # first 8 modes significant

fig = plt.figure(figsize=(10, 4))
ax = fig.add_subplot(111)
svals.plot(ax=ax, yscale='log', label='true')
median.plot(ax=ax, yscale='log', color='.5', label='rule N')
q99.plot(ax=ax, yscale='log', color='.5', ls=':')
q01.plot(ax=ax, yscale='log', color='.5', ls=':')
ax.axvline(cutoff + 0.5, ls=':')
ax.set_xlim(-2, 200)
ax.set_ylim(1e-1, 2.5e4)
ax.set_title('Significance based on Rule N')
ax.legend()

```

Example Figure Mode1 The first 8 modes are significant according to rule N using 200 synthetic runs.

Saving/loading an analysis

py mca.save_analysis('my_analysis') # this will save the data and a respective # info file. The files will be stored in a # special directory mca2 = xMCA() # create a new, empty instance mca2.load_analysis('my_analysis/info.xmca') # analysis can be # loaded via specifying the path to the # info file created earlier

Quickly inspect your results visually

The package provides a method to plot individual modes.

py mca2.set_field_names('West', 'East') pkwargs = {'orientation' : 'vertical'} mca2.plot(mode=1, **pkwargs) Example Figure Mode1 Result of default plot method after performing MCA on T2m of North American west and east coast showing mode 1.

You may want to modify the plot for some better optics:

```py from cartopy.crs import EqualEarth # for different map projections

# map projections for "left" and "right" field
projections = {
    'left': EqualEarth(),
    'right': EqualEarth()
}

pkwargs = {
    "figsize"     : (8, 5),
    "orientation" : 'vertical',
    'cmap_eof'    : 'BrBG',  # colormap amplitude
    "projection"  : projections,
}
mca2.plot(mode=3, **pkwargs)

```

Example Figure Mode 3

You can save the plot to your local disk as a .png file via

py skwargs={'dpi':200} mca2.save_plot(mode=3, plot_kwargs=pkwargs, save_kwargs=skwargs)

:bookmark: Please cite

I am just starting my career as a scientist. Feedback on my scientific work is therefore important to me in order to assess which of my work advances the scientific community. As such, if you use the package for your own research and find it helpful, I would appreciate feedback here on Github, via email, or as a citation:

Niclas Rieger, 2021: nicrie/xmca: version x.y.z. doi:10.5281/zenodo.4749830.

:muscle: Credits

Kudos to the developers and contributors of the following Github projects which I initially used myself and used as an inspiration:

And of course credits to the developers of the extremely useful packages

Owner

  • Name: Niclas Rieger
  • Login: nicrie
  • Kind: user
  • Location: Barcelona
  • Company: Centre de Recerca Matemática

GitHub Events

Total
  • Watch event: 4
Last Year
  • Watch event: 4

Committers

Last synced: almost 3 years ago

All Time
  • Total Commits: 303
  • Total Committers: 4
  • Avg Commits per committer: 75.75
  • Development Distribution Score (DDS): 0.215
Top Committers
Name Email Commits
nrieger n****r@g****m 238
Niclas Rieger n****r@c****t 41
Niclas Rieger 4****e@u****m 18
NiclasRieger 4****r@u****m 6
Committer Domains (Top 20 + Academic)
crm.cat: 1

Issues and Pull Requests

Last synced: 7 months ago

All Time
  • Total issues: 15
  • Total pull requests: 20
  • Average time to close issues: 17 days
  • Average time to close pull requests: 38 minutes
  • Total issue authors: 8
  • Total pull request authors: 2
  • Average comments per issue: 2.73
  • Average comments per pull request: 0.25
  • Merged pull requests: 18
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • nicrie (7)
  • gkb999 (2)
  • Murk89 (1)
  • aidanheerdegen (1)
  • chjones2 (1)
  • L1angY (1)
  • dizcza (1)
  • zhiweijt (1)
Pull Request Authors
  • nicrie (19)
  • sourcery-ai-bot (1)
Top Labels
Issue Labels
enhancement (4) bug (3) documentation (2)
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 73 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 2
  • Total versions: 14
  • Total maintainers: 1
pypi.org: xmca

Maximum Covariance Analysis in Python

  • Versions: 14
  • Dependent Packages: 0
  • Dependent Repositories: 2
  • Downloads: 73 Last month
Rankings
Forks count: 9.3%
Stargazers count: 9.5%
Dependent packages count: 10.1%
Average: 11.5%
Dependent repos count: 11.5%
Downloads: 17.1%
Maintainers (1)
Last synced: 7 months ago

Dependencies

requirements.txt pypi
  • PyYAML *
  • cartopy >=0.18.0
  • h5netcdf *
  • matplotlib >=3.3.2
  • netcdf4 *
  • numpy >=1.19.2
  • parameterized >=0.8.1
  • statsmodels >=0.12.2
  • tqdm *
  • xarray >=0.16.2
.github/workflows/build-pip.yml actions
  • actions/checkout v2 composite
  • actions/setup-python v2 composite
  • codecov/codecov-action v1 composite
docs/environment.yml conda
  • cartopy 0.18.0.*
  • matplotlib
  • numpy
  • python >3.6
  • statsmodels
  • tqdm
  • xarray
  • yaml
setup.py pypi