Coder Social home page Coder Social logo

ci-demo's Introduction

Automatic tests with Pytest

test-power-method coverage

What is this example about?

In this example we will execute some automatic tests for Python code implementing the [power iteration method]. This is an iterative algorithm for finding an approximation for the largest (in absolute value) eigenvalue $\lambda_\mathrm{max}$ of a diagonalisable matrix $A$. It also gives an approximate eigenvector $v$ corresponding to the solution of the eigenvalue equation $A v = \lambda_\mathrm{max} v$.

Implementation and tests

Power method implementation

The power iteration method is implemented in Python by makeing use of the [Numpy] library. The code can be found in power_method.py. For details of the implementation please refer to the source file.

Tests

The tests are provided in the file test_power_method.py. The tests rely on test [Pytest] test framework.

Two integration tests are implemented:

  • Test the final result for the eigenvalue when using the eigenvalues to abort the iterations. The function

    def test_real_symmetric_iter_eigenvalues(matrix_file):
      # ...

    implements this test.

  • Test the final result for the eigenvalue when using the eigenvectors to abort the iterations. The function

    def test_real_symmetric_iter_eigenvector(matrix_file):
      # ...

    implements this test.

Both tests use real-valued symmetric matrices $A \in \mathbb{R}^{n \times n}$ with several different sizes. These matrices are contained in the directory test_data.

How to run the tests

Make sure to have Python version >=3.9.x installed on your system. If you do not have a corresponding Python version already installed you can install it with e.g. [Miniconda].

Python environment

Use on the provided files requirements.txt or environment.yml to set up a Python [virtual environment] or an [Anaconda environment].

Virtual environment

Open a terminal emulator and make sure Python is contained in the PATH:

$ which python3
/usr/bin/python3

Next, execute the following commands to establish the Python environment:

$ mkdir -vp pyenv           # Host venv in current workdir
$ python3 -m venv pyenv     # Create venv 
$ source pyenv/bin/activate # Acivate venv
$ pip install --upgrade pip && pip install -r requirements.txt

Conda environment

Open a terminal emulator and make sure the conda utility is in the PATH. This should be the case if you have installed either [Miniconda3] or [Anaconda3]. Next, establish the Anaconda environment by running the following commands from the shell prompt:

$ conda env create --file environment.yml

This will create an environment named AutomaticTesting which can be activated like so:

$ source activate AutomaticTesting

Running the tests

Pytest is needed to execute the tests. Hence make sure it has been properly installed:

$ python -c "import pytest; print(pytest.__version__)"

This should print the Pytest version number (7.1.2 in my case).

Running the test is as simple as

$ pytest test_power_method.py # Add -v for more verbose output.

If you just want to execute the tests for the eigenvalue criterion for aborting the iterations use:

$ pytest -k eigenvalues test_power_method.py

The pattern eigenvalues will make Pytest select only the test function with a name matching this pattern. Conversely, if you want to limit yourself to the tests for the eigenvector criterion use the eigenvectors pattern:

$ pytest -k eigenvectors test_power_method.py

ci-demo's People

Contributors

github-actions[bot] avatar ttm02 avatar

Watchers

 avatar

Forkers

giammi56

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.