def test_reorder_samples(self): """ test that regural and irregular order give the same results """ model = "linear" # Test normal order x_lbl = ['s1', 's2', 's3', 's4', 's5', 's6'] x = asarray([[0.0, 1.0, 2.0, 3.0, 4.0, 5.0],[0.0, 0.0, 6.0, 7.0, 8.0, 9.0],[0.0, 0.0, 0.0, 10.0, 11.0, 12.0],[0.0, 0.0, 0.0, 0.0, 13.0, 14.0],[0.0, 0.0, 0.0, 0.0, 0.0, 15.0]]) y_lbl = ['s1', 's2', 's3', 's4', 's5', 's6'] y = asarray([[0.0, 1.0, 2.0, 3.0, 4.0, 5.0],[0.0, 0.0, 6.0, 7.0, 8.0, 9.0],[0.0, 0.0, 0.0, 10.0, 11.0, 12.0],[0.0, 0.0, 0.0, 0.0, 13.0, 14.0],[0.0, 0.0, 0.0, 0.0, 0.0, 15.0]]) vals_exp = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 6.0, 7.0] x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram((x_lbl,x), (x_lbl,x), model, []) self.assertFloatEqual(x_vals, vals_exp) self.assertFloatEqual(y_vals, vals_exp) self.assertFloatEqual(x_fit, vals_exp) self.assertFloatEqual(y_fit, vals_exp) # Test altered model = "linear" # order = [5, 1, 3, 4, 0, 2] x_lbl = ['s6', 's2', 's4', 's5', 's1', 's3'] x = asarray([[0.0,0.0,0.0,0.0,0.0,0.0],[9.0,0.0,7.0,8.0,0.0,6.0],[14.0,0.0,0.0,13.0,0.0,0.0],[15.0,0.0,0.0,0.0,0.0,0.0],[5.0,1.0,3.0,4.0,0.0,2.0],[12.0,0.0,10.0,11.0,0.0,0.0]]) y_lbl = ['s1', 's2', 's3', 's4', 's5', 's6'] y=asarray([[0.0,1.0,2.0,3.0,4.0,5.0],[0.0,0.0,6.0,7.0,8.0,9.0],[0.0,0.0,0.0,10.0,11.0,12.0],[0.0,0.0,0.0,0.0,13.0,14.0],[0.0,0.0,0.0,0.0,0.0,15.0],[0.0,0.0,0.0,0.0,0.0,0.0]]) vals_exp = [1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.,13.,14.,15.] x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram((x_lbl,x), (y_lbl,y), model, []) self.assertFloatEqual(x_vals, vals_exp) self.assertFloatEqual(y_vals, vals_exp) self.assertFloatEqual(x_fit, vals_exp) self.assertFloatEqual(y_fit, vals_exp)
def test_models_semivariograms(self): """ test the semivariogram fitting models """ # All models should return the same x_vals, y_vals, x_fit # because we are using the same x x_lbl = ['s1', 's2', 's3', 's4', 's5', 's6'] x = asarray([[0.0, 1.0, 2.0, 3.0, 4.0, 5.0],[0.0, 0.0, 6.0, 7.0, 8.0, 9.0],[0.0, 0.0, 0.0, 10.0, 11.0, 12.0],[0.0, 0.0, 0.0, 0.0, 13.0, 14.0],[0.0, 0.0, 0.0, 0.0, 0.0, 15.0]]) vals_exp = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 6.0, 7.0] model = "nugget" y_lbl = ['s1', 's2', 's3', 's4', 's5', 's6'] y = asarray([[0.0, 5.0, 5.0, 5.0, 5.0, 5.0],[0.0, 0.0, 5.0, 5.0, 5.0, 5.0],[0.0, 0.0, 0.0, 5.0, 5.0, 5.0],[0.0, 0.0, 0.0, 0.0, 5.0, 5.0],[0.0, 0.0, 0.0, 0.0, 0.0, 5.0]]) y_vals_exp = [2.3000000143667378]*(len(x)*2) x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram((x_lbl,x), (x_lbl,x), model, []) self.assertFloatEqual(x_vals, vals_exp) self.assertFloatEqual(y_vals, vals_exp) self.assertFloatEqual(x_fit, vals_exp) self.assertFloatEqual(y_fit, y_vals_exp) model = "exponential" y_lbl = ['s1', 's2', 's3', 's4', 's5', 's6'] y = asarray([[0.0, 1.0, 22.0, 33.0, 44.0, 55.0],[0.0, 0.0, 66.0, 77.0, 88.0, 99.0],[0.0, 0.0, 0.0, 1010.0, 1111.0, 1212.0],[0.0, 0.0, 0.0, 0.0, 1313.0, 1414.0],[0.0, 0.0, 0.0, 0.0, 0.0, 1515.0]]) y_vals_exp = [-9.44475054741e-09, -9.44475054741e-09, -9.44475054741e-09, -9.44475054741e-09, 0.999999998426, 2.0000000063, 3.00000001417, 4.00000002204, 5.99999999885, 6.99999998726] x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram((x_lbl,x), (x_lbl,x), model, []) self.assertFloatEqual(x_vals, vals_exp) self.assertFloatEqual(y_vals, vals_exp) self.assertFloatEqual(x_fit, vals_exp) self.assertFloatEqual(y_fit, y_vals_exp) model = "gaussian" y_lbl = ['s1', 's2', 's3', 's4', 's5', 's6'] y = asarray([[0.0, 1.0, 22.0, 33.0, 44.0, 55.0],[0.0, 0.0, 66.0, 77.0, 88.0, 99.0],[0.0, 0.0, 0.0, 1010.0, 1111.0, 1212.0],[0.0, 0.0, 0.0, 0.0, 1313.0, 1414.0],[0.0, 0.0, 0.0, 0.0, 0.0, 1515.0]]) y_vals_exp = [0.17373665, 0.17373665, 0.17373665, 0.17373665, 0.54915494, 1.55978856, 2.91608962, 4.28808694, 6.24510109, 6.74689019] x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram((x_lbl,x), (x_lbl,x), model, []) self.assertFloatEqual(x_vals, vals_exp) self.assertFloatEqual(y_vals, vals_exp) self.assertFloatEqual(x_fit, vals_exp) self.assertFloatEqual(y_fit, y_vals_exp) model = "periodic" y_lbl = ['s1', 's2', 's3', 's4', 's5', 's6'] y = asarray([[0.0, 1.0, 22.0, 33.0, 44.0, 55.0],[0.0, 0.0, 66.0, 77.0, 88.0, 99.0],[0.0, 0.0, 0.0, 1010.0, 1111.0, 1212.0],[0.0, 0.0, 0.0, 0.0, 1313.0, 1414.0],[0.0, 0.0, 0.0, 0.0, 0.0, 1515.0]]) y_vals_exp = [0.23248033, 0.23248033, 0.23248033, 0.23248033, 0.5528678, 1.45081215, 2.74913327, 4.19164973, 6.39844476, 6.72728412] x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram((x_lbl,x), (x_lbl,x), model, []) self.assertFloatEqual(x_vals, vals_exp) self.assertFloatEqual(y_vals, vals_exp) self.assertFloatEqual(x_fit, vals_exp) self.assertFloatEqual(y_fit, y_vals_exp) model = "linear" y_lbl = x_lbl y = x x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram((x_lbl,x), (x_lbl,x), model, []) self.assertFloatEqual(x_vals, vals_exp) self.assertFloatEqual(y_vals, vals_exp) self.assertFloatEqual(x_fit, vals_exp) self.assertFloatEqual(y_fit, vals_exp)
def main(): option_parser, opts, args = parse_command_line_parameters(**script_info) if opts.binning is None: ranges = [] else: # simple ranges format validation if opts.binning.count('[')!=opts.binning.count(']') or\ opts.binning.count('[')!=opts.binning.count(','): raise ValueError, "The binning input has an error: '%s'; " % opts.binning +\ "\nthe format should be [increment1,top_limit1][increment2,top_limit2]" # spliting in ranges rgn_txt = opts.binning.split('][') # removing left [ and right ] rgn_txt[0] = rgn_txt[0][1:] rgn_txt[-1] = rgn_txt[-1][:-1] # converting into int ranges = [] max = 0 for i,r in enumerate(rgn_txt): values = map(float,r.split(',')) if len(values)!=2: raise ValueError, "All ranges must have only 2 values: [%s]" % r elif i+1!=len(rgn_txt): if values[0]>values[1]: raise ValueError, "The bin value can't be greater than the max value: [%s]" % r elif values<0: raise ValueError, "This value can not be negative: [%s]" % r elif max>values[1]: raise ValueError, "This value can not smaller than the previous one: [%s]" % r else: max=values[1] ranges.append(values) x_samples, x_distmtx = parse_distmat(open(opts.input_path_x,'U')) y_samples, y_distmtx = parse_distmat(open(opts.input_path_y,'U')) (x_val,y_val,x_fit,y_fit) = fit_semivariogram(x_distmtx, y_distmtx, opts.model, ranges) plot(x_val, y_val, 'o', color="white") plot(x_fit, y_fit, linewidth=2.0, color="blue") x_label = 'Distance (m)' y_label = 'Community Dissimilarity' fig_title = 'Semivariogram (%s)' % opts.model xlabel(x_label) ylabel(y_label) title(fig_title) savefig(opts.output_path)
def main(): option_parser, opts, args = parse_command_line_parameters(**script_info) category = opts.category mapping_fp = opts.mapping_fp colors_used = [] if (category and mapping_fp == None) or (category == None and mapping_fp): option_parser.error('If coloring by a metadata category, both the ' 'category and the mapping file must be supplied.') elif mapping_fp and category: mapping_data, mapping_headers, _ = parse_mapping_file( open(mapping_fp, 'U')) if category not in mapping_headers: option_parser.error("The category supplied must exist in the " "metadata mapping file, '%s' does not exist." % category) index = mapping_headers.index(category) categories = list(set([line[index] for line in mapping_data])) list_of_plots = [] if opts.binning is None: ranges = [] else: # simple ranges format validation if opts.binning.count('[')!=opts.binning.count(']') or\ opts.binning.count('[')!=opts.binning.count(','): raise ValueError, "The binning input has an error: '%s'; " % +\ "\nthe format should be [increment1,top_limit1][increment2,top_limit2]" # spliting in ranges rgn_txt = opts.binning.split('][') # removing left [ and right ] rgn_txt[0] = rgn_txt[0][1:] rgn_txt[-1] = rgn_txt[-1][:-1] # converting into int ranges = [] max = 0 for i, r in enumerate(rgn_txt): try: values = map(float, r.split(',')) except ValueError: raise ValueError, "Not a valid format for binning %s" % opts.binning if len(values) != 2: raise ValueError, "All ranges must have only 2 values: [%s]" % r elif i + 1 != len(rgn_txt): if values[0] > values[1]: raise ValueError, "The bin value can't be greater than the max value: [%s]" % r elif values < 0: raise ValueError, "This value can not be negative: [%s]" % r elif max > values[1]: raise ValueError, "This value can not smaller than the previous one: [%s]" % r else: max = values[1] ranges.append(values) x_samples, x_distmtx = parse_distmat(open(opts.input_path_x, 'U')) y_samples, y_distmtx = parse_distmat(open(opts.input_path_y, 'U')) if opts.ignore_missing_samples: ignoring_from_x = list(set(x_samples) - set(y_samples)) ignoring_from_y = list(set(y_samples) - set(x_samples)) if opts.verbose: print '\nFrom %s we are ignoring: %s\n' % (opts.input_path_x, ignoring_from_x) print '\nFrom %s we are ignoring: %s\n' % (opts.input_path_y, ignoring_from_y) print '\nOnly using: %s\n' % ( list(set(x_samples) & set(y_samples))) x_file = StringIO(\ filter_samples_from_distance_matrix((x_samples, x_distmtx), ignoring_from_x)) x_samples, x_distmtx = parse_distmat(x_file) y_file = StringIO(\ filter_samples_from_distance_matrix((y_samples, y_distmtx), ignoring_from_y)) y_samples, y_distmtx = parse_distmat(y_file) else: if x_distmtx.shape != y_distmtx.shape: raise ValueError, 'The distance matrices have different sizes. ' +\ 'You can cancel this error by passing --ignore_missing_samples' figure() if category == None: x_val, y_val, x_fit, y_fit, func_text = fit_semivariogram( (x_samples, x_distmtx), (y_samples, y_distmtx), opts.model, ranges) plot(x_val, y_val, color=opts.dot_color, marker=opts.dot_marker, linestyle="None", alpha=opts.dot_alpha) plot(x_fit, y_fit, linewidth=2.0, color=opts.line_color, alpha=opts.line_alpha) else: for index, single_category in enumerate(categories): good_sample_ids = sample_ids_from_metadata_description( open(mapping_fp), '%s:%s' % (category, single_category)) _y_samples, _y_distmtx = parse_distmat( StringIO( filter_samples_from_distance_matrix((y_samples, y_distmtx), good_sample_ids, negate=True))) _x_samples, _x_distmtx = parse_distmat( StringIO( filter_samples_from_distance_matrix((x_samples, x_distmtx), good_sample_ids, negate=True))) x_val, y_val, x_fit, y_fit, func_text = fit_semivariogram( (_x_samples, _x_distmtx), (_y_samples, _y_distmtx), opts.model, ranges) # retrieve one of the colors the "QIIME" colors and add it to the # list of used colors for the creation of the legends in the plot color_only = get_qiime_hex_string_color(index) colors_used.append(color_only) plot(x_val, y_val, color=color_only, marker=opts.dot_marker, linestyle="None", alpha=opts.dot_alpha) plot(x_fit, y_fit, linewidth=2.0, color=color_only, alpha=opts.line_alpha, label=single_category) if opts.x_min != None and opts.x_max != None: xlim([opts.x_min, opts.x_max]) if opts.y_min != None and opts.y_max != None: ylim([opts.y_min, opts.y_max]) x_label = opts.x_label y_label = opts.y_label fig_title = '%s (%s)' % (opts.fig_title, opts.model) xlabel(x_label) ylabel(y_label) if opts.print_model: title(fig_title + ' ' + func_text) else: title(fig_title) savefig(opts.output_path) # print the legends after the figure is exported to avoid conflicts if category: # if there's a desired format, use that, else default it to png _, extension = splitext(opts.output_path) # remove the dot, else, make_legend will add it to the filename extension = extension.replace('.', '') if extension == '': extension = 'png' make_legend(categories, colors_used, 0, 0, 'black', 'white', opts.output_path, extension, 80)
def main(): option_parser, opts, args = parse_command_line_parameters(**script_info) category = opts.category mapping_fp = opts.mapping_fp colors_used = [] if (category and mapping_fp is None) or (category is None and mapping_fp): option_parser.error('If coloring by a metadata category, both the ' 'category and the mapping file must be supplied.') elif mapping_fp and category: mapping_data, mapping_headers, _ = parse_mapping_file(open(mapping_fp, 'U')) if category not in mapping_headers: option_parser.error("The category supplied must exist in the " "metadata mapping file, '%s' does not exist." % category) index = mapping_headers.index(category) categories = list(set([line[index] for line in mapping_data])) list_of_plots = [] if opts.binning is None: ranges = [] else: # simple ranges format validation if opts.binning.count('[') != opts.binning.count(']') or\ opts.binning.count('[') != opts.binning.count(','): raise ValueError("The binning input has an error: '%s'; " % + "\nthe format should be [increment1,top_limit1][increment2,top_limit2]") # spliting in ranges rgn_txt = opts.binning.split('][') # removing left [ and right ] rgn_txt[0] = rgn_txt[0][1:] rgn_txt[-1] = rgn_txt[-1][:-1] # converting into int ranges = [] max = 0 for i, r in enumerate(rgn_txt): try: values = map(float, r.split(',')) except ValueError: raise ValueError( "Not a valid format for binning %s" % opts.binning) if len(values) != 2: raise ValueError( "All ranges must have only 2 values: [%s]" % r) elif i + 1 != len(rgn_txt): if values[0] > values[1]: raise ValueError( "The bin value can't be greater than the max value: [%s]" % r) elif values < 0: raise ValueError( "This value can not be negative: [%s]" % r) elif max > values[1]: raise ValueError( "This value can not smaller than the previous one: [%s]" % r) else: max = values[1] ranges.append(values) x_samples, x_distmtx = parse_distmat(open(opts.input_path_x, 'U')) y_samples, y_distmtx = parse_distmat(open(opts.input_path_y, 'U')) if opts.ignore_missing_samples: ignoring_from_x = list(set(x_samples) - set(y_samples)) ignoring_from_y = list(set(y_samples) - set(x_samples)) if opts.verbose: print '\nFrom %s we are ignoring: %s\n' % (opts.input_path_x, ignoring_from_x) print '\nFrom %s we are ignoring: %s\n' % (opts.input_path_y, ignoring_from_y) print '\nOnly using: %s\n' % (list(set(x_samples) & set(y_samples))) x_file = StringIO( filter_samples_from_distance_matrix((x_samples, x_distmtx), ignoring_from_x)) x_samples, x_distmtx = parse_distmat(x_file) y_file = StringIO( filter_samples_from_distance_matrix((y_samples, y_distmtx), ignoring_from_y)) y_samples, y_distmtx = parse_distmat(y_file) else: if x_distmtx.shape != y_distmtx.shape: raise ValueError('The distance matrices have different sizes. ' + 'You can cancel this error by passing --ignore_missing_samples') figure() if category is None: x_val, y_val, x_fit, y_fit, func_text = fit_semivariogram( (x_samples, x_distmtx), (y_samples, y_distmtx), opts.model, ranges) plot( x_val, y_val, color=opts.dot_color, marker=opts.dot_marker, linestyle="None", alpha=opts.dot_alpha) plot( x_fit, y_fit, linewidth=2.0, color=opts.line_color, alpha=opts.line_alpha) else: # not all the categories that are going to be enumerated are found in # the distance matrices i.e. the mapping file is a superset that can # contain more samples than the distance matrices used_categories = deepcopy(categories) for index, single_category in enumerate(categories): good_sample_ids = sample_ids_from_metadata_description( open(mapping_fp), '%s:%s' % (category, single_category)) try: _y_samples, _y_distmtx = parse_distmat(StringIO( filter_samples_from_distance_matrix((y_samples, y_distmtx), good_sample_ids, negate=True))) _x_samples, _x_distmtx = parse_distmat(StringIO( filter_samples_from_distance_matrix((x_samples, x_distmtx), good_sample_ids, negate=True))) except ValueError: # no samples found for this category used_categories.remove(single_category) continue x_val, y_val, x_fit, y_fit, func_text = fit_semivariogram( (_x_samples, _x_distmtx), (_y_samples, _y_distmtx), opts.model, ranges) # retrieve one of the colors the "QIIME" colors and add it to the # list of used colors for the creation of the legends in the plot color_only = get_qiime_hex_string_color(index) colors_used.append(color_only) plot(x_val, y_val, color=color_only, marker=opts.dot_marker, linestyle="None", alpha=opts.dot_alpha) plot(x_fit, y_fit, linewidth=2.0, color=color_only, alpha=opts.line_alpha, label=single_category) # set plot limits if requested x_lb, x_ub = xlim() y_lb, y_ub = ylim() if opts.x_min is not None: x_lb = opts.x_min if opts.x_max is not None: x_ub = opts.x_max if opts.y_min is not None: y_lb = opts.y_min if opts.y_max is not None: y_ub = opts.y_max xlim(x_lb, x_ub) ylim(y_lb, y_ub) x_label = opts.x_label y_label = opts.y_label fig_title = '%s (%s)' % (opts.fig_title, opts.model) xlabel(x_label) ylabel(y_label) if opts.print_model: title(fig_title + ' ' + func_text) else: title(fig_title) savefig(opts.output_path) # print the legends after the figure is exported to avoid conflicts if category: # if there's a desired format, use that, else default it to png _, extension = splitext(opts.output_path) # remove the dot, else, make_legend will add it to the filename extension = extension.replace('.', '') if extension == '': extension = 'png' make_legend(used_categories, colors_used, 0, 0, 'black', 'white', opts.output_path, extension, 80)
def test_models_semivariograms(self): """ test the semivariogram fitting models """ # All models should return the same x_vals, y_vals, x_fit # because we are using the same x x_lbl = ['s1', 's2', 's3', 's4', 's5', 's6'] x = asarray([[0.0, 1.0, 2.0, 3.0, 4.0, 5.0], [0.0, 0.0, 6.0, 7.0, 8.0, 9.0], [0.0, 0.0, 0.0, 10.0, 11.0, 12.0], [0.0, 0.0, 0.0, 0.0, 13.0, 14.0], [0.0, 0.0, 0.0, 0.0, 0.0, 15.0]]) vals_exp = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 6.0, 7.0] model = "nugget" y_lbl = ['s1', 's2', 's3', 's4', 's5', 's6'] y = asarray([[0.0, 5.0, 5.0, 5.0, 5.0, 5.0], [0.0, 0.0, 5.0, 5.0, 5.0, 5.0], [0.0, 0.0, 0.0, 5.0, 5.0, 5.0], [0.0, 0.0, 0.0, 0.0, 5.0, 5.0], [0.0, 0.0, 0.0, 0.0, 0.0, 5.0]]) y_vals_exp = [2.3000000143667378] * (len(x) * 2) x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram( (x_lbl, x), (x_lbl, x), model, []) assert_almost_equal(x_vals, vals_exp) assert_almost_equal(y_vals, vals_exp) assert_almost_equal(x_fit, vals_exp) assert_almost_equal(y_fit, y_vals_exp) model = "exponential" y_lbl = ['s1', 's2', 's3', 's4', 's5', 's6'] y = asarray([[0.0, 1.0, 22.0, 33.0, 44.0, 55.0], [0.0, 0.0, 66.0, 77.0, 88.0, 99.0], [0.0, 0.0, 0.0, 1010.0, 1111.0, 1212.0], [0.0, 0.0, 0.0, 0.0, 1313.0, 1414.0], [0.0, 0.0, 0.0, 0.0, 0.0, 1515.0]]) x_vals_exp = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 6.0, 7.0] y_vals_exp = [0.0, 0.0, 0.0, 0.0, 1.0, 22.0, 33.0, 44.0, 66.0, 77.0] x_fit_exp = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 6.0, 7.0] y_fit_exp = [ -1.481486808707005, -1.481486808707005, -1.481486808707005, -1.481486808707005, 9.72783464904061, 20.937152199747878, 32.14646584698613, 43.355775583612704, 65.7743833464588, 76.983681369107 ] x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram( (x_lbl, x), (y_lbl, y), model, []) assert_almost_equal(x_vals, x_vals_exp) assert_almost_equal(y_vals, y_vals_exp) assert_almost_equal(x_fit, x_fit_exp) assert_almost_equal(y_fit, y_fit_exp, decimal=2) model = "gaussian" y_lbl = ['s1', 's2', 's3', 's4', 's5', 's6'] y = asarray([[0.0, 1.0, 22.0, 33.0, 44.0, 55.0], [0.0, 0.0, 66.0, 77.0, 88.0, 99.0], [0.0, 0.0, 0.0, 1010.0, 1111.0, 1212.0], [0.0, 0.0, 0.0, 0.0, 1313.0, 1414.0], [0.0, 0.0, 0.0, 0.0, 0.0, 1515.0]]) y_vals_exp = [ 0.17373844, 0.17373844, 0.17373844, 0.17373844, 0.54915099, 1.5597716, 2.91606171, 4.2880578, 6.24509872, 6.74690541 ] x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram( (x_lbl, x), (x_lbl, x), model, []) assert_almost_equal(x_vals, vals_exp) assert_almost_equal(y_vals, vals_exp) assert_almost_equal(x_fit, vals_exp) assert_almost_equal(y_fit, y_vals_exp, decimal=2) model = "periodic" y_lbl = ['s1', 's2', 's3', 's4', 's5', 's6'] y = asarray([[0.0, 1.0, 22.0, 33.0, 44.0, 55.0], [0.0, 0.0, 66.0, 77.0, 88.0, 99.0], [0.0, 0.0, 0.0, 1010.0, 1111.0, 1212.0], [0.0, 0.0, 0.0, 0.0, 1313.0, 1414.0], [0.0, 0.0, 0.0, 0.0, 0.0, 1515.0]]) y_vals_exp = [ 0.2324873886681871, 0.2324873886681871, 0.2324873886681871, 0.2324873886681871, 0.5528698895985695, 1.4508010363573784, 2.7491053124879112, 4.191607473962063, 6.39840364731269, 6.727263101495738 ] x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram( (x_lbl, x), (x_lbl, x), model, []) assert_almost_equal(x_vals, vals_exp) assert_almost_equal(y_vals, vals_exp) assert_almost_equal(x_fit, vals_exp) assert_almost_equal(y_fit, y_vals_exp, decimal=2) model = "linear" y_lbl = x_lbl y = x x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram( (x_lbl, x), (x_lbl, x), model, []) assert_almost_equal(x_vals, vals_exp) assert_almost_equal(y_vals, vals_exp) assert_almost_equal(x_fit, vals_exp) assert_almost_equal(y_fit, vals_exp, decimal=2)
def test_models_semivariograms(self): """ test the semivariogram fitting models """ # All models should return the same x_vals, y_vals, x_fit # because we are using the same x x_lbl = ['s1', 's2', 's3', 's4', 's5', 's6'] x = asarray( [[0.0, 1.0, 2.0, 3.0, 4.0, 5.0], [0.0, 0.0, 6.0, 7.0, 8.0, 9.0], [0.0, 0.0, 0.0, 10.0, 11.0, 12.0], [0.0, 0.0, 0.0, 0.0, 13.0, 14.0], [0.0, 0.0, 0.0, 0.0, 0.0, 15.0]]) vals_exp = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 6.0, 7.0] model = "nugget" y_lbl = ['s1', 's2', 's3', 's4', 's5', 's6'] y = asarray( [[0.0, 5.0, 5.0, 5.0, 5.0, 5.0], [0.0, 0.0, 5.0, 5.0, 5.0, 5.0], [0.0, 0.0, 0.0, 5.0, 5.0, 5.0], [0.0, 0.0, 0.0, 0.0, 5.0, 5.0], [0.0, 0.0, 0.0, 0.0, 0.0, 5.0]]) y_vals_exp = [2.3000000143667378] * (len(x) * 2) x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram( (x_lbl, x), (x_lbl, x), model, []) assert_almost_equal(x_vals, vals_exp) assert_almost_equal(y_vals, vals_exp) assert_almost_equal(x_fit, vals_exp) assert_almost_equal(y_fit, y_vals_exp) model = "exponential" y_lbl = ['s1', 's2', 's3', 's4', 's5', 's6'] y = asarray( [[0.0, 1.0, 22.0, 33.0, 44.0, 55.0], [0.0, 0.0, 66.0, 77.0, 88.0, 99.0], [0.0, 0.0, 0.0, 1010.0, 1111.0, 1212.0], [0.0, 0.0, 0.0, 0.0, 1313.0, 1414.0], [0.0, 0.0, 0.0, 0.0, 0.0, 1515.0]]) x_vals_exp = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 6.0, 7.0] y_vals_exp = [0.0, 0.0, 0.0, 0.0, 1.0, 22.0, 33.0, 44.0, 66.0, 77.0] x_fit_exp = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 6.0, 7.0] y_fit_exp = [-1.481486808707005, -1.481486808707005, -1.481486808707005, -1.481486808707005, 9.72783464904061, 20.937152199747878, 32.14646584698613, 43.355775583612704, 65.7743833464588, 76.983681369107] x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram( (x_lbl, x), (y_lbl, y), model, []) assert_almost_equal(x_vals, x_vals_exp) assert_almost_equal(y_vals, y_vals_exp) assert_almost_equal(x_fit, x_fit_exp) assert_almost_equal(y_fit, y_fit_exp, decimal=2) model = "gaussian" y_lbl = ['s1', 's2', 's3', 's4', 's5', 's6'] y = asarray( [[0.0, 1.0, 22.0, 33.0, 44.0, 55.0], [0.0, 0.0, 66.0, 77.0, 88.0, 99.0], [0.0, 0.0, 0.0, 1010.0, 1111.0, 1212.0], [0.0, 0.0, 0.0, 0.0, 1313.0, 1414.0], [0.0, 0.0, 0.0, 0.0, 0.0, 1515.0]]) y_vals_exp = [0.17373844, 0.17373844, 0.17373844, 0.17373844, 0.54915099, 1.5597716 , 2.91606171, 4.2880578 , 6.24509872, 6.74690541] x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram( (x_lbl, x), (x_lbl, x), model, []) assert_almost_equal(x_vals, vals_exp) assert_almost_equal(y_vals, vals_exp) assert_almost_equal(x_fit, vals_exp) assert_almost_equal(y_fit, y_vals_exp, decimal=2) model = "periodic" y_lbl = ['s1', 's2', 's3', 's4', 's5', 's6'] y = asarray( [[0.0, 1.0, 22.0, 33.0, 44.0, 55.0], [0.0, 0.0, 66.0, 77.0, 88.0, 99.0], [0.0, 0.0, 0.0, 1010.0, 1111.0, 1212.0], [0.0, 0.0, 0.0, 0.0, 1313.0, 1414.0], [0.0, 0.0, 0.0, 0.0, 0.0, 1515.0]]) y_vals_exp = [0.2324873886681871, 0.2324873886681871, 0.2324873886681871, 0.2324873886681871, 0.5528698895985695, 1.4508010363573784, 2.7491053124879112, 4.191607473962063, 6.39840364731269, 6.727263101495738] x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram( (x_lbl, x), (x_lbl, x), model, []) assert_almost_equal(x_vals, vals_exp) assert_almost_equal(y_vals, vals_exp) assert_almost_equal(x_fit, vals_exp) assert_almost_equal(y_fit, y_vals_exp, decimal=2) model = "linear" y_lbl = x_lbl y = x x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram( (x_lbl, x), (x_lbl, x), model, []) assert_almost_equal(x_vals, vals_exp) assert_almost_equal(y_vals, vals_exp) assert_almost_equal(x_fit, vals_exp) assert_almost_equal(y_fit, vals_exp, decimal=2)
def main(): option_parser, opts, args = parse_command_line_parameters(**script_info) if opts.binning is None: ranges = [] else: # simple ranges format validation if opts.binning.count('[')!=opts.binning.count(']') or\ opts.binning.count('[')!=opts.binning.count(','): raise ValueError, "The binning input has an error: '%s'; " % +\ "\nthe format should be [increment1,top_limit1][increment2,top_limit2]" # spliting in ranges rgn_txt = opts.binning.split('][') # removing left [ and right ] rgn_txt[0] = rgn_txt[0][1:] rgn_txt[-1] = rgn_txt[-1][:-1] # converting into int ranges = [] max = 0 for i,r in enumerate(rgn_txt): try: values = map(float,r.split(',')) except ValueError: raise ValueError, "Not a valid format for binning %s" % opts.binning if len(values)!=2: raise ValueError, "All ranges must have only 2 values: [%s]" % r elif i+1!=len(rgn_txt): if values[0]>values[1]: raise ValueError, "The bin value can't be greater than the max value: [%s]" % r elif values<0: raise ValueError, "This value can not be negative: [%s]" % r elif max>values[1]: raise ValueError, "This value can not smaller than the previous one: [%s]" % r else: max=values[1] ranges.append(values) x_samples, x_distmtx = parse_distmat(open(opts.input_path_x,'U')) y_samples, y_distmtx = parse_distmat(open(opts.input_path_y,'U')) if opts.ignore_missing_samples: ignoring_from_x = list(set(x_samples)-set(y_samples)) ignoring_from_y = list(set(y_samples)-set(x_samples)) if opts.verbose: print '\nFrom %s we are ignoring: %s\n' % (opts.input_path_x, ignoring_from_x) print '\nFrom %s we are ignoring: %s\n' % (opts.input_path_y, ignoring_from_y) print '\nOnly using: %s\n' % (list(set(x_samples) & set(y_samples))) x_file = StringIO(\ filter_samples_from_distance_matrix((x_samples, x_distmtx), ignoring_from_x)) x_samples, x_distmtx = parse_distmat(x_file) y_file = StringIO(\ filter_samples_from_distance_matrix((y_samples, y_distmtx), ignoring_from_y)) y_samples, y_distmtx = parse_distmat(y_file) else: if x_distmtx.shape!=y_distmtx.shape: raise ValueError, 'The distance matrices have different sizes. ' +\ 'You can cancel this error by passing --ignore_missing_samples' (x_val,y_val,x_fit,y_fit,func_text) =\ fit_semivariogram((x_samples,x_distmtx), (y_samples,y_distmtx), opts.model, ranges) plot(x_val, y_val, color=opts.dot_color, marker=opts.dot_marker, linestyle="None", alpha=opts.dot_alpha) plot(x_fit, y_fit, linewidth=2.0, color=opts.line_color, alpha=opts.line_alpha) if opts.x_min!=None and opts.x_max!=None: xlim([opts.x_min,opts.x_max]) if opts.y_min!=None and opts.y_max!=None: ylim([opts.y_min,opts.y_max]) x_label = opts.x_label y_label = opts.y_label fig_title = '%s (%s)' % (opts.fig_title, opts.model) xlabel(x_label) ylabel(y_label) if opts.print_model: title(fig_title + ' ' + func_text) else: title(fig_title) savefig(opts.output_path)
def test_reorder_samples(self): """ test that regural and irregular order give the same results """ model = "linear" # Test normal order x_lbl = ["s1", "s2", "s3", "s4", "s5", "s6"] x = asarray( [ [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], [0.0, 0.0, 6.0, 7.0, 8.0, 9.0], [0.0, 0.0, 0.0, 10.0, 11.0, 12.0], [0.0, 0.0, 0.0, 0.0, 13.0, 14.0], [0.0, 0.0, 0.0, 0.0, 0.0, 15.0], ] ) y_lbl = ["s1", "s2", "s3", "s4", "s5", "s6"] y = asarray( [ [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], [0.0, 0.0, 6.0, 7.0, 8.0, 9.0], [0.0, 0.0, 0.0, 10.0, 11.0, 12.0], [0.0, 0.0, 0.0, 0.0, 13.0, 14.0], [0.0, 0.0, 0.0, 0.0, 0.0, 15.0], ] ) vals_exp = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 6.0, 7.0] x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram((x_lbl, x), (x_lbl, x), model, []) assert_almost_equal(x_vals, vals_exp) assert_almost_equal(y_vals, vals_exp) assert_almost_equal(x_fit, vals_exp) assert_almost_equal(y_fit, vals_exp) # Test altered model = "linear" # order = [5, 1, 3, 4, 0, 2] x_lbl = ["s6", "s2", "s4", "s5", "s1", "s3"] x = asarray( [ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [9.0, 0.0, 7.0, 8.0, 0.0, 6.0], [14.0, 0.0, 0.0, 13.0, 0.0, 0.0], [15.0, 0.0, 0.0, 0.0, 0.0, 0.0], [5.0, 1.0, 3.0, 4.0, 0.0, 2.0], [12.0, 0.0, 10.0, 11.0, 0.0, 0.0], ] ) y_lbl = ["s1", "s2", "s3", "s4", "s5", "s6"] y = asarray( [ [0.0, 1.0, 2.0, 3.0, 4.0, 5.0], [0.0, 0.0, 6.0, 7.0, 8.0, 9.0], [0.0, 0.0, 0.0, 10.0, 11.0, 12.0], [0.0, 0.0, 0.0, 0.0, 13.0, 14.0], [0.0, 0.0, 0.0, 0.0, 0.0, 15.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], ] ) vals_exp = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0] x_vals, y_vals, x_fit, y_fit, func_text = fit_semivariogram((x_lbl, x), (y_lbl, y), model, []) assert_almost_equal(x_vals, vals_exp) assert_almost_equal(y_vals, vals_exp) assert_almost_equal(x_fit, vals_exp) assert_almost_equal(y_fit, vals_exp)