-
Notifications
You must be signed in to change notification settings - Fork 0
/
relaxes.py
1315 lines (998 loc) · 50.9 KB
/
relaxes.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
#! /usr/bin/python
# Classes:
#
# Parameters - Common information required to run experiments
# RateGenerator - build a set of rates for a rate experiment
# ExperimentSetup - generic setup for all the rate experiments
# TwoMechSetup -
#
# PairMechComparison - build kinetic comparisons between two mechanisms
# ConcResponse - calculate concentration-response curve
# Relaxation - calculate relaxation (open states...)
# Recovery - calculate recovery curve (desensitized/shut state relaxation?)
# Train - calculate response to a train of pulses
# ConcInhibition (ND) - calculate low concentration-inhibition relation
# PulseInhibition (ND)- prepulse inhibition (3 barrel) experiment
# RcjTrain (NOT DONE) - calculate train using realistic concentration jumps
#
# TrainExpt - calculate family of trains to see effect of changing a rate
# RecoveryExpt - calculate family of recovery curves
# CrExpt - calculate family of concentration response curves
# RelaxExpt - calculate family of relaxations
# JFExpt
#
# August 2013: ConcResponse added
# October 2013: building pairwise comparisons (for two mechanisms)
from __future__ import print_function
from math import exp, log10
import copy
import numpy
from mechanisms import mechanism
#from qmat import Q_mat
from core_utils import generate_Q, r_tuple_from_r_name, convert, convert_ms
__author__="Andrew"
__date__ ="$Jun 12, 2010 9:10:37 AM$"
class Parameters(dict):
"""Encapsulate some common information required to run the experiment
allow instantiation with a default set.
from http://stackoverflow.com/a/9550596
Data storage by subclassing dict"""
def __init__(self, parameters=None):
if parameters == None:
self.set_defaults()
else:
for d in parameters.keys():
self[d] = parameters[d]
def __getattr__(self, param):
return self[param]
def __setattr__(self, param, value):
self[param] = value
def set_defaults(self):
self['sim_name' ] = "trial"
self['rate_to_change'] = 'd2op_plus'
self['N_trials' ] = 10
self['hi_exp' ] = 1.5
self['lo_exp' ] = -1.5
self['var_power' ] = [1] #default is one rate, varying with full exponent
#extend this list in case multiple
#variables should be varied
#with different powers
#Pick MR_rates that are common, and check them against mechanism later
self['MR_rate' ] = [(1,7), (0,5), (7,8), (5,6)]
self['MR_avoid' ] = [] #this will be augmented according to varying rates
self['zero_conc' ] = 0
self['high_conc' ] = 1e-2
self['MR_avoid_preserve'] = False
def MR_rate_clean(self, mech_rates):
"""Remove rate tuples from 'MR_rate' that are not in the mechanism"""
for rate_tuple in self['MR_rate']:
if rate_tuple not in mech_rates.keys():
self['MR_rate'].remove(rate_tuple)
print ("Removed " + str(rate_tuple) + " from MR_rate")
#check for rate to change in MR params
for _rtc in self['rate_to_change']:
rtc_tuple = r_tuple_from_r_name(mech_rates, _rtc)
if rtc_tuple not in self['MR_avoid'] and not self['MR_avoid_preserve']:
#this blanket hack will remove any special info in MR_avoid
#flag can be used to make MR_avoid invulnerable
self['MR_avoid'].append(rtc_tuple)
print ("Adding "+str(rtc_tuple)+" to MR_avoid (now: "+ str(self['MR_avoid'])+" )\n")
#take the rate to change out of MR use
if rtc_tuple in self['MR_rate']:
self['MR_rate'].remove(rtc_tuple)
class RateGenerator:
"""
Prepare a list of rate sets within which a subset of the rate constants
are varied. Use for generating Q-matrices for a set of experiments.
Input - rate dictionary, rates to vary during experiment,
and the high and low exponents of 10 and number of trials
rates to vary is a list
var_power is a list of factors to make different rates vary with different extents
Output - rates_set : a list of experiment number : rate set pairs
"""
def __init__(self, rates, param):
self.rates_set = []
self.core_rate_set = rates
self.rates_to_vary = param.rate_to_change
self.hi_exp = param.hi_exp
self.lo_exp = param.lo_exp
self.N_trials = param.N_trials
self.exp_step = float(self.hi_exp - self.lo_exp) / self.N_trials
self.var_power = param.var_power
print ("Var_power..."+str(param.var_power))
def make_tuple_list(self):
#construct an ordered list of the keys in rate dictionary
#(state from, to) tuples for the values that will be tweaked
#these tuples are not mutated
#in some cases, might want to vary rates in lock-step -
#like forward binding rates
self.tuples_to_vary = []
for r in self.rates_to_vary:
print (str(r))
t = r_tuple_from_r_name(self.core_rate_set, r)
self.tuples_to_vary.append(t)
def make_rate_vary_list(self):
#construct an ordered list of the original rate values that will be
#mutated- so deepcopy needed to avoid propagation
_b_r = []
for r in self.tuples_to_vary:
_b_r.append(self.core_rate_set.get(r))
self.base_rates_to_change = copy.deepcopy(_b_r)
print ("Base rates to change:" + str(self.base_rates_to_change))
def make(self):
self.make_tuple_list()
self.make_rate_vary_list()
for trial in range(self.N_trials):
exponent = self.lo_exp + trial * self.exp_step
adjusted_rate_set = copy.deepcopy(self.core_rate_set)
#adjust each rate in turn
for base_r, tuple, power in zip(self.base_rates_to_change, self.tuples_to_vary, self.var_power):
#_exp = float(exponent)
_b = float(base_r[1][0])
_adjusted_rate = _b * 10 ** (exponent * power)
adjusted_rate_set[tuple][1][0] = _adjusted_rate
#show calculation
#print base_r, _exp, _b, 10**exponent, _adjusted_rate
self.rates_set.append([trial, adjusted_rate_set])
class ExperimentSetup:
""" Generic base class for all experiments on a single mechanism
Instantiated as mechanism container
Methods : curve_save : write arrays out to disk
text_out : write tables out to disk"""
def __init__(self, mech=None, param=None):
""" Arguments
mech : kinetic mechanism in rate dictionary form
param : instance of experiment class containing parameters as attributes
"""
#get mechanism
if mech == None:
# use default mechanism
self.rates, self.N_states, self.open_states = mechanism()
print ("No mech given, using default [ExperimentSetup]")
else:
self.rates, self.N_states, self.open_states = mechanism(mech)
#set parameters
if param == None:
self.param = Parameters() #set default parameters
print ("No parameters given, using default [ExperimentSetup]")
#Remove tuples from MR_rate that aren't in the mechanism
#self.param.MR_rate_clean(self.rates)
else:
self.param = param
print ("[ExperimentSetup] Cleaning MR parameters")
self.param.MR_rate_clean(self.rates)
#make rate set
self.rs = RateGenerator(self.rates, self.param)
self.rs.make() #the ExperimentSetup object "rs", attribute "rates_set" now has the set of rates
#output specification
self.table = "Trial #\t"+ '\t'.join(n for n in self.param.rate_to_change) + '\n'
self.trace_set = []
self.header = False #default no header for the trace output. Build in some subclasses
def curve_save(self, curve_data, mod=""):
#print ("shapes")
#for elem in curve_data:
# print ("elem"+ str(len(elem)))
# for e in elem:
# print ("e"+ str(len(elem)))
print (mod)
print (self.param.sim_name)
self.trace_coll = numpy.column_stack(curve_data)
if not mod: mod = ""
if self.header:
print (self.header)
numpy.savetxt(self.param.sim_name + mod + ".txt", self.trace_coll, delimiter='\t', header=self.header)
else:
numpy.savetxt(self.param.sim_name + mod + ".txt", self.trace_coll, delimiter='\t')
def text_out(self, extra_data=False):
print ("Printout generated by " + self.__class__.__name__ + "\n")
print (self.table)
self.f=open(self.param.sim_name + '.xls', 'w')
self.f.write("Printout generated by " + self.__class__.__name__ + "\n")
self.f.write(self.table)
if extra_data:
self.f.write(extra_data)
self.f.close()
class TwoMechSetup:
""" base class for comparison experiments on a two mechanisms
subclass of ExperimentSetup with new __init__ method"""
def __init__(self, mech1=None, param1=None, mech2=None, param2=None):
#get mechanism
##setup dummy containers and then replace
## these calls to ExperimentSetup choose the defaults if
self.m1 = ExperimentSetup(mech1, param1)
self.m2 = ExperimentSetup(mech2, param2)
if mech1 == None:
self.m1.name = "3"
self.m1.rates, self.m1.N_states, self.m1.open_states = mechanism(self.m1.name)
print ("No mech1 given, using default #3 [TwoMechSetup]")
#set parameters
if param1 == None:
self.m1.param = Parameters() #set default parameters
self.m1.param.MR_rate_clean(self.m1.rates)
print ("No parameters given, using default [TwoMechSetup]")
if mech2 == None:
self.m2.name = "4"
self.m2.rates, self.m2.N_states, self.m2.open_states = mechanism(self.m2.name)
print ("No mech2 given, using default #4 [TwoMechSetup]")
if param2 == None:
self.m2.param = Parameters() #set default parameters
self.m2.param.MR_rate_clean(self.m2.rates)
print ("No parameters given, using default [TwoMechSetup]")
#output specification - NOT FINALISED
self.table = "Trial #"+ '\t'+ self.m1.name + '\t'+ self.m2.name + '\n'
self.trace_set = [] #??
class Pack_m:
def __init__(self, mechname="3"):
self.rates, self.N_states, self.open_states = mechanism(mechname)
class PairExpt:
"""Run function for pairwise comparisons"""
def __init__(self, pairmech1 = "3", pairmech2 = "4",
common_rate = "d2op_min", Verbose=False):
self.Verbose = Verbose
#data =
self.m1 = Pack_m(pairmech1)
self.m2 = Pack_m(pairmech2)
#call mechanism and pack into single object
self.p1 = Parameters()
self.p2 = Parameters()
#adjust default parameters to be useful; could also alter
## 'N_trials' = 10
## 'hi_exp' = 1.5
## 'lo_exp' = -1.5
## 'MR_rate' = [(1,7), (0,5), (7,8), (5,6)]
## 'MR_avoid' = [(0,2)]
## 'zero_conc' = 0
## 'high_conc' = 1e-2
## 'MR_avoid_preserve' = True
self.p1.rate_to_change = common_rate
self.p2.rate_to_change = common_rate
self.p1.MR_rate_clean(self.m1.rates)
self.p2.MR_rate_clean(self.m2.rates)
#tidying up - a rate to be avoided for MR should not be in the "use" list
#at the moment, the Auto MR will find another way
#adjust MR_rate list in order to hard code?
#This following is method "MR_rate_clean" in Parameters class
"""
print ("p1.MR_avoid:" + str(p1.MR_avoid))
m1_avoid = r_tuple_from_r_name(m1.rates, common_rate, Verbose=True)
print ("p2.MR_avoid:" + str(p2.MR_avoid))
m2_avoid = r_tuple_from_r_name(m2.rates, common_rate, Verbose=True)
p1.MR_avoid = [m1_avoid]
p2.MR_avoid = [m1_avoid]
if m1_avoid in p1.MR_rate:
p1.MR_rate.remove(m1_avoid)
if m2_avoid in p2.MR_rate:
p2.MR_rate.remove(m2_avoid)
print ("p1.MR_avoid:" + str(p1.MR_avoid))
print ("p2.MR_avoid:" + str(p2.MR_avoid))
"""
self.p1.sim_name = "Comp_m" + pairmech1 + "_m" + pairmech2 +"_"+ common_rate
self.p2.sim_name = "Comp_m" + pairmech1 + "_m" + pairmech2 +"_"+ common_rate
self.printout = ""
def run(self):
#parameters determine how rate set is constructed
self.rs1 = RateGenerator(self.m1.rates, self.p1)
self.rs1.make() #rates_set attribute now has sets
self.rs2 = RateGenerator(self.m2.rates, self.p2)
self.rs2.make() #rates_set attribute now has sets
#during loop, mshell1 and 2 will be updated with the new rates
#but the other attributes stay constant
#including bundled parameters
self.mshell1 = copy.deepcopy(self.m1)
self.mshell2 = copy.deepcopy(self.m2)
self.mshell1.param = self.p1
self.mshell2.param = self.p2
if self.Verbose:
print ("\nMech "+ pairmech1 + ", rs1.rates_set: \n" + str(self.rs1.rates_set))
print ("\nMech "+ pairmech2 + ", rs2.rates_set: \n" + str(self.rs2.rates_set))
for _ms1, _ms2 in zip(self.rs1.rates_set, self.rs2.rates_set):
self.mshell1.rates = _ms1[1] #to grab right part
self.mshell2.rates = _ms2[1]
_PMC = PairMechComparison (self.mshell1, self.mshell2)
_PMC.gather_pk_ss()
_PMC.gather_krec()
_rtc_tuple = r_tuple_from_r_name(self.mshell1.rates, self.p1.rate_to_change)
_PMC.make_printable(self.p1.rate_to_change,
self.mshell1.rates[_rtc_tuple][1][0])
self.printout += _PMC.table_line
#only need the final copies of header and key
self.printout = "\nExperiment name: " + self.p1.sim_name + "\n" + \
_PMC.global_head + "\n" + _PMC.key_line + "\n" + self.printout
print (self.printout)
class PairMechComparison:
"""Compare pk/ss vs recovery for two individual mechanisms
Methods:
__init__ : takes or generates mechanisms and parameters
gather_pk_ss : gather and process peak and steady states for two mechs
gather_k_rec : gather and process recovery data for two mechs
make_printable : string to describe data generated
Output:
"""
#cycle against rates in mechanism
# first write comparison of two mechanisms (can be any pair, rates already fixed, Qmat etc defined)
# Iss/Ipeak and krec
# then serve up mechanisms pairwise, across orchestrated rate changes, rate changing etc
# make condition of same rate names or require mechanism-wise rate pairs
def __init__(self, m1=None, m2=None):
"""m1 and m2 are preformed mechanism objects, containing the attributes
rates : the rate dictionary
params : parameter dictionary
open_states : dictionary of state-wise conductances (can be normalised)
N_states : The number of states in the mechanism
---Normalization of open_states dictionary must be consistent across objects
---Preparing these four objects from two rate dictionaries
could probably be a method of this class
"""
if m1 == None and m2 == None:
TMS = TwoMechSetup()
self.m1 = TMS.m1
self.m2 = TMS.m2
else:
#takes this path if called from PairExpt
self.m1 = m1
self.m2 = m2
"""
self.m1.rates = m1.rates
self.m2.rates = m2.rates
self.m1.open_states = m1.open_states
self.m2.open_states = m2.open_states
self.m1.N_states = m1.N_states
self.m2.N_states = m2.N_states
self.m1.param = m1.param
self.m2.param = m2.param
"""
#Initialize hi and lo Qmats here
self.m1.Qlo, self.m1.P_init_lo = generate_Q(
self.m1.N_states, {1: self.m1.param.zero_conc}, self.m1.rates,
self.m1.param.MR_rate, self.m1.param.MR_avoid)
self.m1.Qhi, self.m1.P_init_hi = generate_Q(
self.m1.N_states, {1: self.m1.param.high_conc}, self.m1.rates,
self.m1.param.MR_rate, self.m1.param.MR_avoid)
self.m2.Qlo, self.m2.P_init_lo = generate_Q(
self.m2.N_states, {1: self.m2.param.zero_conc}, self.m2.rates,
self.m2.param.MR_rate, self.m2.param.MR_avoid)
self.m2.Qhi, self.m2.P_init_hi = generate_Q(
self.m2.N_states, {1: self.m2.param.high_conc}, self.m2.rates,
self.m2.param.MR_rate, self.m2.param.MR_avoid)
def gather_pk_ss(self):
"""Make a relaxation for each mechanism and take Iss / Ipeak ratio,
and calculate fold-change"""
# only works if peak is bigger than ss (otherwise will go to 1)
_t_step = numpy.arange (1, 9, .2)
_log_t_step = 1e-7 * 10 ** (_t_step)
# take max 10mM relax mech 1
r1 = Relaxation(self.m1.Qhi, self.m1.P_init_lo)
r1.assemble(_log_t_step, self.m1.open_states)
#take max of hi-conc jump
self.peak1 = numpy.max(r1.relax_sum)
# calc Iss mech 1
self.eqbm1 = r1.relax_sum[-1] #steady state at 100 sec.
# take max 10mM relax mech 2
r2 = Relaxation(self.m2.Qhi, self.m2.P_init_lo)
r2.assemble(_log_t_step, self.m2.open_states)
#take max of hi-conc jump
self.peak2 = numpy.max(r2.relax_sum)
# calc Iss mech 2
self.eqbm2 = r2.relax_sum[-1]
self.fold_change = (self.eqbm1 * self.peak2) / (self.eqbm2 * self.peak1)
def gather_krec(self):
"""get krec for the two mechanisms"""
#getting Qlo and Qhi the wrong way round here gives perfect flat s.s. recovery
rec1 = Recovery_Qmade(self.m1.param, self.m1.N_states,
self.m1.open_states, self.m1.Qlo, self.m1.Qhi, self.m1.P_init_hi, t_range=1e4)
rec1.build_curve()
rec1.get_keff()
rec2 = Recovery_Qmade(self.m2.param, self.m2.N_states,
self.m2.open_states, self.m2.Qlo, self.m2.Qhi, self.m2.P_init_hi, t_range=1e4)
rec2.build_curve()
rec2.get_keff()
self.k_eff1 = rec1.k_eff
self.k_eff2 = rec2.k_eff
self.mean_keff = (rec1.k_eff + rec2.k_eff ) / 2
def make_printable(self, _d_rate=None, _d_value=None):
###not final - adjusted now to be useful?
self.d_rate = _d_value
self.global_head = "Pairwise comparison of two mechanisms\n"
self.global_head += "Mech 1 open states: " + str(self.m1.open_states) + "\n"
self.global_head += "M1 P_init_hi_concentration: \n" + str(self.m1.P_init_hi) + "\n"
self.global_head += "Mech 2 open states: " + str(self.m2.open_states) + "\n"
self.global_head += "M2 P_init_hi_concentration: \n" + str(self.m2.P_init_hi) + "\n"
_data = [ "eqbm1", "peak1", "eqbm2", "peak2", "fold_change", "k_eff1", "k_eff2"]
# add changing rate information to table
self.key_line = "{:>12}".format(_d_rate) + "\t".join("{:>12}".format(d) for d in _data) + "\n"
_data.insert(0, "d_rate")
self.table_line = "\t".join("{:12.5g}".format(self.__dict__[_key]) \
for _key in _data) + "\n"
class ConcResponse:
"""Calculate peak and equilibrium responses over a range of concentrations
Methods:
__init__ : takes rates and P_init
build_curve : gather and process
make_printable : string to describe data generated
Output:
curve as numpy array [3 x steps]: conc, peak, steady_state,
all relaxations and summary of curve as text
"""
def __init__(self, param, N_states, open_states, rates, min_conc, max_conc, steps=19, P_init=None):
""" Arguments:
N_states -- Number of states in the mechanism
rates -- rate dictionary
open_state -- dictionary of open state : conductance pairs
min_conc and max_conc -- pretty self-explanatory
steps -- points in the curve
P_init -- the initial occupancy for each c-jump"""
self.responses = []
self.log_min_conc = log10(min_conc)
self.log_max_conc = log10(max_conc)
self.steps = steps
self.rates = rates
self.exponent = float(self.log_max_conc - self.log_min_conc) / self.steps
self.N_states = N_states
self.open_states = open_states
self.curve = numpy.zeros((self.steps, 4)) #conc, peak, steady_state, 1ms
self.MR_rate = param.MR_rate
self.MR_avoid = param.MR_avoid
if P_init == None:
#take resting (i.e. zero concentration) equilibrium for start of jump
Q_z, self.P_init = generate_Q(self.N_states, {1: param.zero_conc},
self.rates, self.MR_rate, self.MR_avoid)
print ("self.P_init:", self.P_init)
else:
self.P_init = P_init
def build_curve(self, drugs = {1:0}, agonist_to_use = 1):
""" drugs -- dict, keys must include all drugs used in rate dict.
agonist_to_use -- specify which key in the drug dictionary to update"""
_t_step = numpy.arange (1, 9, .2)
_log_t_step = 1e-7 * 10 ** (_t_step)
#put the timebase for the jumps as first column
self.responses.append(_log_t_step)
for n in range (self.steps):
# calculate relaxation at a range of concs
agonist_conc = 10 ** ( self.log_min_conc + n * self.exponent )
#Currently, MR is recalculated on each Q matrix, simple but wasteful
#update drug dictionary
drugs[agonist_to_use] = agonist_conc
Q_conc, P_eq = generate_Q(self.N_states, drugs, self.rates, self.MR_rate, self.MR_avoid)
print ("Calculating relaxation with following drug concentration" +
str(drugs) + ", giving Qmat:\n" + str(Q_conc.Q))
#print 'P_init', P_init
r = Relaxation(Q_conc, self.P_init)
print (self.open_states)
r.assemble(_log_t_step, self.open_states)
#take max of hi-conc jump
max_response = numpy.max(r.relax_sum)
self.responses.append(r.relax_sum)
eqbm = r.relax_sum[-1]
print ("Equilibrium P-open, only true if conductances are normalised: ", eqbm)
onems = r.relax_sum[15] #1ms point is normally the 15th in time series (HACK)
#store points
self.curve [n] = (agonist_conc, max_response, eqbm, onems)
print (self.curve)
self.make_printable()
def make_printable(self):
self.printout = "Concentration Response\n"
self.printout += "open states: " + str(self.open_states) + "\n"
self.printout += "P_init\n" + str(self.P_init) + "\n"
self.printout += "\n\nconc\tpeak\tss\t1ms peak\n"
for line in self.curve.tolist():
for elem in line:
self.printout += str(elem) + "\t"
self.printout += "\n"
class Relaxation:
"""
Calculate relaxation from a Q-matrix
can specify states to put together (as tuple, e.g. P-open - all open states)
must supply time points to calculate over
must supply Q-matrix
This is a core class that is used by the other more complicated experiments
relaxations are first made as 2-D arrays where
columns correspond to individual state occupancies
rows are isochrones
Methods:
__init__ : takes Q matrix and initial occupancy (P_init)
assemble : gather and process
calculate : calculate occupancies during relaxation
calculate_sum : sum up occupancies
make_printable : string to describe data generated
"""
def __init__(self, Q, P, verbose=False):
#???self.state_by_state_relax = {}
self.Q = Q
if verbose: print ("Dimension of Qmatrix (states): ", self.Q.N_states)
self.P_init = P
self.Q.relax(self.P_init)
self.eigenvals = self.Q.eigenval
#default state to calculate for is state 0
#By DC's convention, this is the open state if there's only one.
self.states_sum=(0)
self.verbose = verbose
if verbose:
print ("Q:")
print (self.Q.show())
print ("Q.w:")
print (self.Q.w)
def assemble(self, time_pts, states_to_sum=False):
"""put the relaxation together"""
#need to pass time range
#if "states to sum" (the list of state occupancies that should be summed)
#is provided, it is not False and will be set here
if states_to_sum:
self.states_sum = states_to_sum
else:
self.states_sum = False
self.pts = time_pts
if self.verbose:
print ("Assemble: Length of relaxation in points: ", len(self.pts))
if self.states_sum:
_ss = ""
for x in self.states_sum:
_ss += str(x)+'\t'
print ("States to sum (usu. open states): "+_ss)
#Wow: using numpy.empty rather than numpy.zeros here is a disaster!
self.relax = numpy.zeros([len(self.pts),self.Q.N_states])
#calculate and make output.
self.calculate()
if self.states_sum: self.calculate_sum()
#store final occupancy (critical for chaining together relaxations)
self.P_final = self.relax[-1, :]
self.make_printable()
def calculate(self):
#construct relaxation from sum of occupancy(t) over each state
# need to take the pt-th value from the array self.pts (=time)
#this way of updating self.relax relies on not using numpy.empty
for s in range(self.Q.N_states):
for pt in range(len(self.pts)):
for a, k in zip(self.Q.w[:, s], self.eigenvals):
self.relax [pt, s] += a * exp (k * self.pts[pt])
def calculate_sum(self):
#calculate sum of occupancies (with t) for states given in states_sum
self.relax_sum = numpy.zeros([len(self.pts)])
#Sum slices, each of which is P(s)(full t range), weighted by conductance
for s in self.states_sum.keys():
#self.states_sum[s] is the conductance of state s
self.relax_sum += self.relax [:,s] * self.states_sum[s]
#print self.relax_sum
def make_printable(self):
self.printout = "states sum:" + "\n" + str(self.states_sum) + "\n"
self.printout += "eigenvalues" + "\n" + str(self.eigenvals) + "\n"
self.printout += 'P_init' + "\n" + str(self.P_init) + "\n"
self.printout += 'P_final' + "\n" + str(self.P_final) + "\n"
self.printout += "\n\nby state\n"
for pt in range(len(self.pts)):
self.printout += str(self.pts[pt]) + "\t" + str(self.relax[pt, :]) + "\n"
self.printout += "\n\nsummed\n"
for pt in range(len(self.pts)):
self.printout += str(self.pts[pt]) +"\t" +str(self.relax_sum[pt]) + "\n"
class ConcInhibition:
""" //______|
//------|
//^^^^^^|"""
def __init__(self):
pass
class PulseInhibition:
""" 0_|
0___|
0_____|
wouldn't it be really cool to write out experiments like this one day?
very similar to Recovery"""
def __init__(self, param, N_states, open_states, rates, P_init='zero', conc=0.01, t_range=1000):
self.intervals = convert_ms(t_range)
#print self.intervals
self.np2 = len(self.intervals)
self.conc = conc
self.open_states = open_states
self.Qlo, self.P_init_lo = generate_Q(N_states, {1: param.zero_conc},
rates, param.MR_rate, param.MR_avoid)
self.Qc, self.P_init_c = generate_Q(N_states, {1: self.conc},
rates, param.MR_rate, param.MR_avoid)
self.Qhi, self.P_init_hi = generate_Q(N_states, {1: param.high_conc},
rates, param.MR_rate, param.MR_avoid)
#build data container
self.inhib_curve = numpy.zeros((self.np2, 2))
#can specify initial occupancy, by default take P_inf_hi-conc
if P_init == 'zero':
self.P_init = self.P_init_lo
else:
self.P_init = P_init
def build_curve(self):
_brief_jump = 100 #in ms
_t_steps = convert(_brief_jump)
# wait to make the second pulse trace container until we know _t_steps
self.second_pulses = numpy.zeros((0, len(_t_steps)))
for n, interval in enumerate(self.intervals):
rr = Relaxation(self.Qc, self.P_init)
#send list of a single interval
rr.assemble([interval], self.open_states)
self.P = rr.P_final
rj = Relaxation(self.Qhi, rr.P_final)
rj.assemble(_t_steps, self.open_states)
#take max of hi-conc jump
response = numpy.max(rj.relax_sum)
index_max = numpy.argmax(rj.relax_sum)
exact_max = interval + _t_steps[index_max]
#print self.second_pulses
#store profiles of test jumps
self.second_pulses = numpy.append(self.second_pulses,
numpy.atleast_2d(_t_steps + interval),axis=0)
self.second_pulses = numpy.append(self.second_pulses,
numpy.atleast_2d(rj.relax_sum), axis=0)
#store max against exact interval
self.inhib_curve[n,:] = (exact_max, response)
print (self.inhib_curve)
def make_printable(self):
#self.printout = "states sum:" + "\n" + str(self.states_sum) + "\n"
self.printout = 'P_init' + "\n" + str(self.P_init) + "\n"
#self.printout += 'P_final' + "\n" + str(self.P_final) + "\n"
self.printout += "\n\ninhibition_curve\n"
for line in self.inhib_curve.tolist():
for elem in line:
self.printout += str(elem) + "\t"
self.printout += "\n"
self.printout += "\n\nsecond pulses\n"
for line in self.second_pulses.tolist():
for elem in line:
self.printout += str(elem) + "\t"
self.printout += "\n"
class Recovery:
"""
Two pulse protocols
simulate relaxations of response to second pulse following a long conditioning pulse
collect peaks to build recovery curve
"""
def __init__(self, param, N_states, open_states, rates, P_init='hi', t_range=10000, normalise=False):
self.intervals = convert_ms(t_range)
self.norm = normalise
#print self.intervals
self.np2 = len(self.intervals)
self.open_states = open_states
#lo_rates = copy.deepcopy(rates)
#hi_rates = copy.deepcopy(rates)
print("make Qlo, conc: "+str(param.zero_conc) )
self.Qlo, self.P_init_lo = generate_Q(N_states, {1: param.zero_conc},
rates, param.MR_rate, param.MR_avoid)
print("make Qhi, conc: "+str(param.high_conc) )
self.Qhi, self.P_init_hi = generate_Q(N_states, {1: param.high_conc},
rates, param.MR_rate, param.MR_avoid)
#build data container
self.rec_curve = numpy.zeros((self.np2, 2))
#can specify initial occupancy, by default take P_inf_hi-conc
if P_init == 'hi':
self.P_init = self.P_init_hi
else:
self.P_init = P_init
def build_curve(self):
_brief_jump = 100 #in ms
_t_steps = convert(_brief_jump)
# wait to make the second pulse trace container until we know _t_steps
self.second_pulses = numpy.zeros((0, len(_t_steps)))
for n, interval in enumerate(self.intervals):
rr = Relaxation(self.Qlo, self.P_init)
#send list of a single interval
rr.assemble([interval], self.open_states)
self.P = rr.P_final
rj = Relaxation(self.Qhi, rr.P_final)
rj.assemble(_t_steps, self.open_states)
#take max of hi-conc jump
response = numpy.max(rj.relax_sum)
index_max = numpy.argmax(rj.relax_sum)
t_exact_max = interval + _t_steps[index_max]
#store profiles of test jumps
#print self.second_pulses
self.second_pulses = numpy.append(self.second_pulses,
numpy.atleast_2d(_t_steps + interval),axis=0)
self.second_pulses = numpy.append(self.second_pulses,
numpy.atleast_2d(rj.relax_sum), axis=0)
#store max against exact interval
self.rec_curve[n, :] = (t_exact_max, response)
#print (self.rec_curve)
#normalise responses of recovery curve against limiting value
if self.norm:
self.rec_curve[:, 1] = self.rec_curve[:, 1] / self.rec_curve [-1, 1]
print (self.rec_curve)
def get_keff(self, take_final=False):
""" Optionally called after generating the recovery curve with build_curve
to very crudely obtain the half time
simple modification from threshold.py
simple linear interpolation between bracketing points if no direct hit
"""
# find max in rec (or final value, don't force as default)
if take_final:
_rec_max = self.rec_curve[-1, 1]
else:
_rec_max = numpy.max(self.rec_curve[:, 1])
_rec_min = self.rec_curve[0, 1]
#Bit of a cheat - take the first point. Will be wrong in the case of
#very fast recovery compared to 1st interval. But in this case, _rec_min and _rec_max
#should be similar and caught below
if _rec_min > 0.95 * _rec_max:
print ("No recovery because too little desensitization (fast limit)")
print ("Setting k_eff = 1000")
self.k_eff = 1000 #We could certainly not measure a rate this fast
else:
_half_rec_amp = _rec_max - 0.5 * (_rec_max - _rec_min)
_near_idx = (numpy.abs(self.rec_curve[:, 1] - _half_rec_amp)).argmin()
_near_value = self.rec_curve [_near_idx, 1]
#interpolate
#must be a smarter way to combine the two possibilities?
if _near_value > _half_rec_amp:
#true half time was before our nearest neighbor
_left = self.rec_curve[_near_idx - 1, 1]
_right = self.rec_curve[_near_idx, 1]
_tl = self.rec_curve[_near_idx - 1, 0]
_tr = self.rec_curve[_near_idx, 0]
#inverse of time difference scaled by normalized (point-threshold distance)
self.k_eff = 1 / (_tr - (_tr - _tl) * float(_right - _half_rec_amp)/(_right - _left))
elif _near_value < _half_rec_amp:
#true half time was after our nearest neighbor
_left = self.rec_curve[_near_idx, 1]
_right = self.rec_curve[_near_idx + 1, 1]
_tl = self.rec_curve[_near_idx, 0]
_tr = self.rec_curve[_near_idx + 1, 0]
#as above rearranged to approach from below.
self.k_eff = 1 / (_tl + (_tr - _tl) * float(_half_rec_amp - _left)/(_right - _left))
elif _near_value == _half_rec_amp:
self.k_eff = 1 / self.rec_curve[near_hi_idx, 0]
def make_printable(self):
#self.printout = "states sum:" + "\n" + str(self.states_sum) + "\n"
self.printout = 'P_init' + "\n" + str(self.P_init) + "\n"
#self.printout += 'P_final' + "\n" + str(self.P_final) + "\n"
self.printout += "\n\nrec_curve\n"
for line in self.rec_curve.tolist():
for elem in line:
self.printout += str(elem) + "\t"
self.printout += "\n"
self.printout += "\n\nsecond pulses\n"
for line in self.second_pulses.tolist():
for elem in line:
self.printout += str(elem) + "\t"
self.printout += "\n"
class Recovery_Qmade(Recovery):
"""subclass to initialise recovery simply when Q mats are already made"""
def __init__(self, param, N_states, open_states, Q_lo, Q_hi, P_init_hi, t_range=10000, normalise=False):
self.intervals = convert_ms(t_range)
self.np2 = len(self.intervals)
self.open_states = open_states
self.Qlo = Q_lo
self.Qhi = Q_hi
self.P_init = P_init_hi
self.rec_curve = numpy.zeros((self.np2, 2))
self.norm = normalise
class Train:
"""Trains of pulses"""
def __init__(self, npulse, pwidth, pfreq, param, N_states, open_states, rates):
#pulse sequence will be built as a list because order is important
self.pulse_seq = []
self.n = npulse