예제 #1
0
    def __init__(self, samples: np.ndarray, centers: np.ndarray,
                 labels: np.ndarray,
                 calculate_params: typing.Dict[str, typing.Any]):
        tus.check(samples=(samples, np.ndarray),
                  centers=(centers, np.ndarray),
                  labels=(labels, np.ndarray),
                  calculate_params=(calculate_params, dict))
        tus.check_ndarrays(
            samples=(samples, ('n_samples', 'n_features'),
                     (np.dtype('float32'), np.dtype('float64'))),
            centers=(centers,
                     ('n_clusters',
                      ('n_features',
                       samples.shape[1] if len(samples.shape) > 1 else None)),
                     samples.dtype),
            labels=(labels,
                    (('n_samples',
                      samples.shape[0] if bool(samples.shape) else None), ),
                    (np.dtype('int32'), np.dtype('int64'))))
        self.samples = samples
        self.centers = centers
        self.labels = labels
        self.calculate_params = calculate_params

        self._bounds = None
예제 #2
0
 def __init__(self, projection_vectors: np.ndarray,
              projected_samples: np.ndarray,
              projected_sample_labels: np.ndarray):
     tus.check_ndarrays(
         projection_vectors=(
             projection_vectors,
             ('og_size', 'proj_size'),
             ('float32', 'float64')
         ),
         projected_samples=(
             projected_samples,
             ('samples', ('proj_size', projection_vectors.shape[1])),
             projection_vectors.dtype
         )
     )
     tus.check(projected_sample_labels=(
         projected_sample_labels, np.ndarray))
     if projected_sample_labels.shape[0] != projected_samples.shape[0]:
         raise ValueError(
             'projected_sample_labels should have shape (samples, ...)'
             + f'where samples={projected_samples.shape[0]}'
             + '=projected_samples.shape[0], but has shape '
             + str(projected_sample_labels.shape))
     self.projection_vectors = projection_vectors
     self.projected_samples = projected_samples
     self.projected_sample_labels = projected_sample_labels
예제 #3
0
 def __init__(self, frequency: float, indeps: np.ndarray):
     tus.check(frequency=(frequency, (int, float)))
     tus.check_ndarrays(
         indeps=(indeps, ('n_samples',), ('float32', 'float64'))
     )
     self.frequency = frequency
     self.indeps = indeps
예제 #4
0
 def __init__(self, snapshot_ind: int, mask: np.ndarray, alpha_start: float,
              alpha_end: float):
     tus.check_ndarrays(mask=(mask, ('samples', ), 'bool'))
     self.snapshot_ind = snapshot_ind
     self.mask = mask
     self.alpha_start = alpha_start
     self.alpha_end = alpha_end
예제 #5
0
    def __init__(self,
                 trajectory: ProjectedTrajectory,
                 default_styling: typing.Tuple[typing.Tuple[np.ndarray, dict]],
                 title: str = 'Projected Trajectory',
                 zoom: np.ndarray = None,
                 rotation: typing.Tuple[float, float] = (30, 45),
                 visible_points: typing.Tuple[typing.Tuple[np.ndarray,
                                                           np.ndarray,
                                                           dict]] = None):
        if visible_points is None:
            visible_points = ((trajectory.snapshots[0].projected_samples,
                               np.ones(trajectory.num_samples,
                                       dtype='bool'), dict()), )
        if zoom is None:
            zoom = get_square_bounds_for_all(pts for (pts, mask,
                                                      d) in visible_points)

        tus.check(trajectory=(trajectory, ProjectedTrajectory),
                  default_styling=(default_styling, (list, tuple)),
                  title=(title, str),
                  rotation=(rotation, (list, tuple)),
                  visible_points=(visible_points, (list, tuple)))
        tus.check_ndarrays(zoom=(zoom, (3, 2), ('float32', 'float64')))
        tus.check_listlike(rotation=(rotation, (int, float), 2))

        self.trajectory = trajectory
        self.default_styling = default_styling
        self.title = title
        self.zoom = zoom
        self.rotation = rotation
        self.visible_points = visible_points
예제 #6
0
def match(a: np.ndarray, b: np.ndarray) -> np.ndarray:
    """Finds the matrix R that minimizes the frobenius norm of RA - B, where
    R is orthonormal.

    Args:
        a (np.ndarray[samples, features]): the first matrix to match
        b (np.ndarray[samples, features]): the second matrix to match

    Returns:
        np.ndarray: the orthonormal matching matrix R
    """
    tus.check_ndarrays(a=(a, ('samples', 'features'), ('float32', 'float64')),
                       b=(b, (('samples', a.shape[0]), ('features',
                                                        a.shape[1])), a.dtype))
    m = b @ a.T
    u, _, vh = scipy.linalg.svd(m)
    return np.real(u @ vh)
예제 #7
0
def submask(pts: np.ndarray, mask: np.ndarray, mask2: np.ndarray):
    """Given that there exists some array arr where pts = arr[mask], this
    finds arr[mask2] where mask2 is some subset of mask.

    Args:
        pts (np.ndarray): arr[mask]
        mask (np.ndarray): some bool mask of the unknown array arr
        mask2 (np.ndarray): a subset of mask, i.e. a bool array that is false
            everywhere that mask is false but potentially false where mask is
            true

    Returns:
       arr[mask2]
    """
    tus.check_ndarrays(
        mask=(mask, ('samples',), 'bool'),
        mask2=(mask2, (('samples', mask.shape[0]),), 'bool'))
    shape = [mask.shape[0]]
    shape.extend(pts.shape[1:])
    arr = np.zeros(shape, dtype=pts.dtype)
    arr[mask] = pts
    return arr[mask2]
예제 #8
0
def project_with_matrix(samples: np.ndarray, labels: np.ndarray,
                        mat: np.ndarray) -> ProjectedSnapshot:
    """Projects the given samples using the given projection
    matrix.

    Arguments:
        samples (np.ndarray[samples, features]): the samples to project
        labels (np.ndarray[samples, ...]): the labels for each sample
        mat (np.ndarray[features, proj_size]):

    Returns:
        snap (ProjectedSnapshot): the samples projected using the matrix
    """
    tus.check_ndarrays(
        samples=(samples, ('samples', 'features'), ('float32', 'float64')),
        mat=(
            mat,
            (('features', samples.shape[1]), 'proj_size'),
            samples.dtype
        ))

    return ProjectedSnapshot(mat, samples @ mat, labels)
예제 #9
0
 def test_checkndarrays_emptytensor_pass(self):
     tus.check_ndarrays(a=(np.ndarray(0), None, None))
예제 #10
0
 def test_checkndarrays_exactnameddims_pass(self):
     tus.check_ndarrays(a=(_like((10, 4), 'float32'), (('dim1', 10), 4),
                           None))
예제 #11
0
 def test_checkndarrays_exactdims_pass(self):
     tus.check_ndarrays(a=(_like((10, 4), 'float32'), (10, 4), None))
예제 #12
0
 def test_checkndarrays_namednumdims_fail(self):
     with self.assertRaises(ValueError):
         tus.check_ndarrays(a=(_like((10, 4, 2), 'float32'),
                               ('dim1', 'dim2'), None))
예제 #13
0
 def test_checkndarrays_namednumdims_pass(self):
     tus.check_ndarrays(a=(_like((10, 4), 'float32'), ('dim1', 'dim2'),
                           None))
예제 #14
0
 def test_checkndarrays_withnptype_fail(self):
     with self.assertRaises(ValueError):
         tus.check_ndarrays(a=(np.ndarray(0, dtype='float64'), None,
                               np.float32))
예제 #15
0
 def test_checkndarrays_withnptype_pass(self):
     tus.check_ndarrays(a=(np.ndarray(0, dtype='float64'), None,
                           np.float64))
예제 #16
0
def generate(traj: pca3dvis.trajectory.ProjectedTrajectory,
             markers: typing.Tuple[typing.Tuple[np.ndarray, dict]],
             titles: typing.Tuple[str],
             outfolder: str,
             draft: bool = False,
             clusters: bool = True):
    """Generates a video and corresponding snapshots into the specified
    directory. If draft is true, this uses the draft settings. Otherwise,
    this uses high-quality production settings.

    For fine control over the settings it is recommended that you use the
    underlying pympanim functions.

    Args:
        traj (ProjectedTrajectory): the trajectory to plot
        markers (tuple[tuple[ndarray, dict]]):
            each
        titles (tuple[str]): one title per snapshot in the trajectory
        outfolder (str): where to save. must not already exist
        draft (bool): if true, lower quality settings are used
        clusters (bool): if clusters are detected and zoomed to
    """
    tus.check(traj=(traj, pca3dvis.trajectory.ProjectedTrajectory),
              markers=(markers, (list, tuple)),
              titles=(titles, (list, tuple)),
              outfolder=(outfolder, str),
              draft=(draft, bool),
              clusters=(clusters, bool))
    for i, marker in enumerate(markers):
        tus.check(**{f'marker[{i}]': (marker, (list, tuple))})
        if len(marker) != 2:
            raise ValueError(
                f'expected marker[{i}] is (ndarray, dict), got {marker}')
        mask, kwargs = marker
        tus.check_ndarrays(**{
            f'marker[{i}][0]': (mask, (('samples', traj.num_samples), ),
                                'bool')
        })
        tus.check(**{f'marker[{i}][1]': (kwargs, dict)})
    tus.check_listlike(titles=(titles, str, traj.num_snapshots))

    os.makedirs(outfolder)
    os.makedirs(os.path.join(outfolder, 'snapshots'))

    state = pca3dvis.state.ProjectedState(traj, markers)
    rend = pca3dvis.renderer.ProjectedRenderer((19.2, 10.8) if not draft else
                                               (6.4, 4.8), 100)

    for i, title in enumerate(titles):
        filepath = os.path.join(outfolder, 'snapshots', f'snapshot_{i}')
        ext = '.png' if draft else '.pdf'
        transp = not draft

        state.set_snapshot_visible(i, True)
        state.title = title
        for rot in range(15, 375, 60 if draft else 30):
            state.rotation = (30, rot)
            fig = rend.render_mpl(state)

            fname = filepath + f'_rot{rot}' + ext
            fig.savefig(fname, transparent=transp, dpi=rend.dpi)
            plt.close(fig)

    pts = traj.snapshots[0].projected_samples
    zoom = pca3dvis.state.get_square_bounds_for(pts)
    my_scene = (acts.FluentScene(scenes.SnapshotScene(0)).join(
        scenes.FixedTitleScene(titles[0]),
        False).join(scenes.RotationScene((30, 45), (30, 45 + 360)),
                    False).join(scenes.FixedZoomScene(zoom), False).dilate(
                        pytweening.easeInOutSine).time_rescale_exact(12, 's'))

    if clusters:
        _cluster_scene(my_scene, traj, 0, titles[0], draft)

    for snap_ind in range(1, traj.num_snapshots):
        snap = traj.snapshots[snap_ind]
        npts = snap.projected_samples
        nzoom = pca3dvis.state.get_square_bounds_for(npts)
        mzoom = pca3dvis.state.get_square_bounds_for_all((pts, npts))
        ntitle = titles[snap_ind]
        ititle = titles[snap_ind - 1] + ' -> ' + ntitle

        if not np.allclose(zoom, mzoom):
            (my_scene.push(scenes.ZoomScene(zoom, mzoom)).join(
                scenes.SnapshotScene(snap_ind - 1),
                False).join(scenes.FixedTitleScene(ititle), False).join(
                    scenes.FixedRotationScene((30, 45)), False).dilate(
                        pympanim.easing.smoothstep).time_rescale_exact(
                            2, 's').pop())

        (my_scene.push(scenes.InterpScene(
            snap_ind - 1, snap_ind)).dilate(pytweening.easeInOutCirc).join(
                scenes.FixedZoomScene(mzoom),
                False).join(scenes.FixedTitleScene(ititle), False).push(
                    scenes.RotationScene((30, 45), (30, 45 + 360))).dilate(
                        pytweening.easeInOutSine).dilate(
                            pympanim.easing.squeeze, {
                                'amt': 0.1
                            }).pop('join').time_rescale_exact(6, 's').pop())

        if not np.allclose(mzoom, nzoom):
            (my_scene.push(scenes.ZoomScene(mzoom, nzoom)).join(
                scenes.SnapshotScene(snap_ind),
                False).join(scenes.FixedTitleScene(ititle), False).join(
                    scenes.FixedRotationScene((30, 45)), False).dilate(
                        pympanim.easing.smoothstep).time_rescale_exact(
                            2, 's').pop())

        (my_scene.push(scenes.SnapshotScene(snap_ind)).join(
            scenes.FixedTitleScene(ntitle), False).join(
                scenes.RotationScene((30, 45), (30, 45 + 360)),
                False).join(scenes.FixedZoomScene(nzoom), False).dilate(
                    pytweening.easeInOutSine).time_rescale_exact(10,
                                                                 's').pop())

        if clusters:
            _cluster_scene(my_scene, traj, snap_ind, ntitle, draft)

        pts = npts
        zoom = nzoom

    if draft:
        my_scene.time_rescale(5)

    pympanim.worker.produce(
        acts.Act(state, rend, [my_scene.build()]), 60 if not draft else 30,
        100, -1,
        os.path.join(outfolder, 'video.mp4' if not draft else 'draft.mp4'))
예제 #17
0
 def test_checkndarrays_exactnameddims_fail(self):
     with self.assertRaises(ValueError):
         tus.check_ndarrays(a=(_like((10, 4), 'float32'), (('dim1', 11), 4),
                               None))
예제 #18
0
 def __init__(self, snapshot_ind: int, mask: np.ndarray):
     tus.check_ndarrays(mask=(mask, ('samples', ), 'bool'))
     self.snapshot_ind = snapshot_ind
     self.mask = mask
예제 #19
0
 def test_checkndarrays_noargs_pass(self):
     tus.check_ndarrays()