def sample_point(nRep, keys, indices, values, writeparamfilename = "/tmp/point.par", scaleParams = False): '''given a perturbation of demographic model, writes paramfile and runs point. ''' if scaleParams: ranges = get_ranges() actualValues = [] for i in range(len(keys)): key, index, value = keys[i], indices[i], values[i] interval = ranges[key][index] low, high = float(interval[0]), float(interval[1]) actual = get_real_value(value, low, high) actualValues.append(actual) else: actualValues = values paramDict = generate_params() paramDict_updated = update_params(paramDict, keys, indices, actualValues) write_paramfile(writeparamfilename, paramDict_updated) singrate = paramDict_updated['singrate'] statfilename = "/tmp/point.err" runstatscommand = "coalescent -p " + writeparamfilename + " -n " + str(nRep) + " --drop-singletons " + str(singrate) + " --custom-stats --genmapRandomRegions > " + statfilename #print(runstatscommand) subprocess.check_output(runstatscommand, shell=True) error = calc_error(statfilename, stats, pops) return error
def get_scaled_values(): ranges = get_ranges() params = generate_params() scaledParams = {} for key in ranges.keys(): low, high = float(ranges[key][0]), float(ranges[key][1]) interval = high - low value = params[key] scaled = (value - low) / interval scaledParams[key] = scaled return scaledParams
def execute_optimize(args): '''run scipy.optimize module according to specified parameters''' print(("loading dimensions to search from: " + args.optimize_inputdimensionsfile)) runname, keys, indices = read_dimensionsfile(args.optimize_inputdimensionsfile, runType='optimize') rangeDict = get_ranges() paramDict = generate_params() x0 = [] bounds = [] for i in range(len(keys)): key = keys[i] index = indices[i] value = paramDict[key][index] interval = rangeDict[key][index] low, high = float(interval[0]), float(interval[1]) scaled = get_scaled_value(value, low, high) x0.append(scaled) bounds.append([0,1]) x0 = np.array(x0) stepdict = {'eps':float(args.stepSize)} result = optimize.minimize(sample_point_wrapper, x0, method=args.method, bounds=bounds, options=stepdict) print(result) print( "******************") #translate back to changes to model bestparams = [] assert len(keys) == len(result.x) for i in range(len(keys)): key = keys[i] index = indices[i] interval = rangeDict[key][index] low, high = float(interval[0]), float(interval[1]) realVal = get_real_value(result.x[i], low, high) bestparams.append(result.x[i]) print(("best " + str(key) + "|" + str(index) + "|" + str(realVal))) return