Skip to content
This repository has been archived by the owner on Jan 9, 2021. It is now read-only.

Commit

Permalink
Add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
spascou committed Aug 18, 2020
1 parent 66092cf commit 0a5fc68
Show file tree
Hide file tree
Showing 12 changed files with 855 additions and 150 deletions.
35 changes: 35 additions & 0 deletions examples/cof_simulation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html>
<head>
<style>
.error {
color: red;
}
</style>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm//vega@5"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm//[email protected]"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm//vega-embed@6"></script>
</head>
<body>
<div id="vis"></div>
<script>
(function(vegaEmbed) {
var spec = {"config": {"view": {"continuousWidth": 400, "continuousHeight": 300}}, "data": {"values": [{"time": 0, "control": "auto", "cof": 0.1}, {"time": 80, "control": "auto", "cof": 0.15000000000000002}, {"time": 160, "control": "auto", "cof": 0.2}, {"time": 240, "control": "auto", "cof": 0.25}, {"time": 320, "control": "auto", "cof": 0.3}, {"time": 400, "control": "auto", "cof": 0.35}, {"time": 480, "control": "auto", "cof": 0.39999999999999997}, {"time": 560, "control": "auto", "cof": 0.44999999999999996}, {"time": 640, "control": "auto", "cof": 0.49999999999999994}, {"time": 720, "control": "auto", "cof": 0.5499999999999999}, {"time": 800, "control": "auto", "cof": 0.6}, {"time": 880, "control": "auto", "cof": 0.65}, {"time": 960, "control": "auto", "cof": 0.7000000000000001}, {"time": 1040, "control": "auto", "cof": 0.7500000000000001}, {"time": 1120, "control": "auto", "cof": 0.8000000000000002}, {"time": 1200, "control": "auto", "cof": 0.8500000000000002}, {"time": 1280, "control": "auto", "cof": 0.9000000000000002}, {"time": 1360, "control": "auto", "cof": 0.9500000000000003}, {"time": 1440, "control": "auto", "cof": 1.0000000000000002}, {"time": 1520, "control": "auto", "cof": 1.0500000000000003}, {"time": 1600, "control": "auto", "cof": 1.1000000000000003}, {"time": 1680, "control": "auto", "cof": 1.1500000000000004}, {"time": 1760, "control": "auto", "cof": 1.2000000000000004}, {"time": 1840, "control": "auto", "cof": 1.2500000000000004}, {"time": 1920, "control": "auto", "cof": 1.3000000000000005}, {"time": 2000, "control": "auto", "cof": 1.3500000000000005}, {"time": 2080, "control": "auto", "cof": 1.4000000000000006}, {"time": 2160, "control": "auto", "cof": 1.4500000000000006}, {"time": 2240, "control": "auto", "cof": 1.5000000000000007}, {"time": 2320, "control": "auto", "cof": 1.5500000000000007}, {"time": 2400, "control": "auto", "cof": 1.6000000000000008}, {"time": 2480, "control": "auto", "cof": 1.6500000000000008}, {"time": 2560, "control": "auto", "cof": 1.7000000000000008}, {"time": 2640, "control": "auto", "cof": 1.7500000000000009}, {"time": 2720, "control": "auto", "cof": 1.800000000000001}, {"time": 2800, "control": "auto", "cof": 1.850000000000001}, {"time": 2880, "control": "auto", "cof": 1.900000000000001}, {"time": 2960, "control": "auto", "cof": 1.950000000000001}, {"time": 3040, "control": "auto", "cof": 2.000000000000001}, {"time": 3120, "control": "auto", "cof": 2.0500000000000007}, {"time": 0, "control": "5+100ms", "cof": 0.1}, {"time": 80, "control": "5+100ms", "cof": 0.15000000000000002}, {"time": 160, "control": "5+100ms", "cof": 0.2}, {"time": 240, "control": "5+100ms", "cof": 0.25}, {"time": 320, "control": "5+100ms", "cof": 0.3}, {"time": 500, "control": "5+100ms", "cof": 0.1}, {"time": 580, "control": "5+100ms", "cof": 0.15000000000000002}, {"time": 660, "control": "5+100ms", "cof": 0.2}, {"time": 740, "control": "5+100ms", "cof": 0.25}, {"time": 820, "control": "5+100ms", "cof": 0.3}, {"time": 1000, "control": "5+100ms", "cof": 0.1}, {"time": 1080, "control": "5+100ms", "cof": 0.15000000000000002}, {"time": 1160, "control": "5+100ms", "cof": 0.2}, {"time": 1240, "control": "5+100ms", "cof": 0.25}, {"time": 1320, "control": "5+100ms", "cof": 0.3}, {"time": 1500, "control": "5+100ms", "cof": 0.1}, {"time": 1580, "control": "5+100ms", "cof": 0.15000000000000002}, {"time": 1660, "control": "5+100ms", "cof": 0.2}, {"time": 1740, "control": "5+100ms", "cof": 0.25}, {"time": 1820, "control": "5+100ms", "cof": 0.3}, {"time": 2000, "control": "5+100ms", "cof": 0.1}, {"time": 2080, "control": "5+100ms", "cof": 0.15000000000000002}, {"time": 2160, "control": "5+100ms", "cof": 0.2}, {"time": 2240, "control": "5+100ms", "cof": 0.25}, {"time": 2320, "control": "5+100ms", "cof": 0.3}, {"time": 2500, "control": "5+100ms", "cof": 0.1}, {"time": 2580, "control": "5+100ms", "cof": 0.15000000000000002}, {"time": 2660, "control": "5+100ms", "cof": 0.2}, {"time": 2740, "control": "5+100ms", "cof": 0.25}, {"time": 2820, "control": "5+100ms", "cof": 0.3}, {"time": 3000, "control": "5+100ms", "cof": 0.1}, {"time": 3080, "control": "5+100ms", "cof": 0.15000000000000002}, {"time": 3160, "control": "5+100ms", "cof": 0.2}, {"time": 3240, "control": "5+100ms", "cof": 0.25}, {"time": 3320, "control": "5+100ms", "cof": 0.3}, {"time": 3500, "control": "5+100ms", "cof": 0.1}, {"time": 3580, "control": "5+100ms", "cof": 0.15000000000000002}, {"time": 3660, "control": "5+100ms", "cof": 0.2}, {"time": 3740, "control": "5+100ms", "cof": 0.25}, {"time": 3820, "control": "5+100ms", "cof": 0.3}]}, "mark": "line", "encoding": {"color": {"type": "ordinal", "field": "control", "scale": {"scheme": "dark2"}}, "tooltip": [{"type": "quantitative", "field": "time"}, {"type": "ordinal", "field": "control"}], "x": {"type": "quantitative", "field": "time"}, "y": {"type": "quantitative", "field": "cof"}}, "height": 900, "selection": {"selector001": {"type": "interval", "bind": "scales", "encodings": ["x", "y"]}}, "width": 900, "$schema": "https://vega.github.io/schema/vega-lite/v4.8.1.json"};
var embedOpt = {"mode": "vega-lite"};

function showError(el, error){
el.innerHTML = ('<div class="error" style="color:red;">'
+ '<p>JavaScript Error: ' + error.message + '</p>'
+ "<p>This usually means there's a typo in your chart specification. "
+ "See the javascript console for the full traceback.</p>"
+ '</div>');
throw error;
}
const el = document.getElementById('vis');
vegaEmbed("#vis", spec, embedOpt)
.catch(error => showError(el, error));
})(vegaEmbed);

</script>
</body>
</html>
94 changes: 94 additions & 0 deletions examples/cof_simulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import logging
import os
from typing import List, Optional, Tuple

import altair

from ps2_analysis.fire_groups.data_files import (
update_data_files as update_fire_groups_data_files,
)
from ps2_analysis.fire_groups.fire_mode import FireMode
from ps2_analysis.weapons.infantry.data_files import (
update_data_files as update_infantry_weapons_data_files,
)
from ps2_analysis.weapons.infantry.generate import generate_all_infantry_weapons
from ps2_analysis.weapons.infantry.infantry_weapon import InfantryWeapon

logging.basicConfig(level=logging.INFO)

SERVICE_ID: Optional[str] = os.environ.get("CENSUS_SERVICE_ID")
DATAFILES_DIRECTORY: str = "datafiles"

if not SERVICE_ID:
raise ValueError("CENSUS_SERVICE_ID envvar not found")

update_fire_groups_data_files(
directory=DATAFILES_DIRECTORY, service_id=SERVICE_ID,
)

update_infantry_weapons_data_files(
directory=DATAFILES_DIRECTORY, service_id=SERVICE_ID,
)

infantry_weapons: List[InfantryWeapon] = list(
generate_all_infantry_weapons(data_files_directory=DATAFILES_DIRECTORY)
)

print(f"Generated {len(infantry_weapons)} infantry weapons")

wp: InfantryWeapon = next(x for x in infantry_weapons if x.item_id == 43)

fm: FireMode = wp.fire_groups[0].fire_modes[1]

datapoints: List[dict] = []

auto_sim = fm.simulate_shots(shots=fm.max_consecutive_shots)

t: int
_cursor_coor: Tuple[float, float]
_pellets_coors: List[Tuple[float, float]]
cof: float
_vertical_recoil: Tuple[float, float]
_horizontal_recoil: Tuple[float, float]

for (
t,
_cursor_coor,
_pellets_coors,
cof,
_vertical_recoil,
_horizontal_recoil,
) in auto_sim:
datapoints.append({"time": t, "control": "auto", "cof": cof})

burst_5_100_sim = fm.simulate_shots(
shots=fm.max_consecutive_shots, auto_burst_length=5, control_time=100
)

for (
t,
_cursor_coor,
_pellets_coors,
cof,
_vertical_recoil,
_horizontal_recoil,
) in burst_5_100_sim:
datapoints.append({"time": t, "control": "5+100ms", "cof": cof})


dataset = altair.Data(values=datapoints)

chart = (
altair.Chart(dataset)
.mark_line()
.encode(
x="time:Q",
y="cof:Q",
color=altair.Color("control:O", scale=altair.Scale(scheme="dark2")),
tooltip=["time:Q", "control:O"],
)
.properties(height=900, width=900)
.interactive()
)

chart.save("cof_simulation.html")
Loading

0 comments on commit 0a5fc68

Please sign in to comment.