Skip to content

Commit aad0cd8

Browse files
committed
FEAT: Adding support for arrays to save to and read from disk
1 parent 2c79746 commit aad0cd8

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

arrayfire/array.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,4 +1070,69 @@ def display(a, precision=4):
10701070
safe_call(backend.get().af_print_array_gen(name.encode('utf-8'),
10711071
a.arr, ct.c_int(precision)))
10721072

1073+
def save_array(key, a, filename, append=False):
1074+
"""
1075+
Save an array to disk.
1076+
1077+
Parameters
1078+
----------
1079+
key : str
1080+
A name / key associated with the array
1081+
1082+
a : af.Array
1083+
The array to be stored to disk
1084+
1085+
filename : str
1086+
Location of the data file.
1087+
1088+
append : Boolean. optional. default: False.
1089+
If the file already exists, specifies if the data should be appended or overwritten.
1090+
1091+
Returns
1092+
---------
1093+
index : int
1094+
The index of the array stored in the file.
1095+
"""
1096+
index = ct.c_int(-1)
1097+
safe_call(backend.get().af_save_array(ct.pointer(index),
1098+
key.encode('utf-8'),
1099+
a.arr,
1100+
filename.encode('utf-8'),
1101+
append))
1102+
return index.value
1103+
1104+
def read_array(filename, index=None, key=None):
1105+
"""
1106+
Read an array from disk.
1107+
1108+
Parameters
1109+
----------
1110+
1111+
filename : str
1112+
Location of the data file.
1113+
1114+
index : int. Optional. Default: None.
1115+
- The index of the array stored in the file.
1116+
- If None, key is used.
1117+
1118+
key : str. Optional. Default: None.
1119+
- A name / key associated with the array
1120+
- If None, index is used.
1121+
1122+
Returns
1123+
---------
1124+
"""
1125+
assert((index is not None) or (key is not None))
1126+
out = Array()
1127+
if (index is not None):
1128+
safe_call(backend.get().af_read_array_index(ct.pointer(out.arr),
1129+
filename.encode('utf-8'),
1130+
index))
1131+
elif (key is not None):
1132+
safe_call(backend.get().af_read_array_key(ct.pointer(out.arr),
1133+
filename.encode('utf-8'),
1134+
key.encode('utf-8')))
1135+
1136+
return out
1137+
10731138
from .algorithm import sum

0 commit comments

Comments
 (0)