-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from samgdotson/save_local
Save data locally on first execution
- Loading branch information
Showing
12 changed files
with
328 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[run] | ||
branch = True | ||
source = nrelpy | ||
omit = | ||
nrelpy/tests/* | ||
nrelpy/__init__.py | ||
nrelpy/**/__init__.py | ||
setup.py | ||
nrelpy/version.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ __pycache__/ | |
|
||
# Distribution / packaging | ||
.Python | ||
data/ | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
from nrelpy.utils.data_io import save_local, check_stored_data, DATA_PATH | ||
from pathlib import Path | ||
import sys | ||
import os | ||
import glob | ||
import pandas as pd | ||
|
||
# set up test data | ||
data = {'tech': ['nuclear', 'solar', 'wind', 'naturalgas'], | ||
'variable_cost': [20, 0, 0, 180], # $/GWh | ||
'fixed_cost': [92, 4, 11, 21], | ||
'capital_cost': [5.9, 0.8, 1.4, 1.0], | ||
'capacity_GW': [12, 3, 7, 5], | ||
'capacity_factor': [0.93, 0.17, 0.33, 0.45], | ||
'resentment': [100, 20, 50, 70] | ||
} | ||
tech_df = pd.DataFrame(data) | ||
|
||
db = 'electricity' | ||
yr = 1882 | ||
|
||
data_path_exists = DATA_PATH.exists() | ||
|
||
user_path = DATA_PATH / 'tmp_path' | ||
user_path.mkdir(exist_ok=True, parents=True) | ||
|
||
|
||
def test_save_local_case1(): | ||
""" | ||
This tests the standard use case of `save_local` | ||
where a dataframe, database type, and year are passed. | ||
""" | ||
save_local(tech_df, database=db, year=yr) | ||
file_name = str(DATA_PATH / f'ATBe_{yr}.pkl') | ||
files = glob.glob(file_name) | ||
os.remove(file_name) | ||
assert len(files) == 1 | ||
return | ||
|
||
def test_save_local_case2(): | ||
""" | ||
This tests a standard use case of `save_local` | ||
where a dataframe, database type, and year are passed. | ||
The dataframe is saved as a CSV instead of pickled. | ||
""" | ||
save_local(tech_df, database=db, year=yr, pickle=False) | ||
file_name = str(DATA_PATH / f'ATBe_{yr}.csv') | ||
files = glob.glob(file_name) | ||
os.remove(file_name) | ||
assert len(files) == 1 | ||
return | ||
|
||
|
||
def test_save_local_case3(): | ||
""" | ||
This tests a standard use case of `save_local` | ||
where a dataframe, database type, and year are passed. | ||
The dataframe is saved to a user specified directory. | ||
""" | ||
save_local(tech_df, database=db, year=yr, pickle=True, path=user_path) | ||
file_name = str(user_path / f'ATBe_{yr}.pkl') | ||
files = glob.glob(file_name) | ||
os.remove(file_name) | ||
assert len(files) == 1 | ||
return | ||
|
||
|
||
def test_check_stored_data_case1(): | ||
""" | ||
This tests a standard use case of `check_stored_data` | ||
where a database type and a year are passed. | ||
""" | ||
save_local(tech_df, database=db, year=yr) | ||
df = check_stored_data(database=db, year=yr) | ||
file_name_yr = str(DATA_PATH / f'ATBe_{yr}.pkl') | ||
os.remove(file_name_yr) | ||
assert df.equals(tech_df) | ||
return | ||
|
||
|
||
def test_check_stored_data_case2(): | ||
""" | ||
This tests a standard use case of `check_stored_data` | ||
where a database type and a year are passed. | ||
""" | ||
save_local(tech_df, database=db) | ||
df = check_stored_data(database=db) | ||
file_name_no_yr = str(DATA_PATH / f'ATBe.pkl') | ||
os.remove(file_name_no_yr) | ||
assert df.equals(tech_df) | ||
return | ||
|
||
|
||
def test_check_stored_data_case3(): | ||
""" | ||
This tests saving and reading the data in a csv format. | ||
""" | ||
save_local(tech_df, database=db, pickle=False) | ||
df = check_stored_data(database=db, pickled=False) | ||
file_name_no_yr = str(DATA_PATH / f'ATBe.csv') | ||
os.remove(file_name_no_yr) | ||
assert df.equals(tech_df) | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,42 @@ | ||
from nrelpy.re_potential import * | ||
import pytest | ||
import pandas as pd | ||
|
||
|
||
def test_as_dataframe(): | ||
def test_as_dataframe_standard(): | ||
""" | ||
This tests the base case where no | ||
arguments are passed. | ||
""" | ||
|
||
df = as_dataframe() | ||
|
||
assert type(df) == pd.DataFrame | ||
|
||
return | ||
|
||
|
||
def test_as_dataframe_bad_url(): | ||
""" | ||
This tests the base case where a bad | ||
url is passed. | ||
""" | ||
|
||
bad_url = "https://www.nrel.gov/gis/assets/docs/us-re-technical-potential" | ||
with pytest.raises(HTTPError) as e: | ||
df = as_dataframe(url=bad_url) | ||
|
||
return | ||
|
||
|
||
@pytest.mark.filterwarnings("ignore") | ||
def test_as_dataframe_verbose(): | ||
""" | ||
This tests the verbosity setting of | ||
""" | ||
|
||
df = as_dataframe(verbose=True) | ||
|
||
assert type(df) == pd.DataFrame | ||
|
||
return |
Empty file.
Oops, something went wrong.