Skip to content

Commit

Permalink
Bug fixes (#282)
Browse files Browse the repository at this point in the history
* Fix Make `random(entity)` deterministic based on entity IDs #280

* Fix Subsampling doesn't preserve `Dataset.time_period` #281

* Fix Remove openfisca_core dependencies #259

* Versioning

* Remove Windows test

* Remove Windows CI
  • Loading branch information
nikhilwoodruff authored Sep 26, 2024
1 parent 2e177cc commit 62c11ad
Show file tree
Hide file tree
Showing 8 changed files with 549 additions and 29 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ jobs:
- name: Check version number has been properly updated
run: .github/is-version-number-acceptable.sh
Test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,10 @@ jobs:
author_name: Github Actions[bot]
message: Update PolicyEngine Core
Test:
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest
if: |
(github.repository == 'PolicyEngine/policyengine-core')
&& (github.event.head_commit.message == 'Update PolicyEngine Core')
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
steps:
- name: Checkout repo
uses: actions/checkout@v3
Expand Down
5 changes: 5 additions & 0 deletions changelog_entry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- bump: minor
changes:
added:
- Randomness based on entity IDs as seeds.
- OpenFisca-Core imports.
48 changes: 39 additions & 9 deletions policyengine_core/commons/formulas.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,15 +300,45 @@ def amount_between(
return clip(amount, threshold_1, threshold_2) - threshold_1


def random(entity, reset=True):
if reset:
np.random.seed(0)
x = np.random.rand(entity.count)
if entity.simulation.has_axes:
# Generate the same random number for each entity.
random_number = x[0]
return np.array([random_number] * entity.count)
return x
def random(population):
"""
Generate random values for each entity in the population.
Args:
population: The population object containing simulation data.
Returns:
np.ndarray: Array of random values for each entity.
"""
# Initialize count of random calls if not already present
if not hasattr(population.simulation, "count_random_calls"):
population.simulation.count_random_calls = 0
population.simulation.count_random_calls += 1

# Get known periods or use default calculation period
known_periods = population.simulation.get_holder(
f"{population.entity.key}_id"
).get_known_periods()
period = (
known_periods[0]
if known_periods
else population.simulation.default_calculation_period
)

# Get entity IDs for the period
entity_ids = population(f"{population.entity.key}_id", period)

# Generate random values for each entity
values = np.array(
[
np.random.default_rng(
seed=id * 100 + population.simulation.count_random_calls
).random()
for id in entity_ids
]
)

return values


def is_in(values: ArrayLike, *targets: list) -> ArrayLike:
Expand Down
2 changes: 1 addition & 1 deletion policyengine_core/simulations/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1547,7 +1547,7 @@ def subsample(
)

# Update the dataset and rebuild the simulation
self.dataset = Dataset.from_dataframe(df)
self.dataset = Dataset.from_dataframe(df, self.dataset.time_period)
self.build_from_dataset()
return self

Expand Down
8 changes: 4 additions & 4 deletions policyengine_core/variables/defined_for.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import numpy as np
from numpy.typing import ArrayLike
from openfisca_core.entities import Entity
from openfisca_core.populations import GroupPopulation, Population
from openfisca_core.projectors import EntityToPersonProjector, Projector
from openfisca_core.variables import Variable
from policyengine_core.entities import Entity
from policyengine_core.populations import GroupPopulation, Population
from policyengine_core.projectors import EntityToPersonProjector, Projector
from policyengine_core.variables import Variable


class CallableSubset:
Expand Down
14 changes: 7 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@
general_requirements = [
"pytest>=8,<9",
"numpy~=1.26.4",
"black",
"linecheck<1",
"yaml-changelog<1",
"coverage",
"sortedcontainers<3",
"numexpr<3",
"dpath<3",
"psutil<6",
"wheel<1",
"h5py>=3,<4",
"requests>=2.27.1,<3",
"requests>=2,<3",
"pandas>=1",
"plotly>=5.6.0,<6",
"ipython>=7.17.0,<8",
"plotly>=5,<6",
"ipython>=7,<8",
"pyvis>=0.3.2",
]

dev_requirements = [
"black",
"linecheck<1",
"jupyter-book<1",
"yaml-changelog<1",
"coverage",
"furo<2023",
"markupsafe==2.0.1",
"coverage",
Expand Down
Loading

0 comments on commit 62c11ad

Please sign in to comment.