Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
59 changes: 59 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Build Docker Image

on:
push:
branches: [main, dev, ci]
tags:
- 'v*'

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository_owner }}/demos

jobs:
publish:
name: Build and publish Docker image
runs-on: ubuntu-latest
if: github.event_name != 'pull_request' && github.actor != 'dependabot[bot]'
permissions:
contents: read
packages: write

steps:
- uses: actions/checkout@v4

- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Buildx
uses: docker/setup-buildx-action@v3

- name: Docker meta (tags)
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=sha,enable=${{ startsWith(github.ref, 'refs/tags/v') }}
type=ref,event=branch
type=semver,pattern={{version}},enable=${{ startsWith(github.ref, 'refs/tags/v') }}
type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') }}

- run: echo "${{ steps.meta.outputs.tags }}"
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
push: true
platforms: linux/amd64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
4 changes: 3 additions & 1 deletion demos/models/income_adjustment.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def income_adjustment(persons, households, income_rates, year):
start_time = time.time()
# TODO: CountyID is not being updated by default
# Update income according to county rate
income_rates['lcm_county_id'] = income_rates['lcm_county_id'].map(lambda x: f'{x:0>5}')
income_rates["lcm_county_id"] = income_rates["lcm_county_id"].map(
lambda x: f"{x:0>5}"
)

persons.local.earning *= (
1
Expand Down
29 changes: 20 additions & 9 deletions demos/models/kids_moving.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,14 @@ def run_and_calibrate_model(persons):
demos_config: DEMOSConfig = get_config()
module_config: KidsMovingModuleConfig = demos_config.kids_moving_module_config

child_relate = [2, 3, 4, 7, 9, 14] # This is more `dependent` because `child` is determined by age
child_relate = [
2,
3,
4,
7,
9,
14,
] # This is more `dependent` because `child` is determined by age
target_share = module_config.calibration_target_share

# Get model data
Expand All @@ -149,25 +156,29 @@ def run_and_calibrate_model(persons):
kids_moving = model.predict(model_data).astype(int)

# NOTE: This could be much easier if we set the age at 18 because we could use model_filters
adult_filter = (persons.age >= 18)
adult_filter = persons.age >= 18
age_moved = persons.age.loc[kids_moving[kids_moving == 1].index]
adult_stay = (adult_filter & persons.relate.isin(child_relate)).sum() - (age_moved >=18).sum()
adult_stay = (adult_filter & persons.relate.isin(child_relate)).sum() - (
age_moved >= 18
).sum()
observed_share = adult_stay / adult_filter.sum()
error = (observed_share - target_share)
error = observed_share - target_share

print("Calibrating Kids moving model")
calibrate_iteration = 0
while abs(error) > module_config.calibration_tolerance:
while abs(error) > module_config.calibration_tolerance:
print(f"{calibrate_iteration} iteration error: {error}")
model.fitted_parameters[0] += np.log(observed_share/target_share)
model.fitted_parameters[0] += np.log(observed_share / target_share)

kids_moving = model.predict(model_data).astype(int)
age_moved = persons.age.loc[kids_moving[kids_moving == 1].index]
adult_stay = (adult_filter & persons.relate.isin(child_relate)).sum() - (age_moved >=18).sum()
adult_stay = (adult_filter & persons.relate.isin(child_relate)).sum() - (
age_moved >= 18
).sum()
observed_share = adult_stay / adult_filter.sum()
error = (observed_share - target_share)
error = observed_share - target_share

calibrate_iteration += 1
print(f"{calibrate_iteration} iteration error: {error}")

return kids_moving
return kids_moving
9 changes: 4 additions & 5 deletions demos/models/marriage.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ def update_married_households_random(
module_config.geoid_col,
].values
households.local.loc[new_hh_ids, module_config.geoid_col] = new_hh_geoid
county_assignment = households.local.loc[all_df.loc[first_index & neither_head_index, "household_id"],
"lcm_county_id"].values
county_assignment = households.local.loc[
all_df.loc[first_index & neither_head_index, "household_id"], "lcm_county_id"
].values
households.local.loc[new_hh_ids, "lcm_county_id"] = county_assignment
## Decide who is household head in the households where the head left
head_left_index = (all_df.relate == 0) & (all_df.household_id != all_df.new_hh_id)
Expand Down Expand Up @@ -279,9 +280,7 @@ def update_divorce(persons, households, divorce_list, get_new_households):
geoid_assignment = households.local.loc[
old_household_id, module_config.geoid_col
].values
households.local.loc[new_households, module_config.geoid_col] = (
geoid_assignment
)
households.local.loc[new_households, module_config.geoid_col] = geoid_assignment
county_assignment = households.local.loc[old_household_id, "lcm_county_id"].values
households.local.loc[new_households, "lcm_county_id"] = county_assignment

Expand Down
1 change: 1 addition & 0 deletions demos/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ def marital34(persons):
p = persons.to_frame(columns=["MAR"])
return p.isin([3, 4]).astype(int)


######## NOTE: Not needed for now
# # PERSON VARIABLES
# # -----------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ services:
build:
context: .
dockerfile: Dockerfile
image: demos:0.0.1
image: ghcr.io/${GITHUB_REPOSITORY_OWNER:-nrel}/demos
tty: true
platform: linux/amd64
environment:
Expand Down