Skip to content
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

Add to_json and load_json methods #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions notebooks/walker/Step 3 serialize/walker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import matplotlib.pyplot as plt
import json


class Walker:
Expand Down Expand Up @@ -86,6 +87,26 @@ def _compute_next_step_probability(self, next_step_map):
next_step_probability /= next_step_probability.sum()
return next_step_probability

def to_json(self, filename):
stem = filename.split(sep='.')[0]
context_map_filename = f'{stem}_context_map.npy'
with open(filename, 'w') as f:
json.dump({'sigma_i': self.sigma_i,
'sigma_j': self.sigma_j,
'size': self.size,
'context_map_path': context_map_filename}, f)
np.save(context_map_filename, self.context_map)
return filename

@classmethod
def from_json(cls, filename):
with open(filename, 'r') as f:
walker_dict = json.load(f)
print(walker_dict)
with open(walker_dict['context_map_path'], 'rb') as f:
context_map = np.load(walker_dict['context_map_path'])
return cls(walker_dict['sigma_i'], walker_dict['sigma_j'], walker_dict['size'], context_map)


def plot_trajectory(trajectory, context_map):
""" Plot a trajectory over a context map. """
Expand Down