Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Testing Time #241

Merged
merged 89 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 88 commits
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
eb9afc4
Consolidate package installation steps
ssolson May 11, 2023
6d800b6
Remove redundant package installations
ssolson May 11, 2023
6bd44f2
Combine the Install MHKiT and Run pytest steps
ssolson May 11, 2023
e9327eb
Use environment variables for common values
ssolson May 11, 2023
4e9c0b1
cache requests
ssolson May 11, 2023
6c1bea1
noaa cacheing working
ssolson May 15, 2023
0cb1764
only check for folder eistance once
ssolson May 15, 2023
d36224e
create a utils folder
ssolson May 16, 2023
98d4a00
remove old utils all in one file
ssolson May 16, 2023
44b08b3
use cache utils
ssolson May 16, 2023
91e72ac
tests working with cache utils
ssolson May 16, 2023
d597118
remove duplicate keys
ssolson May 18, 2023
6d537ac
remove unused imports
ssolson May 18, 2023
d422c9c
assert -> ValueError
ssolson May 19, 2023
22ab6eb
datetime clean ups
ssolson May 19, 2023
02ebbf3
datetime and code cleanup
ssolson May 20, 2023
2808b76
cache
ssolson May 20, 2023
e77b9a8
working cdip tests
ssolson May 22, 2023
344d881
remove plot files
ssolson May 22, 2023
076ead1
remove distutils
ssolson May 22, 2023
61dcea5
hindcast cacheing
ssolson May 22, 2023
34ad9a7
WIP use actions cache
ssolson May 22, 2023
d3ffb28
fix indentation
ssolson Jun 2, 2023
dd2a13d
Activate conda env
ssolson Jun 2, 2023
16e9bc2
tests need cache
ssolson Jun 2, 2023
8734409
fix file path
ssolson Jun 2, 2023
359fa8e
remove debug
ssolson Jun 2, 2023
bd8539f
Fix cache loading logic
ssolson Jun 2, 2023
1fa7d07
temp disable plots
ssolson Jun 2, 2023
b9d9a50
static key
ssolson Jun 2, 2023
70b4ea4
remove py 3.7 bc dolfyn failing on get
ssolson Jun 5, 2023
c822493
wind with cache
ssolson Jun 27, 2023
9da366e
add xarray requirement back
ssolson Jun 29, 2023
093d74d
cache in mhkit folder
ssolson Jun 29, 2023
665d336
change cache path
ssolson Jun 29, 2023
e140538
add wind hindcast to initial cache
ssolson Jun 30, 2023
29f2c75
data[0] check
ssolson Jul 6, 2023
6235bf7
Merge branch 'tests' of https://github.com/ssolson/MHKiT-Python into …
ssolson Jul 6, 2023
8b56f42
Cache not persistant across different OS
ssolson Jul 6, 2023
204662a
use artifacts
ssolson Jul 10, 2023
9445d49
catch the write_json flag on cache true
ssolson Jul 28, 2023
87fc154
small updates
ssolson Aug 4, 2023
49ba7e3
Merge branch 'develop' of https://github.com/MHKiT-Software/MHKiT-Pyt…
ssolson Aug 7, 2023
cadb8d1
docstring
ssolson Aug 7, 2023
492a515
pep8 cleanup
ssolson Aug 7, 2023
5bc77db
latest
ssolson Aug 11, 2023
578e468
Merge branch 'develop' of https://github.com/ssolson/MHKiT-Python int…
ssolson Aug 16, 2023
adada4b
remove debug
ssolson Aug 17, 2023
5125cbf
Merge branch 'develop' of https://github.com/ssolson/MHKiT-Python int…
ssolson Aug 17, 2023
ff4b949
clear_cache function
ssolson Aug 17, 2023
e428724
fix cache_dir
ssolson Aug 17, 2023
3dd2b01
comment
ssolson Aug 17, 2023
47995cb
remove cache_cdip as seperate function
ssolson Aug 17, 2023
6762784
remove _json_to_dataframe
ssolson Aug 18, 2023
21245bc
test cache
ssolson Aug 18, 2023
2cd085c
adjust clear_cache
ssolson Aug 18, 2023
c321540
handle not dt index
ssolson Aug 18, 2023
9f8d712
cache available data
ssolson Aug 18, 2023
e1c2e9f
hindcast use handle_cache
ssolson Aug 25, 2023
f67a6ff
fix extension issues
ssolson Aug 31, 2023
c85b0ec
Fix file docstring
ssolson Sep 18, 2023
c59647d
lint
ssolson Sep 18, 2023
9c12fda
type checks
ssolson Sep 18, 2023
f3118fd
ndbc working
ssolson Sep 18, 2023
d9f5f3a
Merge branch 'develop' of https://github.com/MHKiT-Software/MHKiT-Pyt…
ssolson Sep 18, 2023
6b8f288
clean up
ssolson Sep 18, 2023
eb7b8e9
lint
ssolson Sep 19, 2023
ed751bb
fix deprecation & lint
ssolson Sep 19, 2023
dd337d8
check version for backward compatability
ssolson Sep 20, 2023
9d4fc1f
increase tests runs on python versions & platforms
ssolson Sep 21, 2023
28fb6a0
remove py 3.7
ssolson Sep 22, 2023
cfa4c27
update read_json to handle cache json format
ssolson Sep 25, 2023
a7aaf92
remove unused imports
ssolson Sep 25, 2023
5eff214
cache docstring
ssolson Sep 26, 2023
f68e1c1
get_cache_dir -> _get_cache_dir
ssolson Sep 26, 2023
1325ee4
remove unused imports
ssolson Sep 26, 2023
aa3c355
author, date, time
ssolson Sep 26, 2023
0da335a
assert to exception
ssolson Sep 26, 2023
04fabf1
docstring for `get_region data`
ssolson Sep 26, 2023
a36784c
use f-string
ssolson Sep 26, 2023
d7d68f1
lint docstring
ssolson Sep 26, 2023
ab9c568
use handle_cacheing on wtk
ssolson Sep 26, 2023
2f1af83
asserts -> exception
ssolson Sep 26, 2023
666b53f
assert -> exception
ssolson Sep 26, 2023
0198b16
file docstring
ssolson Sep 26, 2023
8f82e88
f-string
ssolson Sep 26, 2023
7883b6f
only date
ssolson Sep 27, 2023
e2e55d9
comment clean up
ssolson Sep 27, 2023
2f91da2
docstring notebook correction
ssolson Oct 12, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 78 additions & 23 deletions .github/workflows/main.yml
akeeste marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Py 3.7 3.8, 3.9 | Windows Mac Linux
name: Py 3.8, 3.9 | Windows Mac Linux

on:
push:
Expand All @@ -9,16 +9,63 @@ on:
branches:
- master
- develop

jobs:
prepare-cache:
runs-on: ubuntu-latest
env:
PYTHON_VER: 3.9
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup Conda
uses: s-weigand/setup-conda@v1
with:
activate-conda: false
conda-channels: conda-forge

- name: Python setup
shell: bash -l {0}
run: |
conda create --name TEST python=${{ env.PYTHON_VER }} numpy cython pip hdf5 libnetcdf cftime netcdf4 --strict-channel-priority
source activate TEST
export PATH="${CONDA_PREFIX}/bin:${CONDA_PREFIX}/Library/bin:$PATH" # so setup.py finds nc-config
pip install -e . --no-deps --force-reinstall

- name: Install dependencies
shell: bash -l {0}
run: |
source activate TEST
python -m pip install --upgrade pip wheel
pip install coverage pytest coveralls .

- name: Prepare data
run: |
# pytest tests/test_specific_file.py::TestClass::test_function
source activate TEST
pytest mhkit/tests/river/test_io.py
pytest mhkit/tests/tidal/test_io.py
pytest mhkit/tests/wave/io/test_cdip.py
pytest mhkit/tests/wave/io/hindcast/test_hindcast.py
pytest mhkit/tests/wave/io/hindcast/test_wind_toolkit.py

- name: Upload data as artifact
uses: actions/upload-artifact@v2
with:
name: data
path: ~/.cache/mhkit

conda-build:
name: conda-${{ matrix.os }}/${{ matrix.python-version }}
needs: [prepare-cache]
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: ["windows-latest", "ubuntu-latest", "macos-latest"]
python-version: [3.7, 3.8, 3.9]
python-version: [3.8, 3.9]
env:
PYTHON_VER: ${{ matrix.python-version }}

steps:
- uses: actions/checkout@v2
Expand All @@ -32,20 +79,23 @@ jobs:
- name: Python ${{ matrix.python-version }}
shell: bash -l {0}
run: |
conda create --name TEST python=${{ matrix.python-version }} numpy cython pip pytest hdf5 libnetcdf cftime netcdf4 --strict-channel-priority
conda create --name TEST python=${PYTHON_VER} numpy cython pip hdf5 libnetcdf cftime netcdf4 --strict-channel-priority
source activate TEST
export PATH="${CONDA_PREFIX}/bin:${CONDA_PREFIX}/Library/bin:$PATH" # so setup.py finds nc-config
pip install -e . --no-deps --force-reinstall

- name: Tests
- name: Install dependencies
shell: bash -l {0}
run: |
source activate TEST
python -m pip install --upgrade pip wheel
pip install coverage
pip install pytest
pip install coveralls
pip install .
pip install coverage pytest coveralls .

- name: Download data from artifact
uses: actions/download-artifact@v2
with:
name: data
path: ~/.cache/mhkit

- name: Run pytest
shell: bash -l {0}
Expand All @@ -65,6 +115,7 @@ jobs:

pip-build:
name: pip-${{ matrix.os }}/${{ matrix.python-version }}
needs: [prepare-cache]
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
Expand All @@ -83,13 +134,16 @@ jobs:
- name: Set up Git repository
uses: actions/checkout@v2

- name: Download data from artifact
uses: actions/download-artifact@v2
with:
name: data
path: ~/.cache/mhkit

- name: Update and install packages
run: |
python -m pip install --upgrade pip wheel
pip install coverage
pip install pytest
pip install coveralls
pip install .
pip install coverage pytest coveralls .

- name: Run pytest
run: |
Expand All @@ -104,13 +158,14 @@ jobs:

hindcast-calls:
name: hindcast-${{ matrix.os }}/${{ matrix.python-version }}
needs: [prepare-cache]
runs-on: ${{ matrix.os }}
strategy:
max-parallel: 1
fail-fast: false
matrix:
os: ["windows-latest", "macos-latest"]
python-version: [3.9]
os: ["windows-latest", "macos-latest", "ubuntu-latest"]
python-version: [3.8, 3.9]

steps:
- uses: actions/checkout@v2
Expand All @@ -129,18 +184,18 @@ jobs:
export PATH="${CONDA_PREFIX}/bin:${CONDA_PREFIX}/Library/bin:$PATH" # so setup.py finds nc-config
pip install -e . --no-deps --force-reinstall

- name: Install MHKiT
shell: bash -l {0}
run: |
source activate TEST
python -m pip install --upgrade pip wheel
pip install coveralls
pip install .
- name: Download data from artifact
uses: actions/download-artifact@v2
with:
name: data
path: ~/.cache/mhkit

- name: Run pytest
- name: Install MHKiT and run pytest
shell: bash -l {0}
run: |
source activate TEST
python -m pip install --upgrade pip wheel
pip install coveralls .
coverage run --rcfile=.github/workflows/.coveragehindcastrc -m pytest -c .github/workflows/pytest-hindcast.ini

- name: Upload coverage data to coveralls.io
Expand Down
98 changes: 66 additions & 32 deletions mhkit/river/io/usgs.py
akeeste marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import pandas as pd
import numpy as np
import os
import json
import requests
import shutil
import pandas as pd
from mhkit.utils.cache import handle_caching


def _read_usgs_json(text):

data = pd.DataFrame()
for i in range(len(text['value']['timeSeries'])):
try:
site_name = text['value']['timeSeries'][i]['variable']['variableDescription'] #text['value']['timeSeries'][i]['sourceInfo']['siteName']
site_data = pd.DataFrame(text['value']['timeSeries'][i]['values'][0]['value'])
site_name = text['value']['timeSeries'][i]['variable']['variableDescription']
site_data = pd.DataFrame(
text['value']['timeSeries'][i]['values'][0]['value'])
site_data.set_index('dateTime', drop=True, inplace=True)
site_data.index = pd.to_datetime(site_data.index, utc=True)
site_data.rename(columns={'value': site_name}, inplace=True)
Expand All @@ -19,8 +23,9 @@ def _read_usgs_json(text):
data = data.combine_first(site_data)
except:
pass

return data # we could also extract metadata and return that here

return data


def read_usgs_file(file_name):
"""
Expand All @@ -30,7 +35,7 @@ def read_usgs_file(file_name):
----------
file_name : str
Name of USGS JSON data file

Returns
-------
data : pandas DataFrame
Expand All @@ -39,18 +44,25 @@ def read_usgs_file(file_name):
"""
with open(file_name) as json_file:
text = json.load(json_file)

data = _read_usgs_json(text)
return data

return data


def request_usgs_data(station, parameter, start_date, end_date,
data_type='Daily', proxy=None, write_json=None):
def request_usgs_data(
station,
parameter,
start_date,
end_date,
data_type='Daily',
proxy=None,
write_json=None,
clear_cache=False):
"""
Loads USGS data directly from https://waterdata.usgs.gov/nwis using a
GET request

The request URL prints to the screen.

Parameters
Expand All @@ -71,36 +83,58 @@ def request_usgs_data(station, parameter, start_date, end_date,
for example {"http": 'localhost:8080'}
write_json : str or None
Name of json file to write data

clear_cache : bool
If True, the cache for this specific request will be cleared.

Returns
-------
data : pandas DataFrame
Data indexed by datetime with columns named according to the parameter's
variable description
"""
assert data_type in ['Daily', 'Instantaneous'], 'data_type must be Daily or Instantaneous'

assert data_type in [
'Daily', 'Instantaneous'], 'data_type must be Daily or Instantaneous'

# Define the path to the cache directory
cache_dir = os.path.join(os.path.expanduser("~"),
".cache", "mhkit", "usgs")

# Create a unique filename based on the function parameters
hash_params = f"{station}_{parameter}_{start_date}_{end_date}_{data_type}"

# Use handle_caching to manage cache
cached_data, metadata, cache_filepath = handle_caching(
hash_params, cache_dir, write_json, clear_cache)

if cached_data is not None:
return cached_data

# If no cached data, proceed with the API request
if data_type == 'Daily':
data_url = 'https://waterservices.usgs.gov/nwis/dv'
api_query = '/?format=json&sites='+station+ \
'&startDT='+start_date+'&endDT='+end_date+ \
'&statCd=00003'+ \
api_query = '/?format=json&sites='+station + \
'&startDT='+start_date+'&endDT='+end_date + \
'&statCd=00003' + \
'&parameterCd='+parameter+'&siteStatus=all'
else:
data_url = 'https://waterservices.usgs.gov/nwis/iv'
api_query = '/?format=json&sites='+station+ \
'&startDT='+start_date+'&endDT='+end_date+ \
api_query = '/?format=json&sites='+station + \
'&startDT='+start_date+'&endDT='+end_date + \
'&parameterCd='+parameter+'&siteStatus=all'

print('Data request URL: ', data_url+api_query)
response = requests.get(url=data_url+api_query,proxies=proxy)

response = requests.get(url=data_url+api_query, proxies=proxy)
text = json.loads(response.text)

if write_json is not None:
with open(write_json, 'w') as outfile:
json.dump(text, outfile)


data = _read_usgs_json(text)

return data

# After making the API request and processing the response, write the
# response to a cache file
handle_caching(hash_params, cache_dir, data=data,
clear_cache_file=clear_cache)

if write_json:
shutil.copy(cache_filepath, write_json)

return data
Loading
Loading