-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from probcomp/alexlew-viz-071524
First pass at integrating Jacob Hoover's and Maddy Bowers's visualization code
- Loading branch information
Showing
11 changed files
with
3,241 additions
and
668 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Visualization | ||
|
||
We provide a Web interface for visualizing the execution of a sequential Monte Carlo algorithm, | ||
based on contributions from Maddy Bowers and Jacob Hoover. | ||
|
||
First, update your model to support visualization by implementing the [`string_for_serialization`](hfppl.modeling.Model.string_for_serialization) method. | ||
Return a string that summarizes the particle's current state. | ||
|
||
To run the interface, change to the `html` directory and run `python -m http.server`. This will start serving | ||
the files in the `html` directory at localhost:8000. (If you are SSH-ing onto a remote machine, you may need | ||
port forwarding. Visual Studio Code automatically handles this for some ports, including 8000.) | ||
Then, when calling [`smc_standard`](hfppl.inference.smc_standard), set `visualization_dir` | ||
to the path to the `html` directory. A JSON record of the run will automatically be saved | ||
to that directory, and a URL will be printed to the console (`http://localhost:8000/smc.html?path=$json_file`). | ||
|
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
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import json | ||
import numpy as np | ||
|
||
|
||
class SMCRecord: | ||
|
||
def __init__(self, n): | ||
self.history = [] | ||
self.most_recent_weights = [0.0 for _ in range(n)] | ||
self.step_num = 1 | ||
|
||
def prepare_string(self, s): | ||
# If the string doesn't have <<< and >>>, prepend <<<>>> at the front. | ||
if "<<<" not in s and ">>>" not in s: | ||
return f"<<<>>>{s}" | ||
return s | ||
|
||
def particle_dict(self, particles): | ||
return [ | ||
{ | ||
"contents": self.prepare_string(p.string_for_serialization()), | ||
"logweight": ( | ||
"-Infinity" if p.weight == float("-inf") else str(float(p.weight)) | ||
), | ||
"weight_incr": str( | ||
float(p.weight) - float(self.most_recent_weights[i]) | ||
), | ||
} | ||
for (i, p) in enumerate(particles) | ||
] | ||
|
||
def add_init(self, particles): | ||
self.history.append( | ||
{ | ||
"step": self.step_num, | ||
"mode": "init", | ||
"particles": self.particle_dict(particles), | ||
} | ||
) | ||
self.most_recent_weights = [p.weight for p in particles] | ||
|
||
def add_smc_step(self, particles): | ||
self.step_num += 1 | ||
self.history.append( | ||
{ | ||
"step": self.step_num, | ||
"mode": "smc_step", | ||
"particles": self.particle_dict(particles), | ||
} | ||
) | ||
self.most_recent_weights = [p.weight for p in particles] | ||
|
||
def add_resample(self, ancestor_indices, particles): | ||
self.step_num += 1 | ||
self.most_recent_weights = [ | ||
self.most_recent_weights[i] for i in ancestor_indices | ||
] | ||
|
||
self.history.append( | ||
{ | ||
"mode": "resample", | ||
"step": self.step_num, | ||
"ancestors": [int(a) for a in ancestor_indices], | ||
"particles": self.particle_dict(particles), | ||
} | ||
) | ||
|
||
self.most_recent_weights = [p.weight for p in particles] | ||
|
||
def to_json(self): | ||
return json.dumps(self.history) |
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.