<commit_before>import numpy as np
from math import sqrt

from blaze.rts.ffi import PythonFn, install, lift
from blaze.engine import executors
from numexpr import evaluate

# evaluating this function over a term has a cost, in the future this
# might be things like calculations for FLOPs associated with the
# operation. Could be a function of the shape of the term Most of the
# costs of BLAS functions can calculated a priori

zerocost = lambda term: 0

#------------------------------------------------------------------------
# Preinstalled Functions
#------------------------------------------------------------------------

# These are side-effectful functions which install the core
# functions into the RTS dispatcher.

# The signature for PythonF

#   :signature: ATerm pattern matching signature

#   :fn: Python callable instance

#   :mayblock: Whether calling this function may block a thread.
#              ( i.e. it waits on a disk or socket )


# TODO: right now these consume everything but later we'll add functions
# which specialize on metadata for contigious, chunked, streams,
# etc...

@lift('Sqrt(<int>)')
def pyadd(a):
    return sqrt(a)

install(
    'Add(<term>,<term>)',
    PythonFn(np.add.types, np.add, False),
    zerocost
)

install(
    'Mul(<term>,<term>)',
    PythonFn(np.multiply.types, np.multiply, False),
    zerocost
)

install(
    'Pow(<term>,<term>)',
    PythonFn(np.power.types, np.power, False),
    zerocost
)

install(
    'Abs(<term>)',
    PythonFn(np.abs.types, np.abs, False),
    zerocost
)

# ==============
# --- Future ---
# ==============

# Specialized just for arrays
# ---------------------------
#
# install(
#     'Add(Array(),Array())',
#     PythonF(np.add.types, np.add, False),
#     zerocost
# )

# Specialized just for contigious arrays
# --------------------------------------
#
# install(
#     'Add(Array(){contigious},Array(){contigious})',
#     PythonF(np.add.types, np.add, False),
#     zerocost
# )

# These also needn't neccessarily be NumPy functions!

numexpr_add = lambda a,b: evaluate('a+b')

# install(
#     'Add(a,b);*',
#     PythonF(np.add.types, numexpr_add, False),
#     zerocost
# )
<commit_msg>Life ucr-dtw into Blaze runtime.<commit_after>import numpy as np
from math import sqrt

from blaze.rts.ffi import PythonFn, install, lift
from blaze.engine import executors
from numexpr import evaluate
from blaze.ts.ucr_dtw import ucr

# evaluating this function over a term has a cost, in the future this
# might be things like calculations for FLOPs associated with the
# operation. Could be a function of the shape of the term Most of the
# costs of BLAS functions can calculated a priori

zerocost = lambda term: 0

#------------------------------------------------------------------------
# Preinstalled Functions
#------------------------------------------------------------------------

# These are side-effectful functions which install the core
# functions into the RTS dispatcher.

# The signature for PythonF

#   :signature: ATerm pattern matching signature

#   :fn: Python callable instance

#   :mayblock: Whether calling this function may block a thread.
#              ( i.e. it waits on a disk or socket )


# TODO: right now these consume everything but later we'll add functions
# which specialize on metadata for contigious, chunked, streams,
# etc...

@lift('Sqrt(<int>)')
def pyadd(a):
    return sqrt(a)

@lift('dtw(Array(), Array(), <term>, <term>)')
def dtw(a):
    return ucr.dtw(a)

install(
    'Add(<term>,<term>)',
    PythonFn(np.add.types, np.add, False),
    zerocost
)

install(
    'Mul(<term>,<term>)',
    PythonFn(np.multiply.types, np.multiply, False),
    zerocost
)

install(
    'Pow(<term>,<term>)',
    PythonFn(np.power.types, np.power, False),
    zerocost
)

install(
    'Abs(<term>)',
    PythonFn(np.abs.types, np.abs, False),
    zerocost
)

# ==============
# --- Future ---
# ==============

# Specialized just for arrays
# ---------------------------
#
# install(
#     'Add(Array(),Array())',
#     PythonF(np.add.types, np.add, False),
#     zerocost
# )

# Specialized just for contigious arrays
# --------------------------------------
#
# install(
#     'Add(Array(){contigious},Array(){contigious})',
#     PythonF(np.add.types, np.add, False),
#     zerocost
# )

# These also needn't neccessarily be NumPy functions!

numexpr_add = lambda a,b: evaluate('a+b')

# install(
#     'Add(a,b);*',
#     PythonF(np.add.types, numexpr_add, False),
#     zerocost
# )

{'fraction_non_alphanumeric': 0.21897163120567376, 'fraction_numerical': 0.0004432624113475177, 'mean_word_length': 2.45823754789272, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1487607', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1357, 'n_words': 424}