def _create_md(obj): ## determine whether a vector of matrix type shape = CLRUtils.shapeFor(obj) if shape is None: raise Exception("CLR: could not serialize type: %s" % str(obj.__class__)) ## try 1D & 2D etype = CLRUtils.elementTypeFor(obj) create = _MD.get(etype, None) if create is not None: return create(shape, obj) else: raise Exception( "CLR: could not serialize array with element type: %s" % str(etype))
def deserialize (self, sock: BinarySocketReader): """ Deserialize message (called after magic & type read) """ super().deserialize(sock) Ridx = CLRUtils.deserializeIndex(sock) Cidx = CLRUtils.deserializeIndex(sock) rows = sock.readInt32() cols = sock.readInt32() rdata = sock.readFloat64Array (len = rows*cols) mat = rdata.reshape(rows, cols, order='F') if Ridx is not None or Cidx is not None: self.value = pd.DataFrame (mat, index=Ridx, columns=Cidx) else: self.value = mat
def serialize(self, sock: BinarySocketWriter): """ Serialize message """ super().serialize(sock) vec = self.value vtype = vec.__class__ ## data-frame has possible string index if vtype == pd.DataFrame: CLRUtils.serializeIndex(vec.index, sock) vec = toRowVector(vec.values) sock.writeFloat64Array(vec) else: sock.writeInt32(0) vec = toRowVector(vec) sock.writeFloat64Array(vec)
def deserialize(self, sock: BinarySocketReader): """ Deserialize message (called after magic & type read) """ super().deserialize(sock) idx = CLRUtils.deserializeIndex(sock) data = sock.readFloat64Array() if idx is not None: self.value = pd.DataFrame(data, index=idx) else: self.value = data
def serialize (self, sock: BinarySocketWriter): """ Serialize message """ super().serialize(sock) mat = self.value shape = mat.shape ## data-frame has possible string index if mat.__class__ == pd.DataFrame: CLRUtils.serializeIndex (mat.index, sock) CLRUtils.serializeIndex (mat.columns, sock) sock.writeInt32(shape[0]) sock.writeInt32(shape[1]) data = mat.as_matrix().flatten('F') sock.writeFloat64Array(data, includelen=False) else: sock.writeInt32(0) sock.writeInt32(0) sock.writeInt32(shape[0]) sock.writeInt32(shape[1]) data = mat.flatten('F') sock.writeFloat64Array(data, includelen=False)