Skip to content

Commit

Permalink
Merge pull request #16 from FormingWorlds/testing
Browse files Browse the repository at this point in the history
Testing
  • Loading branch information
lsoucasse authored Aug 26, 2024
2 parents 135458b + 992da90 commit 69a4609
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 4 deletions.
76 changes: 76 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Tests for MORS

on:
push:
branches:
- master
pull_request:
branches:
- master
workflow_dispatch:

jobs:
test:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@v4

- name: Setup system
run: |
sudo apt-get update
sudo apt-get install libnetcdff-dev netcdf-bin
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- uses: actions/cache@v3
id: cache-virtualenv
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml') }}

- name: Build MORS
if: steps.cache-virtualenv.outputs.cache-hit != 'true'
run: |
python -m pip install -e .[develop]
- uses: actions/cache@v3
id: cache-fwl-data
with:
path: /home/runner/work/fwl_data
key: fwl-data-1

- name: Test with pytest
run: |
export FWL_DATA="/home/runner/work/fwl_data"
coverage run -m pytest
- name: Report coverage
run: |
coverage json
export TOTAL=$(python -c "import json;print(json.load(open('coverage.json'))['totals']['percent_covered_display'])")
echo "total=$TOTAL" >> $GITHUB_ENV
echo "### Total coverage: ${TOTAL}%" >> $GITHUB_STEP_SUMMARY
echo $'\n```' >> $GITHUB_STEP_SUMMARY
coverage report >> $GITHUB_STEP_SUMMARY
echo $'\n```' >> $GITHUB_STEP_SUMMARY
- name: Make coverage badge
if: ${{ github.ref == 'refs/heads/master' && matrix.python-version == '3.10' }}
uses: schneegans/[email protected]
with:
auth: ${{ secrets.GIST_TOKEN }}
gistID: a25c37a328839edd00bb32d8527aec30
filename: covbadge.svg
label: Coverage
message: ${{ env.total }}%
minColorRange: 50
maxColorRange: 90
valColorRange: ${{ env.total }}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
![Coverage](https://gist.githubusercontent.com/lsoucasse/a25c37a328839edd00bb32d8527aec30/raw/covbadge.svg)

# MODEL FOR ROTATION OF STARS (MORS)

**This code is distributed as a python package for the purpose of the [PROTEUS framework](https://github.com/FormingWorlds/PROTEUS), a coupled simulation tool for the long-term evolution of atmospheres and interiors of rocky planets.
Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ homepage = "https://github.com/FormingWorlds/MORS"
[project.optional-dependencies]
develop = [
"bump-my-version",
"coverage[toml]",
"pip-tools",
"pytest"
]
Expand All @@ -60,6 +61,10 @@ mors = "mors.cli:cli"
package-dir = {"mors" = "src/mors"}
include-package-data = true

[tool.coverage.run]
branch = true
source = ["mors"]

[tool.pytest.ini_options]
testpaths = ["tests"]

Expand Down
8 changes: 4 additions & 4 deletions src/mors/stellarevo.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def _shouldCompileNew(starEvoDir,evoModels):
compileNew = True

# Check if previously compiled models already exist
if ( os.path.isfile(starEvoDir+evoModels+".pickle") ):
if ( os.path.isfile(starEvoDir+"/"+evoModels+".pickle") ):
compileNew = False

return compileNew
Expand Down Expand Up @@ -223,7 +223,7 @@ def _CompileNewGrid(starEvoDir,evoModels):
ModelData[MstarAll[iMstar]] = _ReadEvolutionTrack( starEvoDir , evoModels , MstarAll[iMstar] , MstarFilenameMiddle[iMstar] )

# Save compiled models
with open(starEvoDir+evoModels+".pickle",'wb') as f:
with open(starEvoDir+"/"+evoModels+".pickle",'wb') as f:
pickle.dump(ModelData,f)


Expand All @@ -233,7 +233,7 @@ def _ReadEvolutionTrack(starEvoDir,evoModels,Mstar,MstarFilenameMiddle):
"""Loads the stellar evolution models from Spada et al. (2013) for a mass bin and puts it into a dictionary."""

# Set strings for starting and ending of filenames
filename_prefix = starEvoDir + evoModels + "/M"
filename_prefix = starEvoDir + "/" + evoModels + "/M"
filename_postfix1 = "_" + evoModels + ".track1"
filename_postfix2 = "_" + evoModels + ".track2"

Expand Down Expand Up @@ -415,7 +415,7 @@ def _LoadSavedGrid(starEvoDir,evoModels):
"""Takes filename for stellar evo model, returns grid of models."""

# Simply load data
with open(starEvoDir+evoModels+".pickle",'rb') as f:
with open(starEvoDir+"/"+evoModels+".pickle",'rb') as f:
ModelData = pickle.load(f)

return ModelData
Expand Down
21 changes: 21 additions & 0 deletions tests/test_baraffe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import mors
import pytest
from numpy.testing import assert_allclose

TEST_DATA = (
((0.047, 8.5e7, 0.75),(0.74306071e-3, 0.14300000, 1.79809587 )),
((1.113, 3.2e9, 1.05),(1.65441005 , 1.18216231, 2.04256380e3)),
)

@pytest.mark.parametrize("inp,expected", TEST_DATA)
def test_baraffe(inp, expected):

mors.DownloadEvolutionTracks('Baraffe')
baraffe = mors.BaraffeTrack(inp[0])
ret = (
baraffe.BaraffeLuminosity(inp[1]),
baraffe.BaraffeStellarRadius(inp[1]),
baraffe.BaraffeSolarConstant(inp[1], inp[2]),
)

assert_allclose(ret, expected, rtol=1e-5, atol=0)
21 changes: 21 additions & 0 deletions tests/test_spada.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import mors
import pytest
from numpy.testing import assert_allclose

TEST_DATA = (
((0.128, 45.2, 8.5e1),(0.21263373, 1.68612613e+31, 1.74497354e+28)),
((1.113, 17.7, 3.2e3),(1.16581856, 6.81769993e+33, 7.95296357e+28)),
)

@pytest.mark.parametrize("inp,expected", TEST_DATA)
def test_spada(inp, expected):

mors.DownloadEvolutionTracks('Spada')
star = mors.Star(Mstar=inp[0], Omega=inp[1])
ret = (
star.Value(inp[2], 'Rstar'),
star.Value(inp[2], 'Lbol'),
star.Value(inp[2], 'Leuv'),
)

assert_allclose(ret, expected, rtol=1e-5, atol=0)
File renamed without changes.

0 comments on commit 69a4609

Please sign in to comment.