-
Notifications
You must be signed in to change notification settings - Fork 21
Enable newer Python versions and use of own test data #9
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
Open
NinaWie
wants to merge
4
commits into
gengchenmai:master
Choose a base branch
from
NinaWie:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| # Project-specific | ||
| *code-workspace | ||
| model_dir/ | ||
| *.egg-info | ||
|
|
||
| # Compiled source # | ||
| ################### | ||
| *.com | ||
|
|
||
This file contains hidden or 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
10,006 changes: 10,006 additions & 0 deletions
10,006
spacegraph/data_collection/example_pois.geojson
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import argparse | ||
| import os | ||
| import json | ||
| import pickle | ||
| import numpy as np | ||
| import geopandas as gpd | ||
| from sklearn.metrics import pairwise_distances | ||
| from collections import defaultdict | ||
|
|
||
| from sklearn.neighbors import BallTree | ||
|
|
||
|
|
||
| def get_nearest(src_points, candidates, k_neighbors=10, remove_first=True): | ||
| """Find nearest neighbors for all source points from a set of candidate points""" | ||
| # Create tree from the candidate points | ||
| tree = BallTree(candidates, leaf_size=15, metric="euclidean") | ||
| # Find closest points and distances | ||
| distances, indices = tree.query( | ||
| src_points, k=k_neighbors + int(remove_first) | ||
| ) | ||
| # Return indices and distances | ||
| return (indices[:, remove_first:], distances[:, remove_first:]) | ||
|
|
||
|
|
||
| def get_ordered_unique(arr): | ||
| set_new, elems_new = set(), [] | ||
| for elem in arr: | ||
| if elem not in set_new: | ||
| set_new.add(elem) | ||
| elems_new.append(elem) | ||
| return elems_new | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument( | ||
| "-d", | ||
| "--data_path", | ||
| default="data_collection/example_pois.geojson", | ||
| type=str | ||
| ) | ||
| parser.add_argument("-p", "--positive_samples", default=10, type=int) | ||
| parser.add_argument( | ||
| "-o", | ||
| "--out_path", | ||
| default="data_collection/example_poi_data", | ||
| type=str | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| out_path = args.out_path | ||
| os.makedirs(out_path, exist_ok=True) | ||
| nr_neighbors = args.positive_samples | ||
|
|
||
| # LOAD data | ||
| poi = gpd.read_file(args.data_path) | ||
| mapping_prev_ids = { | ||
| i: int(old_id) | ||
| for i, old_id in enumerate(poi["id"].values) | ||
| } | ||
| with open(os.path.join(out_path, "poi_id_mapping.json"), "w") as outfile: | ||
| json.dump(mapping_prev_ids, outfile) | ||
| print("Saved mapping from old IDs to new IDs") | ||
| poi["id"] = np.arange(len(poi)) | ||
| poi.set_index("id", inplace=True) | ||
|
|
||
| # PART 1: POI types | ||
| # add the main categories: | ||
| poi_type_cols = [col for col in poi if col.startswith("poi_type_")] | ||
| all_types = set() | ||
| for poi_col in poi_type_cols: | ||
| for elem in poi[poi_col].unique(): | ||
| all_types.add(elem) | ||
| poi_id_mapping = {elem: i for i, elem in enumerate(list(all_types))} | ||
| # reversed | ||
| id_poi_mapping = {str(i): elem for elem, i in poi_id_mapping.items()} | ||
|
|
||
| # SAVE the poi types | ||
| with open(os.path.join(out_path, "poi_type.json"), "w") as outfile: | ||
| json.dump(id_poi_mapping, outfile) | ||
| print("Saved POI types") | ||
|
|
||
| # PART 2: POI list with categories | ||
| # update table | ||
| for col in poi_type_cols: | ||
| # transfer into numerical category IDs | ||
| poi[col] = poi[col].map(poi_id_mapping) | ||
| # train test splot | ||
| rand_perm = np.random.permutation(len(poi)) | ||
| train_cutoff = int(len(poi) * 0.8) | ||
| val_cutoff = int(len(poi) * 0.9) | ||
| split_label_arr = np.array(["training" | ||
| for _ in range(len(poi))]).astype(str) | ||
| split_label_arr[rand_perm[train_cutoff:val_cutoff]] = "validation" | ||
| split_label_arr[rand_perm[val_cutoff:]] = "test" | ||
| poi["split"] = split_label_arr | ||
| poi.loc[poi["split"] == "validati", "split"] = "validation" | ||
| # convert table into tuple | ||
| my_poi_data = [] | ||
| for elem_id, row in poi.iterrows(): | ||
| this_tuple = ( | ||
| elem_id, | ||
| (row["geometry"].x, row["geometry"].y), | ||
| tuple([row[poi_type] for poi_type in poi_type_cols]), | ||
| row["split"], | ||
| ) | ||
| my_poi_data.append(this_tuple) | ||
| number_of_pois = len(id_poi_mapping) | ||
|
|
||
| # Save the poi data with the categories | ||
| with open(os.path.join(out_path, "pointset.pkl"), "wb") as outfile: | ||
| pickle.dump((number_of_pois, my_poi_data), outfile) | ||
| print("Saved POI-label data") | ||
|
|
||
| # PART 3: sample the spatially closest | ||
| coord_arr = np.swapaxes( | ||
| np.vstack([poi["geometry"].x.values, poi["geometry"].y.values]), 1, 0 | ||
| ) | ||
| closest, distance_of_closest = get_nearest( | ||
| coord_arr, coord_arr, k_neighbors=nr_neighbors | ||
| ) | ||
| print("Finished positive sampling") | ||
|
|
||
| # convert index | ||
| poi_id_list = list(poi.index) | ||
| poi_id_array = np.array(poi_id_list) | ||
| poi_id_set = set(poi_id_list) | ||
|
|
||
| # Negative sampling: | ||
| all_tuples = [] | ||
| for counter, positive_sampled_index in enumerate(closest): | ||
| elem_id = poi_id_list[counter] | ||
| positive_sampled = poi_id_array[positive_sampled_index] | ||
| leftover = list(poi_id_set - set([elem_id] + list(positive_sampled))) | ||
| negative_sampled = list(np.random.choice(leftover, nr_neighbors)) | ||
|
|
||
| mode = poi.loc[elem_id, "split"] | ||
| all_tuples.append( | ||
| ( | ||
| elem_id, tuple(positive_sampled), mode, negative_sampled, | ||
| distance_of_closest[counter] | ||
| ) | ||
| ) | ||
| print("Finisher negative sampling") | ||
|
|
||
| for mode in ["training", "validation", "test"]: | ||
| out_tuple = [ | ||
| the_tuple for the_tuple in all_tuples if the_tuple[2] == mode | ||
| ] | ||
| with open( | ||
| os.path.join(out_path, f"neighborgraphs_{mode}.pkl"), "wb" | ||
| ) as outfile: | ||
| pickle.dump(out_tuple, outfile) | ||
| print("Saved graph data", mode) |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| python -m spacegraph_codebase.Place2Vec.train \ | ||
| --data_dir data_collection/example_poi_data/\ | ||
| --model_dir ./model_dir/global_example_data/ \ | ||
| --log_dir ./model_dir/global_example_data/ \ | ||
| --num_context_sample 10 \ | ||
| --embed_dim 32 \ | ||
| --dropout 0.5 \ | ||
| --enc_agg mean \ | ||
| --model_type global \ | ||
| --num_rbf_anchor_pts 0 \ | ||
| --spa_enc theory \ | ||
| --spa_embed_dim 64 \ | ||
| --freq 16 \ | ||
| --max_radius 10000 \ | ||
| --min_radius 100 \ | ||
| --spa_f_act sigmoid \ | ||
| --freq_init geometric \ | ||
| --spa_enc_use_layn F \ | ||
| --spa_enc_use_postmat T \ | ||
| --g_spa_enc theory \ | ||
| --g_spa_embed_dim 64 \ | ||
| --g_freq 32 \ | ||
| --g_max_radius 40000 \ | ||
| --g_min_radius 50 \ | ||
| --g_spa_f_act relu \ | ||
| --g_freq_init geometric \ | ||
| --g_spa_enc_use_layn T \ | ||
| --g_spa_enc_use_postmat T \ | ||
| --num_hidden_layer 1 \ | ||
| --hidden_dim 512 \ | ||
| --use_layn T \ | ||
| --skip_connection T \ | ||
| --use_dec T \ | ||
| --init_decoder_atten_type concat \ | ||
| --init_decoder_atten_act leakyrelu \ | ||
| --init_decoder_atten_f_act sigmoid \ | ||
| --init_decoder_atten_num 1 \ | ||
| --init_decoder_use_layn T \ | ||
| --init_decoder_use_postmat T \ | ||
| --decoder_atten_type concat \ | ||
| --decoder_atten_act leakyrelu \ | ||
| --decoder_atten_f_act sigmoid \ | ||
| --decoder_atten_num 1 \ | ||
| --decoder_use_layn T \ | ||
| --decoder_use_postmat T \ | ||
| --join_dec_type max \ | ||
| --act sigmoid \ | ||
| --opt adam \ | ||
| --lr 0.001 \ | ||
| --max_iter 5000 \ | ||
| --batch_size 512 \ | ||
| --log_every 50 \ | ||
| --val_every 50 \ | ||
| # --load_model |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| numpy==1.16.4 | ||
| matplotlib==2.2.4 | ||
| torch==1.0.1 | ||
| sklearn==0.20.3 | ||
| pyproj==2.2.2 | ||
| pandas==0.24.2 | ||
| scipy==1.2.1 | ||
| numpy>=1.16.4 | ||
| matplotlib>=2.2.4 | ||
| torch>=1.0.1 | ||
| pyproj>=2.2.2 | ||
| pandas>=0.24.2 | ||
| scipy>=1.2.1 |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| from sets import Set | ||
|
|
||
| import torch | ||
| import torch.nn as nn | ||
| from torch.nn import init | ||
|
|
||
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.
I added this in order to improve model loading, compared to the long file name with all parameters in the file name.