Skip to content

Commit

Permalink
Adding working root sim
Browse files Browse the repository at this point in the history
  • Loading branch information
JBris committed Sep 9, 2024
1 parent 745eff4 commit 650b006
Show file tree
Hide file tree
Showing 8 changed files with 1,050 additions and 350 deletions.
95 changes: 56 additions & 39 deletions app/conf/form/common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ components:
max: 1
step: 0.1
value: 0.5
persistence: true
- id: fine-root-threshold-input
param: fine_root_threshold
label: Fine root threshold ratio
help: Threshold for classifying a root as a fine root, rather than a structural root (mm)
label: Fine root threshold
help: Threshold for classifying a root as a fine root, rather than a structural root (cm)
class_name: dash_bootstrap_components.Input
kwargs:
type: number
min: 0
step: 0.1
value: 1.5
step: 0.01
value: 0.06
persistence: true
- id: outer-primary-root-num-input
param: outer_root_num
label: Number of outer roots
Expand All @@ -46,6 +48,7 @@ components:
max: 20
step: 1
value: 10
persistence: true
- id: inner-primary-root-num-input
param: inner_root_num
label: Number of inner roots
Expand All @@ -57,6 +60,7 @@ components:
max: 20
step: 1
value: 8
persistence: true
- id: min-length-primary-root-input
param: min_primary_length
label: Primary root minimum length
Expand All @@ -68,6 +72,7 @@ components:
max: 50
step: 1
value: 20
persistence: true
- id: max-length-primary-root-input
param: max_primary_length
label: Primary root maximum length
Expand All @@ -79,6 +84,7 @@ components:
max: 50
step: 1
value: 30
persistence: true
- id: base-diameter-root-input
param: base_diameter
label: Base root diameter
Expand All @@ -87,31 +93,34 @@ components:
kwargs:
type: number
min: 0.001
max: 10
step: 0.1
max: 1
step: 0.01
value: 0.11
persistence: true
- id: root-apex-input
param: apex_diameter
label: Apex diameter
help: The diameter of the root apex (cm)
class_name: dash_bootstrap_components.Input
kwargs:
type: number
min: 0.001
min: 0.01
max: 1
step: 0.1
value: 0.1
step: 0.01
value: 0.02
persistence: true
- id: diameter-reduction-factor-input
param: diameter_reduction
label: Diameter reduction
help: The diameter reduction factor for subsequent root segments.
class_name: dash_bootstrap_components.Input
kwargs:
type: number
min: 0.001
min: 0
max: 1
step: 0.1
value: 0.7
value: 0.2
persistence: true
- id: min-num-secondary-roots-input
param: min_sec_root_num
label: Minimum number of secondary roots
Expand All @@ -120,9 +129,9 @@ components:
kwargs:
type: number
min: 0
max: 10
step: 1
value: 3
value: 1
persistence: true
- id: max-num-secondary-roots-input
param: max_sec_root_num
label: Maximum number of secondary roots
Expand All @@ -131,9 +140,10 @@ components:
kwargs:
type: number
min: 0
max: 10
max: 20
step: 1
value: 4
value: 3
persistence: true
- id: secondary-root-growth-input
param: growth_sec_root
label: Secondary root growth rate
Expand All @@ -143,7 +153,8 @@ components:
type: number
min: 0
step: 0.1
value: 0.5
value: 0.2
persistence: true
- id: min-length-secondary-root-input
param: min_sec_root_length
label: Secondary root minimum length
Expand All @@ -155,6 +166,7 @@ components:
max: 250
step: 1
value: 100
persistence: true
- id: max-length-secondary-root-input
param: max_sec_root_length
label: Secondary root maximum length
Expand All @@ -166,6 +178,7 @@ components:
max: 500
step: 1
value: 220
persistence: true
- id: segments-per-root-input
param: segments_per_root
label: Segments per root
Expand All @@ -176,7 +189,8 @@ components:
min: 1
max: 500
step: 1
value: 10
value: 50
persistence: true
- id: root-length-reduction-input
param: length_reduction
label: Length reduction
Expand All @@ -188,6 +202,7 @@ components:
max: 1
step: 0.1
value: 0.5
persistence: true
- id: root-vary-input
param: root_vary
label: Root segment variance
Expand All @@ -199,28 +214,7 @@ components:
max: 360
step: 1
value: 30
- id: origin-min-input
param: origin_min
label: Origin minimum
help: The minimum distance of the initial primary root from the origin (cm)
class_name: dash_bootstrap_components.Input
kwargs:
type: number
min: 0.001
max: 1
step: 0.01
value: 0.01
- id: origin-max-input
param: origin_max
label: Origin maximum
help: The maximum distance of the initial primary root from the origin (cm)
class_name: dash_bootstrap_components.Input
kwargs:
type: number
min: 0.01
max: 1
step: 0.01
value: 0.1
persistence: true
- id: enable-soil-input
param: enable_soil
label: Enable soil
Expand All @@ -238,6 +232,7 @@ components:
max: 100
step: 10
value: 10
persistence: true
- id: soil-layer-width-input
param: soil_layer_width
label: Soil layer width
Expand All @@ -249,6 +244,7 @@ components:
max: 100
step: 10
value: 10
persistence: true
- id: soil-n-layers
param: soil_n_layers
label: Number soil layers
Expand All @@ -260,6 +256,7 @@ components:
max: 20
step: 1
value: 1
persistence: true
- id: soil-n-cols
param: soil_n_cols
label: Number soil columns
Expand All @@ -271,6 +268,7 @@ components:
max: 20
step: 1
value: 1
persistence: true
- id: random-seed-input
param: random_seed
label: Random seed
Expand All @@ -279,6 +277,19 @@ components:
kwargs:
type: number
step: 1
persistence: true
- id: max-val-attempts-input
param: max_val_attempts
label: Maximum validation attempts
help: The maximum number of attempts to validate each root (for plausibility) within the root system.
class_name: dash_bootstrap_components.Input
kwargs:
type: number
min: 0
max: 1000
step: 1
value: 50
persistence: true
- id: simulation-tag-input
param: simulation_tag
label: Simulation tag
Expand All @@ -287,6 +298,7 @@ components:
kwargs:
type: text
value: default
persistence: true
simulation:
collapsible: true
children:
Expand All @@ -298,6 +310,11 @@ components:
children: Run model
color: primary
className: me-1
- id: download-sim-data-input
label: Download
help: Download the simulation output data
class_name: dash_daq.BooleanSwitch.BooleanSwitch
kwargs: {}
- id: save-param-button
label: Save
help: Save current parameter configuration to file
Expand Down
42 changes: 25 additions & 17 deletions app/pages/generate_root_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
# Imports
######################################

import base64
import os.path as osp
from datetime import datetime

import dash_bootstrap_components as dbc
import pandas as pd
import yaml
from dash import ALL, Input, Output, State, callback, dcc, get_app, html, register_page

from deeprootgen.data_model import RootSimulationModel
Expand Down Expand Up @@ -117,10 +120,6 @@ def save_param(n_clicks: int, param_inputs: list) -> None:
k = input["param"]
inputs[k] = param_inputs[i]

from datetime import datetime

import yaml

outfile = osp.join(
"outputs", f"{datetime.today().strftime('%Y-%m-%d-%H-%M')}-{PAGE_ID}.yaml"
)
Expand All @@ -138,10 +137,6 @@ def save_param(n_clicks: int, param_inputs: list) -> None:
prevent_initial_call=True,
)
def update_output(list_of_contents: list, list_of_names: list) -> tuple:
import base64

import yaml

_, content_string = list_of_contents[0].split(",")
decoded = base64.b64decode(content_string)
input_dict = yaml.safe_load(decoded.decode("utf-8"))
Expand All @@ -156,17 +151,23 @@ def update_output(list_of_contents: list, list_of_names: list) -> tuple:
Input({"index": f"{PAGE_ID}-run-sim-button", "type": ALL}, "n_clicks"),
State({"type": f"{PAGE_ID}-parameters", "index": ALL}, "value"),
State({"index": f"{PAGE_ID}-enable-soil-input", "type": ALL}, "on"),
State({"index": f"{PAGE_ID}-download-sim-data-input", "type": ALL}, "on"),
prevent_initial_call=True,
)
def run_root_model(n_clicks: list, form_values: list, enable_soils: list) -> dcc.Graph:
def run_root_model(
n_clicks: list, form_values: list, enable_soils: list, download_data: list
) -> dcc.Graph:
"""Run and plot the root model.
Args:
n_clicks (list):
Number of times the button has been clicked.
form_values (list):
The form input data.
enable_soils (list):
Enable visualisation of soil data.
download_data (list):
Whether to download the simulation results data.
Returns:
dcc.Graph: The visualised root model.
"""
Expand All @@ -186,18 +187,25 @@ def run_root_model(n_clicks: list, form_values: list, enable_soils: list) -> dcc

input_params = RootSimulationModel.parse_obj(form_inputs)
simulation = RootSystemSimulation(
simulation_tag=input_params.simulation_tag, random_seed=input_params.random_seed
simulation_tag=input_params.simulation_tag,
random_seed=input_params.random_seed,
visualise=True,
)
results = simulation.run(input_params)

from datetime import datetime
download_data = download_data[0]
if download_data:
from datetime import datetime

now = datetime.today().strftime("%Y-%m-%d-%H-%M")
outfile = osp.join("outputs", f"{now}-nodes.csv")
df = pd.DataFrame(results.nodes)
df.to_csv(outfile, index=False)
now = datetime.today().strftime("%Y-%m-%d-%H-%M")
outfile = osp.join("outputs", f"{now}-nodes.csv")
df = pd.DataFrame(results.nodes)
df.to_csv(outfile, index=False)
download_file = dcc.send_file(outfile)
else:
download_file = None

return results.figure, dcc.send_file(outfile)
return results.figure, download_file


######################################
Expand Down
4 changes: 3 additions & 1 deletion deeprootgen/data_model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
RootEdgeModel,
RootNodeModel,
RootSimulationModel,
RootSimulationResults,
RootSimulationResultModel,
RootType,
RootTypeModel,
)
Loading

0 comments on commit 650b006

Please sign in to comment.