Skip to content

Instantly share code, notes, and snippets.

View FrancescAlted's full-sized avatar

Francesc Alted FrancescAlted

View GitHub Profile
@FrancescAlted
FrancescAlted / Estimating-Pi-ironArray.py
Last active September 12, 2022 08:11
Estimating the value of Pi for an ironArray blog (https://medium.com/p/bd4b08b799a8)
import numpy as np
import iarray as ia
from iarray import udf
import math
# Params for array construction
shape = (40_000, 40_000)
ia.set_config_defaults(dtype=np.float32, fp_mantissa_bits=15)
@udf.scalar()
def circle_filter(val: udf.float32, row: udf.int64, col: udf.int64,
nrows: udf.int64, ncols: udf.int64) -> udf.float32:
x = (2. * row / nrows) - 1.
y = (2. * col / ncols) - 1.
if ((x ** 2 + y ** 2) <= 1) and val >= 0.5:
return 1.
return math.nan
area_circle = ia.nansum(circle)
area_square = ia.nansum(square)
print(f"PI estimate: {4 * area_circle / area_square}")
expr = ia.expr_from_udf(filter_func,
[rand_data],
[shape[0], shape[1], True])
circle = expr.eval()
expr = ia.expr_from_udf(filter_func,
[rand_data],
[shape[0], shape[1], False])
square = expr.eval()
@udf.jit()
def filter_func(out: udf.Array(udf.float32, 2),
vals: udf.Array(udf.float32, 2),
nrows: udf.int64, ncols: udf.int64,
iscircle: udf.bool) -> udf.int32:
n = out.window_shape[0]
m = out.window_shape[1]
row_start = out.window_start[0]
col_start = out.window_start[1]
for i in range(n):
@udf.scalar()
def square_filter(val: udf.float32) -> udf.float32:
if val >= 0.5:
return 1.
return math.nan
import iarray as ia
shape = (40_000, 40_000)
ia.set_config_defaults(dtype=np.float32, fp_mantissa_bits=15)
rand_data = ia.random.random_sample(shape)
@FrancescAlted
FrancescAlted / circle_filter.py
Created September 12, 2022 07:07
Computing π using ironArray
@udf.scalar()
def circle_filter(val: udf.float32, row: udf.int64, col: udf.int64,
nrows: udf.int64, ncols: udf.int64)
-> udf.float32:
x = (2. * row / nrows) - 1.
y = (2. * col / ncols) - 1.
if ((x ** 2 + y ** 2) <= 1) and val >= 0.5:
return 1.
return math.nan
@FrancescAlted
FrancescAlted / read-binary-data.py
Created August 27, 2022 08:26
Benchmark comparing npy, npz, jdb and blosc2 storage formats
# Benchmark comparing npy, npz, jdb and blosc2 storage formats
import sys
import numpy as np
import jdata as jd
import blosc2
from time import time
N = 10_000