• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

yeliudev / nncore / 6185555099

14 Sep 2023 12:38PM UTC coverage: 16.593% (+0.1%) from 16.475%
6185555099

push

github

yeliudev
Upgrade dist functions

10 of 10 new or added lines in 4 files covered. (100.0%)

674 of 4062 relevant lines covered (16.59%)

3.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.0
/nncore/utils/data.py
1
# Copyright (c) Ye Liu. Licensed under the MIT License.
2

3
import numpy as np
20✔
4

5

6
def swap_element(matrix, i, j, dim=0):
20✔
7
    """
8
    Swap two elements of an array or a tensor.
9

10
    Args:
11
        matrix (:obj:`np.ndarray` | :obj:`torch.Tensor`): The array or tensor
12
            to be swapped.
13
        i (int | tuple): Index of the first element.
14
        j (int | tuple): Index of the second element.
15
        dim (int, optional): The dimension to swap. Default: ``0``.
16

17
    Returns:
18
        :obj:`np.ndarray` | :obj:`torch.Tensor`: The swapped array or tensor.
19
    """
20
    inds = [slice(0, matrix.shape[d]) for d in range(dim)]
×
21

22
    i_inds = inds + [i]
×
23
    j_inds = inds + [j]
×
24

25
    meth = 'copy' if isinstance(matrix, np.ndarray) else 'clone'
×
26
    m_i = getattr(matrix[i_inds], meth)()
×
27
    m_j = getattr(matrix[j_inds], meth)()
×
28

29
    matrix[i_inds] = m_j
×
30
    matrix[j_inds] = m_i
×
31

32
    return matrix
×
33

34

35
def is_seq_of(seq, item_type, seq_type=(list, tuple)):
20✔
36
    """
37
    Check whether it is a sequence of some type.
38

39
    Args:
40
        seq (Sequence): The sequence to be checked.
41
        item_type (tuple[type] | type): Expected item type.
42
        seq_type (tuple[type] | type, optional): Expected sequence type.
43
            Default: ``(list, tuple)``.
44

45
    Returns:
46
        bool: Whether the sequence is valid.
47
    """
48
    if not isinstance(seq, seq_type):
20✔
49
        return False
20✔
50

51
    for item in seq:
20✔
52
        if not isinstance(item, item_type):
20✔
53
            return False
20✔
54

55
    return True
20✔
56

57

58
def is_list_of(seq, item_type):
20✔
59
    """
60
    Check whether it is a list of some type.
61

62
    A partial method of :obj:`is_seq_of`.
63
    """
64
    return is_seq_of(seq, item_type, seq_type=list)
20✔
65

66

67
def is_tuple_of(seq, item_type):
20✔
68
    """
69
    Check whether it is a tuple of some type.
70

71
    A partial method of :obj:`is_seq_of`.
72
    """
73
    return is_seq_of(seq, item_type, seq_type=tuple)
20✔
74

75

76
def slice(seq, length, type='list'):
20✔
77
    """
78
    Slice a sequence into several sub sequences by length.
79

80
    Args:
81
        seq (list | tuple): The sequence to be sliced.
82
        length (list[int] | int): The expected length or list of lengths.
83
        type (str, optional): The type of returned object. Expected values
84
            include ``'list'`` and ``'tuple'``. Default: ``'list'``.
85

86
    Returns:
87
        list[list]: The sliced sequences.
88
    """
89
    assert type in ('list', 'tuple')
20✔
90

91
    if isinstance(length, int):
20✔
92
        assert len(seq) % length == 0
×
93
        length = [length] * int(len(seq) / length)
×
94
    elif not isinstance(length, list):
20✔
95
        raise TypeError("'length' must be an integer or a list of integers")
20✔
96
    elif sum(length) != len(seq):
20✔
97
        raise ValueError('the total length do not match the sequence length')
20✔
98

99
    out, idx = [], 0
20✔
100
    for i in range(len(length)):
20✔
101
        out.append(seq[idx:idx + length[i]])
20✔
102
        idx += length[i]
20✔
103

104
    if type == 'tuple':
20✔
105
        out = tuple(out)
×
106

107
    return out
20✔
108

109

110
def concat(seq):
20✔
111
    """
112
    Concatenate a sequence of sequences.
113

114
    Args:
115
        seq (list | tuple): The sequence to be concatenated.
116

117
    Returns:
118
        list | tuple: The concatenated sequence.
119
    """
120
    seq_type = type(seq)
20✔
121

122
    out = []
20✔
123
    for item in seq:
20✔
124
        out += item
20✔
125

126
    return seq_type(out)
20✔
127

128

129
def interleave(seq):
20✔
130
    """
131
    Interleave a sequence of sequences.
132

133
    Args:
134
        seq (list | tuple): The sequence to be interleaved.
135

136
    Returns:
137
        list | tuple: The interleaved sequence.
138
    """
139
    seq_type = type(seq)
×
140
    return seq_type([v for s in zip(*seq) for v in s])
×
141

142

143
def flatten(seq):
20✔
144
    """
145
    Flatten a sequence of sequences and items.
146

147
    Args:
148
        seq (list | tuple): The sequence to be flattened.
149

150
    Returns:
151
        list | tuple: The flattened sequence.
152
    """
153
    seq_type = type(seq)
×
154

155
    out = []
×
156
    for item in seq:
×
157
        if isinstance(item, (list, tuple)):
×
158
            out += flatten(item)
×
159
        else:
160
            out.append(item)
×
161

162
    return seq_type(out)
×
163

164

165
def to_dict_of_list(in_list):
20✔
166
    """
167
    Convert a list of dicts to a dict of lists.
168

169
    Args:
170
        in_list (list): The list of dicts to be converted.
171

172
    Returns:
173
        dict: The converted dict of lists.
174
    """
175
    for i in range(len(in_list) - 1):
×
176
        if in_list[i].keys() != in_list[i + 1].keys():
×
177
            raise ValueError('dict keys are not consistent')
×
178

179
    out_dict = dict()
×
180
    for key in in_list[0]:
×
181
        out_dict[key] = [item[key] for item in in_list]
×
182

183
    return out_dict
×
184

185

186
def to_list_of_dict(in_dict):
20✔
187
    """
188
    Convert a dict of lists to a list of dicts.
189

190
    Args:
191
        in_dict (dict): the dict of lists to be converted.
192

193
    Returns:
194
        list: The converted list of dicts.
195
    """
196
    values = in_dict.values()
×
197
    for i in range(len(in_dict) - 1):
×
198
        if len(values[i]) != len(values[i + 1]):
×
199
            raise ValueError('lengths of lists are not consistent')
×
200

201
    out_list = []
×
202
    for i in range(len(in_dict)):
×
203
        out_list.append({k: v[i] for k, v in in_dict.items()})
×
204

205
    return out_list
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc