Beispiel #1
0
def _add_face(vert_set_index: int, vert_dict: dict, verts: torch.Tensor,
              faces: torch.Tensor, location: list, curr_vert_num: int):
    r""" Adds a face to the set of observed faces and verticies
    """
    top_verts = np.array([[0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1]])
    bottom_verts = np.array([[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0]])
    left_verts = np.array([[0, 0, 0], [0, 0, 1], [0, 1, 1], [0, 1, 0]])
    right_verts = np.array([[1, 0, 0], [1, 1, 0], [1, 1, 1], [1, 0, 1]])
    front_verts = np.array([[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]])
    back_verts = np.array([[0, 1, 0], [0, 1, 1], [1, 1, 1], [1, 1, 0]])
    vert_sets = [
        top_verts, bottom_verts, left_verts, right_verts, front_verts,
        back_verts
    ]

    vert_set = vert_sets[vert_set_index]
    face = np.array([0, 1, 2, 3]) + curr_vert_num
    update = 4
    for e, vs in enumerate(vert_set):
        new_position = vs + np.array(location)
        if str(new_position) in vert_dict:
            index = vert_dict[str(new_position)]
            face[e] = index
            for idx in range(e + 1, 4):
                face[idx] -= 1
            update -= 1
        else:
            vert_dict[str(new_position)] = len(verts) + 1
            verts.append(list(new_position))
    faces.append(face)
    curr_vert_num += update
    return vert_dict, verts, faces, curr_vert_num
Beispiel #2
0
    def _check_equation(self, checker: AnswerChecker, outputs: torch.Tensor,
                        batch: ProblemInstance):
        """
        Verify whether the outputted equation is correct or not.

        :param AnswerChecker checker: AnswerChecker instance to compute equation and check answer
        :param torch.Tensor outputs:
            LongTensor containing generated equations.
            - If the model should generate op-tokens, Shape = [B, M, T], where B = batch size, M = beams, and T = length
            - Otherwise, Shape = [B, M, T, 1+2A], where A = maximum arity.
        :param batch:
        :return:
        """
        # Retrieve size information
        batch_sz, beam_sz = outputs.shape[:2]

        # Get the target field information
        required_field = _unwrap_parallel(self._module).required_field
        # Retrieve the target field
        field = getattr(self.evalset, required_field + '_field')
        # Recover string representation of gold set and generated beams
        golds = field.convert_ids_to_equations(getattr(batch, required_field))
        beams = [
            field.convert_ids_to_equations(outputs[i]) for i in range(batch_sz)
        ]

        outputs = []
        for i in range(batch_sz):
            # For each batch, retrieve information about written numbers and expected answer tuples
            numbers = batch.text.number_value[i]
            expected = batch.expected[i]

            # Test whether the produced equation in each beam
            results = [
                checker.check(beam, numbers, expected) for beam in beams[i]
            ]
            # Record outputs: (index, goldset output, generated output, correctness)
            outputs.append((i, golds[i], beams[i], results))

        return outputs
Beispiel #3
0
def fmri_prob_atlas(orig: Tensor,
                    recons: Tensor,
                    out_path: str,
                    rows: int,
                    cols: int,
                    bg_img: str,
                    mask_path: str,
                    display: str = 'vertical',
                    view_type: str = 'filled_contours',
                    draw_cross: bool = False,
                    threshold: str = 'auto',
                    **kwargs):
    mask = nl.image.load_img(mask_path)

    def save_prob_atlas(x, out_path):
        img = nl.image.new_img_like(mask,
                                    x.numpy(),
                                    affine=mask.affine,
                                    copy_header=True)
        nlplt.plot_prob_atlas(img,
                              bg_img=bg_img,
                              view_type=view_type,
                              draw_cross=draw_cross,
                              threshold=threshold)
        plt.savefig(out_path)

    i = 0
    n = min(rows * cols, orig.shape[0])

    # Save the individual probability atlases to separate png files
    for _ in range(rows):
        done = False
        for _ in range(cols):
            if i >= n:
                done = True
                break
            save_prob_atlas(orig[i], f'{out_path}_{i}_orig.tmp.png')
            save_prob_atlas(recons[i], f'{out_path}_{i}_recons.tmp.png')
            i += 1
        if done:
            break

    # Arrange all of the png files into a grid
    orig = []
    recons = []
    to_tensor = ToTensor()

    def tmpimg_to_tensor(path):
        img = to_tensor(Image.open(path)).unsqueeze(dim=0)
        os.remove(path)
        return img

    i = 0
    for _ in range(rows):
        done = False
        for _ in range(cols):
            if i >= n:
                done = True
                break
            orig.append(tmpimg_to_tensor(f'{out_path}_{i}_orig.tmp.png'))
            recons.append(tmpimg_to_tensor(f'{out_path}_{i}_recons.tmp.png'))
            i += 1
        if done:
            break

    orig = torch.cat(orig, dim=0)
    recons = torch.cat(recons, dim=0)
    plot2d(orig=orig,
           recons=recons,
           out_path=out_path,
           rows=rows,
           cols=cols,
           display=display,
           **kwargs)