-
Notifications
You must be signed in to change notification settings - Fork 12
/
cellblender_reaction_output.py
1229 lines (1028 loc) · 55.5 KB
/
cellblender_reaction_output.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
"""
This file contains the classes for CellBlender's Reaction Output.
"""
import glob
import os
import tempfile
import json
import cellblender
# blender imports
import bpy
from bpy.props import BoolProperty, CollectionProperty, EnumProperty, \
FloatProperty, FloatVectorProperty, IntProperty, \
IntVectorProperty, PointerProperty, StringProperty
#from bpy.app.handlers import persistent
#import math
#import mathutils
# python imports
import re
# CellBlender imports
import cellblender
from . import data_model
from . import parameter_system
from . import cellblender_release
from . import cellblender_utils
from . import cellblender_pbc
from cellblender.cellblender_utils import project_files_path, mcell_files_path, get_python_path
# Reaction Output Operators:
class MCELL_OT_rxn_output_add(bpy.types.Operator):
bl_idname = "mcell.rxn_output_add"
bl_label = "Add Reaction Data Output"
bl_description = "Add new reaction data output to an MCell model"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
mcell = context.scene.mcell
mcell.rxn_output.rxn_output_list.add()
mcell.rxn_output.active_rxn_output_index = len(
mcell.rxn_output.rxn_output_list)-1
check_rxn_output(self, context)
return {'FINISHED'}
class MCELL_OT_rxn_output_remove(bpy.types.Operator):
bl_idname = "mcell.rxn_output_remove"
bl_label = "Remove Reaction Data Output"
bl_description = "Remove selected reaction data output from an MCell model"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
mcell = context.scene.mcell
mcell.rxn_output.rxn_output_list.remove(
mcell.rxn_output.active_rxn_output_index)
mcell.rxn_output.active_rxn_output_index -= 1
if (mcell.rxn_output.active_rxn_output_index < 0):
mcell.rxn_output.active_rxn_output_index = 0
if mcell.rxn_output.rxn_output_list:
check_rxn_output(self, context)
return {'FINISHED'}
class MCELL_OT_rxn_output_disable_all(bpy.types.Operator):
bl_idname = "mcell.rxn_output_disable_all"
bl_label = "Disable All Plotting"
bl_description = "Disable All Items for Plotting"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
mcell = context.scene.mcell
for rxn_output in mcell.rxn_output.rxn_output_list:
rxn_output.plotting_enabled = False
return {'FINISHED'}
class MCELL_OT_rxn_output_enable_all(bpy.types.Operator):
bl_idname = "mcell.rxn_output_enable_all"
bl_label = "Enable All Plotting"
bl_description = "Enable All Items for Plotting"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
mcell = context.scene.mcell
for rxn_output in mcell.rxn_output.rxn_output_list:
rxn_output.plotting_enabled = True
return {'FINISHED'}
class MCELL_OT_add_all_world(bpy.types.Operator):
bl_idname = "mcell.rxn_out_all_world"
bl_label = "Add all in world"
bl_description = "Add a count statement for each molecule in the world"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
mcell = context.scene.mcell
mol_list = mcell.molecules.molecule_list
for mol in mol_list:
new_rxn_out = mcell.rxn_output.rxn_output_list.add()
mcell.rxn_output.active_rxn_output_index = len(mcell.rxn_output.rxn_output_list)-1
new_rxn_out.molecule_name = mol.name
new_rxn_out.count_location = "World"
new_rxn_out.rxn_or_mol = "Molecule"
check_rxn_output(self, context)
return {'FINISHED'}
# This is just a means to store a temporary file path for the duration of a
# Blender session.
class ReactionDataTmpFile:
reactdata_tmpfile = None
def create_reactdata_tmpfile(data_path):
if not ReactionDataTmpFile.reactdata_tmpfile:
with tempfile.NamedTemporaryFile(
delete=False, mode='wt', prefix="cellblender") as tmpfile:
tmpfile.write(data_path)
ReactionDataTmpFile.reactdata_tmpfile = tmpfile.name
else:
with open(ReactionDataTmpFile.reactdata_tmpfile, 'w') as tmpfile:
tmpfile.write(data_path)
def remove_mdl_string_suffix(path):
s = '_MDLString'
path_ext = os.path.splitext(path)
if path_ext[0].endswith(s):
return path_ext[0][:-len(s)] + path_ext[1]
else:
return path
class MCELL_OT_plot_rxn_output_with_selected(bpy.types.Operator):
bl_idname = "mcell.plot_rxn_output_with_selected"
bl_label = "Plot"
bl_description = "Plot the reactions using selected plotting package"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
mcell = context.scene.mcell
plot_sep = mcell.rxn_output.plot_layout
plot_legend = mcell.rxn_output.plot_legend
###use_sweep = mcell.rxn_output.use_sweep
combine_seeds = mcell.rxn_output.combine_seeds
mol_colors = mcell.rxn_output.mol_colors
# Look up the plotting module by its name
mod_name = None
for plot_module in cellblender.cellblender_info[
'cellblender_plotting_modules']:
mod_name = plot_module.get_name()
if mod_name == mcell.rxn_output.plotter_to_use:
break
if mod_name == None:
return {'FINISHED'}
files_path = mcell_files_path() # This will include the "mcell" on the end
DATA_LAYOUT_FILE = "data_layout.json"
KEY_MCELL4_MODE = "mcell4_mode"
mcell4_mode = False
if os.path.exists(DATA_LAYOUT_FILE):
f = open ( os.path.join(DATA_LAYOUT_FILE), 'r' )
layout_spec = json.loads ( f.read() )
f.close()
mcell4_mode = KEY_MCELL4_MODE in layout_spec and layout_spec[KEY_MCELL4_MODE]
if not mcell4_mode:
# output obtained by running simulation in cellblender
# Determine if this data structure is in the newer sweep format or not
use_sweep = 'output_data' in os.listdir(files_path)
else:
# output obtained from mcell4 - expecting current directory to contain
# data_layout.json
files_path = os.getcwd()
use_sweep = True
root_path = files_path
data_paths = []
if use_sweep:
# Build the list from the sweep data file
root_path = files_path
f = open ( os.path.join(files_path, DATA_LAYOUT_FILE), 'r' )
layout_spec = json.loads ( f.read() )
f.close()
data_layout_version = 0
if 'version' in layout_spec:
data_layout_version = layout_spec['version']
data_layout = layout_spec['data_layout']
print ( "data_layout = " + str(layout_spec) )
num_runs = 1
if data_layout_version == 2:
for level in data_layout:
if (level[0] != '/DIR') and (level[0] != '/FILE_TYPE') and (level[0] != '/SEED'):
# Multiply by the number of sweep points in each level (stored at index 1)
num_runs *= len(level[1])
# append a counter to keep track of looping through levels
level.append ( 0 )
print ( "num_runs = " + str(num_runs) + ", data_layout = " + str(layout_spec) )
# Use the counters to count up the paths from the reversed layout
data_layout.reverse()
run_num = 0
while run_num < num_runs:
print ( "Preparing run " + str(run_num) )
counters = ""
for level in data_layout:
counters = counters + " " + level[0] + ":" + str(level[2])
print ( " Counters = " + counters )
run_path = ""
par_path = ""
for level in data_layout:
if (level[0] == '/DIR'):
run_path = os.path.join ( level[1][0], run_path )
elif (level[0] == '/FILE_TYPE'):
run_path = os.path.join ( level[1][0], run_path )
elif (level[0] == '/SEED'):
pass
else:
# Build on to the run path (used for getting data)
run_path = os.path.join ( level[0] + "_index_" + str(level[2]), run_path )
# Build on to the par path (used for displaying label)
if len(par_path) > 0:
par_path = "," + par_path
par_path = level[0] + "=" + str(level[1][level[2]]) + par_path
run_num += 1
run_path = run_path.strip ( os.path.sep )
# Each point in data_paths will contain a run_path and a parameter "path"
data_paths.append ( [ run_path, par_path ] )
print ( "Run path = " + run_path )
print ( "Parameter Point for run is: " + par_path )
# Increment the counters
for level in data_layout:
if (level[0] == '/DIR'):
pass
elif (level[0] == '/FILE_TYPE'):
pass
elif (level[0] == '/SEED'):
pass
else:
level[2] += 1
if (level[2] < len(level[1])):
# This counter didn't roll over, so there's no need to carry into the next one. Break.
break
else:
# This counter did roll over, so go back to zero and continue to increment the next one
level[2] = 0
else:
for level in data_layout:
if (level[0] != 'dir') and (level[0] != 'file_type') and (level[0] != 'SEED'):
# Multiply by the number of sweep points in each level (stored at index 1)
num_runs *= len(level[1])
# append a counter to keep track of looping through levels
level.append ( 0 )
print ( "num_runs = " + str(num_runs) + ", data_layout = " + str(layout_spec) )
# Use the counters to count up the paths from the reversed layout
data_layout.reverse()
run_num = 0
while run_num < num_runs:
print ( "Preparing run " + str(run_num) )
counters = ""
for level in data_layout:
counters = counters + " " + level[0] + ":" + str(level[2])
print ( " Counters = " + counters )
run_path = ""
for level in data_layout:
if (level[0] == 'dir'):
run_path = os.path.join ( level[1][0], run_path )
elif (level[0] == 'file_type'):
run_path = os.path.join ( level[1][0], run_path )
elif (level[0] == 'SEED'):
pass
else:
run_path = os.path.join ( level[0] + "_index_" + str(level[2]), run_path )
run_num += 1
run_path = run_path.strip ( os.path.sep )
# Each point in data_paths will contain a run_path and a parameter "path"
data_paths.append ( [ run_path, run_path ] )
print ( "Run path = " + run_path )
# Increment the counters
for level in data_layout:
if (level[0] == 'dir'):
pass
elif (level[0] == 'file_type'):
pass
elif (level[0] == 'SEED'):
pass
else:
level[2] += 1
if (level[2] < len(level[1])):
# This counter didn't roll over, so there's no need to carry into the next one. Break.
break
else:
# This counter did roll over, so go back to zero and continue to increment the next one
level[2] = 0
else:
# This is a pre-sweeping directory structure, so build a list containing a single item
root_path = os.path.join(files_path, "react_data")
data_paths.append ( [ "", "" ] )
# Plot the data via this module
# print("Preparing to call %s" % (mod_name))
# The mcell_files_path is now where the MDL lives:
create_reactdata_tmpfile(files_path)
plot_spec_string = "xlabel=time(s) ylabel=count "
if plot_legend != 'x':
plot_spec_string = plot_spec_string + "legend=" + plot_legend
for data_path in data_paths:
for rxn_output in mcell.rxn_output.rxn_output_list:
print("mdl_file_prefix: " + rxn_output.mdl_file_prefix)
if rxn_output.plotting_enabled:
molecule_name = rxn_output.molecule_name
object_name = rxn_output.object_name
region_name = rxn_output.region_name
file_name = None
if rxn_output.rxn_or_mol == 'Molecule':
if rxn_output.count_location == 'World':
file_name = "%s.World.dat" % (molecule_name)
elif rxn_output.count_location == 'Object':
file_name = "%s.%s.dat" % (molecule_name, object_name)
elif rxn_output.count_location == 'Region':
file_name = "%s.%s.%s.dat" % (molecule_name,
object_name, region_name)
elif rxn_output.rxn_or_mol == 'Reaction':
rxn_name = rxn_output.reaction_name
if rxn_output.count_location == 'World':
file_name = "%s.World.dat" % (rxn_name)
elif rxn_output.count_location == 'Object':
file_name = "%s.%s.dat" % (rxn_name, object_name)
elif rxn_output.count_location == 'Region':
file_name = "%s.%s.%s.dat" % (rxn_name,
object_name, region_name)
elif rxn_output.rxn_or_mol == 'MDLString':
if not mcell4_mode:
file_name = rxn_output.mdl_file_prefix + "_MDLString.dat"
else:
# in MCell4, we need to keep the name of the file the user entered
file_name = rxn_output.mdl_file_prefix + ".dat"
elif rxn_output.rxn_or_mol == 'File':
file_name = rxn_output.data_file_name
print ( "Preparing to plot a File with file_name = " + file_name )
if file_name:
first_pass = True
if rxn_output.rxn_or_mol == 'File':
# Assume that the file path is blend file relative (begins with "//")
if file_name.startswith ( "//" ):
# Convert the file name from blend file relative to react_data folder relative:
candidate_file_list = [ os.path.pardir + os.path.sep + os.path.pardir + os.path.sep + file_name[2:] ]
else:
# Use the file name as absolute
candidate_file_list = [ file_name ]
else:
# Prepend a search across all seeds for this file
file_name = os.path.join("seed_*", file_name)
if len(data_path[0]) > 0:
base_path = os.path.join(root_path, data_path[0])
else:
base_path = root_path
print ( "glob of " + os.path.join(base_path, file_name) )
candidate_file_list = glob.glob(os.path.join(base_path, file_name))
print ( "Candidate list = " + str(candidate_file_list) )
if not candidate_file_list and '_MDLString' in file_name:
# try to search without the _MDLString suffix
pattern_wo_mdlstring = os.path.join(base_path, remove_mdl_string_suffix(file_name))
print ( " - glob did not find any files, searching for " + pattern_wo_mdlstring)
candidate_file_list = glob.glob(pattern_wo_mdlstring)
if not candidate_file_list:
# try to search with '.' replaced with '_'
name_ext = os.path.splitext(file_name)
pattern_wo_dots = os.path.join(base_path, name_ext[0].replace('.', '_') + name_ext[1])
print ( " - glob did not find any files, searching for " + pattern_wo_dots)
candidate_file_list = glob.glob(pattern_wo_dots)
# Without sorting, the seeds may not be increasing
candidate_file_list.sort()
#print("Candidate file list for %s:" % (file_name))
#print(" ", candidate_file_list)
if not mcell4_mode and not mcell.rxn_output.ignore_start_time:
# TODO: support this also for mcell4
# Use the start_time.txt file to find files modified since MCell was started (minus 10 seconds)
start_time = os.stat(os.path.join(project_files_path(), "start_time.txt")).st_mtime - 10
# This file is both in the list and newer than the run time for MCell
candidate_file_list = [ffn for ffn in candidate_file_list if os.stat(ffn).st_mtime >= start_time]
print ( "Candidate list = " + str(candidate_file_list) )
for ffn in candidate_file_list:
print ( " Files path = " + str(files_path) )
print ( " Candidate file = " + str(ffn) )
print ( " Parameter Point = " + str(data_path[1]) )
par_string = ""
if len(data_path[1]) > 0:
par_string = " ppt=" + data_path[1]
f = None
if rxn_output.rxn_or_mol == 'File':
# Use the file name as it is
f = ffn
else:
# Create f as a relative path containing seed/file
#split1 = os.path.split(ffn)
#split2 = os.path.split(split1[0])
#f = os.path.join(split2[1], split1[1])
f = ffn[len(root_path)+1:]
color_string = ""
if rxn_output.rxn_or_mol == 'Molecule' and mol_colors:
# Use molecule colors for graphs
# Should be standardized!!
mol_mat_name = "mol_%s_mat" % (molecule_name)
#print ("Molecule Material Name = ", mol_mat_name)
#Look up the material
mats = bpy.data.materials
mol_color = mats.get(mol_mat_name).diffuse_color
#print("Molecule color = ", mol_mat.diffuse_color)
mol_color_red = int(255 * mol_color[0])
mol_color_green = int(255 * mol_color[1])
mol_color_blue = int(255 * mol_color[2])
color_string = " color=#%2.2x%2.2x%2.2x " % (
mol_color_red, mol_color_green, mol_color_blue)
base_name = os.path.basename(f)
if combine_seeds:
title_string = " title=" + base_name
else:
title_string = " title=" + f
if plot_sep == ' ':
# No title when all are on the same plot since only
# last will show
title_string = ""
if combine_seeds:
psep = " "
if first_pass:
psep = plot_sep
first_pass = False
plot_spec_string = (plot_spec_string + psep + color_string + title_string + par_string + " f=" + f)
else:
plot_spec_string = (plot_spec_string + plot_sep + color_string + title_string + par_string + " f=" + f)
plot_spec_string += " tf="+ReactionDataTmpFile.reactdata_tmpfile
print("Plotting from", root_path)
print("Plotting spec", plot_spec_string)
python_path = get_python_path(mcell=mcell)
plot_module.plot(root_path, plot_spec_string, python_path)
return {'FINISHED'}
# Reaction Output callback functions
def check_rxn_output(self, context):
""" Format reaction data output. """
mcell = context.scene.mcell
rxn_output_list = mcell.rxn_output.rxn_output_list
rxn_output = rxn_output_list[
mcell.rxn_output.active_rxn_output_index]
mol_list = mcell.molecules.molecule_list
reaction_list = mcell.reactions.reaction_name_list
molecule_name = rxn_output.molecule_name
reaction_name = rxn_output.reaction_name
obj_list = mcell.model_objects.object_list
object_name = rxn_output.object_name
region_name = rxn_output.region_name
rxn_output_name = ""
status = ""
count_name = ""
if rxn_output.rxn_or_mol == 'Reaction':
count_name = reaction_name
name_list = reaction_list
elif rxn_output.rxn_or_mol == 'Molecule':
count_name = molecule_name
name_list = mol_list
elif rxn_output.rxn_or_mol == 'MDLString':
count_name = molecule_name
rxn_output.status = ""
# Can't do these here because this causes another check_rxn_output call (infinite recursion)
#rxn_output.name = rxn_output.mdl_string
#rxn_output.name = "MDL: " + rxn_output.mdl_string
return
elif rxn_output.rxn_or_mol == 'File':
print ( "Checked file name and got " + rxn_output.name )
rxn_output.status = ""
# Can't do these here because this causes another check_rxn_output call (infinite recursion)
#rxn_output.name = rxn_output.mdl_string
rxn_output_name = "FILE: " + rxn_output.data_file_name
if rxn_output.name != rxn_output_name:
rxn_output.name = rxn_output_name
return
else:
pass
try:
region_list = bpy.data.objects[object_name].mcell.regions.region_list
except KeyError:
# The object name isn't a blender object
region_list = []
# Check for illegal names (Starts with a letter. No special characters.)
count_filter = r"(^[A-Za-z]+[0-9A-Za-z_.]*)"
c = re.match(count_filter, count_name)
if c is None:
status = "Name error: %s" % (count_name)
else:
# Check for undefined molecule or reaction names
c_name = c.group(1)
if not c_name in name_list:
status = "Undefined: %s" % (c_name)
# Use different formatting depending on where we are counting
if rxn_output.count_location == 'World':
rxn_output_name = "Count %s in World" % (count_name)
elif rxn_output.count_location == 'Object':
if not object_name in obj_list:
status = "Undefined object: %s" % object_name
else:
rxn_output_name = "Count %s in/on %s" % (
count_name, object_name)
elif rxn_output.count_location == 'Region':
if not region_name in region_list:
status = "Undefined region: %s" % region_name
else:
rxn_output_name = "Count %s in/on %s[%s]" % (
count_name, object_name, region_name)
# Only update reaction output if necessary to avoid infinite recursion
if rxn_output.name != rxn_output_name:
rxn_output.name = rxn_output_name
# Check for duplicate reaction data
rxn_output_keys = rxn_output_list.keys()
if rxn_output_keys.count(rxn_output.name) > 1 and not status:
status = "Duplicate reaction output: %s" % (rxn_output.name)
rxn_output.status = status
return
def update_name_and_check_rxn_output(self, context):
# Set the name to show the MDL
mcell = context.scene.mcell
rxn_output_list = mcell.rxn_output.rxn_output_list
rxn_output = rxn_output_list[mcell.rxn_output.active_rxn_output_index]
if rxn_output.rxn_or_mol == 'MDLString':
rxn_output.name = "MDL: " + rxn_output.mdl_string
elif rxn_output.rxn_or_mol == 'File':
rxn_output.name = "FILE: " + rxn_output.data_file_name
# Now perform the normal reaction output check:
check_rxn_output(self, context)
return
# Reaction Output Panel Classes
class MCELL_UL_check_reaction_output_settings(bpy.types.UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
if item.status:
layout.label(text=item.status, icon='ERROR')
else:
col = layout.column()
if item.plotting_enabled:
col.label(text=item.name, icon='CHECKMARK')
else:
col.label(text=item.name, icon='BLANK1')
col = layout.column()
if item.plotting_enabled:
col.prop(item, "plotting_enabled", text="", icon='HIDE_OFF')
else:
col.prop(item, "plotting_enabled", text="", icon='HIDE_ON')
# Reaction Output Property Groups
class MCellReactionOutputProperty(bpy.types.PropertyGroup):
name: StringProperty(
name="Reaction Output", update=check_rxn_output)
description: StringProperty(name="Description", default="")
molecule_name: StringProperty(
name="Molecule",
description="Count the selected molecule.",
update=check_rxn_output)
reaction_name: StringProperty(
name="Reaction",
description="Count the selected reaction.",
update=check_rxn_output)
# allows the user to define a literal mdl string to count using complex expressions. E.g. 2*S1
mdl_string: StringProperty(
name="MDL Definition",
description="Count using a literal MDL definition.",
update=update_name_and_check_rxn_output)
mdl_file_prefix: StringProperty(
name="MDL File Prefix",
description="Prefix name for this file." )
data_file_name: StringProperty ( name = "Data File Name", subtype='FILE_PATH', default="", description="Data File to Plot.", update=check_rxn_output )
object_name: StringProperty(
name="Object", update=check_rxn_output)
region_name: StringProperty(
name="Region", update=check_rxn_output)
count_location_enum = [
('World', "World", ""),
('Object', "Object", ""),
('Region', "Region", "")]
count_location: EnumProperty(
items=count_location_enum, name="Count Location",
description="Count all molecules in the selected location.",
update=check_rxn_output)
rxn_or_mol_enum = [
('Reaction', "Reaction", ""),
('Molecule', "Molecule", ""),
('MDLString', "MDLString", ""),
('File', "File", "")]
rxn_or_mol: EnumProperty(
items=rxn_or_mol_enum, name="Count Reaction or Molecule",
default='Molecule',
description="Select between counting a reaction or molecule.",
update=update_name_and_check_rxn_output)
plotting_enabled: BoolProperty ( default=True, description="Enable this item in plotting output" )
description_show_help: BoolProperty ( default=False, description="Toggle more information about this parameter" )
mdl_string_show_help: BoolProperty ( default=False, description="Toggle more information about this item" )
mdl_file_prefix_show_help: BoolProperty ( default=False, description="Toggle more information about this item" )
data_file_name_show_help: BoolProperty ( default=False, description="Toggle more information about this item" )
# plot_command: StringProperty(name="Command") # , update=check_rxn_output)
status: StringProperty(name="Status")
def build_data_model_from_properties ( self, context ):
print ( "Reaction Output building Data Model" )
ro_dm = {}
ro_dm['data_model_version'] = "DM_2018_01_11_1330"
ro_dm['name'] = self.name
ro_dm['description'] = self.description
ro_dm['molecule_name'] = self.molecule_name
ro_dm['reaction_name'] = self.reaction_name
ro_dm['mdl_string'] = self.mdl_string
ro_dm['mdl_file_prefix'] = self.mdl_file_prefix
ro_dm['data_file_name'] = self.data_file_name
ro_dm['object_name'] = self.object_name
ro_dm['region_name'] = self.region_name
ro_dm['count_location'] = self.count_location
ro_dm['rxn_or_mol'] = self.rxn_or_mol
ro_dm['plotting_enabled'] = self.plotting_enabled
return ro_dm
@staticmethod
def upgrade_data_model ( dm ):
# Upgrade the data model as needed. Return updated data model or None if it can't be upgraded.
print ( "------------------------->>> Upgrading MCellReactionOutputProperty Data Model" )
if not ('data_model_version' in dm):
# Make changes to move from unversioned to DM_2014_10_24_1638
dm['data_model_version'] = "DM_2014_10_24_1638"
if dm['data_model_version'] == "DM_2014_10_24_1638":
dm['mdl_string'] = ""
dm['data_model_version'] = "DM_2015_07_24_2311"
if dm['data_model_version'] == "DM_2015_07_24_2311":
# Versions prior to this re-used the molecule name to hold the file prefix, so check and copy
if dm['rxn_or_mol'] == "MDLString":
dm['mdl_file_prefix'] = dm['molecule_name']
dm['molecule_name'] = ""
else:
dm['mdl_file_prefix'] = ""
dm['data_model_version'] = "DM_2015_10_07_1500"
if dm['data_model_version'] == "DM_2015_10_07_1500":
# Add the plotting_enabled flag with a default of true (since previous versions plotted everything)
dm['plotting_enabled'] = True
# Add the data file name with the default of an empty string
dm['data_file_name'] = ""
# Update the data model version for this item
dm['data_model_version'] = "DM_2016_03_15_1800"
if dm['data_model_version'] == "DM_2016_03_15_1800":
# Change on January 11th, 2018 to add a description field to reaction output items
dm['description'] = ""
dm['data_model_version'] = "DM_2018_01_11_1330"
# Check that the upgraded data model version matches the version for this property group
if dm['data_model_version'] != "DM_2018_01_11_1330":
data_model.flag_incompatible_data_model ( "Upgrade Error: Unable to upgrade MCellReactionOutputProperty data model to current version." )
return None
return dm
def build_properties_from_data_model ( self, context, dm ):
# Check that the data model version matches the version for this property group
if dm['data_model_version'] != "DM_2018_01_11_1330":
data_model.handle_incompatible_data_model ( "Build Error: Unable to upgrade MCellReactionOutputProperty data model to current version." )
self.name = dm["name"]
self.description = dm["description"]
self.molecule_name = dm["molecule_name"]
self.reaction_name = dm["reaction_name"]
self.mdl_string = dm['mdl_string']
self.mdl_file_prefix = dm['mdl_file_prefix']
self.data_file_name = dm['data_file_name']
self.object_name = dm["object_name"]
self.region_name = dm["region_name"]
self.count_location = dm["count_location"]
self.rxn_or_mol = dm["rxn_or_mol"]
self.plotting_enabled = dm["plotting_enabled"]
def check_properties_after_building ( self, context ):
print ( "check_properties_after_building not implemented for " + str(self) )
def remove_properties ( self, context ):
print ( "Removing all Reaction Output Properties... no collections to remove." )
import cellblender
def get_plotters_as_items(scene, context):
prefs = [ "MatPlotLib Plotter", "XmGrace Plotter", "GnuPlot Plotter", "Java Plotter", "Simple Plotter" ]
found = []
available = []
for plot_module in cellblender.cellblender_info['cellblender_plotting_modules']:
mod_name = plot_module.get_name()
found.append ( mod_name )
# Order the items according to hard-coded preferences
for p in prefs:
if p in found:
available.append ( p )
# Include the unlisted plotters at the end
for p in found:
if not (p in available):
available.append ( p )
items = []
for p in available:
items.append ( (p, p, "") )
return items
class MCellReactionOutputPropertyGroup(bpy.types.PropertyGroup):
rxn_step: PointerProperty ( name="Step",
type=parameter_system.Parameter_Reference )
output_buf_size: PointerProperty ( name="OutBufSize",
type=parameter_system.Parameter_Reference )
active_rxn_output_index: IntProperty(
name="Active Reaction Output Index", default=0)
virt_x : IntProperty(name="X Position", default=0,
description="The X coordinate of the Virtual Box")
virt_y : IntProperty(name="Y Position", default=0,
description="The Y coordinate of the Virtual Box")
virt_z : IntProperty(name="Z Position", default=0,
description="The Z coordinate of the Virtual Box")
all_enc : BoolProperty(name="Enable All enclosed", default = False,
description = 'Count all molecules or reactions that occur in the area enclosed by region ')
est_conc: BoolProperty(name="Enable Estimate Concentration", default = False,
description = 'Estimate the concentration of the volume molecule at that region, averaged since the beginning of the simulation.')
trig : BoolProperty(name = "Enable Triggers", default = False, description= "Tags molecules with their locations")
fr_bk: BoolProperty(name = "Show Hits/Crossings options",default = False)
hit_back : BoolProperty(name = "BACK_HITS",default = False)
hit_front : BoolProperty(name = "FRONT_HITS",default = False)
hit_all : BoolProperty(name = "ALL_HITS",default = False)
cross_front: BoolProperty(name = "FRONT_CROSSINGS",default = False)
cross_back : BoolProperty(name = "BACK_CROSSINGS",default = False)
cross_all : BoolProperty(name = "ALL_CROSSINGS",default = False)
rxn_output_list: CollectionProperty(type=MCellReactionOutputProperty, name="Reaction Output List")
plot_layout_enum = [
(' page ', "Separate Page for each Plot", ""),
(' plot ', "One Page, Multiple Plots", ""),
(' ', "One Page, One Plot", "")]
plot_layout: EnumProperty (
items=plot_layout_enum, name="",
description="Select the Page and Plot Layout",
default=' plot ' )
plot_legend_enum = [
('x', "No Legend", ""),
('0', "Legend with Automatic Placement", ""),
('1', "Legend in Upper Right", ""),
('2', "Legend in Upper Left", ""),
('3', "Legend in Lower Left", ""),
('4', "Legend in Lower Right", ""),
# ('5', "Legend on Right", ""), # This appears to duplicate option 7
('6', "Legend in Center Left", ""),
('7', "Legend in Center Right", ""),
('8', "Legend in Lower Center", ""),
('9', "Legend in Upper Center", ""),
('10', "Legend in Center", "")]
plot_legend: EnumProperty (
items=plot_legend_enum, name="",
description="Select the Legend Display and Placement",
default='0' )
combine_seeds: BoolProperty(
name="Combine Seeds",
description="Combine all seeds onto the same plot.",
default=True)
mol_colors: BoolProperty(
name="Molecule Colors",
description="Use Molecule Colors for line colors.",
default=False)
#use_sweep: BoolProperty(
# name="Use Sweep",
# description="Plot from the sweep file.",
# default=False)
plotter_to_use: EnumProperty(
name="", # "Plot with:",
description="Plotter to use.",
items=get_plotters_as_items )
always_generate: BoolProperty(
name="Always Generate All Plot Data",
description="Generate All Plot Data Regardless of Current View Setting",
default=True)
ignore_start_time: BoolProperty(
name="Ignore Start Time",
description="Ignore the start_time.txt file when plotting.",
default=False)
def init_properties ( self, parameter_system ):
self.rxn_step.init_ref (
parameter_system, user_name="Step",
user_expr="", user_units="", user_descr="Step\n"
"Output reaction data every t seconds.\nUses simulation time step when blank.")
self.output_buf_size.init_ref (
parameter_system, user_name="OutputBufSize",
user_expr="", user_units="", user_descr="OutputBufSize\n"
"Write output to disk after every N lines. Default is N=10000.")
def build_data_model_from_properties ( self, context ):
print ( "Reaction Output Panel building Data Model" )
ro_dm = {}
ro_dm['data_model_version'] = "DM_2016_03_15_1800"
try:
ro_dm['rxn_step'] = self.rxn_step.get_expr()
ro_dm['output_buf_size'] = self.output_buf_size.get_expr()
except:
print("WARNING: Exception caught while calling get_expr(), the resultign data model might not be correct.")
ro_dm['plot_layout'] = self.plot_layout
ro_dm['plot_legend'] = self.plot_legend
ro_dm['combine_seeds'] = self.combine_seeds
ro_dm['mol_colors'] = self.mol_colors
ro_dm['always_generate'] = self.always_generate
ro_list = []
for ro in self.rxn_output_list:
ro_list.append ( ro.build_data_model_from_properties(context) )
ro_dm['reaction_output_list'] = ro_list
return ro_dm
@staticmethod
def upgrade_data_model ( dm ):
# Upgrade the data model as needed. Return updated data model or None if it can't be upgraded.
print ( "------------------------->>> Upgrading MCellReactionOutputPropertyGroup Data Model" )
# Upgrade the data model as needed
if not ('data_model_version' in dm):
# Make changes to move from unversioned to DM_2014_10_24_1638
dm['data_model_version'] = "DM_2014_10_24_1638"
if dm['data_model_version'] == "DM_2014_10_24_1638":
dm['rxn_step'] = ""
dm['data_model_version'] = "DM_2015_05_15_1214"
if dm['data_model_version'] == "DM_2015_05_15_1214":
# Set "always_generate" to true to be consistent with previous behavior which generated all files
dm['always_generate'] = True
dm['data_model_version'] = "DM_2016_03_15_1800"
if dm['data_model_version'] == "DM_2016_03_15_1800":
dm['output_buf_size'] = ""
dm['data_model_version'] = "DM_2016_06_30_1600"
# Check that the upgraded data model version matches the version for this property group
if dm['data_model_version'] != "DM_2016_06_30_1600":
data_model.flag_incompatible_data_model ( "Error: Unable to upgrade MCellReactionOutputPropertyGroup data model to current version." )
return None
if "reaction_output_list" in dm:
for item in dm["reaction_output_list"]:
if MCellReactionOutputProperty.upgrade_data_model ( item ) == None:
return None
return dm
def build_properties_from_data_model ( self, context, dm ):
# Check that the data model version matches the version for this property group
if dm['data_model_version'] != "DM_2016_06_30_1600":
data_model.handle_incompatible_data_model ( "Error: Unable to upgrade MCellReactionOutputPropertyGroup data model to current version." )
self.init_properties(context.scene.mcell.parameter_system)
self.plot_layout = dm["plot_layout"]
self.plot_legend = dm["plot_legend"]
self.rxn_step.set_expr ( dm["rxn_step"] )
self.output_buf_size.set_expr ( dm["output_buf_size"] )
self.combine_seeds = dm["combine_seeds"]
self.mol_colors = dm["mol_colors"]
self.always_generate = dm["always_generate"]
while len(self.rxn_output_list) > 0:
self.rxn_output_list.remove(0)
if "reaction_output_list" in dm:
for r in dm["reaction_output_list"]:
self.rxn_output_list.add()
self.active_rxn_output_index = len(self.rxn_output_list)-1
ro = self.rxn_output_list[self.active_rxn_output_index]
# ro.init_properties(context.scene.mcell.parameter_system)
ro.build_properties_from_data_model ( context, r )
def check_properties_after_building ( self, context ):
print ( "check_properties_after_building not implemented for " + str(self) )
def remove_properties ( self, context ):
# Note that the two "Panel Parameters" (rxn_step and output_buf_size) in this group are static and should not be removed.
#self.rxn_step.clear_ref ( ps )
#self.output_buf_size.clear_ref ( ps )
print ( "Removing all Reaction Output Properties..." )
self.active_rxn_output_index = 0
for item in self.rxn_output_list:
item.remove_properties(context)