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

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

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

from itertools import product 

import numpy as np 

 

 

def _obj2str(a, none_char='-'): 

""" Casts object a to str. """ 

if isinstance(a, float): 

# if the float is 2.49999999 then round 

9 ↛ 10line 9 didn't jump to line 10, because the condition on line 9 was never true if str(a)[::-1].find('.') > 5: 

return '{:.5f}'.format(a) 

elif a is None: 

return none_char 

return str(a) 

 

 

_array2str = np.vectorize(_obj2str) 

 

 

def print_table(matrix, sum_=False): 

""" Prints matrix data in a nice table format. 

 

The matrix element matrix[i][j] should correspond to information about 

order j+2 and n-body i+1. 

 

Example 

-------- 

>> matrix = numpy.array([[None, None], [4.0, 3.0]]) 

>> print_table(matrix) 

 

body/order | 2 | 3 

------------------------ 

1 | - | - 

2 | 4.0 | 3.0 

 

Parameters 

---------- 

matrix : numpy.ndarray 

matrix to be printed 

sum: : bool 

whether or not to print the sum along each row and column 

""" 

table_str = table_array_to_string(matrix, sum_) 

print(table_str) 

 

 

def table_array_to_string(matrix, sum_=False): 

""" Generate nice table string from a numpy array with floats/ints """ 

table_array = _generate_table_array(matrix, sum_) 

table_array_str = _array2str(table_array) 

table_str = _generate_table_str(table_array_str) 

return table_str 

 

 

def _generate_table_array(table_array, sum_=False): 

""" Generate table in numpy array format """ 

 

# initialze table 

n_rows, n_cols = table_array.shape 

A = _build_table_frame(order=n_cols+1, nbody=n_rows, sum_=sum_) 

 

# fill table 

for order, nbody in product(range(2, n_cols+2), range(1, n_rows+1)): 

if nbody <= order: 

A[nbody, order-1] = table_array[nbody-1, order-2] 

 

if sum_: 

for i, row in enumerate(A[1:-1, 1:-1], start=1): 

A[i, -1] = sum(val for val in row if val is not None) 

for i, col in enumerate(A[1:-1, 1:-1].T, start=1): 

A[-1, i] = sum(val for val in col if val is not None) 

A[-1, -1] = '' 

 

return A 

 

 

def _generate_table_str(table_array): 

""" Generate a string from a numpy array of strings """ 

table_str = [] 

n_rows, n_cols = table_array.shape 

 

# find maximum widths for each column 

widths = [] 

for i in range(n_cols): 

widths.append(max(len(val) for val in table_array[:, i])+2) 

 

# formatting str for each row 

row_format = '|'.join('{:^'+str(width)+'}' for width in widths) 

 

# finalize 

for i in range(n_rows): 

if i == 1: 

table_str.append('-' * (sum(widths)+n_cols-1)) 

table_str.append(row_format.format(*table_array[i, :])) 

table_str = '\n'.join(table_str) 

return table_str 

 

 

def _build_table_frame(order, nbody, sum_=False): 

""" Builds/initializes table/array. """ 

if sum_: 

A = np.empty((nbody+2, order+1), dtype='object') 

A[0, -1] = 'sum' 

A[-1, 0] = 'sum' 

else: 

A = np.empty((nbody+1, order), dtype='object') 

 

A[0][0] = 'body/order' 

A[0, 1:order] = range(2, order+1) 

A[1:nbody+1, 0] = range(1, nbody+1) 

return A 

 

 

if __name__ == "__main__": 

# input dummy cutoff table 

# insert row for nbody=1 

cutoffs = np.array([[None, None, None, None, None], 

[6.0, 6.0, 6.0, 3.7, 3.7], 

[5.0, 5.0, 5.0, 3.0, 3.0], 

[3.7, 3.7, 3.7, 0.0, 0.0]]) 

 

# input dummy cluster count table 

cluster_counts = np.array([[1, 3, 5, 5, 2], 

[12, 22, 39, 42, 58], 

[19, 41, 123, 421, 912], 

[42, 112, 410, 617, 3271]]) 

 

print_table(cutoffs) 

print('\n') 

print_table(cluster_counts, sum_=False) 

print('\n') 

print_table(cluster_counts, sum_=True)