|
11 | 11 | from larray.core.group import Group
|
12 | 12 | from larray.core.axis import Axis
|
13 | 13 | from larray.core.array import LArray, get_axes, ndtest, zeros, zeros_like, sequence, aslarray
|
14 |
| -from larray.util.misc import float_error_handler_factory, is_interactive_interpreter, renamed_to, inverseop |
| 14 | +from larray.util.misc import float_error_handler_factory, is_interactive_interpreter, renamed_to, inverseop, basestring |
15 | 15 | from larray.inout.session import ext_default_engine, get_file_handler
|
16 | 16 |
|
17 | 17 |
|
@@ -929,7 +929,7 @@ def transpose(self, *args):
|
929 | 929 | >>> arr2 = ndtest((2, 2))
|
930 | 930 | >>> sess = Session([('arr1', arr1), ('arr2', arr2)])
|
931 | 931 | >>> def print_summary(s):
|
932 |
| - ... print(s.summary("{name} -> {axes_names}")) |
| 932 | + ... print(s.summary({LArray: "{key} -> {axes_names}"})) |
933 | 933 | >>> print_summary(sess)
|
934 | 934 | arr1 -> a, b, c
|
935 | 935 | arr2 -> a, b
|
@@ -1057,44 +1057,100 @@ def apply(self, func, *args, **kwargs):
|
1057 | 1057 |
|
1058 | 1058 | def summary(self, template=None):
|
1059 | 1059 | """
|
1060 |
| - Returns a summary of the content of the session (arrays only). |
| 1060 | + Returns a summary of the content of the session. |
1061 | 1061 |
|
1062 | 1062 | Parameters
|
1063 | 1063 | ----------
|
1064 |
| - template: str |
1065 |
| - Template describing how items are summarized (see examples). |
1066 |
| - Available arguments are 'name', 'axes_names' and 'title' |
| 1064 | + template: dict {object type: str} or dict {object type: func} |
| 1065 | + Template describing how items are summarized. |
| 1066 | + For each object type, it is possible to provide either a string template or a function taking the |
| 1067 | + the key and value of a session item as parameters and returning a string (see examples). |
| 1068 | + A string template contains specific arguments written inside brackets {}. |
| 1069 | + Available arguments are: |
| 1070 | +
|
| 1071 | + - for groups: 'key', 'name', 'axis_name', 'labels' and 'length', |
| 1072 | + - for axes: 'key', 'name', 'labels' and 'length', |
| 1073 | + - for arrays: 'key', 'axes_names', 'shape', 'dtype' and 'title', |
| 1074 | + - for all other types: 'key', 'value'. |
1067 | 1075 |
|
1068 | 1076 | Returns
|
1069 | 1077 | -------
|
1070 | 1078 | str
|
1071 |
| - Short representation of the content of the session (arrays only). |
| 1079 | + Short representation of the content of the session. |
1072 | 1080 | .
|
1073 | 1081 | Examples
|
1074 | 1082 | --------
|
1075 |
| - >>> arr1 = ndtest((2, 2), title='array 1') |
1076 |
| - >>> arr2 = ndtest(4, title='array 2') |
1077 |
| - >>> arr3 = ndtest((3, 2), title='array 3') |
1078 |
| - >>> s = Session([('arr1', arr1), ('arr2', arr2), ('arr3', arr3)]) |
| 1083 | + >>> axis1 = Axis("a=a0..a2") |
| 1084 | + >>> group1 = axis1['a0,a1'] >> 'a01' |
| 1085 | + >>> arr1 = ndtest((2, 2), title='array 1', dtype=np.int64) |
| 1086 | + >>> arr2 = ndtest(4, title='array 2', dtype=np.int64) |
| 1087 | + >>> arr3 = ndtest((3, 2), title='array 3', dtype=np.int64) |
| 1088 | + >>> s = Session([('axis1', axis1), ('group1', group1), ('arr1', arr1), ('arr2', arr2), ('arr3', arr3)]) |
| 1089 | +
|
| 1090 | + Default template |
| 1091 | +
|
1079 | 1092 | >>> print(s.summary()) # doctest: +NORMALIZE_WHITESPACE
|
1080 |
| - arr1: a, b |
| 1093 | + axis1: a ['a0' 'a1' 'a2'] (3) |
| 1094 | + group1: a['a0', 'a1'] >> a01 (2) |
| 1095 | + arr1: a, b (2 x 2) [int64] |
1081 | 1096 | array 1
|
1082 |
| - arr2: a |
| 1097 | + arr2: a (4) [int64] |
1083 | 1098 | array 2
|
1084 |
| - arr3: a, b |
| 1099 | + arr3: a, b (3 x 2) [int64] |
1085 | 1100 | array 3
|
1086 |
| - >>> print(s.summary("{name} -> {axes_names}")) |
1087 |
| - arr1 -> a, b |
1088 |
| - arr2 -> a |
1089 |
| - arr3 -> a, b |
| 1101 | +
|
| 1102 | + Using a specific template |
| 1103 | +
|
| 1104 | + >>> def print_array(key, array): |
| 1105 | + ... axes_names = ', '.join(array.axes.display_names) |
| 1106 | + ... shape = ' x '.join(str(i) for i in array.shape) |
| 1107 | + ... return "{} -> {} ({})\\n title = {}\\n dtype = {}".format(key, axes_names, shape, |
| 1108 | + ... array.title, array.dtype) |
| 1109 | + >>> template = {Axis: "{key} -> {name} [{labels}] ({length})", |
| 1110 | + ... Group: "{key} -> {name}: {axis_name}{labels} ({length})", |
| 1111 | + ... LArray: print_array} |
| 1112 | + >>> print(s.summary(template)) |
| 1113 | + axis1 -> a ['a0' 'a1' 'a2'] (3) |
| 1114 | + group1 -> a01: a['a0', 'a1'] (2) |
| 1115 | + arr1 -> a, b (2 x 2) |
| 1116 | + title = array 1 |
| 1117 | + dtype = int64 |
| 1118 | + arr2 -> a (4) |
| 1119 | + title = array 2 |
| 1120 | + dtype = int64 |
| 1121 | + arr3 -> a, b (3 x 2) |
| 1122 | + title = array 3 |
| 1123 | + dtype = int64 |
1090 | 1124 | """
|
1091 | 1125 | if template is None:
|
1092 |
| - template = "{name}: {axes_names}\n {title}\n" |
1093 |
| - templ_kwargs = [{'name': k, |
1094 |
| - 'axes_names': ', '.join(v.axes.display_names), |
1095 |
| - 'title': v.title} |
1096 |
| - for k, v in self.items() if isinstance(v, LArray)] |
1097 |
| - return '\n'.join(template.format(**kwargs) for kwargs in templ_kwargs) |
| 1126 | + template = {} |
| 1127 | + if Axis not in template: |
| 1128 | + template[Axis] = "{key}: {name} [{labels}] ({length})" |
| 1129 | + if Group not in template: |
| 1130 | + template[Group] = "{key}: {axis_name}{labels} >> {name} ({length})" |
| 1131 | + if LArray not in template: |
| 1132 | + template[LArray] = "{key}: {axes_names} ({shape}) [{dtype}]\n {title}" |
| 1133 | + |
| 1134 | + def display(k, v): |
| 1135 | + t = Group if isinstance(v, Group) else type(v) |
| 1136 | + tmpl = template.get(t, "{key}: {value}") |
| 1137 | + if not (isinstance(tmpl, basestring) or callable(tmpl)): |
| 1138 | + raise TypeError("Expected a string template or a function for type {}. " |
| 1139 | + "Got {}".format(type(v), type(tmpl))) |
| 1140 | + if isinstance(tmpl, basestring): |
| 1141 | + if isinstance(v, Axis): |
| 1142 | + return tmpl.format(key=k, name=v.name, labels=v.labels_summary(), length=len(v)) |
| 1143 | + elif isinstance(v, Group): |
| 1144 | + return tmpl.format(key=k, name=v.name, axis_name=v.axis.name, labels=v.key, length=len(v)) |
| 1145 | + elif isinstance(v, LArray): |
| 1146 | + return tmpl.format(key=k, axes_names=', '.join(v.axes.display_names), |
| 1147 | + shape=' x '.join(str(i) for i in v.shape), title=v.title, dtype=v.dtype) |
| 1148 | + else: |
| 1149 | + return tmpl.format(key=k, value=str(v)) |
| 1150 | + else: |
| 1151 | + return tmpl(k, v) |
| 1152 | + |
| 1153 | + return '\n'.join(display(k, v) for k, v in self.items()) |
1098 | 1154 |
|
1099 | 1155 |
|
1100 | 1156 | def _exclude_private_vars(vars_dict):
|
|
0 commit comments