Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def func0(a, b):
return a + b
func0_boosted = boost(func0)
A = Union[int, Array[int, "1d", "C"]]
@boost
def func2(a: A, b: float):
return a - func_tmp(b)
A1 = Array[int, "1d", "C", "memview"]
@boost
def func3(c: const(A1)):
return c[0] + 1
ts = Transonic()
def func1(a, b):
n = 10
if ts.is_transpiled:
result = ts.use_block("block0")
else:
def compare(result, dtype, ndim, memview, mem_layout=None, positive_indices=None):
A = Array[dtype, ndim, memview, mem_layout, positive_indices]
assert A.format_as_backend_type(type_formatter) == result
import numpy as np
import transonic as ts
from transonic import Type, NDim, Array, Union
T = Type("T")
T1 = Type("T1")
N = NDim("N")
A = Array[T, N]
A1 = Array[T1, N + 1]
# for coverage
assert repr(N - 1) == "N - 1"
print(repr(A1))
repr(Union[A, A1])
@ts.boost
def compute(a: A, b: A1, c: T, d: A, e: str):
print(e)
tmp = a + b
return tmp
import numpy as np
from transonic import boost, Array
A = Array[float, "1d"]
def fxfy(ft: A, fn: A, theta: A):
sin_theta = np.sin(theta)
cos_theta = np.cos(theta)
fx = cos_theta * ft - sin_theta * fn
fy = sin_theta * ft + cos_theta * fn
return fx, fy
def fxfy_loops(ft: A, fn: A, theta: A):
n0 = theta.size
fx = np.empty_like(ft)
fy = np.empty_like(fn)
for index in range(n0):
sin_theta = np.sin(theta[index])
`reconstruction_loop` originally part of CellProfiler,
code licensed under both GPL and BSD licenses.
Website: http://www.cellprofiler.org
Copyright (c) 2003-2009 Massachusetts Institute of Technology
Copyright (c) 2009-2011 Broad Institute
All rights reserved.
Original author: Lee Kamentsky
"""
import numpy as np
from transonic import boost, Array, const
Au = Array[np.uint32, "1d", "C", "positive_indices"]
A = Array[np.int32, "1d", "C", "positive_indices"]
M = Array[np.int32, "1d", "C", "memview"]
@boost(boundscheck=False)
def reconstruction_loop(
ranks: Au,
prev: A,
next: A,
strides: const(M),
current_idx: np.intp,
image_stride: np.intp,
):
"""The inner loop for reconstruction.
This algorithm uses the rank-order of pixels. If low intensity pixels have
a low rank and high intensity pixels have a high rank, then this loop
import numpy as np
from transonic import boost, Optional, Array
A = Array[np.uint8, "2d", "memview"]
A1d = Array[np.intp, "1d", "memview"]
@boost(wraparound=False, boundscheck=False, cdivision=True, nonecheck=False)
def _dilate(
image: A,
selem: A,
out: Optional[A] = None,
shift_x: np.int8 = 0,
shift_y: np.int8 = 0,
):
"""Return greyscale morphological dilation of an image.
Morphological dilation sets a pixel at (i,j) to the maximum over all pixels
in the neighborhood centered at (i,j). Dilation enlarges bright regions
and shrinks dark regions.
import numpy as np
import foo
from transonic import Transonic, Type, NDim, Array
T = Type(float, complex)
N = NDim(1, 2)
A = Array[T, N]
A1 = Array[T, N + 1]
ts = Transonic()
class MyClass:
def __init__(self, a, b):
self.a = a
self.b = b
def compute(self, n):
a = self.a
b = self.b
if ts.is_transpiled:
Not yet implemented...
Many things can be expressed in Pythran specifications (see
https://pythran.readthedocs.io/en/latest/MANUAL.html#concerning-pythran-specifications),
in particular stride arrays and partial shapes...
We could also express these concepts in strings, mainly following Pythran...
"""
from transonic import boost, Type, NDim, Shape, Array
T = Type(int, float)
# here the shape of the array is only defined with the ShapeVar
A = Array[T, Shape("[3, :]", "[3, :, :]", "[::, ::]", "[::, ::, ::]")]
@boost
def compute(a: A, b: A, c: T):
return a + b
# if there is a NDimVar, we can use the ellipsis
A1 = Array[T, NDim(1, 3), Shape("[3, ...]", "[::, ...]")]
@boost
def compute1(a: A1, b: A1, c: T):
return c * (a + b)
import numpy as np
from transonic import boost, Array
A = Array[float, "3d"]
A1 = Array[float, "1d"]
def expr(a, b):
return np.arctan2(2 * np.exp(a) ** 2 + 4 * np.log(a * b) ** 3, 2 / a)
def broadcast(a: A, b: A1, out: A):
out[:] = expr(a, b)
def broadcast_loops(a: A, b: A1, out: A):
n0, n1, n2 = a.shape
for i0 in range(n0):
for i1 in range(n1):
for i2 in range(n2):
import numpy as np
from transonic import Type, NDim, Array, boost
T = Type(int, np.complex128)
N = NDim(1, 3)
A = Array[T, N]
A1 = Array[np.float32, N + 1]
@boost
def compute(a: A, b: A, c: T, d: A1, e: str):
print(e)
tmp = a + b
return tmp