1+ from numpy .typing import ArrayLike # For type hints
2+ from typing import Any
3+
14import numpy as np
25from ._libmoocore import ffi
36
47
5- def get1_and_is_copied (x , x_ ) :
8+ def get1_and_is_copied (x : Any , x_ : Any ) -> tuple [ Any , bool ] :
69 x_copied = id (x ) != id (x_ )
710 return x , x_copied
811
912
10- def asarray_maybe_copy (x , dtype = float ):
13+ def asarray_maybe_copy (
14+ x : ArrayLike , dtype : type = float
15+ ) -> tuple [np .ndarray , bool ]:
1116 """Convert to numpy array of dtype=float and detect copies."""
1217 return get1_and_is_copied (np .asarray (x , dtype = dtype ), x )
1318
1419
15- def unique_nosort (array , ** kwargs ) :
20+ def unique_nosort (array : ArrayLike , ** kwargs : Any ) -> np . ndarray :
1621 """Return unique values without sorting them.
1722
1823 See https://github.com/numpy/numpy/issues/7265
@@ -22,7 +27,9 @@ def unique_nosort(array, **kwargs):
2227 return uniq [index .argsort ()]
2328
2429
25- def np2d_to_double_array (x , ctype_shape = ("int" , "int" )):
30+ def np2d_to_double_array (
31+ x : ArrayLike , ctype_shape : tuple [str , str ] = ("int" , "int" )
32+ ) -> tuple [ffi .CData , ffi .CData , ffi .CData ]:
2633 nrows = ffi .cast (ctype_shape [0 ], x .shape [0 ])
2734 ncols = ffi .cast (ctype_shape [1 ], x .shape [1 ])
2835 # FIXME: This may cause an unexpected copy. Make this an assert and force
@@ -32,23 +39,29 @@ def np2d_to_double_array(x, ctype_shape=("int", "int")):
3239 return x , nrows , ncols
3340
3441
35- def np1d_to_c_array (x , ctype_data , ctype_size ):
42+ def np1d_to_c_array (
43+ x : ArrayLike , ctype_data : str , ctype_size : str
44+ ) -> tuple [ffi .CData , ffi .CData ]:
3645 size = ffi .cast (ctype_size , x .shape [0 ])
3746 ctype_dtype = np .intc () if ctype_data == "int" else None
3847 x = np .ascontiguousarray (x , dtype = ctype_dtype )
3948 x = ffi .from_buffer (ctype_data + "[]" , x )
4049 return x , size
4150
4251
43- def np1d_to_double_array (x , ctype_size = "int" ):
52+ def np1d_to_double_array (
53+ x : ArrayLike , ctype_size : str = "int"
54+ ) -> tuple [ffi .CData , ffi .CData ]:
4455 return np1d_to_c_array (x , ctype_data = "double" , ctype_size = ctype_size )
4556
4657
47- def np1d_to_int_array (x , ctype_size = "int" ):
58+ def np1d_to_int_array (
59+ x : ArrayLike , ctype_size : str = "int"
60+ ) -> tuple [ffi .CData , ffi .CData ]:
4861 return np1d_to_c_array (x , ctype_data = "int" , ctype_size = ctype_size )
4962
5063
51- def atleast_1d_of_length_n (x , n ) :
64+ def atleast_1d_of_length_n (x : ArrayLike , n : int ) -> np . ndarray :
5265 x = np .atleast_1d (x )
5366 if len (x ) == 1 :
5467 return np .full ((n ), x [0 ])
@@ -59,7 +72,7 @@ def atleast_1d_of_length_n(x, n):
5972 )
6073
6174
62- def is_integer_value (n ) :
75+ def is_integer_value (n : Any ) -> bool :
6376 if isinstance (n , int ):
6477 return True
6578 if n is None :
@@ -73,7 +86,7 @@ def is_integer_value(n):
7386 return False
7487
7588
76- def _get_seed_for_c (seed ) :
89+ def _get_seed_for_c (seed : Any ) -> ffi . CData :
7790 if not is_integer_value (seed ):
7891 seed = np .random .default_rng (seed ).integers (2 ** 32 - 2 , dtype = np .uint32 )
7992 return ffi .cast ("uint32_t" , seed )
0 commit comments