Skip to content

Commit

Permalink
minor updates to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
eshanking committed Mar 8, 2024
1 parent f7fdf03 commit a287d14
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 75 deletions.
32 changes: 1 addition & 31 deletions build/lib/fears/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,6 @@ def __init__(self,
else:
self.experiment_type = experiment_type

# if experiment_type == 'dose-survival' and len(inoculants) > 1:
# raise Exception('The experiment type is set to dose-survival (default), but more than one inoculant is given.')
# elif experiment_type == 'inoculant-survival' and len(self.max_doses) > 1:
# # print('here')
# raise Exception('The experiment type is set to inoculant-survival, but more than one max dose is given.')

# if experiment_type == 'inoculant-survival' and inoculants is None:
# raise Exception('The experiment type is set to inoculant-survival, but no inoculants are given.')
self.ramp = ramp
if self.experiment_type == 'ramp_up_down':
self.p_landscape = Population(constant_pop = True,
Expand Down Expand Up @@ -151,19 +143,6 @@ def __init__(self,
self.set_ramp_ud(self.p_landscape)
self.set_ramp_ud(self.p_seascape)


# p_bottleneck = Population(max_dose = max_doses,
# n_sims = self.n_sims,
# curve_type = 'bottleneck'
# **self.population_options)
# self.populations.append(p_bottleneck)

# p_no_bottleneck = Population(max_dose = max_doses,
# n_sims = self.n_sims,
# curve_type = 'constant'
# **self.population_options)
# self.populations.append(p_no_bottleneck)

if self.experiment_type == 'dose-survival':
for curve_type in self.curve_types:
for max_dose in self.max_doses:
Expand Down Expand Up @@ -490,23 +469,14 @@ def run_experiment(self):
save_folder = 'k_abs=' + str(p.k_abs)
save_folder.replace('.',',')
else:
save_folder = 'slope=' + str(p.slope)
save_folder = 'slope=' + str(p.k_abs)
save_folder.replace('.',',')
# self.save_counts(counts,n,save_folder)
data_dict = {'counts':counts,
'drug_curve':drug}
self.save_dict(data_dict,save_folder,num=n)
if not self.debug:
pickle.dump(self, open(self.experiment_info_path,"wb"))
# pickle.dump(self, open(self.experiment_info_path,"wb"))


# fig_savename = 'slope = ' + str(p.slope)
# self.figures = self.figures.append(fig)
# pbar.update()
# self.rate_survival_results.index = np.arange(len(self.rate_survival_results))

# pbar.close() # close progress bar

# save counts as a csv in the given subfolder with the label 'num'
def save_counts(self,counts,num,save_folder,prefix='sim_'):
Expand Down
11 changes: 9 additions & 2 deletions build/lib/fears/utils/pharm.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,18 @@ def gen_impulses(pop):

# generate the drug dose regimen

if pop.dwell:
dwell_time = int(pop.dwell_time/pop.timestep_scale)
else:
dwell_time = 0

if pop.regimen_length is None:
regimen_length = pop.n_timestep
else:
regimen_length = pop.regimen_length
regimen_length = pop.regimen_length + pop.dwell_time

if regimen_length > pop.n_timestep:
regimen_length = pop.n_timestep

while impulse_indx[i] < regimen_length*pop.timestep_scale-pop.dose_schedule:
impulse_indx.append(pop.dose_schedule*(i+1))
Expand All @@ -108,7 +116,6 @@ def gen_impulses(pop):
u[impulse_indx]=1 # list of impulses at the simulated drug dosage times

if pop.dwell:
dwell_time = int(pop.dwell_time/pop.timestep_scale)
u[0:dwell_time] = 0

return u
Expand Down
16 changes: 12 additions & 4 deletions build/lib/fears/utils/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ def plot_landscape(p,conc=10**0,
plot_sub_network=False,
sub_network=None,
sub_network_color='white',
weight_list=None,
**kwargs):
"""
Plots a graph representation of this landscape on the current matplotlib figure.
Expand Down Expand Up @@ -872,7 +873,11 @@ def plot_landscape(p,conc=10**0,


if trajectory_list is not None:
for trajectory in trajectory_list:
for i,trajectory in enumerate(trajectory_list):

if weight_list is not None:
weight = weight_list[i]
arrowprops['lw'] = weight

trajectory_pairs = []

Expand Down Expand Up @@ -918,7 +923,7 @@ def plot_landscape(p,conc=10**0,
break

# Draw trajectory arrows

ax.annotate('', xy=end_pos, xytext=start_pos,
arrowprops=arrowprops)

Expand Down Expand Up @@ -1006,6 +1011,7 @@ def plot_kaplan_meier(pop,
event_times,
label=None,
t_max=None,
t_vect = None,
n_sims=None,
ax=None,
mode='resistant',
Expand Down Expand Up @@ -1038,7 +1044,9 @@ def plot_kaplan_meier(pop,
else:
ylabel='% survival'

ax.plot(survival_curve,label=label,**kwargs)
if t_vect is None:
t_vect = np.arange(t_max)
ax.plot(t_vect,survival_curve,label=label,**kwargs)

if errorband:
# compute error bars
Expand All @@ -1053,7 +1061,7 @@ def plot_kaplan_meier(pop,
err[t] = 100*((p*q)/n)**0.5 #
t = np.arange(t_max)

ax.fill_between(t,survival_curve-err,survival_curve+err,alpha=0.4)
ax.fill_between(t_vect,survival_curve-err,survival_curve+err,alpha=0.4)

xticks = ax.get_xticks()
xlabels = xticks
Expand Down
32 changes: 1 addition & 31 deletions fears/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,6 @@ def __init__(self,
else:
self.experiment_type = experiment_type

# if experiment_type == 'dose-survival' and len(inoculants) > 1:
# raise Exception('The experiment type is set to dose-survival (default), but more than one inoculant is given.')
# elif experiment_type == 'inoculant-survival' and len(self.max_doses) > 1:
# # print('here')
# raise Exception('The experiment type is set to inoculant-survival, but more than one max dose is given.')

# if experiment_type == 'inoculant-survival' and inoculants is None:
# raise Exception('The experiment type is set to inoculant-survival, but no inoculants are given.')
self.ramp = ramp
if self.experiment_type == 'ramp_up_down':
self.p_landscape = Population(constant_pop = True,
Expand Down Expand Up @@ -151,19 +143,6 @@ def __init__(self,
self.set_ramp_ud(self.p_landscape)
self.set_ramp_ud(self.p_seascape)


# p_bottleneck = Population(max_dose = max_doses,
# n_sims = self.n_sims,
# curve_type = 'bottleneck'
# **self.population_options)
# self.populations.append(p_bottleneck)

# p_no_bottleneck = Population(max_dose = max_doses,
# n_sims = self.n_sims,
# curve_type = 'constant'
# **self.population_options)
# self.populations.append(p_no_bottleneck)

if self.experiment_type == 'dose-survival':
for curve_type in self.curve_types:
for max_dose in self.max_doses:
Expand Down Expand Up @@ -490,23 +469,14 @@ def run_experiment(self):
save_folder = 'k_abs=' + str(p.k_abs)
save_folder.replace('.',',')
else:
save_folder = 'slope=' + str(p.slope)
save_folder = 'slope=' + str(p.k_abs)
save_folder.replace('.',',')
# self.save_counts(counts,n,save_folder)
data_dict = {'counts':counts,
'drug_curve':drug}
self.save_dict(data_dict,save_folder,num=n)
if not self.debug:
pickle.dump(self, open(self.experiment_info_path,"wb"))
# pickle.dump(self, open(self.experiment_info_path,"wb"))


# fig_savename = 'slope = ' + str(p.slope)
# self.figures = self.figures.append(fig)
# pbar.update()
# self.rate_survival_results.index = np.arange(len(self.rate_survival_results))

# pbar.close() # close progress bar

# save counts as a csv in the given subfolder with the label 'num'
def save_counts(self,counts,num,save_folder,prefix='sim_'):
Expand Down
11 changes: 9 additions & 2 deletions fears/utils/pharm.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,18 @@ def gen_impulses(pop):

# generate the drug dose regimen

if pop.dwell:
dwell_time = int(pop.dwell_time/pop.timestep_scale)
else:
dwell_time = 0

if pop.regimen_length is None:
regimen_length = pop.n_timestep
else:
regimen_length = pop.regimen_length
regimen_length = pop.regimen_length + pop.dwell_time

if regimen_length > pop.n_timestep:
regimen_length = pop.n_timestep

while impulse_indx[i] < regimen_length*pop.timestep_scale-pop.dose_schedule:
impulse_indx.append(pop.dose_schedule*(i+1))
Expand All @@ -108,7 +116,6 @@ def gen_impulses(pop):
u[impulse_indx]=1 # list of impulses at the simulated drug dosage times

if pop.dwell:
dwell_time = int(pop.dwell_time/pop.timestep_scale)
u[0:dwell_time] = 0

return u
Expand Down
16 changes: 12 additions & 4 deletions fears/utils/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ def plot_landscape(p,conc=10**0,
plot_sub_network=False,
sub_network=None,
sub_network_color='white',
weight_list=None,
**kwargs):
"""
Plots a graph representation of this landscape on the current matplotlib figure.
Expand Down Expand Up @@ -872,7 +873,11 @@ def plot_landscape(p,conc=10**0,


if trajectory_list is not None:
for trajectory in trajectory_list:
for i,trajectory in enumerate(trajectory_list):

if weight_list is not None:
weight = weight_list[i]
arrowprops['lw'] = weight

trajectory_pairs = []

Expand Down Expand Up @@ -918,7 +923,7 @@ def plot_landscape(p,conc=10**0,
break

# Draw trajectory arrows

ax.annotate('', xy=end_pos, xytext=start_pos,
arrowprops=arrowprops)

Expand Down Expand Up @@ -1006,6 +1011,7 @@ def plot_kaplan_meier(pop,
event_times,
label=None,
t_max=None,
t_vect = None,
n_sims=None,
ax=None,
mode='resistant',
Expand Down Expand Up @@ -1038,7 +1044,9 @@ def plot_kaplan_meier(pop,
else:
ylabel='% survival'

ax.plot(survival_curve,label=label,**kwargs)
if t_vect is None:
t_vect = np.arange(t_max)
ax.plot(t_vect,survival_curve,label=label,**kwargs)

if errorband:
# compute error bars
Expand All @@ -1053,7 +1061,7 @@ def plot_kaplan_meier(pop,
err[t] = 100*((p*q)/n)**0.5 #
t = np.arange(t_max)

ax.fill_between(t,survival_curve-err,survival_curve+err,alpha=0.4)
ax.fill_between(t_vect,survival_curve-err,survival_curve+err,alpha=0.4)

xticks = ax.get_xticks()
xlabels = xticks
Expand Down
2 changes: 1 addition & 1 deletion fears/utils/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def get_entropy_timetrace(sim_num=0,condition_num=0,exp=None,exp_info_path=None)
sim = exp + os.sep + sim
data_dict = results_manager.get_data(sim)

data = data_dict['counts']
data = data_dict['counts']/np.sum(data_dict['counts'],axis=1)

return stats.entropy(data.T)

Expand Down

0 comments on commit a287d14

Please sign in to comment.