python_export_constants.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2021 by
  4. * The Salk Institute for Biological Studies
  5. *
  6. * Use of this source code is governed by an MIT-style
  7. * license that can be found in the LICENSE file or at
  8. * https://opensource.org/licenses/MIT.
  9. *
  10. ******************************************************************************/
  11. #include "api/python_export_constants.h"
  12. namespace MCell {
  13. namespace API {
  14. std::string get_customization_import(const std::string& customization_module) {
  15. return
  16. std::string("if os.path.exists(os.path.join(") + MODEL_PATH + ", '" + customization_module + ".py')):\n"
  17. " import " + customization_module + "\n"
  18. "else:\n"
  19. " " + customization_module + " = None\n"
  20. ;
  21. }
  22. std::string get_argparse_w_customization_begin(const std::string& customization_module) {
  23. return
  24. "if " + customization_module + " and '" + CUSTOM_ARGPARSE_AND_PARAMETERS + "' in dir(" + customization_module + "):\n"
  25. " # custom argument processing and parameter setup\n"
  26. " " + customization_module + "." + CUSTOM_ARGPARSE_AND_PARAMETERS + "()\n"
  27. "else:\n"
  28. " if len(sys.argv) == 1:\n"
  29. " # no arguments\n"
  30. " pass\n"
  31. " elif len(sys.argv) == 3 and sys.argv[1] == '-seed':\n"
  32. " # overwrite value of seed defined in module parameters\n"
  33. " " + SHARED + "." + PARAMETER_OVERRIDES + "['" + PARAM_SEED + "'] = int(sys.argv[2])\n"
  34. ;
  35. }
  36. std::string get_argparse_checkpoint_iteration() {
  37. return
  38. std::string(" elif len(sys.argv) == 3 and sys.argv[1] == '-chkpt':\n") +
  39. " " + CHECKPOINT_ITERATION + " = int(sys.argv[2])\n" +
  40. " elif len(sys.argv) == 5 and sys.argv[1] == '-seed' and sys.argv[3] == '-chkpt':\n" +
  41. " " + SHARED + "." + PARAMETER_OVERRIDES + "['" + PARAM_SEED + "'] = int(sys.argv[2])\n" +
  42. " " + CHECKPOINT_ITERATION + " = int(sys.argv[4])\n"
  43. ;
  44. }
  45. std::string get_resume_from_checkpoint_code() {
  46. return
  47. "# resume simulation if a checkpoint was created\n"
  48. "checkpoint_dir = m.run_utils.get_last_checkpoint_dir(" PARAM_SEED ")\n"
  49. "if checkpoint_dir:\n"
  50. " # change sys.path so that the only the checkpointed files are loaded\n"
  51. " sys.path = m.run_utils.remove_cwd(sys.path)\n"
  52. " sys.path.append(checkpoint_dir)\n"
  53. " \n"
  54. " # prepare import of the 'model' module from the checkpoint\n"
  55. " model_spec = importlib.util.spec_from_file_location(\n"
  56. " 'model', os.path.join(checkpoint_dir, 'model.py'))\n"
  57. " model_module = importlib.util.module_from_spec(model_spec)\n"
  58. " \n"
  59. " # run import, this also resumes simulation from the checkpoint\n"
  60. " model_spec.loader.exec_module(model_module)\n"
  61. "\n"
  62. " # exit after simulation has finished successfully\n"
  63. " sys.exit(0)\n\n"
  64. ;
  65. }
  66. std::string get_argparse_w_customization_end() {
  67. return
  68. " else:\n"
  69. " print(\"Error: invalid command line arguments\")\n"
  70. " print(\" usage: \" + sys.argv[0] + \"[-seed N]\")\n"
  71. " sys.exit(1)\n";
  72. }
  73. std::string get_user_defined_configuration(const std::string& customization_module) {
  74. return
  75. "if " + customization_module + " and '" + CUSTOM_CONFIG + "' in dir(" + customization_module + "):\n"
  76. " # user-defined model configuration\n"
  77. " " + customization_module + "." + CUSTOM_CONFIG + "(" + MODEL + ")\n"
  78. ;
  79. }
  80. std::string get_abs_path(const std::string file) {
  81. return std::string("os.path.join(") + MODEL_PATH + ", '" + file + "')";
  82. }
  83. std::string get_import(const std::string module) {
  84. return "import " + module + "\n";
  85. }
  86. std::string get_import_star(const std::string module) {
  87. return "from " + module + " import *\n";
  88. }
  89. std::string get_template_custom_init_and_run(const std::string& parameters_module) {
  90. return
  91. "\"\"\"\n"
  92. "def custom_init_and_run(model):\n"
  93. " # When uncommented, this function is called after all the model\n"
  94. " # components defined in CellBlender were added to the model.\n"
  95. " # It allows to add additional model components before initialization \n"
  96. " # is done and then to customize how simulation is ran.\n"
  97. " # The module parameters must be imported locally otherwise\n"
  98. " # changes to shared.parameter_overrides done elsewhere won't be applied.\n"
  99. " import " + parameters_module + " as " + PARAMETERS + "\n" +
  100. " model.initialize()\n"
  101. " model.run_iterations(parameters.ITERATIONS)\n"
  102. " model.end_simulation()\n"
  103. "\"\"\"\n";
  104. }
  105. } // namespace API
  106. } // namespace MCell