-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Model to latex with mira task (#4831)
Co-authored-by: Yohann Paris <[email protected]>
- Loading branch information
1 parent
c09f3ea
commit c8e6da7
Showing
11 changed files
with
182 additions
and
9 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
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
Empty 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
Empty 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import sys | ||
import json | ||
import traceback | ||
from taskrunner import TaskRunnerInterface | ||
import sympy | ||
from mira.sources.amr import model_from_json | ||
|
||
def cleanup(): | ||
pass | ||
|
||
def main(): | ||
exitCode = 0 | ||
|
||
try: | ||
taskrunner = TaskRunnerInterface(description="Generate latex") | ||
taskrunner.on_cancellation(cleanup) | ||
|
||
data = taskrunner.read_input_str_with_timeout() | ||
amr = json.loads(data) | ||
model = model_from_json(amr) | ||
|
||
odeterms = {var: 0 for var in model.get_concepts_name_map().keys()} | ||
|
||
for t in model.templates: | ||
if hasattr(t, "subject"): | ||
var = t.subject.name | ||
odeterms[var] -= t.rate_law.args[0] | ||
|
||
if hasattr(t, "outcome"): | ||
var = t.outcome.name | ||
odeterms[var] += t.rate_law.args[0] | ||
|
||
# Time | ||
if model.time and model.time.name: | ||
time = model.time.name | ||
else: | ||
time = "t" | ||
|
||
t = sympy.Symbol(time) | ||
|
||
# Observables | ||
if len(model.observables) != 0: | ||
obs_eqs = [ | ||
f"{{{obs.name}}}(t) = " + sympy.latex(obs.expression.args[0]) | ||
for obs in model.observables.values() | ||
] | ||
|
||
# Construct Sympy equations | ||
odesys = [ | ||
sympy.latex(sympy.Eq(sympy.diff(sympy.Function(var)(t), t), terms)) | ||
for var, terms in odeterms.items() | ||
] | ||
|
||
#add observables. | ||
odesys += obs_eqs | ||
#Reformat: | ||
odesys = "\\begin{align} \n " + " \\\\ \n ".join([eq for eq in odesys]) + "\n\\end{align}" | ||
|
||
taskrunner.write_output_dict_with_timeout({"response": odesys}) | ||
print("Generate latex succeeded") | ||
|
||
except Exception as e: | ||
sys.stderr.write(f"Error: {str(e)}\n") | ||
sys.stderr.write(traceback.format_exc()) | ||
sys.stderr.flush() | ||
exitCode = 1 | ||
|
||
|
||
taskrunner.log("Shutting down") | ||
taskrunner.shutdown() | ||
sys.exit(exitCode) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
18 changes: 18 additions & 0 deletions
18
...oftware/uncharted/terarium/hmiserver/service/tasks/GenerateModelLatexResponseHandler.java
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,18 @@ | ||
package software.uncharted.terarium.hmiserver.service.tasks; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class GenerateModelLatexResponseHandler extends TaskResponseHandler { | ||
|
||
public static final String NAME = "mira_task:generate_model_latex"; | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
} |
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