Skip to content

Base

round_any(x, base, round_type='round')

Rounds x to the nearest multiple of base with the rounding done according to round_type.

Parameters:

Name Type Description Default
x Union[float, np.ndarray]

Number or array to round.

required
base float

Rounds x to nearest integer multiple of value of base.

required
round_type str

One of the following, indicating how to round x -

  • 'round'
  • 'ceil'
  • 'float'
'round'

Returns:

Type Description
Union[float, np.ndarray]

Rounded version of x.

Example
round_any(3, 5) = 5
round_any(3, 5, 'floor') = 0
Source code in coppafish/utils/base.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def round_any(x: Union[float, np.ndarray], base: float, round_type: str = 'round') -> Union[float, np.ndarray]:
    """
    Rounds `x` to the nearest multiple of `base` with the rounding done according to `round_type`.

    Args:
        x: Number or array to round.
        base: Rounds `x` to nearest integer multiple of value of `base`.
        round_type: One of the following, indicating how to round `x` -

            - `'round'`
            - `'ceil'`
            - `'float'`

    Returns:
        Rounded version of `x`.

    Example:
        ```
        round_any(3, 5) = 5
        round_any(3, 5, 'floor') = 0
        ```
    """
    if round_type == 'round':
        return base * np.round(x / base)
    elif round_type == 'ceil':
        return base * np.ceil(x / base)
    elif round_type == 'floor':
        return base * np.floor(x / base)
    else:
        raise ValueError(f"round_type specified was {round_type} but it should be one of the following:\n"
                         f"round, ceil, floor")

setdiff2d(array1, array2)

Finds all elements in array1 that are not in array2. Returned array will only contain unique elements E.g.

If array1 has [4,0] twice, array2 has [4,0] once, returned array will not have [4,0].

If array1 has [4,0] twice, array2 does not have [4,0], returned array will have [4,0] once.

Parameters:

Name Type Description Default
array1 np.ndarray

float [n_elements1 x element_dim].

required
array2 np.ndarray

float [n_elements2 x element_dim].

required

Returns:

Type Description
np.ndarray

float [n_elements_diff x element_dim].

Source code in coppafish/utils/base.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def setdiff2d(array1: np.ndarray, array2: np.ndarray) -> np.ndarray:
    """
    Finds all elements in `array1` that are not in `array2`.
    Returned array will only contain unique elements E.g.

    If `array1` has `[4,0]` twice, `array2` has `[4,0]` once, returned array will not have `[4,0]`.

    If `array1` has `[4,0]` twice, `array2` does not have `[4,0]`, returned array will have `[4,0]` once.

    Args:
        array1: `float [n_elements1 x element_dim]`.
        array2: `float [n_elements2 x element_dim]`.

    Returns:
        `float [n_elements_diff x element_dim]`.
    """
    set1 = set([tuple(x) for x in array1])
    set2 = set([tuple(x) for x in array2])
    return np.array(list(set1-set2))