From 61ba0752a131c886a04e0517206723ca829b9ea9 Mon Sep 17 00:00:00 2001 From: "gitlab@srinivas.gs" Date: Wed, 17 Jul 2019 16:12:13 -0400 Subject: [PATCH] many bug fixes and improvemnents to xfit --- +xolotl/+examples/burstingCostFcn.m | 6 ++-- +xolotl/testConductances.m | 1 + +xtools/binCost.m | 46 +++++++++++++++++++++++++++ @xfit/binCost.m | 49 ----------------------------- @xfit/evaluate.m | 4 +-- @xfit/fit.m | 2 +- @xfit/xfit.m | 15 ++++++--- examples/demo_xfit.m | 11 +++++-- 8 files changed, 71 insertions(+), 63 deletions(-) create mode 100644 +xtools/binCost.m delete mode 100644 @xfit/binCost.m diff --git a/+xolotl/+examples/burstingCostFcn.m b/+xolotl/+examples/burstingCostFcn.m index eb45d0a8..9c16aeef 100644 --- a/+xolotl/+examples/burstingCostFcn.m +++ b/+xolotl/+examples/burstingCostFcn.m @@ -26,9 +26,9 @@ % accumulate errors -C = xfit.binCost([950 1050],metrics.burst_period); -C = C + xfit.binCost([.1 .3],metrics.duty_cycle_mean); -C = C + xfit.binCost([7 10],metrics.n_spikes_per_burst_mean); +C = xtools.binCost([950 1050],metrics.burst_period); +C = C + xtools.binCost([.1 .3],metrics.duty_cycle_mean); +C = C + xtools.binCost([7 10],metrics.n_spikes_per_burst_mean); % safety -- if something goes wrong, return a large cost if isnan(C) diff --git a/+xolotl/testConductances.m b/+xolotl/testConductances.m index fda76fb7..cfbe1510 100644 --- a/+xolotl/testConductances.m +++ b/+xolotl/testConductances.m @@ -8,6 +8,7 @@ if exist([xroot filesep 'cond_check.mat'],'file') == 2 load([xroot filesep 'cond_check.mat']) else + warning('It looks like this is the first time conductances are being checked. This will take a while...') ok_channels = {}; end diff --git a/+xtools/binCost.m b/+xtools/binCost.m new file mode 100644 index 00000000..e6790ca9 --- /dev/null +++ b/+xtools/binCost.m @@ -0,0 +1,46 @@ +% __ _ _ +% __ __/ _(_) |_ +% \ \/ / |_| | __| +% > <| _| | |_ +% /_/\_\_| |_|\__| +% +% +% ### binCost +% +% **Syntax** +% +% ```matlab +% c = xfit.binCost(allowed_range, actual_value) +% ``` +% +% **Description** +% +% A static method that computes a quadratic cost +% when `actual_value` is outside of the minimum and maximum +% set by the 2-vector `allowed_range`, and returns zero otherwise. +% +% This method can be used as part of a simulation function +% when it is important for neurocomputational properties of interest to +% fit within a given range, rather than necessarily fit a value perfectly. + +function c = binCost(allowed_range,actual_value) + + +if isnan(actual_value) + c = 1; + return +end + +w = (allowed_range(2) - allowed_range(1))/2; +m = (allowed_range(2) + allowed_range(1))/2; + +if actual_value < allowed_range(1) + d = m - actual_value; + c = (1- (w/d)); +elseif actual_value > allowed_range(2) + d = actual_value - m; + c = (1- (w/d)); +else + % no cost + c = 0; +end diff --git a/@xfit/binCost.m b/@xfit/binCost.m deleted file mode 100644 index 8a020bf6..00000000 --- a/@xfit/binCost.m +++ /dev/null @@ -1,49 +0,0 @@ -%{ - __ _ _ -__ __/ _(_) |_ -\ \/ / |_| | __| - > <| _| | |_ -/_/\_\_| |_|\__| - - -### binCost - -**Syntax** - -```matlab -c = xfit.binCost(allowed_range, actual_value) -``` - -**Description** - -A static method that computes a quadratic cost -when `actual_value` is outside of the minimum and maximum -set by the 2-vector `allowed_range`, and returns zero otherwise. - -This method can be used as part of a simulation function -when it is important for neurocomputational properties of interest to -fit within a given range, rather than necessarily fit a value perfectly. - -%} - -function c = binCost(allowed_range,actual_value) - - -if isnan(actual_value) - c = 1; - return -end - -w = (allowed_range(2) - allowed_range(1))/2; -m = (allowed_range(2) + allowed_range(1))/2; - -if actual_value < allowed_range(1) - d = m - actual_value; - c = (1- (w/d)); -elseif actual_value > allowed_range(2) - d = actual_value - m; - c = (1- (w/d)); -else - % no cost - c = 0; -end diff --git a/@xfit/evaluate.m b/@xfit/evaluate.m index 8809ba33..3e04d377 100644 --- a/@xfit/evaluate.m +++ b/@xfit/evaluate.m @@ -24,7 +24,7 @@ and return a cost (a double). configured in the xfit object: - `x` (the xolotl object) -- `sim_func` +- `SimFcn` - `parameter_names` %} @@ -34,4 +34,4 @@ and return a cost (a double). for i = 1:length(self.parameter_names) self.x.set(self.parameter_names{i},params(i)) end -c = self.sim_func(self.x, self.data); +c = self.SimFcn(self.x, self.data); diff --git a/@xfit/fit.m b/@xfit/fit.m index 54a5087d..83f6b606 100644 --- a/@xfit/fit.m +++ b/@xfit/fit.m @@ -29,7 +29,7 @@ function fit(self) assert(~isempty(self.parameter_names),'No parameter names defined') assert(~isempty(self.x),'Xolotl object not configured') -assert(~isempty(self.sim_func),'Simulation function not set') +assert(~isempty(self.SimFcn),'Simulation function not set') if isempty(self.seed) && ~isempty(self.ub) && ~isempty(self.lb) % pick a random seed within bounds diff --git a/@xfit/xfit.m b/@xfit/xfit.m index 0b9f1fa1..695b910a 100644 --- a/@xfit/xfit.m +++ b/@xfit/xfit.m @@ -10,7 +10,7 @@ x@xolotl % function to minimize - sim_func@function_handle + SimFcn@function_handle % parameters to optimize parameter_names@cell @@ -18,25 +18,30 @@ lb ub + PlotFcn + - display_type = 'iter' + Display = 'iter' engine - % logging - timestamp % this can be used to store any user-defined data data + options + end % end props properties (SetAccess = private) best_cost - options + + + % logging + timestamp end methods diff --git a/examples/demo_xfit.m b/examples/demo_xfit.m index fb6164cc..17398246 100644 --- a/examples/demo_xfit.m +++ b/examples/demo_xfit.m @@ -9,9 +9,11 @@ % we instantiate the xfit object % and specify the particle swarm engine p = xfit('particleswarm'); +p.options.UseParallel = true; % we assign a cost function -p.sim_func = @xolotl.examples.burstingCostFcn; +p.SimFcn = @xolotl.examples.burstingCostFcn; + % we assign the xolotl object p.x = x; @@ -34,6 +36,10 @@ plot(time,V,'k') title('Before optimization') +subplot(2,1,2); hold on +set(gca,'XLim',[0 10],'YLim',[-80 50]) + + % perform the optimization procedure p.fit; @@ -43,8 +49,7 @@ % visualize the results of the optimization -subplot(2,1,2); hold on -set(gca,'XLim',[0 10],'YLim',[-80 50]) + x.t_end = 10e3; V = x.integrate; time = (1:length(V))*1e-3*x.dt;