def test_Linlogbinfinder_zeroBehaviour(self): b = binner.Bins(float, 0, 20, 'linlog', 2) data = [(1, 7), (0, 8)] exp_result = [8.0, 7.0] + [None] * 10 self.assertEqual(b.bin_average(data).tolist(), exp_result) data.append((-1, 2)) self.assertRaises(binner.BinLimitError, b.bin_average, data) b = binner.Bins(float, 1, 20, 'linlog', 2) data = [(1, 7), (0, 8)] self.assertRaises(binner.BinLimitError, b.bin_average, data)
def test_custom_bins(self): """Test arbitrary bins.""" bins = binner.Bins(int, 0, 0, 'custom', [1, 2.5, 4.5]) self.assertEqual(bins.widths.tolist(), [2, 2]) self.assertEqual(bins.centers.tolist(), [1.5, 3.5]) bins = binner.Bins(int, 0, 0, 'custom', [-1.5, 0, 6.5, 6.6]) self.assertEqual(bins.widths.tolist(), [1, 7, 0]) bins = binner.Bins(int, 0, 0, 'custom', [-10, -4, -1.5]) self.assertEqual(bins.widths.tolist(), [6, 3])
def test_Linlogbins_float(self): b = binner.Bins(float, 7, 12, 'linlog', 2) exp_bins = (7, 8, 9, 10, 11, 22) self.assertEqual(b.bin_limits, exp_bins) exp_widths = [1, 1, 1, 1, 11] self.assertEqual(b.widths.tolist(), exp_widths) exp_centers = [7.5, 8.5, 9.5, 10.5, (22 + 11) / 2.0] self.assertEqual(b.centers.tolist(), exp_centers) b = binner.Bins(float, 0, 11, 'linlog', 2) exp_widths = [1.0] * len(b) self.assertEqual(b.widths.tolist(), exp_widths)
def plot_commSizeDist(nc, ncName): """Plot community size distribution.""" bins = binner.Bins(int, 1, nc.getGiantSize(), 'linlog', 1.5) binned_data = bins.bin_count_divide(nc.getCommunitySizes()) # Set plotting properties l, b, r, t = 0.1, 0.1, 0.98, (0.93 if print_titles else 0.98) axes_rect = [l, b, r - l, t - b] #[left, bottom, width, height] pylab.rcParams.update(fig_utils.get_rcParams(float(fig_width_cm)), fig_ratio=0.8, font_sizes=font_sizes) fig = pylab.figure() ax = fig.add_axes(axes_rect) ax.loglog(bins.centers, binned_data, 'b.-') ax.set_xlabel(r"Community size") ax.set_ylabel(r"Number of communities") if print_titles: ax.set_title(r"Community size distribution (binned)") # Adjust axis V = ax.axis() ax.axis((1, bins.bin_limits[-1], V[2], V[3])) # Save figure. fig_utils.savefig(fig, ncName + "_sizeDist", save_formats)
def binData(data, binfunction, bintype, binvalue, minValue=None, maxValue=None, dataType=None): #if data is tuple, the first value is the key if minValue == None or maxValue == None: dataIterator = data.__iter__() firstValue = dataIterator.next( ) #assume that data has more than 1 value try: firstKey = firstValue[0] #data is tuples if minValue == None: minValue = min(imap(lambda x: x[0], data)) if maxValue == None: maxValue = max(imap(lambda x: x[0], data)) except Exception: #data is not tuples firstKey = firstValue if minValue == None: minValue = min(data) if maxValue == None: maxValue = max(data) if dataType == None: dataType = type(firstKey) if bintype == "linlog": minValue = int(minValue) bins = binner.Bins(dataType, minValue, maxValue, bintype, binvalue) function = getattr(bins, 'bin_' + binfunction) binned = function(data) return bins, binned
def test_Maxlogbins(self): minValue = 1 maxValue = 20 b = binner.Bins(int, minValue, maxValue, 'maxlog') self.assertTrue(b.bin_limits[0] == minValue) self.assertTrue(b.bin_limits[-1] == maxValue) for w in b.widths: self.assertTrue(w >= 1)
def test_Logbins_int(self): b = binner.Bins(int, 1, 5, 'log', 2) expected_bins = (1, 2, 4, 8) self.assertEqual(b.bin_limits, expected_bins) expected_centers = [1, 2.5, 6] self.assertEqual(b.centers.tolist(), expected_centers) expected_widths = [1, 2, 5] self.assertEqual(b.widths.tolist(), expected_widths)
def createBins(x, minValueX=None, maxValueX=None, xscale='log', nbinsX=1.5): xidx = getLimits2D(x, xscale=xscale) if minValueX == None: minValueX = min(x[xidx]) if maxValueX == None: maxValueX = max(x[xidx]) bins = binner.Bins(float, minValueX, maxValueX, xscale, nbinsX) return bins
def test_Linbins_int(self): N_bin = 3 b = binner.Bins(int, 1, 5, 'lin', N_bin) self.assertEqual(len(b), N_bin) self.assertEqual(b.widths.sum(), 5) N_bin = 10 b = binner.Bins(int, 1, 10, 'lin', N_bin) self.assertEqual(len(b), N_bin) self.assertEqual(b.widths.tolist(), [1] * N_bin) self.assertEqual(b.centers.tolist(), list(range(1, 11))) N_bin = 25 b = binner.Bins(int, 26, 75, 'lin', N_bin) self.assertEqual(len(b), N_bin) self.assertEqual(b.widths.tolist(), [2] * N_bin) self.assertEqual(b.centers.tolist(), [x + 0.5 for x in range(26, 75, 2)])
def test_Linbins_float(self): N_bin = 3 b = binner.Bins(float, 1, 5, 'lin', N_bin) self.assertEqual(len(b), N_bin) self.assertEqual(b.widths.sum(), 4.0) exp_centers = [ 1 + (5 - 1) / 6.0, 1 + 3 * (5 - 1) / 6.0, 1 + 5 * (5 - 1) / 6.0 ] for c, ce in zip(exp_centers, b.centers): self.assertEqual("%.8f" % c, "%.8f" % ce) for w, we in zip([(5 - 1) / 3.0] * N_bin, b.widths): self.assertEqual("%.8f" % w, "%.8f" % we)
def plot_avgCommWeight(nc, ncName, net): """Plot average weight of communities.""" def w_gen(comm, net): w_avg_total = np.mean(list(net.weights)) for c in comm: subnet = transforms.getSubnet(net, c) if len(subnet): w = np.sum(list(subnet.weights)) / (len(subnet) * w_avg_total) yield len(subnet), w bins = binner.Bins(int, 1, nc.getGiantSize(), 'linlog', 1.5) perc = (0.1, 0.25, 0.5, 0.75, 0.9) binned_data = bins.bin_percentile(w_gen(nc, net), perc) # Set plotting properties l, b, r, t = 0.1, 0.1, 0.98, (0.93 if print_titles else 0.98) axes_rect = [l, b, r - l, t - b] #[left, bottom, width, height] pylab.rcParams.update(fig_utils.get_rcParams(float(fig_width_cm)), fig_ratio=0.8, font_sizes=font_sizes) fig = pylab.figure() ax = fig.add_axes(axes_rect) plot_styles = ('b:', 'b--', 'r-', 'b--', 'b:') labels = (r"%d$^{\mathrm{th}}$ percentile" % int(100 * perc[0]), r"%d$^{\mathrm{th}}$ percentile" % int(100 * perc[1]), r"Median", r"%d$^{\mathrm{th}}$ percentile" % int(100 * perc[3]), r"%d$^{\mathrm{th}}$ percentile" % int(100 * perc[4])) for p, data, sty, lbl in zip(perc, binned_data, plot_styles, labels): ax.loglog(bins.centers, data, sty, label=lbl) ax.set_xlabel(r"Community size") ax.set_ylabel(r"$\langle w_{\mathrm{comm}} \rangle /" r"\langle w_{\mathrm{total}} \rangle$") if print_titles: ax.set_title( r"Ratio of avg community weigth to avg total weight (binned)") ax.legend(loc='best') # Adjust axis V = ax.axis() ax.axis((1, bins.bin_limits[-1], V[2], V[3])) # Save figure. fig_utils.savefig(fig, ncName + "_weightDist", save_formats)
def setUp(self): # Construct good data self.weights = [7.5, 2, 4, 3, 100, 0.1, 0.9, 2, 3] self.coords = [20, 5, 5, 10, 16, 7, 7, 11, 12] self.values = [10, 0.5, 3.5, 1, 1.6, 100, 2, 10, 20] self.data = zip(self.coords, self.values) self.weighted_data = zip(self.coords, self.values, self.weights) # Construct bad data self.bad_coords_A = [20, -1, 5, 10, 16, 7, 7, 11, 12] self.bad_coords_B = [20, 2, 5, 10, 30, 7, 7, 11, 12] self.bad_data_A = zip(self.bad_coords_A, self.values) self.bad_data_B = zip(self.bad_coords_B, self.values) self.bad_wdata_A = zip(self.bad_coords_A, self.values, self.weights) self.bad_wdata_B = zip(self.bad_coords_B, self.values, self.weights) self.bins = binner.Bins(int, 0, 20, 'lin', 10)
def densityPlot(x, y, bins2D=None, bins=None, xscale='log', yscale='log', nbinsX=1.5, nbinsY=1.5, weights=None): """ 2-Plot static distance vs temporal distance Plot fraction of Finite distances vs temporal distance Plot the distance distribution """ x = np.array(x) y = np.array(y) if bins2D == None or bins == None: xidx, yidx = getLimits2D(x, y, xscale, yscale) minValueX, maxValueX = min(x[xidx]), max(x[xidx]) minValueY, maxValueY = min(y[yidx]), max(y[yidx]) print minValueX, maxValueX, minValueY, maxValueY bins2D = binner.Bins2D(float, minValueX, maxValueX, xscale, nbinsX, float, minValueY, maxValueY, yscale, nbinsY) bins = binner.Bins(float, minValueX, maxValueX, xscale, nbinsX) binned_data, b1, b2 = np.histogram2d(x, y, bins=bins2D.bin_limits, weights=weights) binned_data = binned_data.astype(np.float64) X, Y = bins2D.edge_grids z, b = np.histogram(x, bins=bins.bin_limits, weights=weights) for i in np.nonzero(z)[0]: binned_data[i, :] = binned_data[i, :] / z[i] binned_data = binned_data.transpose() binned_data = np.ma.masked_array(binned_data, binned_data == 0) if weights == None: z1, b = np.histogram(x, bins=bins.bin_limits, weights=y) else: z1, b = np.histogram(x, bins=bins.bin_limits, weights=y * weights) z1 = z1.astype(np.float64) for i in np.nonzero(z)[0]: z1[i] = z1[i] / z[i] return X, Y, binned_data, bins, z1