def closest_pairs(spots_r, spots_c): """Calculate the closest neighbours of given reference spots. Parameters ---------- spots_r, spots_c : coordinate lists The coordinates (lists of 3-tuples of floats) of objects. Returns ------- edm : euclidean distance matrix pairs : list((int, int)) The list of pairs of closest neighbours (index numbers). """ pairs = [] edm = dist_matrix(np.vstack([spots_r, spots_c])) # create a mask to ignore the reference spots ref_mask = [1] * len(spots_r) + [0] * len(spots_c) for refid in range(len(spots_r)): # the result of find_neighbor() must be adjusted by the length of the # spots_r list to retrieve the index number for the spots_c list: nearest = find_neighbor(refid, edm, ref_mask) - len(spots_r) pair = (refid, nearest) print_summary(edm, spots_c, spots_r, pair) pairs.append(pair) return (edm, pairs)
#!/usr/bin/python '''Test script for the "volpy" module.''' from volpy import dist_matrix from numpy import loadtxt, array_str, set_printoptions basedir = 'TESTDATA/filaments/' infile = basedir + 'testdata-filaments.csv' outfile = basedir + 'result_volpy_fil_detailed.txt' data = loadtxt(open(infile, 'r'), delimiter=',') dist_mat = dist_matrix(data) set_printoptions(threshold=999999) outstr = array_str(dist_mat, max_line_width=999999) output = open(outfile, 'w') output.write(outstr) print 'Parsed %i points from "%s"' % \ (len(data), infile) print('Written distance matrix to "%s"' % outfile)