-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpython_interop_demo.exs
More file actions
executable file
·134 lines (109 loc) · 3.7 KB
/
python_interop_demo.exs
File metadata and controls
executable file
·134 lines (109 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env elixir
# Demonstration of ExZarr and zarr-python interoperability
#
# This script shows how arrays created by ExZarr can be read by Python's zarr library,
# and vice versa.
#
# Usage:
# elixir examples/python_interop_demo.exs
Mix.install([{:ex_zarr, path: "."}])
defmodule PythonInteropDemo do
def run do
IO.puts("=== ExZarr - zarr-python Interoperability Demo ===\n")
# Create a temporary directory for the demo
demo_dir = "/tmp/exzarr_python_demo_#{System.unique_integer([:positive])}"
File.mkdir_p!(demo_dir)
# Demo 1: Create with ExZarr, read with Python
demo_exzarr_to_python(demo_dir)
IO.puts("")
# Demo 2: Create with Python, read with ExZarr
demo_python_to_exzarr(demo_dir)
# Cleanup
File.rm_rf!(demo_dir)
IO.puts("\n=== Demo Complete ===")
end
defp demo_exzarr_to_python(demo_dir) do
IO.puts("1. Creating array with ExZarr...")
path = Path.join(demo_dir, "exzarr_array")
# Create a 2D array with ExZarr
{:ok, array} =
ExZarr.create(
shape: {10, 10},
chunks: {5, 5},
dtype: :float64,
compressor: :zlib,
storage: :filesystem,
path: path
)
:ok = ExZarr.save(array, path: path)
IO.puts(" Created #{inspect(array.shape)} array at #{path}")
IO.puts(" Dtype: #{array.dtype}, Compressor: #{array.compressor}")
# Try to read with Python
IO.puts("\n2. Reading with zarr-python...")
case System.cmd("python3", [
"-c",
"""
import zarr
import sys
try:
z = zarr.open_array('#{path}', mode='r')
print(f" Shape: {z.shape}")
print(f" Dtype: {z.dtype}")
print(f" Chunks: {z.chunks}")
sys.exit(0)
except Exception as e:
print(f" Error: {e}")
sys.exit(1)
"""
]) do
{output, 0} ->
IO.puts(output)
IO.puts(" ✓ Python successfully read the ExZarr array!")
{output, _} ->
IO.puts(" ✗ Python failed to read: #{output}")
IO.puts(" (This is expected if zarr-python is not installed)")
end
end
defp demo_python_to_exzarr(demo_dir) do
IO.puts("3. Creating array with zarr-python...")
path = Path.join(demo_dir, "python_array")
# Create with Python
case System.cmd("python3", [
"-c",
"""
import zarr
import numpy as np
import sys
try:
z = zarr.open_array('#{path}', mode='w', shape=(20, 20), chunks=(10, 10), dtype='int32')
z[:, :] = np.arange(400).reshape((20, 20))
print(" Created (20, 20) array with Python")
print(f" Dtype: {z.dtype}")
sys.exit(0)
except Exception as e:
print(f" Error: {e}")
sys.exit(1)
"""
]) do
{output, 0} ->
IO.puts(output)
# Try to read with ExZarr
IO.puts("\n4. Reading with ExZarr...")
case ExZarr.open(path: path) do
{:ok, array} ->
IO.puts(" Shape: #{inspect(array.shape)}")
IO.puts(" Dtype: #{array.dtype}")
IO.puts(" Chunks: #{inspect(array.chunks)}")
IO.puts(" ✓ ExZarr successfully read the Python array!")
{:error, reason} ->
IO.puts(" ✗ Failed to read: #{inspect(reason)}")
end
{output, _} ->
IO.puts(" ✗ Python failed to create array: #{output}")
IO.puts(" (This is expected if zarr-python is not installed)")
IO.puts("\n4. Skipping ExZarr read test")
end
end
end
# Run the demo
PythonInteropDemo.run()