Skip to content

Commit

Permalink
Merge pull request #33 from matejak/refactor
Browse files Browse the repository at this point in the history
Generally simplify things without revolution
  • Loading branch information
matejak committed Jun 6, 2024
2 parents 9d7752c + 1c38669 commit 51d5137
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 274 deletions.
4 changes: 0 additions & 4 deletions estimage/persistence/event/ini.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ def _event_to_string_dict(self, event):
task_name=event.task_name
)
if (val := event.value_before) is not None:
if event.quantity == "state":
val = val
to_save["value_before"] = str(val)
if (val := event.value_after) is not None:
if event.quantity == "state":
val = val
to_save["value_after"] = str(val)

return to_save
Expand Down
4 changes: 4 additions & 0 deletions estimage/plugins/base/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ class BaseForm(FlaskForm):
def __init__(self, ** kwargs):
self.extending_fields = []
super().__init__(** kwargs)

@classmethod
def supporting_js(cls, forms):
return ""
29 changes: 21 additions & 8 deletions estimage/plugins/demo/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,36 @@
import flask
import flask_login

from ...webapp import web_utils
from ... import simpledata, persistence
from ...webapp import web_utils, routers
from . import forms
from .. import demo


bp = flask.Blueprint("demo", __name__, template_folder="templates")


def _get_card_loader(flavor, backend):
card_class = flask.current_app.get_final_class("BaseCard")
loader = type("loader", (flavor, persistence.SAVERS[card_class][backend], persistence.LOADERS[card_class][backend]), dict())
return card_class, loader


def get_retro_loader():
return _get_card_loader(simpledata.RetroCardIO, "ini")


def get_proj_loader():
return _get_card_loader(simpledata.ProjCardIO, "ini")


@web_utils.is_primary_menu_of("demo", bp, "Estimagus Demo")
@bp.route('/demo', methods=("GET", "POST"))
@flask_login.login_required
def next_day():
user = flask_login.current_user
user_id = user.get_id()

cls, loader = web_utils.get_retro_loader()
cards_by_id, model = web_utils.get_all_tasks_by_id_and_user_model("retro", user_id)
_, loader = get_retro_loader()

start_date = flask.current_app.config["RETROSPECTIVE_PERIOD"][0]
start_date = flask.current_app.get_config_option("RETROSPECTIVE_PERIOD")[0]
doer = demo.Demo(loader, start_date)
doer.start_if_on_start()

Expand All @@ -41,4 +54,4 @@ def reset():
reset_form = forms.ResetForm()
if reset_form.validate_on_submit():
demo.reset_data()
return flask.redirect(flask.url_for("demo.next_day"))
return flask.redirect(web_utils.head_url_for("demo.next_day"))
4 changes: 2 additions & 2 deletions estimage/plugins/demo/templates/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<div class="row">
<h1>Execution Demo</h1>
<p>Day {{ day_index + 1 }}</p>
{{ render_form(plugin_form, action=url_for("demo.next_day")) }}
{{ render_form(reset_form, button_map={"reset": "danger"}, action=url_for("demo.reset")) }}
{{ render_form(plugin_form, action=head_url_for("demo.next_day")) }}
{{ render_form(reset_form, button_map={"reset": "danger"}, action=head_url_for("demo.reset")) }}
</div>
</div>
{% endblock %}
Expand Down
29 changes: 13 additions & 16 deletions estimage/statops/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,8 @@ def get_time_to_completion(velocity_mean, velocity_stdev, distance, confidence=0


def get_prob_of_completion(velocity_mean, velocity_stdev, distance, time):
if distance == 0:
return 1
if velocity_stdev == 0:
return 0 if velocity_mean * time < distance else 1
if time == 0:
return 0
dist = sp.stats.norm(loc=velocity_mean * time, scale=np.sqrt(velocity_stdev ** 2 * time))
return 1 - dist.cdf(distance)
times = np.ones(1) * time
return get_prob_of_completion_vector(velocity_mean, velocity_stdev, distance, times)[0]


def get_prob_of_completion_vector(velocity_mean, velocity_stdev, distance, times):
Expand Down Expand Up @@ -212,17 +206,20 @@ def estimate_lognorm(grids, samples):
return result


def autoestimate_lognorm(samples):
grids = get_1d_lognorm_grid(0.01, 5.0, samples.mean(), 10)
res = estimate_lognorm(grids, samples)

grids = get_1d_lognorm_grid(res[1] - 0.5, res[1] + 0.5, samples.mean(), 10)
res2 = estimate_lognorm(grids, samples)
def autoestimage_lognorm_general(samples, first_grid, radii, counts):
mean = samples.mean()
res = estimate_lognorm(first_grid, samples)
for (radius, count) in zip(radii, counts):
grid = get_1d_lognorm_grid(res[1] - radius, res[1] + radius, mean, count)
res = estimate_lognorm(grid, samples)
return res

grids = get_1d_lognorm_grid(res2[1] - 0.2, res2[1] + 0.2, samples.mean(), 20)
res3 = estimate_lognorm(grids, samples)

return res3
def autoestimate_lognorm(samples):
grids = get_1d_lognorm_grid(0.01, 5.0, samples.mean(), 10)
res = autoestimage_lognorm_general(samples, grids, (0.5, 0.2), (10, 20))
return res


def get_nonzero_velocity(velocity):
Expand Down
3 changes: 3 additions & 0 deletions estimage/webapp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ def create_app_common(app):
LOGIN.init_app(app)
LOGIN.user_loader(users.load_user)
LOGIN.login_view = "login.auto_login"
# Don't display the "log in to proceed" message, as it is often more confusing than helpful
# in connection with random logouts and autologins
LOGIN.login_message = ""

CACHE.init_app(app, config=app.config)

Expand Down

0 comments on commit 51d5137

Please sign in to comment.