def test_hist_from_returned_objects(self): plt.figure() # UniformAPPDSampler t = time.time() sampler = UniformAPPDSampler( ranges=self.ranges, cal_width=self.width, cal_height=self.height, reference=self.reference, temperature=5.0, appd_range_bins=20, init_jobs=4, appd_range_dicovery_samples=HISTOGRAM_TEST_SAMPLES, width=int(self.width / 4), height=int(self.height / 4), min_cropped_size=(int(self.width / 4 / 1.5), int(self.height / 4 / 1.5))) sampler = ParallelBufferedSampler(sampler=sampler, n_jobs=4, buffer_size=50) print( "ParallelBufferedSampler with UniformAPPDSampler initialization time: %.02f" % (time.time() - t)) appds = list() t = time.time() for i in range(HISTOGRAM_TEST_SAMPLES): # The sampler will enforce the min correct size c = sampler.next() appds.append( c.appd(reference=self.reference, width=self.width, height=self.height, normalized=True)) _ = c.rectify(image=self.test_image, mode='preserving') print( "ParallelBufferedSampler with UniformAPPDSampler, APPD calculation and rectification for %d normalized samples (full res): %.02f, AVG: %.04f" % (HISTOGRAM_TEST_SAMPLES, time.time() - t, (time.time() - t) / HISTOGRAM_TEST_SAMPLES)) plt.hist(appds, ec="k") sampler.stop() plt.grid() plt.savefig( path_here( 'ParallelBufferedSampler_UniformAPPDSampler_test_hist_from_returned_objects.png' )) plt.close() # Save a plot that shows the sample sequence (useful for debugging bad random initialization) plt.figure() plt.plot(appds, 'x') plt.grid() plt.savefig( path_here( 'ParallelBufferedSampler_UniformAPPDSampler_test_hist_from_returned_objects_sequence.png' )) plt.close()
def test_ParallelBufferedSampler(self): plt.figure() # UniformAPPDSampler t = time.time() sampler = UniformAPPDSampler( ranges=self.ranges, cal_width=self.width, cal_height=self.height, reference=self.reference, temperature=5.0, appd_range_bins=10, init_jobs=4, appd_range_dicovery_samples=HISTOGRAM_TEST_SAMPLES, width=int(self.width / 4), height=int(self.height / 4), min_cropped_size=(int(self.width / 4 / 1.5), int(self.height / 4 / 1.5))) sampler = ParallelBufferedSampler(sampler=sampler, n_jobs=4, buffer_size=50) print( "ParallelBufferedSampler with UniformAPPDSampler initialization time: %.02f" % (time.time() - t)) t = time.time() bins, hist_edges, appds = sampler.histogram( self.reference, n_samples=HISTOGRAM_TEST_SAMPLES, return_values=True, n_bins=10, width=int(self.width / 4), height=int(self.height / 4), normalized=True) print( "ParallelBufferedSampler with UniformAPPDSampler time for %d normalized samples (1/16 res): %.02f, AVG: %.04f" % (HISTOGRAM_TEST_SAMPLES, time.time() - t, (time.time() - t) / HISTOGRAM_TEST_SAMPLES)) plt.bar(hist_edges[:-1], bins, width=np.diff(hist_edges), ec="k", align="edge", label="UniformAPPDSampler", alpha=0.5) sampler.stop() plt.grid() plt.legend() plt.savefig( path_here( 'ParallelBufferedSampler_UniformAPPDSampler_test_histogram_normalized_small.png' )) plt.close() # Save a plot that shows the sample sequence plt.figure() plt.plot(appds, 'x') plt.grid() plt.savefig( path_here( 'ParallelBufferedSampler_UniformAPPDSampler_test_histogram_normalized_sequence.png' )) plt.close()
def test_compare_noncached_and_cached_TriangularParallelBufferedSampler( self): plt.figure() # UniformAPPDSampler sampler = UniformAPPDSampler( ranges=self.ranges, cal_width=self.width, cal_height=self.height, sampler='triangular', reference=self.reference, temperature=5.0, appd_range_bins=20, init_jobs=4, appd_range_dicovery_samples=HISTOGRAM_TEST_SAMPLES, width=int(self.width / 4), height=int(self.height / 4), min_cropped_size=(int(self.width / 4 / 1.5), int(self.height / 4 / 1.5))) t = time.time() # # NONCACHED ----------------------------------------- # noncached_sampler = ParallelBufferedSampler(sampler=sampler, n_jobs=4, buffer_size=50) # print("Non-cached TriangularParallelBufferedSampler with UniformAPPDSampler initialization time: %.02f" % (time.time()-t)) # # appds = list() # t = time.time() # for i in range(2*HISTOGRAM_TEST_SAMPLES): # # The sampler will enforce the min correct size # c = noncached_sampler.next() # appds.append(c.appd(reference=self.reference, width=self.width, height=self.height, normalized=True)) # _ = c.rectify(image=self.test_image, mode='preserving') # print("Non-cached TriangularParallelBufferedSampler with UniformAPPDSampler, APPD calculation and rectification for %d normalized samples (full res): %.02f, AVG: %.04f" % # (HISTOGRAM_TEST_SAMPLES, time.time()-t, (time.time()-t)/HISTOGRAM_TEST_SAMPLES)) # plt.hist(appds, ec="k", label='non-cached, t=%ds' % int(time.time()-t)) # noncached_sampler.stop() # CACHED ----------------------------------------- t = time.time() cached_sampler = ParallelBufferedSampler(sampler=sampler, n_jobs=1, buffer_size=5, cache_size=200) print( "Cached TriangularParallelBufferedSampler with UniformAPPDSampler initialization time: %.02f" % (time.time() - t)) appds = list() t = time.time() for i in range(2 * HISTOGRAM_TEST_SAMPLES): # The sampler will enforce the min correct size c = cached_sampler.next() appds.append( c.appd(reference=self.reference, width=self.width, height=self.height, normalized=True)) _ = c.rectify(image=self.test_image, mode='preserving') print( "Cached TriangularParallelBufferedSampler with UniformAPPDSampler, APPD calculation and rectification for %d normalized samples (full res): %.02f, AVG: %.04f" % (HISTOGRAM_TEST_SAMPLES, time.time() - t, (time.time() - t) / HISTOGRAM_TEST_SAMPLES)) plt.hist(appds, ec="k", label='cached, t=%ds' % int(time.time() - t)) cached_sampler.stop() # ---------------------------------------------- plt.grid() plt.legend() plt.savefig( path_here( 'non_cached_vs_cached_TriangularParallelBufferedSampler.png')) plt.close() plt.figure() plt.plot(appds, 'x') plt.grid() plt.savefig( path_here( 'non_cached_vs_cached_TriangularParallelBufferedSampler_sequence.png' )) plt.close()