def newFigure(self):
        if self.nextFigure == None:
            self.figure = Figure((self.width // 2) - 2, 0)
        else:
            self.figure = self.nextFigure

        self.nextFigure = Figure((self.width // 2) - 2, 0) # add a new figure to the game in the middle top
Esempio n. 2
0
    def Solve(self, problem):
        print problem.name
        problem_name = problem.name
        answer = -1
        scores = []

        #Load all figures and make them black OR white (no grey!)
        self.figure_a = Figure(problem.figures['A'].visualFilename)
        self.figure_b = Figure(problem.figures['B'].visualFilename)
        self.figure_c = Figure(problem.figures['C'].visualFilename)
        self.figure_d = Figure(problem.figures['D'].visualFilename)
        self.figure_e = Figure(problem.figures['E'].visualFilename)
        self.figure_f = Figure(problem.figures['F'].visualFilename)
        self.figure_g = Figure(problem.figures['G'].visualFilename)
        self.figure_h = Figure(problem.figures['H'].visualFilename)
        self.solutions = []
        problem_figure_keys = sorted(problem.figures.keys())
        num_solutions = 8
        for i in range(num_solutions):
            figure_sol = Figure(
                problem.figures[problem_figure_keys[i]].visualFilename)
            self.solutions.append(figure_sol)

        answer = self.get_solution()
        '''
        print problem.name
        print "Scores :", scores
        print "Correct answer: ", problem.correctAnswer
        print "Answer selected: ", answer, '\n'
        '''
        return answer
Esempio n. 3
0
    def newTurn(self):
        newFigure = Figure()
        self.dropFigure()
        used = self._getUsedPlaces()
        for i in range(4):
            if tuple(newFigure.position[i]) in used:
                break
        else:
            self.figure = newFigure
            count = 0
            while self.removeFullLines():
                self.linesRemoved += 1
                count += 1

            if count == 1:
                self.scoresMsg.emit(100)
            elif count == 2:
                self.scoresMsg.emit(300)
            elif count == 3:
                self.scoresMsg.emit(600)
            elif count == 4:
                self.scoresMsg.emit(1000)

            self.msg2statusbar.emit("Lines removed: " + str(self.linesRemoved))
            return
        self.gameOver()
Esempio n. 4
0
def generate_label_train_samples(c):
    """
    Generate label train sample (image patches).
    :param c:
    :return:
    """
    list_file = c.list_file
    target_folder = c.labels_folder

    print('generate_label_train_samples with list_file={0} and save to {1}'.format(list_file, target_folder))
    input("Press Enter to continue...")

    with open(list_file) as f:
        lines = f.readlines()

    # Remove whitespace characters, and then construct the figures
    figures = [Figure(line.strip()) for line in lines]

    # Clear the folder

    if os.path.exists(target_folder):
        shutil.rmtree(target_folder)
    os.mkdir(target_folder)

    for figure in figures:
        print("Processing {0}".format(figure.id))
        figure.load_gt_annotation(which_annotation='label')
        figure.load_image()
        if c.color_type == cv2.IMREAD_COLOR:
            figure.crop_label_patches(is_gray=False)
        else:
            figure.crop_label_patches(is_gray=True)
        figure.save_label_patches(target_folder)
Esempio n. 5
0
    def basic(self):
        f = Figure("Basic")
        self.forward_pixels = 210
        self.side_pixels = 80

        # Step 1 - Forward Slow
        f.add_leader_step(
            Step.Forward(Step.Foot.LEFT, self.forward_pixels,
                         self.seconds_per_beat * 2))
        f.add_follower_step(
            Step.Backward(Step.Foot.RIGHT, self.forward_pixels,
                          self.seconds_per_beat * 2))

        # Step 2 - Forward Slow
        f.add_leader_step(
            Step.Forward(Step.Foot.RIGHT, self.forward_pixels,
                         self.seconds_per_beat * 2))
        f.add_follower_step(
            Step.Backward(Step.Foot.LEFT, self.forward_pixels,
                          self.seconds_per_beat * 2))

        # Step 3 - Side Quick
        f.add_leader_step(
            Step.Side(Step.Foot.LEFT, self.side_pixels, self.seconds_per_beat))
        f.add_follower_step(
            Step.Side(Step.Foot.RIGHT, self.side_pixels,
                      self.seconds_per_beat))

        # Step 4 - Close the feet
        f.add_leader_step(Step.Close(Step.Foot.RIGHT, self.seconds_per_beat))
        f.add_follower_step(Step.Close(Step.Foot.LEFT, self.seconds_per_beat))

        return f
Esempio n. 6
0
 def new_figure(self):
     if not self.pass_Figures:
         self.pass_Figures = Figure.Figures
     pass_figure = randint(0, len(self.pass_Figures) - 1)
     self.Figure = Figure(3, 0, pass_figure)
     self.figures.append(self.Figure)
     if self.intersects():
         self.state = "gameover"
Esempio n. 7
0
 def __init__(self, parent):
     super().__init__(parent)
     self.board = [0 for i in range(200)]
     self.timer = QBasicTimer()
     self.setFocusPolicy(Qt.StrongFocus)
     self.figure = Figure()
     self.cube = 20
     self.linesRemoved = 0
     self.scores = 0
Esempio n. 8
0
    def Solve(self, problem):
        print problem.name
        problem_name = problem.name
        answer = -1
        scores = []

        if 'Problem B' in problem_name or 'Problem C' in problem_name:
            print problem_name, 'skipped\n'
            return answer

        start_time = time.time()

        # Load all figures and make them black OR white (no grey!)
        print 'Loading figures for %s...' % problem_name
        self.figure_a = Figure(problem.figures['A'].visualFilename)
        self.figure_b = Figure(problem.figures['B'].visualFilename)
        self.figure_c = Figure(problem.figures['C'].visualFilename)
        self.figure_d = Figure(problem.figures['D'].visualFilename)
        self.figure_e = Figure(problem.figures['E'].visualFilename)
        self.figure_f = Figure(problem.figures['F'].visualFilename)
        self.figure_g = Figure(problem.figures['G'].visualFilename)
        self.figure_h = Figure(problem.figures['H'].visualFilename)

        self.figures = [
            self.figure_a, self.figure_b, self.figure_c, self.figure_d,
            self.figure_e, self.figure_f, self.figure_g, self.figure_h
        ]

        print 'Identifying objects in each figure...'
        for figure in self.figures:
            figure.identify_objects()
            figure.find_centroids()

        print 'Loading all solutions and identifying the objects in each...'
        self.solutions = []
        problem_figure_keys = sorted(problem.figures.keys())
        num_solutions = 8
        for i in range(num_solutions):
            figure_sol = Figure(
                problem.figures[problem_figure_keys[i]].visualFilename)
            figure_sol.identify_objects()
            figure_sol.find_centroids()
            self.solutions.append(figure_sol)

        print 'Searching for a solution...'
        answer = self.get_solution()

        print 'Time to find solution: ', time.time() - start_time, ' seconds\n'
        return answer
Esempio n. 9
0
    def __init__(self, name):

        app_dir = os.path.dirname(__file__)
        sound_folder = 'sound'
        sound_line = 'success.wav'
        sound_end = 'end.wav'

        self.sounds = {
            'line': QSound(os.path.join(app_dir, sound_folder, sound_line)),
            'end': QSound(os.path.join(app_dir, sound_folder, sound_end))
        }

        self.board = []
        super().__init__()

        self.timer = QBasicTimer()
        self.speed = 390
        self.normal_speed = self.speed
        self.need_acceleration = False

        self.need_animation = False
        self.animation_sleep = False
        self.animation_counter = 0

        self.lines_to_remove = []

        self.need_new_figure = False
        self.cur_x = 0
        self.cur_y = 0
        self.score = 0
        self.is_started = False
        self.is_stopped = False

        self.figure_counter = -1
        self.level = 1

        self.setFocusPolicy(Qt.StrongFocus)
        self.clear()

        self.cur_block = Figure()
        self.next_block = Figure()

        self.result = []
        self.name = name
Esempio n. 10
0
    def figure3(self):

        # create figure object
        fig = Figure(self.figurefolder,"figure3", ncols = 2, nrows = 1, figsize = [12/2.54, 7/2.54],bottom = 0.18, top=0.8)

        simulationList = []
        for i in range(1,7):
            case = str(i)
            pretext = "ICE" + case
            if case in ["5", "6"]:
                posttext = "8h"
Esempio n. 11
0
    def figure_subtract(self, fig1, fig2):
        if fig1.image.size != fig2.image.size:
            raise Exception('Figures must be same size to SUBTRACT them')

        image = copy.deepcopy(fig1.image)

        for obj2 in fig2.objects:
            for xy in obj2.area:
                image.putpixel(xy, 255)

        return Figure(image)
Esempio n. 12
0
    def figure_xor(self, fig1, fig2):
        im1 = fig1.image
        im2 = fig2.image
        if im1.size != im2.size:
            raise Exception('Images must be same size to XOR them')
        size = im1.size
        image = Image.new('L', size, color=255)

        for x in range(size[0]):
            for y in range(size[1]):
                xy = x, y
                if im1.getpixel(xy) != im2.getpixel(xy):
                    image.putpixel(xy, 0)
        return Figure(image)
Esempio n. 13
0
    def __init__(self, problem):
        self.verbose = True
        self.name = problem.name
        self.problemType = problem.problemType

        self.figures = {}
        for fig in problem.figures.itervalues():
            self.figures[fig.name] = Figure(fig, self.name)

        self.hasVisual = problem.hasVisual
        self.hasVerbal = problem.hasVerbal
        self.global_objects = {}
        self.set_global_objects()
        self.global_objects_count = len(self.global_objects)
Esempio n. 14
0
    def __init__(self):
        # set title for the game window
        pygame.display.set_caption("Hangman")

        # clock that sets the amount of frames per second
        self.fps_clock = pygame.time.Clock()

        self.settings = Settings()

        # create display surface on which everithing is drawn
        self.DISPLAY_SURFACE = pygame.display.set_mode((self.settings.WINDOW_WIDTH, 
                                                    self.settings.WINDOW_HEIGHT))

        # create instance of figure class
        self.figure = Figure(self.DISPLAY_SURFACE)
        self.ta = TextArea(self.DISPLAY_SURFACE)
Esempio n. 15
0
    def figure_add(self, fig1, fig2):
        if fig1.image.size != fig2.image.size:
            raise Exception('Figures must be same size to SUBTRACT them')

        size = fig1.image.size
        image = Image.new('L', size, color=255)

        for obj1 in fig1.objects:
            for xy in obj1.area:
                image.putpixel(xy, 0)

        for obj2 in fig2.objects:
            for xy in obj2.area:
                image.putpixel(xy, 0)

        return Figure(image)
Esempio n. 16
0
def generate_statistics(c):
    """
    Generate label statistics for deciding some parameters of the algorithm
    :param c:
    :return:
    """
    list_file = c.list_file

    print('generate_statistics with list_file={0}'.format(list_file))
    input("Press Enter to continue...")

    with open(list_file) as f:
        lines = f.readlines()

    # Remove whitespace characters, and then construct the figures
    figures = [Figure(line.strip()) for line in lines]

    print_progress_bar(0, len(figures), prefix='Progress:', suffix='Complete', length=50)
    for i, figure in enumerate(figures):
        figure.load_gt_annotation(which_annotation='label')
        figure.load_image()
        print_progress_bar(i + 1, len(figures), prefix='Progress:', suffix='Complete', length=50)

    # Figure Image Statistics
    image_width, image_height = [], []
    for figure in figures:
        height, width = figure.image_orig.shape[:2]
        image_width.append(width)
        image_height.append(height)

    print('\nimage width statistics:')
    print(pd.Series(image_width).describe())
    print('\nimage height statistics:')
    print(pd.Series(image_height).describe())

    # Label Statistics
    label_width, label_height = [], []
    for figure in figures:
        for panel in figure.panels:
            width, height = panel.label_rect[2:]
            label_width.append(width)
            label_height.append(height)

    print('\nLabel width statistics:')
    print(pd.Series(label_width).describe())
    print('\nLabel height statistics:')
    print(pd.Series(label_height).describe())
Esempio n. 17
0
def generate_nonlabel_train_samples(c):

    list_file = c.list_file
    target_folder = c.nonlabels_folder

    print('generate_nonlabel_train_samples with list_file={0} and target_folder={1}'.format(list_file, target_folder))
    input("Press Enter to continue...")

    with open(list_file) as f:
        lines = f.readlines()

    # Remove whitespace characters, and then construct the figures
    figures = [Figure(line.strip()) for line in lines]

    # Clear the folder
    if not os.path.exists(target_folder):
        os.mkdir(target_folder)

    for figure in figures:
        print("Processing {0}".format(figure.id))
        figure.load_image()
        image_height, image_width = figure.image_orig.shape[:2]
        for i in range(50):
            x, y, s = random.randint(-5, image_width-5), random.randint(-5, image_height-5), round(random.gauss(20, 7))
            if (s < 5) or (s > 80):
                continue
            if (x + s - image_width > s / 2) or (0-x > s/2):
                continue
            if (y + s - image_height > s / 2) or (0-y > s/2):
                continue

            rect = (x, y, s, s)
            patch_file = figure.id + "_".join(str(x) for x in rect) + ".png"

            x += figure.PADDING
            y += figure.PADDING
            if c.color_type == cv2.IMREAD_COLOR:
                patch = figure.image[y:y+s, x:x+s]
            else:
                patch = figure.image_gray[y:y+s, x:x+s]

            # if patch.shape

            patch_file = os.path.join(target_folder, patch_file)
            cv2.imwrite(patch_file, patch)
Esempio n. 18
0
def main(_):
    splitter = PanelSplitterObjDet()

    with open(FLAGS.eval_path) as f:
        lines = f.readlines()

    for idx, filepath in enumerate(lines):
        print(str(idx) + ': ' + filepath)
        filepath = filepath.strip()
        figure = Figure(filepath, padding=0)
        figure.load_image()

        st = time.time()
        panel_split(splitter, figure)
        print('Elapsed time = {}'.format(time.time() - st))

        #  save results
        figure.save_annotation(FLAGS.result_folder, 'panel')
Esempio n. 19
0
    def alt_basic(self):

        f = Figure("Alternative Basic")
        pixels_per_front_step = 60
        pixels_per_side_step = 80

        # Rumba - Step 1 Slow : left foot side
        f.add_leader_step(
            Step.Side(Step.Foot.LEFT, pixels_per_side_step,
                      self.seconds_per_beat * 2))
        f.add_follower_step(
            Step.Follow(Step.Foot.RIGHT, self.seconds_per_beat * 2))

        # Rumba - Step 2 Quick: rock back on right
        f.add_leader_step(
            Step.Backward(Step.Foot.RIGHT, pixels_per_front_step,
                          self.seconds_per_beat))
        f.add_follower_step(Step.Follow(Step.Foot.LEFT, self.seconds_per_beat))

        # Rumba - Step 3 Quick : replace weight
        f.add_leader_step(Step.Step(Step.Foot.LEFT, self.seconds_per_beat))
        f.add_follower_step(Step.Follow(Step.Foot.RIGHT,
                                        self.seconds_per_beat))

        # Rumba - Step 4 Slow : right foot side
        f.add_leader_step(
            Step.Side(Step.Foot.RIGHT, pixels_per_side_step,
                      self.seconds_per_beat * 2))
        f.add_follower_step(
            Step.Follow(Step.Foot.LEFT, self.seconds_per_beat * 2))

        # Rumba - Step 5 Quick: rock forward on left
        f.add_leader_step(
            Step.Forward(Step.Foot.LEFT, pixels_per_front_step,
                         self.seconds_per_beat))
        f.add_follower_step(Step.Follow(Step.Foot.RIGHT,
                                        self.seconds_per_beat))

        # Rumba - Step 6 Quick : replace weight
        f.add_leader_step(Step.Step(Step.Foot.RIGHT, self.seconds_per_beat))
        f.add_follower_step(Step.Follow(Step.Foot.LEFT, self.seconds_per_beat))

        return f
Esempio n. 20
0
def read_samples(path, auto_folder, model_classifier, model_svm):

    with open(path) as f:
        lines = f.readlines()
    # Remove whitespace characters, and then construct the figures
    figures = [Figure(line.strip()) for line in lines]

    X = []
    Y = []
    for i, figure in enumerate(figures):
        figure.load_image()

        # load ground-truth annotation
        gt_rois, gt_labels = load_ground_truth_annotation(figure)

        # load auto annotation
        auto_rois = load_auto_annotation(figure, auto_folder)

        # sort auto annotation with respect to distances to left-up corner (0, 0)
        distances = [roi[0] + roi[1] for roi in auto_rois]
        indexes = np.argsort(distances)
        auto_rois = auto_rois[indexes]

        # match auto to gt to assign y
        y = np.full([auto_rois.shape[0]],
                    len(LABEL_CLASS_MAPPING))  # initialize as non-label
        for gt_i, gt_roi in enumerate(gt_rois):
            ious = [iou_rect(auto_roi, gt_roi) for auto_roi in auto_rois]
            max_index = np.argmax(ious)
            if ious[max_index] > 0.25:
                y[max_index] = LABEL_CLASS_MAPPING[map_label(gt_labels[gt_i])]
        Y.append(y)

        # extract features
        x = feature_extraction(figure, auto_rois)

        # if len(x) > 0:
        #     p_label, p_acc, p_val = svmutil.svm_predict(y, x.tolist(), model_svm, '-b 1')
        #     x = np.array(p_val)

        X.append(x)

    return X, Y
Esempio n. 21
0
def plot4Sets(caseCollection,
              simulationCollection,
              annotationCollection,
              simulationDataFrames,
              figurefolder,
              figurename,
              ncVariable,
              designVariable,
              conversionNC=1.0,
              conversionDesign=1.0,
              xmax=1000,
              ymax=1000,
              xAxisLabel=None,
              xAxisUnit=None,
              yAxisLabel=None,
              yAxisUnit=None,
              keisseja=10000,
              yPositionCorrection=100,
              outlierParameter=0.2):

    relativeChangeDict = Data.emptyDictionaryWithKeys(caseCollection)
    print(" ")
    print(figurename)
    # create figure object
    fig = Figure(figurefolder,
                 figurename,
                 ncols=2,
                 nrows=2,
                 sharex=True,
                 sharey=True)
    # plot timeseries with unit conversion
    maks = 0
    mini = 0
    for ind, case in enumerate(caseCollection):
        for emul in list(simulationCollection[case])[:keisseja]:
            dataset = simulationCollection[case][emul].getTSDataset()
            muuttuja = dataset[ncVariable]

            alku = simulationDataFrames[case].loc[emul][
                designVariable] * conversionDesign
            loppu = muuttuja.sel(
                time=slice(2.5, 3.5)).mean().values * conversionNC
            relChangeParam = loppu / alku

            relativeChangeDict[case][emul] = relChangeParam

            if relChangeParam > 1 + outlierParameter:
                color = Colorful.getDistinctColorList("red")
                zorderParam = 10
            elif relChangeParam < 1 - outlierParameter:
                color = Colorful.getDistinctColorList("blue")
                zorderParam = 9
            else:
                color = "white"
                zorderParam = 6

            maks = max(relChangeParam, maks)
            mini = min(relChangeParam, mini)

            fig.getAxes(True)[ind].plot(alku,
                                        loppu,
                                        marker="o",
                                        markerfacecolor=color,
                                        markeredgecolor="black",
                                        markeredgewidth=0.2,
                                        markersize=6,
                                        alpha=0.5,
                                        zorder=zorderParam)
Esempio n. 22
0
                                                    yPositionCorrection=300)
        simulationDataFrames = mergeDataFrameWithParam(simulationDataFrames,
                                                       cloudTopParameters,
                                                       "zcRel")
        cloudTopFig.save()

    if cloudTopOutliersFlag:
        cloutTopOutliers = {}
        for ind, case in enumerate(list(simulationDataFrames)):
            cloudTopOutliers[case] = simulationDataFrames[case].where(
                simulationDataFrames[case]["zcRel"] <
                simulationDataFrames[case]["zcRel"])

        fig2 = Figure(figurefolder,
                      "cloudtopOutliers",
                      ncols=2,
                      nrows=2,
                      sharex=True,
                      sharey=True)
        # plot timeseries with unit conversion
        cloudTopOutliersColors = Colorful.getIndyColorList(
            len(cloudTopOutliers))
        for ind, case in enumerate(caseCollection):
            for emulInd, emul in enumerate(cloudTopOutliers):
                try:
                    simulation = simulationCollection[case][emul]
                except KeyError:
                    continue
                dataset = simulation.getTSDataset()
                muuttuja = dataset["zc"][1:] / (
                    simulationDataFrames[case].loc[emul]["pblh_m"])
Esempio n. 23
0
    def getIce4Simulations(self):
        return self.ice4simulations

    def getAllSimulations(self):
        return self.allSimulations

    def getUCLALESSALSASimulations(self):
        return self.uclalesSimulations


figObject = ManuscriptFigures(
    "/home/aholaj/Nextcloud/figures_updated/manuscriptSimulationData_Rad.csv",
    os.environ["SIMULATIONFIGUREFOLDER"])

fig = Figure(figObject.figurefolder, "figure2RAD", ncols=2, nrows=3)

simulationList = FigureRadSimulationList()

for k in simulationList.getAllSimulations():
    try:
        figObject.simulationCollection[k].getTSDataset()
        figObject.simulationCollection[k].setTimeCoordToHours()
    except FileNotFoundError:
        if "ovchinnikov" in str(
                figObject.simulationCollection[k].getFolder()).lower():
            print(
                "Ovchinnikov data is not available. Continue with existingsimulations"
            )
            continue
        else:
    def figureDesignVariables(self):
        nrows = 4
        ncols = ceil(len(self.designVariablePool) / nrows)

        self.figures["figureDesignVariables"] = Figure(
            self.figureFolder,
            "figureDesignVariables",
            figsize=[self.figureWidth, 7],
            ncols=ncols,
            nrows=nrows,
            bottom=0.04,
            hspace=0.17,
            wspace=0.07,
            top=0.98,
            left=0.02,
            right=0.98)
        fig = self.figures["figureDesignVariables"]
        rightUp = [0.57, 0.70]
        leftUp = [0.25, 0.73]
        leftDown = [0.1, 0]
        rightDown = [0.45, 0.05]
        middleDown = [0.33, 0.05]
        default = [0.5, 0.5]
        specsPositions = [[0.3, 0.05], [0.2, 0.05], [0.3, 0.5], [0.3, 0.5],
                          [0.3, 0.4], [0.3, 0.5], [0.05, 0.50], [0.05, 0.50],
                          [0.05, 0.50], [0.3, 0.05], middleDown]
        meanStr = "\mu"
        stdStr = "\sigma"

        logVariables = ["ks", "as", "cs", "rdry_AS_eff"]
        for ind, variable in enumerate(self.designVariablePool):
            ax = fig.getAxes(ind)

            minimi = numpy.nan
            maximi = numpy.nan
            if variable == "pblh":
                variableSourceName = "pbl"
            else:
                variableSourceName = variable

            if hasattr(self, "filteredSourceData") and (self.filteredSourceData
                                                        is not None):
                sourceDataVariable = self.filteredSourceData[
                    variableSourceName]
                sourceDataVariable = Data.dropInfNanFromDataFrame(
                    sourceDataVariable)
                if variable in logVariables:
                    sourceDataVariable = numpy.log10(sourceDataVariable)
                sourceDataVariable = Data.dropInfNanFromDataFrame(
                    sourceDataVariable)

                if variable == "cos_mu":
                    sourceDataVariable = sourceDataVariable[
                        sourceDataVariable > Data.getEpsilon()]
                sourceDataVariable.plot.density(
                    ax=ax, color=Colorful.getDistinctColorList("grey"))

                variableSpecs = f"""{PlotTweak.getLatexLabel(f'min={sourceDataVariable.min():.2f}')}
{PlotTweak.getLatexLabel(f'{meanStr}={sourceDataVariable.mean():.2f}')}
{PlotTweak.getLatexLabel(f'{stdStr}={sourceDataVariable.std():.2f}')}
{PlotTweak.getLatexLabel(f'max={sourceDataVariable.max():.2f}')}"""

                ax.annotate(variableSpecs,
                            xy=specsPositions[ind],
                            size=8,
                            bbox=dict(pad=0.6, fc="w", ec="w", alpha=0.9),
                            xycoords="axes fraction")

            isAnnotated = True
            for tt, trainingSet in enumerate(self.trainingSetList):

                if variable in self.completeDataFrame[trainingSet].keys():

                    if self.completeDataFrame[trainingSet] is None:
                        continue

                    trainingSetVariable = self.completeDataFrame[trainingSet][
                        variable]
                    if variable in logVariables:
                        loga = PlotTweak.getLatexLabel("log_{10} ")
                        trainingSetVariable = numpy.log10(trainingSetVariable)
                        trainingSetVariable = Data.dropInfNanFromDataFrame(
                            trainingSetVariable)
                    else:
                        loga = ""

                    minimi = numpy.nanmin([minimi, trainingSetVariable.min()])
                    maximi = numpy.nanmax([maximi, trainingSetVariable.max()])
                    trainingSetVariable.plot.density(
                        ax=ax, color=self.trainingSetColors[trainingSet])

                    variableSpecs = f"""{PlotTweak.getLatexLabel(f'min={trainingSetVariable.min():.2f}')}
{PlotTweak.getLatexLabel(f'{meanStr}={trainingSetVariable.mean():.2f}')}
{PlotTweak.getLatexLabel(f'{stdStr}={trainingSetVariable.std():.2f}')}
{PlotTweak.getLatexLabel(f'max={trainingSetVariable.max():.2f}')}"""

                    if not hasattr(self, "filteredSourceData") and isAnnotated:
                        ax.annotate(variableSpecs,
                                    xy=specsPositions[ind],
                                    size=8,
                                    bbox=dict(pad=0.6,
                                              fc="w",
                                              ec="w",
                                              alpha=0.9),
                                    xycoords="axes fraction")
                        isAnnotated = False

            annotation = f"({Data.getNthLetter(ind)}) {loga}{PlotTweak.getMathLabel(variable)}"
            PlotTweak.setAnnotation(ax,
                                    annotation,
                                    xPosition=0.05,
                                    yPosition=0.9,
                                    xycoords="axes fraction")
            ax.set_ylabel("")
            ax.set_xlim([minimi, maximi])
            PlotTweak.hideYTickLabels(ax)

        ax = fig.getAxes(11)
        ax.axis("off")

        legendLabelColors = PlotTweak.getPatches(self.allSetColors)

        artist = ax.legend(handles=legendLabelColors,
                           loc=(0.0, 0.00),
                           frameon=True,
                           framealpha=1.0,
                           ncol=1)

        ax.add_artist(artist)
Esempio n. 25
0
    def get_solution(self):
        answer = -1

        # *** REAL CODE ***
        # Check for holistic symmetry
        vertical_symmetry_measures = []
        horizontal_symmetry_measures = []
        for solution in self.solutions:
            self.figure_sol = solution
            holistic_image = self.create_merged_image()
            vertical_symmetry_measures.append(
                self.get_vertical_symmetry_measure(holistic_image))
            horizontal_symmetry_measures.append(
                self.get_horizontal_symmetry_measure(holistic_image))
        # Check vertical
        max_measure = max(vertical_symmetry_measures)
        if max_measure > self.threshold:
            return vertical_symmetry_measures.index(max_measure) + 1
        # Check horizontal
        max_measure = max(horizontal_symmetry_measures)
        if max_measure > self.threshold:
            return horizontal_symmetry_measures.index(max_measure) + 1

        # Horizontal transforms alone have been sufficient for the practice problems encountered
        transform = self.get_transform()
        print transform[0]
        if transform[0] == 'add':
            fig_sum = self.figure_add(self.figure_g, self.figure_h)
            answer = self.find_most_similar_solution(fig_sum)

        elif transform[0] == 'subtract':
            fig_diff = self.figure_subtract(self.figure_g, self.figure_h)
            answer = self.find_most_similar_solution(fig_diff)

        elif transform[0] == 'xor':
            fig_xor = self.figure_xor(self.figure_g, self.figure_h)
            answer = self.find_most_similar_solution(fig_xor)

        elif transform[0] == 'and':
            fig_and = self.figure_and(self.figure_g, self.figure_h)
            answer = self.find_most_similar_solution(fig_and)

        elif transform[0] == 'resize':
            # if at this point, only 2 objects in figure
            # Get size of object in question and get scale factor from transform data
            obj = self.figure_h.objects[0]
            width_obj, height_obj = obj.size()
            width_trans, height_trans = transform[1]
            scale = (1 + width_trans / float(width_obj),
                     1 + height_trans / float(height_obj))

            im = self.figure_h.image
            width, height = im.size
            im_resized = im.resize(
                (int(width * scale[0]), int(height * scale[1])),
                Image.BILINEAR)
            width_new, height_new = im_resized.size
            width_diff = width_new - width
            height_diff = height_new - height
            box = (width_diff / 2, height_diff / 2, width_new - width_diff / 2,
                   height_new - height_diff / 2)
            fig = Figure(im_resized.crop(box))
            answer = self.find_most_similar_solution(fig)

        elif transform[0] == 'add and translate':
            translate_distance = transform[1]
            obj1 = self.figure_g.objects[0]
            size = self.figure_g.image.size

            init_l_val = 255
            obj1_new = Object((0, 0), 0)
            obj2_new = Object((0, 0), 0)
            obj1_new.remove_pixel((0, 0))
            obj2_new.remove_pixel((0, 0))

            # Slide first two objects away from each other
            for coord in obj1.area:
                obj1_new.add_pixel(
                    (coord[0] + translate_distance * 2, coord[1]))
                obj2_new.add_pixel(
                    (coord[0] - translate_distance * 2, coord[1]))

            # Make new image with translated objects
            image_new = Image.new('L', size, color=init_l_val)
            for xy in obj1_new.area:
                image_new.putpixel(xy, 0)
            for xy in obj2_new.area:
                image_new.putpixel(xy, 0)
            for xy in obj1.area:
                image_new.putpixel(xy, 0)

            fig = Figure(image_new)
            answer = self.find_most_similar_solution(fig)

        elif transform[0] == 'duplicate and separate':
            translate_distance = transform[1]
            objs_orig = self.figure_g.objects
            size = self.figure_g.image.size

            # Duplicate objects and separate them
            objs_left = []
            objs_right = []
            for obj in objs_orig:
                objs_left.append(
                    self.translate_object(
                        obj, (translate_distance[0], translate_distance[1])))
                objs_right.append(
                    self.translate_object(
                        obj, (-translate_distance[0], translate_distance[1])))

            # Make new image with translated objects
            image_left_right = self.image_from_objects(size,
                                                       objs_left + objs_right)

            fig = Figure(image_left_right)
            answer = self.find_most_similar_solution(fig)

        elif transform[0] == 'horizontal pass through':
            size = self.figure_g.image.size
            im_centroid = (size[0] / 2, size[1] / 2)
            max_x = 0
            for obj in self.figure_g.objects:
                if obj.centroid[0] < im_centroid[0]:
                    if obj.max_x > max_x:
                        max_x = obj.max_x
            max_distance = int(size[0] / 2)

            for i in xrange(int(max_distance / 4 - 1), max_distance, 2):
                objects_new = []
                for obj in self.figure_g.objects:
                    # On the left side
                    if obj.centroid[0] < im_centroid[0]:
                        obj_new = self.translate_object(obj, (i, 0))
                    # On the right side
                    else:
                        obj_new = self.translate_object(obj, (-i, 0))
                    objects_new.append(obj_new)
                image_new = self.image_from_objects(size, objects_new)
                for solution in self.solutions:
                    if self.is_equal(image_new, solution.image):
                        answer = self.solutions.index(solution) + 1

        elif transform[0] == 'rotate figures in row':
            figure_to_match = transform[1]
            answer = self.find_most_similar_solution(figure_to_match)

        elif transform[0] == "shape count combination":
            req_shape, req_count = transform[1]

            for solution in self.solutions:
                # Check that solution meets shape/count requirements
                if len(solution.objects
                       ) == req_count and solution.objects[0] == req_shape:
                    answer = self.solutions.index(solution) + 1

        return answer
Esempio n. 26
0
def load_samples(path):
    with open(path) as f:
        lines = f.readlines()
    # Remove whitespace characters, and then construct the figures
    figures = [Figure(line.strip()) for line in lines]
    return figures
Esempio n. 27
0
    def get_solution(self):
        answer = -1

        # *** REAL CODE ***
        # Check for holistic symmetry
        vertical_symmetry_measures = []
        horizontal_symmetry_measures = []
        for solution in self.solutions:
            self.figure_sol = solution
            holistic_image = self.create_merged_image()
            vertical_symmetry_measures.append(
                self.get_vertical_symmetry_measure(holistic_image))
            horizontal_symmetry_measures.append(
                self.get_horizontal_symmetry_measure(holistic_image))
        # Check vertical
        max_measure = max(vertical_symmetry_measures)
        if max_measure > self.threshold:
            return vertical_symmetry_measures.index(max_measure) + 1
        # Check horizontal
        max_measure = max(horizontal_symmetry_measures)
        if max_measure > self.threshold:
            return horizontal_symmetry_measures.index(max_measure) + 1

        # Horizontal transforms alone have been sufficient for the practice problems encountered
        transform = self.get_transform(self.figure_a, self.figure_b,
                                       self.figure_c)

        # These values used later on
        self.figure_g.identify_objects()
        self.figure_g.find_centroids()
        self.figure_h.identify_objects()
        self.figure_h.find_centroids()

        if transform[0] == 'resize':
            # if at this point, only 2 objects in figure
            # Get size of object in question and get scale factor from transform data
            obj = self.figure_h.objects[1]
            width_obj, height_obj = obj.size()
            width_trans, height_trans = transform[1]
            scale = (1 + width_trans / float(width_obj),
                     1 + height_trans / float(height_obj))

            im = self.figure_h.image
            width, height = im.size
            im_resized = im.resize(
                (int(width * scale[0]), int(height * scale[1])),
                Image.BILINEAR)
            width_new, height_new = im_resized.size
            width_diff = width_new - width
            height_diff = height_new - height
            box = (width_diff / 2, height_diff / 2, width_new - width_diff / 2,
                   height_new - height_diff / 2)
            fig = Figure(im_resized.crop(box))
            answer = self.find_most_similar_solution(fig)

        elif transform[0] == 'add and translate':
            translate_distance = transform[1]
            obj1 = self.figure_g.objects[1]
            size = self.figure_g.image.size

            init_l_val = 255
            obj1_new = Object((0, 0), 0)
            obj2_new = Object((0, 0), 0)
            obj1_new.remove_pixel((0, 0))
            obj2_new.remove_pixel((0, 0))

            # Slide first two objects away from each other
            for coord in obj1.area:
                obj1_new.add_pixel(
                    (coord[0] + translate_distance * 2, coord[1]))
                obj2_new.add_pixel(
                    (coord[0] - translate_distance * 2, coord[1]))

            # Make new image with translated objects
            image_new = Image.new('L', size, color=init_l_val)
            for xy in obj1_new.area:
                image_new.putpixel(xy, 0)
            for xy in obj2_new.area:
                image_new.putpixel(xy, 0)
            for xy in obj1.area:
                image_new.putpixel(xy, 0)

            fig = Figure(image_new)
            answer = self.find_most_similar_solution(fig)

        elif transform[0] == 'horizontal pass through':
            for solution in self.solutions:
                solution.identify_objects()
                num_dark_obj = 0
                for obj in solution.objects:
                    if obj.l_val < 128:
                        num_dark_obj += 1
                if num_dark_obj < 2:
                    continue

                size = solution.image.size
                im_centroid = (size[0] / 2, size[1] / 2)
                max_distance = size[0] / 2
                for i in xrange(2, max_distance, 2):
                    objects_new = []
                    for obj in solution.objects:
                        if obj.l_val < 128:
                            # On the left side
                            if obj.centroid[0] < im_centroid[0]:
                                obj_new = self.translate_object(obj, (i, 0))
                            # On the right side
                            else:
                                obj_new = self.translate_object(obj, (-i, 0))

                            objects_new.append(obj_new)
                    image_new = self.image_from_objects(size, objects_new)
                    if self.is_equal(image_new, solution.image):
                        answer = self.solutions.index(solution) + 1
                        return answer

        return answer
Esempio n. 28
0
def test_lstm():
    parser = OptionParser()

    parser.add_option(
        "--eval_path",
        dest="eval_path",
        help="Path to eval data.",
        default='/Users/jie/projects/PanelSeg/ExpPython/eval.txt')
    parser.add_option(
        "--eval_auto_folder",
        dest="eval_auto_folder",
        default='/Users/jie/projects/PanelSeg/ExpPython/eval/rpn_hog/rpn')
    parser.add_option(
        "--classify_model_path",
        dest="classify_model_path",
        default=
        '/Users/jie/projects/PanelSeg/ExpPython/models/label50+bg_cnn_3_layer_color-0.9910.h5'
    )
    # parser.add_option("--svm_model_path", dest="svm_model_path",
    #                   default='/Users/jie/projects/PanelSeg/Exp/LabelClassifySvmTrain/SVMModel-51classes-with-neg/svm_model_rbf_8.0_0.125')
    parser.add_option(
        "--lstm_model_path",
        dest="lstm_model_path",
        default=
        '/Users/jie/projects/PanelSeg/ExpPython/models/lstm_model_train_0.25eval_epoch_9.h5'
    )
    parser.add_option(
        "--result_folder",
        dest="result_folder",
        default='/Users/jie/projects/PanelSeg/ExpPython/eval/rpn_hog_lstm/eval'
    )

    (options, args) = parser.parse_args()

    # model_classifier = load_model(options.classify_model_path)
    # model_classifier.summary()

    # model_svm = svmutil.svm_load_model(options.svm_model_path)
    model_svm = None

    model_lstm = load_model(options.lstm_model_path)
    model_lstm.summary()

    with open(options.eval_path) as f:
        lines = f.readlines()

    for idx, filepath in enumerate(lines):
        print(str(idx) + ': ' + filepath)
        # if '1757-1626-0002-0000008402-001' not in filepath:
        #     continue
        # if idx < 37:
        #     continue
        filepath = filepath.strip()
        figure = Figure(filepath)
        figure.load_image()
        st = time.time()

        # load detection results by RPN
        rois = load_auto_annotation(figure, options.eval_auto_folder)

        # sort auto annotation with respect to distances to left-up corner (0, 0)
        distances = [roi[0] + roi[1] for roi in rois]
        indexes = np.argsort(distances)
        rois = rois[indexes]

        x = feature_extraction(figure, rois)

        if rois.size == 0:
            figure.fg_rois, figure.fg_scores, figure.fg_labels = None, None, None
        else:
            # x = x.tolist()
            # y = np.zeros(len(x)).tolist()
            # p_label, p_acc, p_val = svmutil.svm_predict(y, x, model_svm, '-b 1')
            # x = np.array(p_val)

            _x = np.expand_dims(x, axis=0)
            y_hat = model_lstm.predict(_x)

            # figure.fg_rois, figure.fg_scores, figure.fg_labels = max_y_hat(rois, y_hat[0])
            figure.fg_rois, figure.fg_scores, figure.fg_labels = beam_search_with_neg(
                rois, y_hat[0], 5)

        print('Elapsed time = {}'.format(time.time() - st))

        # Save detection results
        figure.save_annotation(options.result_folder)
Esempio n. 29
0
    def create_figure(self, x1, y1, x2, y2, color):

        figure = Figure(x1, y1, x2, y2, color, color, "figure", self.canvas)
        self.figures.append(figure)
    def __figureUpdraft_vs_CloudRadiativeWarming(self, updraftVariableName,
                                                 names: dict, dataColor):

        self.figures[names["fig"]] = Figure(self.figureFolder,
                                            names["fig"],
                                            figsize=[self.figureWidth, 4],
                                            ncols=2,
                                            nrows=2,
                                            bottom=0.11,
                                            hspace=0.08,
                                            wspace=0.12,
                                            top=0.94)
        fig = self.figures[names["fig"]]

        xstart = -140
        xend = 50

        ystart = 0.0
        yend = 1.0
        yticks = numpy.arange(0, yend + .01, 0.1)
        tickLabels = [f"{t:.1f}" for t in yticks]

        yShowList = Data.cycleBoolean(len(yticks))

        color_obs = Colorful.getDistinctColorList("grey")
        condition = {}
        for ind, trainingSet in enumerate(self.trainingSetList):
            ax = fig.getAxes(ind)

            dataframe = self.completeDataFrameFiltered[trainingSet]

            if dataframe is None:
                continue

            condition["notMatchObservation"] = ~ ( (dataframe[updraftVariableName] > dataframe["drflx"]*self.observationParameters["slope"]+ self.observationParameters["intercept"]-self.observationParameters["error"]) &\
                                                   (dataframe[updraftVariableName] < dataframe["drflx"]*self.observationParameters["slope"]+ self.observationParameters["intercept"]+self.observationParameters["error"]))

            dataFrameInside = dataframe[~condition["notMatchObservation"]]
            percentageInside = len(dataFrameInside) / len(dataframe) * 100.

            radiativeWarming = dataframe["drflx"].values
            updraft = dataframe[updraftVariableName].values

            poly1d_Observation = numpy.poly1d(
                numpy.asarray([
                    self.observationParameters["slope"],
                    self.observationParameters["intercept"]
                ]))  #?0.44 ×CTRC+
            ax.plot(radiativeWarming,
                    poly1d_Observation(radiativeWarming),
                    color=color_obs)
            ax.fill_between(sorted(radiativeWarming),
                            poly1d_Observation(sorted(radiativeWarming)) -
                            self.observationParameters["error"] *
                            numpy.ones(numpy.shape(radiativeWarming)),
                            poly1d_Observation(sorted(radiativeWarming)) +
                            self.observationParameters["error"] *
                            numpy.ones(numpy.shape(radiativeWarming)),
                            alpha=0.2)

            slope, intercept, r_value, p_value, std_err = stats.linregress(
                radiativeWarming, updraft)
            coef = [slope, intercept]
            rSquared = numpy.power(r_value, 2)

            fitColor = "k"
            dataframe.plot.scatter(ax=ax,
                                   x="drflx",
                                   y=updraftVariableName,
                                   alpha=0.3,
                                   color=dataColor)

            poly1d_fn = numpy.poly1d(coef)

            linearFit = []
            for radWarmingValue in list(
                    self.completeDataFrame[trainingSet]["drflx"]):
                linearFit.append(poly1d_fn(radWarmingValue))

            self.completeDataFrame[trainingSet][
                self.linearFitVariable] = linearFit
            ax.plot(radiativeWarming,
                    poly1d_fn(radiativeWarming),
                    color=fitColor)

            ax.set_xlim([xstart, xend])
            ax.set_ylim([ystart, yend])

            PlotTweak.setAnnotation(ax,
                                    self.annotationCollection[trainingSet],
                                    xPosition=PlotTweak.getXPosition(ax, 0.02),
                                    yPosition=PlotTweak.getYPosition(ax, 0.93))

            PlotTweak.setXaxisLabel(ax, "")
            PlotTweak.setYaxisLabel(ax, "")

            xticks = PlotTweak.setXticks(ax,
                                         start=xstart,
                                         end=xend,
                                         interval=10,
                                         integer=True)

            xShownLabelsBoolean = PlotTweak.setXLabels(ax,
                                                       xticks,
                                                       start=xstart,
                                                       end=xend,
                                                       interval=40)
            xShownLabelsBoolean = Data.cycleBoolean(len(xShownLabelsBoolean))
            PlotTweak.setXTickSizes(ax, xShownLabelsBoolean)

            if ind == 0:
                collectionOfLabelsColors = {
                    names["legend"]: dataColor,
                    "Fit": "k",
                    "Observations": color_obs
                }
                legendLabelColors = PlotTweak.getPatches(
                    collectionOfLabelsColors)

                artist = ax.legend(handles=legendLabelColors,
                                   loc=(0.17, 1.02),
                                   frameon=True,
                                   framealpha=1.0,
                                   ncol=3)

                ax.add_artist(artist)

            ax.text(-25,
                    0.67,
                    f"""{PlotTweak.getLatexLabel('y=a + b * x')}
{PlotTweak.getLatexLabel(f'a={intercept:.4f}')}
{PlotTweak.getLatexLabel(f'b={slope:.6f}')}
{PlotTweak.getLatexLabel(f'R^2={rSquared:.2f}')}
{PlotTweak.getLatexLabel('p_{in}=' + f'{percentageInside:.1f}')}%""",
                    fontsize=6)

            ax.set_yticks(yticks)
            ax.set_yticklabels(tickLabels)
            PlotTweak.setYTickSizes(ax, yShowList)
            PlotTweak.hideLabels(ax.yaxis, yShowList)

            if ind in [1, 3]:
                PlotTweak.hideYTickLabels(ax)

            if ind in [0, 1]:
                PlotTweak.hideXTickLabels(ax)
            if ind == 0:
                ax.text(PlotTweak.getXPosition(ax, -0.27),
                        PlotTweak.getYPosition(ax, -0.5),
                        PlotTweak.getUnitLabel(names["legend"] + "\ w_{pos}",
                                               "m\ s^{-1}"),
                        size=8,
                        rotation=90)
            if ind == 2:
                ax.text(0.3,
                        -0.25,
                        PlotTweak.getUnitLabel("Cloud\ rad.\ warming",
                                               "W\ m^{-2}"),
                        size=8)