Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""Compatibility functions for numpy versions in lib 

2 

3np_new_unique 

4------------- 

5Optionally provides the count of the number of occurrences of each 

6unique element. 

7 

8Copied from Numpy source, under license: 

9 

10Copyright (c) 2005-2015, NumPy Developers. 

11All rights reserved. 

12 

13Redistribution and use in source and binary forms, with or without 

14modification, are permitted provided that the following conditions are 

15met: 

16 

17* Redistributions of source code must retain the above copyright 

18 notice, this list of conditions and the following disclaimer. 

19 

20* Redistributions in binary form must reproduce the above 

21 copyright notice, this list of conditions and the following 

22 disclaimer in the documentation and/or other materials provided 

23 with the distribution. 

24 

25* Neither the name of the NumPy Developers nor the names of any 

26 contributors may be used to endorse or promote products derived 

27 from this software without specific prior written permission. 

28 

29THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 

30"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 

31LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 

32A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 

33OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 

34SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 

35LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 

36DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 

37THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 

38(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 

39OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 

40""" 

41 

42from distutils.version import LooseVersion 

43 

44import numpy as np 

45 

46NP_LT_114 = LooseVersion(np.__version__) < LooseVersion('1.14') 

47 

48np_matrix_rank = np.linalg.matrix_rank 

49np_new_unique = np.unique 

50 

51 

52def recarray_select(recarray, fields): 

53 """" 

54 Work-around for changes in NumPy 1.13 that return views for recarray 

55 multiple column selection 

56 """ 

57 from pandas import DataFrame 

58 fields = [fields] if not isinstance(fields, (tuple, list)) else fields 

59 if len(fields) == len(recarray.dtype): 

60 selection = recarray 

61 else: 

62 recarray = DataFrame.from_records(recarray) 

63 selection = recarray[fields].to_records(index=False) 

64 

65 return selection 

66 

67 

68def lstsq(a, b, rcond=None): 

69 """ 

70 Shim that allows modern rcond setting with backward compat for NumPY 

71 earlier than 1.14 

72 """ 

73 if NP_LT_114 and rcond is None: 

74 rcond = -1 

75 return np.linalg.lstsq(a, b, rcond=rcond)