File: NumPyUtil.py

package info (click to toggle)
uranium 5.0.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,304 kB
  • sloc: python: 31,765; sh: 132; makefile: 12
file content (30 lines) | stat: -rw-r--r-- 830 bytes parent folder | download | duplicates (2)
1
2
3
4
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
# Copyright (c) 2016 Ultimaker B.V.
# Uranium is released under the terms of the LGPLv3 or higher.
from typing import Union, List, cast

import numpy
from copy import deepcopy


def immutableNDArray(nda: Union[List, numpy.ndarray]) -> numpy.ndarray:
    """Creates an immutable copy of the given narray

    If the array is already immutable then it just returns it.
    :param nda: :type{numpy.ndarray} the array to copy. May be a list
    :return: :type{numpy.ndarray} an immutable narray
    """

    if nda is None:
        return None

    if type(nda) is list:
        data = numpy.array(nda, numpy.float32)
        data.flags.writeable = False
    else:
        data = cast(numpy.ndarray, nda)
    if not data.flags.writeable:
        return data

    copy = deepcopy(data)
    copy.flags.writeable = False
    return copy