-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
simplify the selection of years in the training_pipeline #142
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
52d7058
simplify the selection of years in the training_pipeline
JialuJialu 141cc30
reformat and change Adam to ClippedAdam
JialuJialu c81e044
reformat
JialuJialu d59ad3b
reformat again
JialuJialu 78bebf2
Merge branch 'main' of https://github.com/BasisResearch/cities into j…
rfl-urbaniak 4256351
update gitignore
rfl-urbaniak 4c6cb8a
update scripts
rfl-urbaniak 51ce123
change back the optimizer and add back the downstream variable
JialuJialu 0870b85
reformat
JialuJialu 2c05ea0
fix type error
JialuJialu 8824f7f
fix unused variables
JialuJialu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -68,8 +68,8 @@ def prep_wide_data_for_inference( | |
4. Loads the required transformed features. | ||
5. Merges fixed covariates into a joint dataframe based on a common ID column. | ||
6. Ensures that the GeoFIPS (geographical identifier) is consistent across datasets. | ||
7. Extracts common years for which both intervention and outcome data are available. | ||
8. Shifts the outcome variable forward by the specified number of time steps. | ||
7. Shifts the outcome variable forward by the specified number of time steps determined by forward_shift. | ||
8. Extracts common years for which both intervention and outcome data are available. | ||
9. Prepares tensors for input features (x), interventions (t), and outcomes (y). | ||
10. Creates indices for states and units, preparing them as tensors. | ||
11. Validates the shapes of the tensors. | ||
|
@@ -125,49 +125,17 @@ def prep_wide_data_for_inference( | |
assert f_covariates_joint["GeoFIPS"].equals(intervention["GeoFIPS"]) | ||
|
||
# extract data for which intervention and outcome overlap | ||
year_min = max( | ||
intervention.columns[2:].astype(int).min(), | ||
outcome.columns[2:].astype(int).min(), | ||
) | ||
|
||
year_max = min( | ||
intervention.columns[2:].astype(int).max(), | ||
outcome.columns[2:].astype(int).max(), | ||
) | ||
|
||
assert all(intervention["GeoFIPS"] == outcome["GeoFIPS"]) | ||
|
||
outcome_years_to_keep = [ | ||
year | ||
for year in outcome.columns[2:] | ||
if year_min <= int(year) <= year_max + forward_shift | ||
] | ||
|
||
outcome_years_to_keep = [ | ||
year for year in outcome_years_to_keep if year in intervention.columns[2:] | ||
] | ||
|
||
outcome = outcome[outcome_years_to_keep] | ||
|
||
# shift outcome `forward_shift` steps ahead | ||
# for the prediction task | ||
outcome_shifted = outcome.copy() | ||
|
||
for i in range(len(outcome_years_to_keep) - forward_shift): | ||
outcome_shifted.iloc[:, i] = outcome_shifted.iloc[:, i + forward_shift] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The shifting could be completed in one step through renaming. |
||
years_to_drop = [ | ||
f"{year}" for year in range(year_max - forward_shift + 1, year_max + 1) | ||
] | ||
outcome_shifted.drop(columns=years_to_drop, inplace=True) | ||
|
||
outcome.drop(columns=["GeoFIPS", "GeoName"], inplace=True) | ||
intervention.drop(columns=["GeoFIPS", "GeoName"], inplace=True) | ||
intervention = intervention[outcome_shifted.columns] | ||
outcome_shifted = outcome.rename(lambda x: str(int(x) - forward_shift), axis=1) | ||
years_available = [ | ||
year for year in intervention.columns if year in outcome_shifted.columns | ||
] | ||
intervention = intervention[years_available] | ||
outcome_shifted = outcome_shifted[years_available] | ||
|
||
assert intervention.shape == outcome_shifted.shape | ||
|
||
years_available = outcome_shifted.columns.astype(int).values | ||
|
||
unit_index = pd.factorize(f_covariates_joint["GeoFIPS"].values)[0] | ||
state_index = pd.factorize(f_covariates_joint["GeoFIPS"].values // 1000)[0] | ||
|
||
|
@@ -197,6 +165,7 @@ def prep_wide_data_for_inference( | |
|
||
model_args = (N_t, N_cov, N_s, N_u, state_index, unit_index) | ||
|
||
int_year_available = [int(year) for year in years_available] | ||
return { | ||
"model_args": model_args, | ||
"x": x, | ||
|
@@ -222,7 +191,10 @@ def train_interactions_model( | |
guide = AutoNormal(conditioned_model) | ||
|
||
svi = SVI( | ||
model=conditioned_model, guide=guide, optim=Adam({"lr": lr}), loss=Trace_ELBO() | ||
model=conditioned_model, | ||
guide=guide, | ||
optim=ClippedAdam({"lr": lr}), | ||
loss=Trace_ELBO(), | ||
) | ||
|
||
losses = [] | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The line 106 to line 114 works the same as
outcome_years_to_keep = [year for year in outcome.columns[2:] if year in intervention.columns[2:]
It seems that the intent was to write
outcome_years_to_keep = [year for year in outcome.columns[2:] if year - forward_shift in intervention.columns[2:]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I think the issue was dropping a variable that was needed elsewhere.