gen_viz_output.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 <sstream>
  12. #include "api/pybind11_stl_include.h"
  13. #include "api/python_export_utils.h"
  14. #include "gen_viz_output.h"
  15. #include "api/viz_output.h"
  16. #include "api/species.h"
  17. namespace MCell {
  18. namespace API {
  19. void GenVizOutput::check_semantics() const {
  20. }
  21. void GenVizOutput::set_initialized() {
  22. vec_set_initialized(species_list);
  23. initialized = true;
  24. }
  25. void GenVizOutput::set_all_attributes_as_default_or_unset() {
  26. class_name = "VizOutput";
  27. output_files_prefix = STR_UNSET;
  28. species_list = std::vector<std::shared_ptr<Species>>();
  29. mode = VizMode::ASCII;
  30. every_n_timesteps = 1;
  31. }
  32. std::shared_ptr<VizOutput> GenVizOutput::copy_viz_output() const {
  33. std::shared_ptr<VizOutput> res = std::make_shared<VizOutput>(DefaultCtorArgType());
  34. res->class_name = class_name;
  35. res->output_files_prefix = output_files_prefix;
  36. res->species_list = species_list;
  37. res->mode = mode;
  38. res->every_n_timesteps = every_n_timesteps;
  39. return res;
  40. }
  41. std::shared_ptr<VizOutput> GenVizOutput::deepcopy_viz_output(py::dict) const {
  42. std::shared_ptr<VizOutput> res = std::make_shared<VizOutput>(DefaultCtorArgType());
  43. res->class_name = class_name;
  44. res->output_files_prefix = output_files_prefix;
  45. for (const auto& item: species_list) {
  46. res->species_list.push_back((is_set(item)) ? item->deepcopy_species() : nullptr);
  47. }
  48. res->mode = mode;
  49. res->every_n_timesteps = every_n_timesteps;
  50. return res;
  51. }
  52. bool GenVizOutput::__eq__(const VizOutput& other) const {
  53. return
  54. output_files_prefix == other.output_files_prefix &&
  55. vec_ptr_eq(species_list, other.species_list) &&
  56. mode == other.mode &&
  57. every_n_timesteps == other.every_n_timesteps;
  58. }
  59. bool GenVizOutput::eq_nonarray_attributes(const VizOutput& other, const bool ignore_name) const {
  60. return
  61. output_files_prefix == other.output_files_prefix &&
  62. true /*species_list*/ &&
  63. mode == other.mode &&
  64. every_n_timesteps == other.every_n_timesteps;
  65. }
  66. std::string GenVizOutput::to_str(const bool all_details, const std::string ind) const {
  67. std::stringstream ss;
  68. ss << get_object_name() << ": " <<
  69. "output_files_prefix=" << output_files_prefix << ", " <<
  70. "\n" << ind + " " << "species_list=" << vec_ptr_to_str(species_list, all_details, ind + " ") << ", " << "\n" << ind + " " <<
  71. "mode=" << mode << ", " <<
  72. "every_n_timesteps=" << every_n_timesteps;
  73. return ss.str();
  74. }
  75. py::class_<VizOutput> define_pybinding_VizOutput(py::module& m) {
  76. return py::class_<VizOutput, std::shared_ptr<VizOutput>>(m, "VizOutput", "Defines a visualization output with locations of molecules \nthat can be then loaded by CellBlender.\n")
  77. .def(
  78. py::init<
  79. const std::string&,
  80. const std::vector<std::shared_ptr<Species>>,
  81. const VizMode,
  82. const double
  83. >(),
  84. py::arg("output_files_prefix") = STR_UNSET,
  85. py::arg("species_list") = std::vector<std::shared_ptr<Species>>(),
  86. py::arg("mode") = VizMode::ASCII,
  87. py::arg("every_n_timesteps") = 1
  88. )
  89. .def("check_semantics", &VizOutput::check_semantics)
  90. .def("__copy__", &VizOutput::copy_viz_output)
  91. .def("__deepcopy__", &VizOutput::deepcopy_viz_output, py::arg("memo"))
  92. .def("__str__", &VizOutput::to_str, py::arg("all_details") = false, py::arg("ind") = std::string(""))
  93. .def("__eq__", &VizOutput::__eq__, py::arg("other"))
  94. .def("dump", &VizOutput::dump)
  95. .def_property("output_files_prefix", &VizOutput::get_output_files_prefix, &VizOutput::set_output_files_prefix, "Prefix for the viz output files.\nWhen not set, the default prefix value is computed from the simulation seed\nwhen the model is initialized to: \n'./viz_data/seed_' + str(seed).zfill(5) + '/Scene'.\n")
  96. .def_property("species_list", &VizOutput::get_species_list, &VizOutput::set_species_list, py::return_value_policy::reference, "Specifies a list of species to be visualized, when empty, all_species will be generated.")
  97. .def_property("mode", &VizOutput::get_mode, &VizOutput::set_mode, "Specified the output format of the visualization files. \nVizMode.ASCII is a readable representation, VizMode.CELLBLENDER is a binary representation \nthat cannot be read using a text editor but is faster to generate. \n")
  98. .def_property("every_n_timesteps", &VizOutput::get_every_n_timesteps, &VizOutput::set_every_n_timesteps, "Specifies periodicity of visualization output.\nValue is truncated (floored) to an integer.\nValue 0 means that the viz output is ran only once at iteration 0. \n")
  99. ;
  100. }
  101. std::string GenVizOutput::export_to_python(std::ostream& out, PythonExportContext& ctx) {
  102. if (!export_even_if_already_exported() && ctx.already_exported(this)) {
  103. return ctx.get_exported_name(this);
  104. }
  105. std::string exported_name = "viz_output_" + std::to_string(ctx.postinc_counter("viz_output"));
  106. if (!export_even_if_already_exported()) {
  107. ctx.add_exported(this, exported_name);
  108. }
  109. bool str_export = export_as_string_without_newlines();
  110. std::string nl = "";
  111. std::string ind = " ";
  112. std::stringstream ss;
  113. if (!str_export) {
  114. nl = "\n";
  115. ind = " ";
  116. ss << exported_name << " = ";
  117. }
  118. ss << "m.VizOutput(" << nl;
  119. if (output_files_prefix != STR_UNSET) {
  120. ss << ind << "output_files_prefix = " << "'" << output_files_prefix << "'" << "," << nl;
  121. }
  122. if (species_list != std::vector<std::shared_ptr<Species>>() && !skip_vectors_export()) {
  123. ss << ind << "species_list = " << export_vec_species_list(out, ctx, exported_name) << "," << nl;
  124. }
  125. if (mode != VizMode::ASCII) {
  126. ss << ind << "mode = " << mode << "," << nl;
  127. }
  128. if (every_n_timesteps != 1) {
  129. ss << ind << "every_n_timesteps = " << f_to_str(every_n_timesteps) << "," << nl;
  130. }
  131. ss << ")" << nl << nl;
  132. if (!str_export) {
  133. out << ss.str();
  134. return exported_name;
  135. }
  136. else {
  137. return ss.str();
  138. }
  139. }
  140. std::string GenVizOutput::export_vec_species_list(std::ostream& out, PythonExportContext& ctx, const std::string& parent_name) {
  141. // does not print the array itself to 'out' and returns the whole list
  142. std::stringstream ss;
  143. ss << "[";
  144. for (size_t i = 0; i < species_list.size(); i++) {
  145. const auto& item = species_list[i];
  146. if (i == 0) {
  147. ss << " ";
  148. }
  149. else if (i % 16 == 0) {
  150. ss << "\n ";
  151. }
  152. if (!item->skip_python_export()) {
  153. std::string name = item->export_to_python(out, ctx);
  154. ss << name << ", ";
  155. }
  156. }
  157. ss << "]";
  158. return ss.str();
  159. }
  160. } // namespace API
  161. } // namespace MCell