Skip to content

Commit

Permalink
Merge branch 'PolicyEngine:master' into parameternode-274
Browse files Browse the repository at this point in the history
  • Loading branch information
SylviaDu99 authored Oct 20, 2024
2 parents 5e8bc8b + f280ca5 commit b9352b1
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 12 deletions.
33 changes: 33 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,35 @@ 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).

## [3.10.0] - 2024-10-17 15:59:10

### Added

- Two tests related to extensions that were previously removed

### Changed

- Shallow copy GroupEntities and PopulationEntity when cloning TaxBenefitSystem object

## [3.9.0] - 2024-10-09 20:29:35

### Changed

- Shallow copy entities between TaxBenefitSystem objects when cloning

## [3.8.2] - 2024-10-01 19:33:10

### Changed

- Updated README.md

## [3.8.1] - 2024-09-27 10:43:12

### Changed

- Set test runner's default period as underlying simulation's default period
- Prevented crashing when absolutely no date is provided anywhere for tests

## [3.8.0] - 2024-09-26 12:00:25

### Added
Expand Down Expand Up @@ -853,6 +882,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0



[3.10.0]: https://github.com/PolicyEngine/policyengine-core/compare/3.9.0...3.10.0
[3.9.0]: https://github.com/PolicyEngine/policyengine-core/compare/3.8.2...3.9.0
[3.8.2]: https://github.com/PolicyEngine/policyengine-core/compare/3.8.1...3.8.2
[3.8.1]: https://github.com/PolicyEngine/policyengine-core/compare/3.8.0...3.8.1
[3.8.0]: https://github.com/PolicyEngine/policyengine-core/compare/3.7.1...3.8.0
[3.7.1]: https://github.com/PolicyEngine/policyengine-core/compare/3.7.0...3.7.1
[3.7.0]: https://github.com/PolicyEngine/policyengine-core/compare/3.6.6...3.7.0
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ format:

install:
pip install -e .[dev]
pip install policyengine-us
pip install policyengine-uk

test-country-template:
policyengine-core test policyengine_core/country_template/tests -c policyengine_core.country_template
Expand Down
108 changes: 108 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,111 @@
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

This package, a fork of [OpenFisca-Core](https://github.com/OpenFisca/OpenFisca-Core), powers PolicyEngine country models and apps.

# Prerequisites

Python 3.10 or beyond is required.

# Setting Up

## 1. Fork the repo

```
https://github.com/PolicyEngine/policyengine-core/fork
```

## 2. Clone your own fork

## 3. Install dependencies

```
make install
```

If you are using Windows (not recommended), please install `make` first.
You can either directly download from [Make for Windows](https://gnuwin32.sourceforge.net/packages/make.htm);
or install [Chocolatey](https://chocolatey.org/install), and simply install `make` with

```
choco install make
```

(See **Acknowledgements** for credit)

# Contributing

## Choosing an Issue

All of our code changes are made against a GitHub issue. If you're new to the project, go to **Issues** and search for good first issues `label: "good first issue"`. If you see an open issue that no one's opened a PR against, it's all yours! Feel free to make some edits, then open a PR, as described below.

## Developing

Keep your fork's `master` branch in sync with the original repo by pulling the original repo's code at times; typically (if the original repo is called "upstream" by Git) this means running `git pull upstream master`, then `git push origin master` to sync the code to your local repo.

Create branches on your fork off of your master or main branch. Periodically, if you're working on something for a while, you might also run `git rebase master` within your feature branch to sync your code with any new changes.

## Testing, Formatting, Changelogging

You've finished your contribution, but now what? Before opening a PR, we ask contributors to do three things.

### Step 1: Testing

To test your changes against our series of automated tests, run

```
make test
```

We also ask that you add tests for any new features or bug-fixes you add, so we can gradually build up the code coverage. Our tests are written in the Python standard, [Pytest](https://docs.pytest.org/en/7.1.x/getting-started.html), and will be run again against the production environment, as well.

### Step 2: Formatting

In addition to the tests, we use [Black](https://github.com/psf/black) to lint our codebase, so before opening a pull request, Step 2 is to lint the code by running

```
make format
```

This will automatically format the code for you; no need to do anything else.

### Step 3: Changelogging

Finally, we ask contributors to make it clear for our users what changes have been made by contributing to a changelog. This changelog is formatted in YAML and describes the changes you've made to the code. This should follow the below format:

```
- bump: {major, minor, patch}
changes:
{added, removed, changed, fixed}:
- <variable or program>
```

For more info on the syntax, check out the [semantic versioning docs](https://www.semver.org) and [keep a changelog](https://www.keepachangelog.com).

Write your changelog info into the empty file called `changelog_entry.yaml`. When you open your PR, this will automatically be added to the overall changelog.

## Opening a Pull Request

Now you've finished your contribution! Please open a pull request (PR) from your branch to the live `master` branch and request review. At times, it may take some time for the team to review your PR, especially for larger contributions, so please be patient--we will be sure to get to it.

In the first line of your PR, please make sure to include the following:

```
Fixes {issue_number}
```

This makes it much easier for us to maintain and prune our issue board.

Please try to be detailed in your PRs about the changes you made and why you made them. You may find yourself looking back at them for reference in the future, or needing insight about someone else's changes. Save yourself a conversation and write it all in the PR!

Here are some [best practices](https://deepsource.io/blog/git-best-practices/) for using Git.

When you're ready for review, switch the PR from `Draft` to `Ready for review`.

# License

Distributed under the AGPL License. See `LICENSE` for more info.

# Acknowledgements

- Thanks to Othneil Drew for his [README template](https://github.com/othneildrew/Best-README-Template).
- [Installing make on Windows](https://stackoverflow.com/questions/32127524/how-to-install-and-use-make-in-windows)
24 changes: 24 additions & 0 deletions changelog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -687,3 +687,27 @@
- Randomness based on entity IDs as seeds.
- OpenFisca-Core imports.
date: 2024-09-26 12:00:25
- bump: patch
changes:
changed:
- Set test runner's default period as underlying simulation's default period
- Prevented crashing when absolutely no date is provided anywhere for tests
date: 2024-09-27 10:43:12
- bump: patch
changes:
changed:
- Updated README.md
date: 2024-10-01 19:33:10
- bump: minor
changes:
changed:
- Shallow copy entities between TaxBenefitSystem objects when cloning
date: 2024-10-09 20:29:35
- bump: minor
changes:
added:
- Two tests related to extensions that were previously removed
changed:
- Shallow copy GroupEntities and PopulationEntity when cloning TaxBenefitSystem
object
date: 2024-10-17 15:59:10
5 changes: 5 additions & 0 deletions policyengine_core/simulations/simulation_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)

from policyengine_core.variables import Variable
from datetime import datetime


class SimulationBuilder:
Expand Down Expand Up @@ -507,6 +508,10 @@ def add_group_entity(
def set_default_period(self, period_str: str) -> None:
if period_str:
self.default_period = str(periods.period(period_str))
else:
# If at absolute worst, no period is specified anywhere,
# use current year
self.default_period = periods.period(datetime.now().year)

def get_input(self, variable: str, period_str: str) -> Any:
if variable not in self.input_buffer:
Expand Down
15 changes: 15 additions & 0 deletions policyengine_core/taxbenefitsystems/tax_benefit_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,9 @@ def clone(self) -> "TaxBenefitSystem":
"parameters",
"_parameters_at_instant_cache",
"variables",
"entities",
"person_entity",
"group_entities",
):
new_dict[key] = value

Expand All @@ -676,8 +679,20 @@ def clone(self) -> "TaxBenefitSystem":
for variable_name, variable in self.variables.items()
}

# Apply shallow copies to all relevant entities
new_dict["entities"] = [copy.copy(entity) for entity in self.entities]
new_dict["person_entity"] = copy.copy(self.person_entity)
new_dict["group_entities"] = [
copy.copy(entity) for entity in self.group_entities
]

# For all shallow-copied entities, set entity._tax_benefit_system to the new system
for entity in new_dict["entities"]:
entity.set_tax_benefit_system(new)
for entity in new_dict["group_entities"]:
entity.set_tax_benefit_system(new)
new_dict["person_entity"].set_tax_benefit_system(new)

return new

def entities_plural(self) -> dict:
Expand Down
1 change: 1 addition & 0 deletions policyengine_core/tools/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def apply(self):
self.simulation = builder.build_from_dict(
self.tax_benefit_system, input
)
self.simulation.default_calculation_period = builder.default_period
except (VariableNotFoundError, SituationParsingError):
raise
except Exception as e:
Expand Down
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,14 @@

setup(
name="policyengine-core",
version="3.8.0",
version="3.10.0",
author="PolicyEngine",
author_email="[email protected]",
classifiers=[
"Development Status :: 5 - Production/Stable",
"License :: OSI Approved :: GNU Affero General Public License v3",
"Operating System :: POSIX",
"Programming Language :: Python",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
Expand All @@ -73,7 +72,7 @@
"policyengine-core=policyengine_core.scripts.policyengine_command:main",
],
},
python_requires=">=3.9",
python_requires=">=3.10",
extras_require={
"dev": dev_requirements,
},
Expand Down
27 changes: 19 additions & 8 deletions test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
"from policyengine_uk import Microsimulation\n",
"from policyengine_core.reforms import Reform\n",
"\n",
"reform = Reform.from_dict({\n",
" \"gov.hmrc.vat.standard_rate\": {\n",
" \"2024-01-01.2100-12-31\": 0.22\n",
" }\n",
"}, country_id=\"uk\")\n",
"reform = Reform.from_dict(\n",
" {\"gov.hmrc.vat.standard_rate\": {\"2024-01-01.2100-12-31\": 0.22}},\n",
" country_id=\"uk\",\n",
")\n",
"\n",
"\n",
"baseline = Microsimulation()\n",
Expand Down Expand Up @@ -459,11 +458,23 @@
" \"diesel_spending\",\n",
" \"domestic_energy_consumption\",\n",
"]\n",
"baseline_income = baseline.calculate(\"household_net_income\", period=2024, map_to=\"person\")\n",
"reformed_income = reformed.calculate(\"household_net_income\", period=2024, map_to=\"person\")\n",
"baseline_income = baseline.calculate(\n",
" \"household_net_income\", period=2024, map_to=\"person\"\n",
")\n",
"reformed_income = reformed.calculate(\n",
" \"household_net_income\", period=2024, map_to=\"person\"\n",
")\n",
"difference_income = reformed_income - baseline_income\n",
"\n",
"baseline.calculate_dataframe([\"age\", \"vat\", \"consumption\", \"full_rate_vat_expenditure_rate\", *IMPUTATIONS])[difference_income > 0]"
"baseline.calculate_dataframe(\n",
" [\n",
" \"age\",\n",
" \"vat\",\n",
" \"consumption\",\n",
" \"full_rate_vat_expenditure_rate\",\n",
" *IMPUTATIONS,\n",
" ]\n",
")[difference_income > 0]"
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/yaml_tests/test_with_extension.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
parents: [parent]
children: [child1, child2]
output:
local_town_child_allowance: 200
local_town_child_allowance: 200
7 changes: 7 additions & 0 deletions tests/test_us.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def test_policyengine_us_microsimulation_runs():
from policyengine_us import Microsimulation

baseline = Microsimulation()
baseline.subsample(100)

baseline.calculate("household_net_income")

0 comments on commit b9352b1

Please sign in to comment.