def compute(self, matrix, mapping, method, slices): num_slices = len(slices) valid_data = np.ma.masked_invalid(matrix.data) method = create_method(method) if method is None: raise Exception("Unknown test method: {0}".format(method)) results = method.create_results(num_slices, mapping.num_groups) pool = mp.Pool(self.num_cores) for slice_results_index, slice in enumerate(slices): self.log.info("[{0}]".format(matrix.slice_names[slice])) self.log.info(" Counting mutations ...") mut_counts, group_indices = self._count_mutations(valid_data, slice, mapping) self.log.info(" Calculating observed estimator ...") observed = np.empty(mapping.num_groups) observed[:] = np.nan for group_index in group_indices: #TODO parallelize group_rows = mapping.group_row_indices[group_index] observed[group_index] = method.observed(valid_data[slice, group_rows, :]) self.log.info(" Bootstrapping with {0} repetitions ...".format(self.num_samplings)) background = valid_data[slice].compressed().tolist() estimations = np.empty(self.num_samplings) for mut_count, group_indices in mut_counts: self.log.debug(" With {0} mutations -> {1} groups ...".format(mut_count, len(group_indices))) #for i in xrange(self.num_samplings): # sample = random.sample(background, mut_count) # sampling_estimations[i] = method.estimator(sample) iter = repeat((method, background, mut_count), self.num_samplings) res = pool.map_async(sampling, iter) while not res.ready(): try: time.sleep(0.1) except KeyboardInterrupt: pool.terminate() raise estimations[:] = np.array(res.get()) results[slice_results_index, group_indices] = method.compare(observed[group_indices], estimations) return results
def combine(self, results, method): method = create_method(method) return method.combine(results)