Exemplo n.º 1
0
def format_loan_table(loans: List[Loan], include_id=False):
    """Format the given list of loans into a markdown table.

    Arguments:
        loans (list[Loan]): The list of loans to format into a table.
        include_id (bool): True if the id of the loan should be included in
            the table, false otherwise

    Returns:
        (str) The markdown formatted table
    """
    tus.check(loans=(loans, (tuple, list)), include_id=(include_id, bool))
    tus.check_listlike(loans=(loans, Loan))

    result_lines = [
        'Lender|Borrower|Amount Given|Amount Repaid|Unpaid?|Original Thread'
        + '|Date Given|Date Paid Back' + ('|id' if include_id else ''),
        ':--|:--|:--|:--|:--|:--|:--|:--' + ('|:--' if include_id else '')
    ]
    line_fmt = '|'.join('{' + a + '}' for a in (
        'lender', 'borrower', 'principal', 'principal_repayment',
        'unpaid_bool', 'permalink', 'created_at_pretty', 'repaid_at_pretty',
        *(['id'] if include_id else [])
    ))
    for loan in loans:
        loan_dict = loan.dict().copy()
        loan_dict['permalink'] = loan_dict.get('permalink', '')
        loan_dict['unpaid_bool'] = '***UNPAID***' if loan.unpaid_at is not None else ''
        loan_dict['created_at_pretty'] = loan.created_at.strftime('%b %d, %Y')
        loan_dict['repaid_at_pretty'] = (
            loan.repaid_at.strftime('%b %d, %Y') if loan.repaid_at is not None else ''
        )
        result_lines.append(line_fmt.format(**loan_dict))

    return '\n'.join(result_lines)
Exemplo n.º 2
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
Exemplo n.º 3
0
    def reshape(self, shape: typing.Tuple[int]) -> 'FluentModule':
        """Reshapes the data to the specified shape. Must correspond to the
        same total number of features.

        .. note::

            The batch dimension is preserved.

        :param shape: the new shape for the data
        :type shape: tuple[int]
        :returns: self
        :rtype: FluentModule
        """
        tus.check(shape=(shape, (list, tuple)))
        tus.check_listlike(shape=(shape, int, (1, None)))
        for features in shape:
            if features <= 0:
                raise ValueError(f'shape={shape} must be positive')

        old_num_features = reduce(operator.mul, self.shape)
        new_num_features = reduce(operator.mul, shape)
        if old_num_features != new_num_features:
            raise ValueError(
                f'cannot view {self.shape} as {shape}: expected ' +
                f'{old_num_features} but got {new_num_features}')

        self.sequence.append(self._wrap(Reshape(*shape)))
        self.shape = tuple(shape)
        if self.is_verbose:
            print(f'  Reshape -> {self.shape}')
        return self
Exemplo n.º 4
0
    def overlay(self, overlay: FrameGenerator, pos: typing.Tuple[int, int]):
        """Overlays the current frame generator with the given one at the given
        position. Note that the overlayed frame generator must have the same
        duration as the current one and must fit entirely within the frame
        """
        tus.check(overlay=(overlay, FrameGenerator), pos=(pos, (list, tuple)))
        tus.check_listlike(pos=(pos, int, 2))

        return self.apply(OverlayFrameGenerator, overlay, pos)
Exemplo n.º 5
0
 def __init__(self,
              child: FrameGenerator,
              new_frame_size: typing.Tuple[int, int],
              resample: int = PIL.Image.LANCZOS):
     tus.check(child=(child, FrameGenerator),
               new_frame_size=(new_frame_size, (list, tuple)),
               resample=(resample, int))
     tus.check_listlike(new_frame_size=(new_frame_size, int, 2))
     self.child = child
     self.new_frame_size = tuple(new_frame_size)
     self.resample = resample
Exemplo n.º 6
0
    def __init__(self, shape: typing.Tuple[int], assume_wrapped: bool = False):
        tus.check(shape=(shape, (list, tuple)),
                  assume_wrapped=(assume_wrapped, bool))
        tus.check_listlike(shape=(shape, int, (1, None)))
        for features in shape:
            if features <= 0:
                raise ValueError(f'shape={shape} must be positive')

        self.shape = tuple(shape)
        self.sequence = []
        self.is_verbose = False
        self.wrapped = assume_wrapped
Exemplo n.º 7
0
    def __init__(self, urls, weights):
        tus.check(urls=(urls, (list, tuple)), weights=(weights, (list, tuple)))
        tus.check_listlike(urls=(urls, str), weights=(weights, (int, float)))

        self.urls = urls
        self.weights = [float(w) for w in weights]
        self._rolling_sum_weights = []

        _sum = 0.0
        for weight in self.weights:
            _sum += weight
            self._rolling_sum_weights.append(_sum)
Exemplo n.º 8
0
    def __init__(self, scenes: typing.Tuple[Scene]):
        tus.check(scenes=(scenes, (list, tuple)))
        tus.check_listlike(scenes=(scenes, Scene))
        self.scenes = tuple(scenes)

        scenes_end_at = []
        cur = 0
        for scene in scenes:
            cur += scene.duration
            scenes_end_at.append(cur)
        self.scenes_end_at = tuple(scenes_end_at)
        self._scene_hint = 0
        self._last_scene = None
Exemplo n.º 9
0
    def __init__(self, snapshots: typing.List[PCTrajectoryGenSnapshot]) -> None:
        tus.check_listlike(snapshots=(snapshots, PCTrajectoryGenSnapshot))

        fdtype = snapshots[0].principal_vectors.dtype
        num_pcs = snapshots[0].principal_values.shape[0]
        for i, snap in enumerate(snapshots):
            if snap.principal_vectors.dtype != fdtype:
                raise ValueError(f'mismatched floating data types; for snapshots[0] is {fdtype}, '
                                 + f'for snapshots[{i}] is {snap.principal_vectors.dtype}')
            if snap.principal_values.shape[0] != num_pcs:
                raise ValueError(f'mismatched number of pcs; for snapshots[0] is {num_pcs}, '
                                 + f'for snapshots[{i}] is {snap.principal_values.shape[0]}')

        self.snapshots = snapshots
Exemplo n.º 10
0
    def __init__(self, frame_size_inches: typing.Tuple[float, float],
                 dpi: int):
        tus.check(frame_size_inches=(frame_size_inches, (list, tuple)),
                  dpi=(dpi, int))
        tus.check_listlike(frame_size_inches=(frame_size_inches, float, 2))
        self.frame_size_inches = tuple(frame_size_inches)
        self.dpi = dpi

        self._frame_size = (int(frame_size_inches[0] * dpi),
                            int(frame_size_inches[1] * dpi))

        # correct for rounding
        frame_size_inches = (self._frame_size[0] / dpi,
                             self._frame_size[1] / dpi)
Exemplo n.º 11
0
    def __init__(self, children: typing.Tuple[Scene],
                 sep_enters: bool) -> None:
        tus.check(children=(children, (list, tuple)),
                  sep_enters=(sep_enters, bool))
        tus.check_listlike(children=(children, Scene, (1, None)))

        duration = children[0].duration

        for i, child in enumerate(children):
            if child.duration != duration:
                raise ValueError(f'children[0].duration={duration}, but ' +
                                 f'children[{i}].duration={child.duration}')

        self.children = tuple(children)
        self.sep_enters = sep_enters
Exemplo n.º 12
0
def to_trajectory(sample_labels: torch.tensor, all_hid_acts: typing.List[torch.tensor],
                  num_pcs: int) -> PCTrajectoryGen:
    """Converts the given labels and hidden activations to a trajectory with the specified
    number of principal components.

    Args:
        sample_labels (torch.tensor): The labels / targets for the corresponding samples
            size: [num_samples, ...]
            dtype: any

            first index is which sample

        all_hid_acts (list[torch.tensor]): the hidden activations through time or layers
            size: [num_layers]
            each element:
                size: [num_samples, layer_size]
                dtype: float-like

                the first index is which sample
                the second index is which feature

    Returns:
        The trajectory found by calculating the top num_pcs pcs and then projecting the samples
        onto those pcs. For plotting it may be helpful to use a PCTrajectoryFFMatchInfo which works
        for the Gen version as well (since it is agnostic to labels)
    """
    tus.check(sample_labels=(sample_labels, torch.tensor),
              all_hid_acts=(all_hid_acts, (list, tuple)),
              num_pcs=(num_pcs, int))
    tus.check_listlike(all_hid_acts=(all_hid_acts, torch.Tensor))

    snapshots = []
    for idx, hid_acts in enumerate(all_hid_acts):
        torch.check_tensors(
            **{f'hid_acts[{idx}]': (
                hid_acts,
                (
                    ('num_samples', sample_labels.shape[0]),
                    ('layer_size', None)
                ),
                (torch.float, torch.double)
            )}
        )
        pc_vals, pc_vecs = pca.get_hidden_pcs(hid_acts, num_pcs)
        projected = pca.project_to_pcs(hid_acts, pc_vecs, out=None)
        snapshots.append(PCTrajectoryGenSnapshot(pc_vecs, pc_vals, projected, sample_labels))

    return PCTrajectoryGen(snapshots)
Exemplo n.º 13
0
    def __init__(self, frame_size_in: typing.Tuple[float, float], dpi: int):
        tus.check(
            frame_size_in=(frame_size_in, (list, tuple)),
            dpi=(dpi, int)
        )
        tus.check_listlike(frame_size_in=(frame_size_in, (int, float), 2))

        # this fixes rounding
        _frame_size = (
            int(round(frame_size_in[0] * dpi)),
            int(round(frame_size_in[1] * dpi))
        )
        frame_size_in = (_frame_size[0] / dpi, _frame_size[1] / dpi)

        self.frame_size_in = frame_size_in
        self.dpi = dpi
        self._frame_size = _frame_size
Exemplo n.º 14
0
    def __init__(self, anchors, tokens):
        if isinstance(anchors, str):
            anchors = (anchors, )

        tus.check(anchors=(anchors, (list, tuple)),
                  tokens=(tokens, (list, tuple)))
        tus.check_listlike(anchors=(anchors, str))
        if not anchors:
            raise ValueError('at least one anchor must be specified')
        for idx, token in enumerate(tokens):
            tus.check(**{f'tokens_{idx}': (token, dict)})
            tus.check(
                **{
                    f'tokens_{idx}_token': (token['token'], Token),
                    f'tokens_{idx}_optional': (token['optional'], bool)
                })
        self.anchors = anchors
        self.tokens = tokens
Exemplo n.º 15
0
    def __init__(self, state: ActState, renderer: ActRenderer,
                 scenes: typing.Tuple[Scene]):
        tus.check(state=(state, ActState),
                  renderer=(renderer, ActRenderer),
                  scenes=(scenes, (list, tuple)))
        tus.check_listlike(scenes=(scenes, Scene))
        self.state = state
        self.renderer = renderer
        self.scenes = tuple(scenes)

        scenes_end_at = []
        cur = 0
        for scene in scenes:
            cur += scene.duration
            scenes_end_at.append(cur)
        self.scenes_end_at = scenes_end_at
        self._scene_hint = 0
        self._last_scene = None
Exemplo n.º 16
0
    def __init__(self, children: typing.Tuple[FrameGenerator]):
        tus.check(children=(children, (list, tuple)))
        tus.check_listlike(children=(children, FrameGenerator, (1, None)))

        self.children = tuple(children)
        self.children_end_at = []
        cur = 0
        frame_size = self.children[0].frame_size

        for i, child in enumerate(self.children):
            if child.frame_size != frame_size:
                raise ValueError(
                    f'children[0].frame_size = {frame_size}, but ' +
                    f'children[{i}].frame_size = {child.frame_size}')
            cur += child.duration
            self.children_end_at.append(cur)

        self._search_hint = 0
Exemplo n.º 17
0
    def __init__(self,
                 cluster,
                 timeout_seconds,
                 back_off,
                 ttl_seconds,
                 auth,
                 verify=None,
                 disable_database_delete=True,
                 protected_databases=None,
                 disable_collection_delete=True,
                 protected_collections=None):
        """Initializes Config by setting the corresponding attributes. For
        auth if it is a StatefulAuth it is wrapped with a StatefulAuthWrapper.
        """
        if isinstance(auth, StatefulAuth):
            auth = StatefulAuthWrapper(auth)
        if protected_databases is None:
            protected_databases = []
        if protected_collections is None:
            protected_collections = []

        tus.check(cluster=(cluster, Cluster),
                  timeout_seconds=(timeout_seconds, int),
                  back_off=(back_off, BackOffStrategy),
                  ttl_seconds=(ttl_seconds, (int, type(None))),
                  auth=(auth, Auth),
                  verify=(verify, (str, type(None))),
                  disable_database_delete=(disable_database_delete, bool),
                  protected_databases=(protected_databases, (list, tuple)),
                  disable_collection_delete=(disable_collection_delete, bool),
                  protected_collections=(protected_collections, (list, tuple)))
        tus.check_listlike(protected_databases=(protected_databases, str),
                           protected_collections=(protected_collections, str))

        self.cluster = cluster
        self.timeout_seconds = timeout_seconds
        self.back_off = back_off
        self.ttl_seconds = ttl_seconds
        self.auth = auth
        self.verify = verify
        self.disable_database_delete = disable_database_delete
        self.protected_databases = protected_databases
        self.disable_collection_delete = disable_collection_delete
        self.protected_collections = protected_collections
Exemplo n.º 18
0
    def __init__(self, words: typing.List[str], char_delay: int):
        tus.check_listlike(words=(words, str))
        tus.check(char_delay=(char_delay, (int, float)))
        super().__init__(len(words), 1, 2)

        self.words = words
        self.char_delay = float(char_delay)
        self.encoded_words = None

        if PRE_ENCODE:
            self.encoded_words = []
            for word in self.words:
                inputs = [menc.encode_input(i) for i in word]
                inputs.append(menc.encode_input_stop())
                outputs = [
                    menc.encode_output(i, self.char_delay) for i in word
                ]
                outputs.append(menc.encode_output_stop())
                self.encoded_words.append(
                    (Sequence(raw=inputs), Sequence(raw=outputs)))
Exemplo n.º 19
0
    def __init__(self,
                 frame_size: typing.Tuple[int, int],
                 dpi: typing.Union[int, float],
                 bitrate: int,
                 fps: int,
                 outfile: str,
                 max_ooo_frames: int = 5000,
                 block_size: int = 4048) -> None:
        tus.check(frame_size=(frame_size, (list, tuple)),
                  dpi=(dpi, (int, float)),
                  bitrate=(bitrate, int),
                  fps=(fps, int),
                  outfile=(outfile, str),
                  max_ooo_frames=(max_ooo_frames, (int, float)),
                  block_size=(block_size, int))
        tus.check_listlike(frame_size=(frame_size, (int, float), 2))

        wo_ext, ext = os.path.splitext(outfile)
        if ext == '':
            outfile = wo_ext + '.mp4'
        elif ext != '.mp4':
            raise NotImplementedError(
                f'only mp4 encoding is supported, but got {ext}')

        if os.path.exists(outfile):
            raise FileExistsError(outfile)

        self.frame_size = frame_size
        self.dpi = dpi
        self.bitrate = bitrate
        self.fps = fps
        self.outfile = outfile
        self.ffmpeg_proc = None
        self.next_frame = 0
        self.ooo_frames = dict()
        self.max_ooo_frames = max_ooo_frames
        self.block_size = block_size
        self.receive_queues = []
        self.perfs = []
Exemplo n.º 20
0
    def __init__(self, base: FrameGenerator, overlay: FrameGenerator,
                 pos: typing.Tuple[int, int]):
        tus.check(base=(base, FrameGenerator),
                  overlay=(overlay, FrameGenerator),
                  pos=(pos, (tuple, list)))
        tus.check_listlike(pos=(pos, int, 2))

        if (pos[0] < 0 or pos[1] < 0
                or pos[0] + overlay.frame_size[0] > base.frame_size[0]
                or pos[1] + overlay.frame_size[1] > base.frame_size[1]):
            raise ValueError(f'cannot fit overlay at {pos} of size ' +
                             f'{overlay.frame_size} onto frame of size ' +
                             str(base.frame_size))

        if base.duration != overlay.duration:
            raise ValueError(
                f'durations dont match: base has {base.duration}' +
                f' and overlay has {overlay.duration}')

        self.base = base
        self.overlay = overlay
        self.pos = pos
Exemplo n.º 21
0
 def __init__(self, children):
     tus.check(children=(children, (tuple, list)))
     tus.check_listlike(children=(children, Token))
     self.children = children
Exemplo n.º 22
0
 def test_checklistlike_manystr_pass(self):
     tus.check_listlike(a=(['a', 'b'], str))
Exemplo n.º 23
0
 def test_checklistlike_singleint_fail(self):
     with self.assertRaises(ValueError):
         tus.check_listlike(a=([1], str))
Exemplo n.º 24
0
 def test_checklistliek_minlen_fail(self):
     with self.assertRaises(ValueError):
         tus.check_listlike(a=([3.5, 2.5, 1.0], float, (4, None)))
Exemplo n.º 25
0
 def test_checklistlike_mixed_fail(self):
     with self.assertRaises(ValueError):
         tus.check_listlike(a=(['a', 5], str))
Exemplo n.º 26
0
 def test_checklistlike_minlen_pass(self):
     tus.check_listlike(a=([3.5, 2.5, 1.0], float, (2, None)))
Exemplo n.º 27
0
 def test_checklistlike_exactlen_fail(self):
     with self.assertRaises(ValueError):
         tus.check_listlike(a=([3.5], float, 2))
Exemplo n.º 28
0
 def test_checklistlike_exactlen_pass(self):
     tus.check_listlike(a=([3.5], float, 1))
Exemplo n.º 29
0
 def test_checklistlike_multiplelist_fail(self):
     with self.assertRaises(ValueError):
         tus.check_listlike(a=([3.5], float), b=(['john'], float))
Exemplo n.º 30
0
 def test_checklistlike_multiplelist_pass(self):
     tus.check_listlike(a=([3.5], float), b=(['john', 'doe'], str))