Skip to content

Commit 7a110db

Browse files
author
Fedor Baart
committed
fix missings and empty array
1 parent 622b89f commit 7a110db

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

mmi/__init__.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,37 @@
66

77
def send_array(socket, A, flags=0, copy=False, track=False, metadata=None):
88
"""send a numpy array with metadata over zmq"""
9-
md = dict(
10-
dtype=str(A.dtype),
11-
shape=A.shape,
12-
timestamp=datetime.datetime.now().isoformat()
13-
)
9+
10+
# create a metadata dictionary for the message
11+
md = {}
12+
# always add a timestamp
13+
md['timestamp'] = datetime.datetime.now().isoformat()
14+
15+
# copy extra metadata
16+
if metadata:
17+
md.update(metadata)
18+
19+
# if we don't have an array
20+
if A is None:
21+
# send only json
22+
socket.send_json(md, flags)
23+
# and we're done
24+
return
25+
26+
# add array metadata
27+
md['dtype'] = str(A.dtype)
28+
md['shape'] = A.shape
1429
try:
30+
# If an array has a fill value assume it's an array with missings
31+
# store the fill_Value in the metadata and fill the array before sending.
1532
md['fill_value'] = A.fill_value
1633
A = A.filled()
1734
except AttributeError:
1835
# no masked array, nothing to do
1936
pass
2037

21-
if metadata:
22-
md.update(metadata)
23-
if A is None:
24-
# send only json
25-
socket.send_json(md, flags)
26-
else:
27-
# send json, followed by array
28-
socket.send_json(md, flags | zmq.SNDMORE)
38+
# send json, followed by array
39+
socket.send_json(md, flags | zmq.SNDMORE)
2940
# Make a copy if required and pass along the buffer
3041
msg = buffer(np.ascontiguousarray(A))
3142
socket.send(msg, flags, copy=copy, track=track)

tests/test_simple.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,18 @@ def test_sndrcv(self):
1818
mmi.send_array(req, A)
1919
B, metadata = mmi.recv_array(rep)
2020
numpy.testing.assert_array_equal(A, B)
21-
21+
def test_metadata_only(self):
22+
"""send a message with only metadata"""
23+
req = ctx.socket(zmq.REQ)
24+
req.connect('tcp://localhost:9002')
25+
rep = ctx.socket(zmq.REP)
26+
rep.bind('tcp://*:9002')
27+
mmi.send_array(req, A=None)
28+
_, metadata = mmi.recv_array(rep)
29+
self.assertTrue('timestamp' in metadata)
2230

2331
def test_missing(self):
32+
"""send an array with missing data"""
2433
A = np.array([1, 2, 3, 4])
2534
A = np.ma.masked_less(A, 2)
2635
req = ctx.socket(zmq.REQ)

0 commit comments

Comments
 (0)