1
1
import itertools
2
+ import re
2
3
from collections import namedtuple
3
4
4
5
from .core .pycompat import dask_array_type
5
6
6
7
8
+ # see http://docs.scipy.org/doc/numpy/reference/c-api.generalized-ufuncs.html
9
+ DIMENSION_NAME = r'\w+'
10
+ CORE_DIMENSION_LIST = '(?:' + DIMENSION_NAME + '(?:,' + DIMENSION_NAME + ')*)?'
11
+ ARGUMENT = r'\(' + CORE_DIMENSION_LIST + r'\)'
12
+ ARGUMENT_LIST = ARGUMENT + '(?:,' + ARGUMENT + ')*'
13
+ SIGNATURE = '^' + ARGUMENT_LIST + '->' + ARGUMENT_LIST + '$'
14
+
15
+
7
16
class Signature (object ):
8
17
"""Core dimensions signature for a given function
9
18
@@ -12,9 +21,9 @@ class Signature(object):
12
21
Attributes
13
22
----------
14
23
input_core_dims : list of tuples
15
- A list of tuples of dimension names expected on each input variable.
24
+ A list of tuples of core dimension names on each input variable.
16
25
output_core_dims : list of tuples
17
- A list of tuples of dimension names expected on each output variable.
26
+ A list of tuples of core dimension names on each output variable.
18
27
"""
19
28
def __init__ (self , input_core_dims , output_core_dims = ((),)):
20
29
if dtypes is None :
@@ -40,7 +49,18 @@ def all_output_core_dims(self):
40
49
41
50
@classmethod
42
51
def from_string (cls , string ):
43
- raise NotImplementedError
52
+ """Create a Signature object from a NumPy gufunc signature.
53
+
54
+ Parameters
55
+ ----------
56
+ string : str
57
+ Signature string, e.g., (m,n),(n,p)->(m,p).
58
+ """
59
+ if not re .match (SIGNATURE , string ):
60
+ raise ValueError ('not a valid gufunc signature: {}' .format (string ))
61
+ return cls (* [[re .findall (DIMENSION_NAME , arg )
62
+ for arg in re .findall (ARGUMENT , arg_list )]
63
+ for arg_list in string .split ('->' )])
44
64
45
65
@classmethod
46
66
def from_ufunc (cls , ufunc ):
0 commit comments