Skip to content

Added extensive Readable Comments #59

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
wants to merge 1 commit into
base: main
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
36 changes: 28 additions & 8 deletions geoplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
from string import Template
from agent_torch.core.helpers import get_by_path

# HTML/JS template string is being used here to render cesium-based 3D visualisation.
# Populated with actual data, timestamps, etc.
geoplot_template = """
<!doctype html>
<html lang="en">
Expand Down Expand Up @@ -214,10 +216,20 @@


def read_var(state, var):
'''
Helper function to get access to nested fields in the state using '/'

'''
return get_by_path(state, re.split("/", var))


class GeoPlot:
'''
init function for initializing the geoplot visualizer.
Args:
config: dictionary containing simulation metadata like num_episodes, num_steps...etc.
options: dictionary containing with required config like cesium_token, feature, type of visualization,..etc.
'''
def __init__(self, config, options):
self.config = config
(
Expand All @@ -235,19 +247,25 @@ def __init__(self, config, options):
)

def render(self, state_trajectory):
'''
Converts simulation trajectory into GeoJSON and HTML visualization.
Args:
state_trajectory: A 2D list representing simulation states across episodes & steps.
'''
coords, values = [], []
name = self.config["simulation_metadata"]["name"]
geodata_path, geoplot_path = f"{name}.geojson", f"{name}.html"
name = self.config["simulation_metadata"]["name"] # Extracting output file names from simulation metadata
geodata_path, geoplot_path = f"{name}.geojson", f"{name}.html"

# Extracting the final state of each episode and read coordinates and property values
for i in range(0, len(state_trajectory) - 1):
final_state = state_trajectory[i][-1]

coords = np.array(read_var(final_state, self.entity_position)).tolist()
coords = np.array(read_var(final_state, self.entity_position)).tolist() # coordinates of each entity
values.append(
np.array(read_var(final_state, self.entity_property)).flatten().tolist()
)

start_time = pd.Timestamp.utcnow()
start_time = pd.Timestamp.utcnow() # Creating timestamp for each simulation step
timestamps = [
start_time + pd.Timedelta(seconds=i * self.step_time)
for i in range(
Expand All @@ -256,7 +274,8 @@ def render(self, state_trajectory):
)
]

geojsons = []
# Creating GeoJSON time-series data for each entity
geojsons = []
for i, coord in enumerate(coords):
features = []
for time, value_list in zip(timestamps, values):
Expand All @@ -265,19 +284,20 @@ def render(self, state_trajectory):
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [coord[1], coord[0]],
"coordinates": [coord[1], coord[0]], # [longitude, latitude]
},
"properties": {
"value": value_list[i],
"value": value_list[i], # value of the property at this timestamp
"time": time.isoformat(),
},
}
)
geojsons.append({"type": "FeatureCollection", "features": features})

with open(geodata_path, "w", encoding="utf-8") as f:
json.dump(geojsons, f, ensure_ascii=False, indent=2)
json.dump(geojsons, f, ensure_ascii=False, indent=2) # writing GeoJSON data to disk

# Filling the HTML/JS Template with actual data
tmpl = Template(geoplot_template)
with open(geoplot_path, "w", encoding="utf-8") as f:
f.write(
Expand Down