Skip to content

Commit

Permalink
Merge pull request #938 from jdebacker/more_demog
Browse files Browse the repository at this point in the history
Fix to `demographics.py` for case of no token prompt
  • Loading branch information
jdebacker authored Jun 12, 2024
2 parents 4e6ae04 + 7255bbe commit c16cf28
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 62 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.11.9] - 2024-06-12 01:00:00

### Added

- Update `demographics.py` in the case input prompt not work.
- Add new utility to dump the parameters to a JSON file


## [0.11.8] - 2024-06-09 01:00:00

### Added
Expand Down
4 changes: 2 additions & 2 deletions docs/book/content/api/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ ogcore.utils
:members: init_poolmanager

.. automodule:: ogcore.utils
:members: mkdirs, pct_diff_func, convex_combo, read_file,
:members: mkdirs, pct_diff_func, convex_combo,
pickle_file_compare, comp_array, comp_scalar, dict_compare,
to_timepath_shape, get_initial_path, safe_read_pickle, rate_conversion,
save_return_table, print_progress, fetch_files_from_web, not_connected,
avg_by_bin, extrapolate_arrays, get_legacy_session, shift_bio_clock,
pct_change_unstationarized
pct_change_unstationarized, param_dump_json
2 changes: 1 addition & 1 deletion ogcore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
from ogcore.txfunc import *
from ogcore.utils import *

__version__ = "0.11.8"
__version__ = "0.11.9"
15 changes: 9 additions & 6 deletions ogcore/demographics.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,15 @@ def get_un_data(
with open(os.path.join("un_api_token.txt"), "r") as file:
UN_TOKEN = file.read().strip()
else: # if file not exist, prompt user for token
UN_TOKEN = input(
"Please enter your UN API token (press return if you do not have one): "
)
# write the UN_TOKEN to a file to find in the future
with open(os.path.join("un_api_token.txt"), "w") as file:
file.write(UN_TOKEN)
try:
UN_TOKEN = input(
"Please enter your UN API token (press return if you do not have one): "
)
# write the UN_TOKEN to a file to find in the future
with open(os.path.join("un_api_token.txt"), "w") as file:
file.write(UN_TOKEN)
except EOFError:
UN_TOKEN = ""

# get data from url
payload = {}
Expand Down
6 changes: 2 additions & 4 deletions ogcore/parameters.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import os
import numpy as np
import scipy.interpolate as si
import pkg_resources
import paramtools

# import ogcore
import ogcore
from ogcore import elliptical_u_est
from ogcore.utils import rate_conversion, extrapolate_arrays
from ogcore.constants import BASELINE_DIR
Expand Down Expand Up @@ -35,7 +33,7 @@ def __init__(
self.num_workers = num_workers

# put OG-Core version in parameters to save for reference
self.ogcore_version = pkg_resources.get_distribution("ogcore").version
self.ogcore_version = ogcore.__version__

# does cheap calculations to find parameter values
self.initialize()
Expand Down
68 changes: 43 additions & 25 deletions ogcore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
from zipfile import ZipFile
import urllib
from tempfile import NamedTemporaryFile
from io import BytesIO, StringIO
from io import BytesIO
import numpy as np
import pandas as pd
from scipy.interpolate import CubicSpline
import pickle
from pkg_resources import resource_stream, Requirement
import importlib.resources
import urllib3
import ssl
import json

EPSILON = 1e-10 # tolerance or comparison functions

Expand Down Expand Up @@ -78,28 +77,6 @@ def convex_combo(var1, var2, nu):
return combo


def read_file(path, fname):
"""
Read the contents of 'path'. If it does not exist, assume the file
is installed in a .egg file, and adjust accordingly.
Args:
path (str): path name for new directory
fname (str): filename
Returns:
file contents (str)
"""

if not os.path.exists(os.path.join(path, fname)):
buf = resource_stream("ogcore", fname)
_bytes = buf.read()
return StringIO(_bytes.decode("utf-8"))
else:
return open(os.path.join(path, fname))


def pickle_file_compare(
fname1, fname2, tol=1e-3, exceptions={}, relative=False
):
Expand Down Expand Up @@ -1298,3 +1275,44 @@ def pct_change_unstationarized(
)

return pct_changes


def param_dump_json(p, path=None):
"""
This function creates a JSON file with the model parameters of the
format used for the default_parameters.json file.
Args:
p (OG-Core Specifications class): model parameters object
path (string): path to save JSON file to
Returns:
JSON (string): JSON on model parameters
"""
converted_data = {}
spec = p.specification(
meta_data=False, include_empty=True, serializable=True, use_state=True
)
for key in p.keys():
val = dict(spec[key][0])["value"]
if isinstance(val, np.ndarray):
converted_data[key] = val.tolist()
else:
converted_data[key] = val

# Parameters that need to be turned into annual rates for default_parameters.json
# g_y_annual
# beta_annual
# delta_annual
# delta_tau_annual
# delta_g_annual
# world_int_rate_annual

# Convert to JSON string
json_str = json.dumps(converted_data, indent=4)

if path is not None:
with open(path, "w") as f:
f.write(json_str)
else:
return json_str
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="ogcore",
version="0.11.8",
version="0.11.9",
author="Jason DeBacker and Richard W. Evans",
license="CC0 1.0 Universal (CC0 1.0) Public Domain Dedication",
description="A general equilibribum overlapping generations model for fiscal policy analysis",
Expand Down
1 change: 0 additions & 1 deletion tests/test_parameter_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Tests of parameter_plots.py module
"""

from tracemalloc import start
import pytest
import os
import sys
Expand Down
43 changes: 21 additions & 22 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,6 @@ def test_convex_combo():
assert np.allclose(expected, combo)


def test_read_file():
"""
Test of utils.read_file() function
"""
path = os.path.join(CUR_PATH, "test_io_data")
fname = "SS_fsolve_inputs.pkl"
bytes_data = utils.read_file(path, fname)

assert isinstance(bytes_data, io.TextIOWrapper)


def test_read_file_from_egg():
"""
Test of utils.read_file() function, case of reading file from .egg
"""
path = os.path.join(CUR_PATH)
fname = "default_parameters.json"
bytes_data = utils.read_file(path, fname)

assert isinstance(bytes_data, io.StringIO)


def test_pickle_file_compare():
"""
Test of utils.pickle_file_compare() function
Expand Down Expand Up @@ -947,3 +925,24 @@ def test_pct_change_unstationarized(p1, p2, expected):
for key in pct_change.keys():
print("Checking ", key)
assert np.allclose(pct_change[key], expected[key])


def test_param_dump_json():
"""
Test of the param_dump_json function
"""
p = Specifications()
j_str = utils.param_dump_json(p)
assert isinstance(j_str, str)


def test_param_dump_json_save(tmpdir):
"""
Test of the param_dump_json function
"""
p = Specifications()
utils.param_dump_json(p, path=os.path.join(tmpdir, "test.json"))
# read in file
with open(os.path.join(tmpdir, "test.json"), "r") as f:
j_str = f.read()
assert isinstance(j_str, str)

0 comments on commit c16cf28

Please sign in to comment.