def getBestCoord(self, stars_coords): """ Parameters ---------- stars_coords : list Coordinates of inspected stars got from sub-filters Returns ------- list Coordinates with highest probability of membership to the searched group (one list of coordinates) """ check_depth(stars_coords, 2) if not len(stars_coords): warnings.warn(" There are no stars coordinates to inspect") return None best_coo = None best_prob = 0 for coords in stars_coords: prob = self.evaluate([coords])[0] if prob >= best_prob: best_coo = coords best_prob = prob # TODO: assert best_coo is not None return best_coo
def filter(self, stars_coords, treshold=None): """ Parameters ---------- stars_coords : list Coordinates of inspected stars treshold : float Treshold value for filtering (number from 0 to 1) Returns ------- List of True/False whether coordinates belong to the searched group of objects """ if not treshold: treshold = self.treshold check_depth(stars_coords, 2) return [self.evaluate([coo])[0] >= treshold for coo in stars_coords]
def plot2DProbabSpace(star_filter, plot_ranges, N, searched_coords=[], contaminatiom_coords=[]): """ Plot probability space Parameters ---------- star_filter : StarsFilter object Trained stars filter plot_ranges : iterable Ranges (max/min) for all axis N : int Number of points per axis searched_coords : list, iterable List of coordinates of searched objects contaminatiom_coords : list, iterable List of coordinates of contamination objects Returns ------- tuple x, y, Z """ if check_depth(plot_ranges, 1, ifnotraise=False): plot_ranges = [plot_ranges, plot_ranges] x = np.linspace(plot_ranges[0][0], plot_ranges[0][1], N) y = np.linspace(plot_ranges[1][0], plot_ranges[1][1], N) X, Y = np.meshgrid(x, y) z = np.array(star_filter.evaluateCoordinates(np.c_[X.ravel(), Y.ravel()])) Z = z.reshape(X.shape) plt.xlim(plot_ranges[0][0], plot_ranges[0][1]) plt.ylim(plot_ranges[1][0], plot_ranges[1][1]) plt.pcolor(X, Y, Z) plt.colorbar() if len(searched_coords) or len(contaminatiom_coords): s = np.array(searched_coords).T c = np.array(contaminatiom_coords).T plt.plot(s[0], s[1], "m*", label="Searched objects", markersize=17) plt.plot(c[0], c[1], "k*", label="Contamination objects", markersize=17) plt.legend() return x, y, Z
def plot1DProbabSpace(star_filter, plot_ranges, N, searched_coords=[], contaminatiom_coords=[]): """ Plot probability space Parameters ---------- star_filter : StarsFilter object Trained stars filter plot_ranges : iterable Ranges (max/min) for all axis N : int Number of points per axis searched_coords : list, iterable List of coordinates of searched objects contaminatiom_coords : list, iterable List of coordinates of contamination objects Returns ------- tuple x, y """ if check_depth(plot_ranges, 2, ifnotraise=False): plot_ranges = plot_ranges[0] x = np.linspace(plot_ranges[0], plot_ranges[1]) y = star_filter.evaluateCoordinates([[y] for y in x]) plt.plot(x, y, linewidth=3) if len(searched_coords) or len(contaminatiom_coords): s = [qq[0] for qq in searched_coords] c = [qq[0] for qq in contaminatiom_coords] s_weights = np.ones_like(s) / len(s) c_weights = np.ones_like(c) / len(c) plt.hist(s, bins=x, histtype='bar', weights=s_weights, label="Searched objects") plt.hist(c, bins=x, histtype='bar', weights=c_weights, label="Contamination objects") plt.legend() return x, np.array(y)
def evaluate(self, coords): """ Get probability of membership Parameters ---------- coords : list of lists List of prameter space coordinates Returns ------- list of floats List of probabilities """ # TODO: # if coords != np.ndarray: coords = np.array( coords ) # checkDepth(coords, 2) prediction = self.learner.predict_proba(coords)[:, 1] check_depth(prediction, 1) where_are_NaNs = np.isnan(prediction) prediction[where_are_NaNs] = 0 return prediction
def save_lists_query(self, query=[], fi_name="query_file.txt", PATH=".", DELIM=None, overwrite=False, header=None): ''' Save queries into the file which can be loaded for another query Parameters ---------- query : list List of lists which contains Returns ------- None ''' path = os.path.join(PATH, fi_name) if not DELIM: DELIM = self.DELIMITER if not check_depth(query, 2, ifnotraise=False): query = [query] if not header and query[0]: return False try: if overwrite: query_file = open(path, "w+") else: query_file = open(path, "a+") except IOError as err: raise InvalidFilesPath(err) if header and not query_file.readline(): query_file.write("#" + DELIM.join([str(it) for it in header])) for line in query: query_file.write(DELIM.join([str(it) for it in line]) + "\n") query_file.close()
def getStatistic(self, right_coords, wrong_coords, treshold=None): """ Parameters ---------- right_coords : list Parameter-space coordinates of searched objects wrong_coords : list Parameter-space coordinates of other objects treshold : float Treshold value for filtering (number from 0 to 1) Returns ------- statistic information : dict precision (float) True positive / (true positive + false positive) true_positive_rate (float) Proportion of positives that are correctly identified as such true_negative_rate :(float) Proportion of negatives that are correctly identified as such false_positive_rate (float) Proportion of positives that are incorrectly identified as negatives false_negative_rate (float) Proportion of negatives that are incorrectly identified as positives """ check_depth(right_coords, 2) check_depth(wrong_coords, 2) right_num = len(right_coords) wrong_num = len(wrong_coords) true_pos = sum([ 1 for guess in self.filter(right_coords, treshold) if guess == True ]) false_neg = right_num - true_pos true_neg = sum([ 1 for guess in self.filter(wrong_coords, treshold) if guess == False ]) false_pos = wrong_num - true_neg precision = round(computePrecision(true_pos, false_pos), 3) stat = (("precision", precision), ("true_positive_rate", round(true_pos / right_num, 3)), ("true_negative_rate", round(true_neg / wrong_num, 3)), ("false_positive_rate", round(1 - (true_pos / right_num), 3)), ("false_negative_rate", round(1 - (true_neg / wrong_num), 3))) return collections.OrderedDict(stat) """return {"precision" : precision,