Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tschm committed Aug 1, 2023
1 parent 0e82eac commit 0805b84
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
24 changes: 16 additions & 8 deletions cvx/cli/smallest_eigenvalue.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
# -*- coding: utf-8 -*-
from typing import Dict

import fire # type: ignore
import numpy as np

from cvx.bson.file import read_bson


def smallest_ev(bson_file) -> None:
def smallest_ev(bson_file) -> Dict[str, float]:
"""
Compute the smallest eigenvalue of a matrix stored in a bson file.
The key for the matrix shall be "cov".
Compute the smallest eigenvalue of matrices stored in a bson file.
There are faster methods to compute the smallest eigenvalue, e.g. inverse power iteration.
Here, we only use this as an example to work with the bson interface
There are faster methods to compute the smallest eigenvalue, e.g. an inverse power iteration.
Here, we only use this as an example to work with the bson interface.
On the command line
poetry run smallest-eigenvalue cli/data/test.bson
"""
for key, matrix in read_bson(bson_file).items():
print(key)
print(np.min(np.linalg.eigh(matrix)[0]))

eigenvalues = {
key: np.min(np.linalg.eigh(matrix)[0])
for key, matrix in read_bson(bson_file).items()
}

for key, ev in eigenvalues.items():
print(key, ev)

return eigenvalues


def main(): # pragma: no cover
Expand Down
4 changes: 3 additions & 1 deletion cvx/cli/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def cli(metric: str, latitude: float = 37.4419, longitude: float = -122.143) ->
Parameters
----------
metric : str
The metric to get the current weather for
The metric to get the current weather for.
Use time, temperature, windspeed, winddirection ot weathercode
For details: https://open-meteo.com/en/docs
latitude : float, optional
The latitude to get the current weather for, by default 37.4419
longitude : float, optional
Expand Down
5 changes: 4 additions & 1 deletion tests/test_smallest_eigenvalue.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import pytest

from cvx.cli.smallest_eigenvalue import smallest_ev


def test_smallest_eigenvalue(resource_dir):
file = resource_dir / "test.bson"

smallest_ev(file)
data = smallest_ev(file)
assert data["cov"] == pytest.approx(8.673386399472251e-05)

0 comments on commit 0805b84

Please sign in to comment.