Send data, e.g. {key: np.ndarray}, with metadata
496278
def zmq_send_data(socket, data, flags=0, copy=True, track=False):
    """"""

    header, payload = [], []

    for key in sorted(data.keys()):
        arr = data[key]

        if not isinstance(arr, np.ndarray):
            raise DataError('Only ndarray types can be serialized')

        header.append(dict(dtype=str(arr.dtype),
                           shape=arr.shape,
                           key=key,
                           aligned=arr.flags['ALIGNED']))
        # Force contiguity
        payload.append(arr)

    # Send the header
    msg = [json.dumps(header).encode('ascii')]
    msg.extend(payload)

    return socket.send_multipart(msg, flags, copy=copy, track=track)
1020421
def set_data(self, data):
        ''' 
        '''
        data = array.array('B', struct.unpack("{}B".format(len(data) * 4), struct.pack("{}I".format(len(data)), *data)))
        self._intf._send_tcp_data(data)
84520
def copy_meta_data(hdr_to, hdr_from):
    
    """
    warnings.warn('copy_meta_data() is depreciated and may be removed in future versions', category=DeprecationWarning)
    logger = Logger.getInstance()
    try:
        set_pixel_spacing(hdr_to, get_pixel_spacing(hdr_from))
    except AttributeError as e:
        logger.warning('The voxel spacing could not be set correctly. Signaled error: {}'.format(e))
    try:
        set_offset(hdr_to, get_offset(hdr_from))
    except AttributeError as e:
        logger.warning('The image offset could not be set correctly. Signaled error: {}'.format(e))
951605
def _set_metadata(self, value):
        '''
        
        '''
        if value is None:
            setattr(self, '_PMMail__metadata', {})
        elif isinstance(value, dict):
            for k, v in value.items():
                if (not isinstance(k, str) and not isinstance(k, int)) \
                    or (not isinstance(v, str) and not isinstance(v, int)):
                    raise TypeError('Metadata keys and values can only be strings or integers')
            setattr(self, '_PMMail__metadata', value)
        else:
            raise TypeError('Metadata must be a dictionary of key-value pairs')
43733
def serialize_ndarray_readable(o):
    
    return dict(
        _type='np.ndarray',
        dtype=o.dtype,
        value=hint_tuples(o.tolist()))
1237150
def send(self, data):
        """"""
        self._send_data(int_to_hex(len(data)))

        self._send_data(data)
218794
def sendto(self, data, addr=None):
        
        if not isinstance(data, (bytes, bytearray, memoryview)):
            raise TypeError("data: expecting a bytes-like instance, got {!r}"
                                .format(type(data).__name__))
        self._check_status()
        if not self._writable:
            raise TransportError('transport is not writable')
        try:
            self._handle.send(addr, data, self._on_send_complete)
        except pyuv.error.UVError as e:
            error = TransportError.from_errno(e.args[0])
            # Try to discern between permanent and transient errors. Permanent
            # errors close the transport. This list is very likely not complete.
            if error.errno != pyuv.errno.UV_EBADF:
                raise error
            self._error = error
            self.abort()
        self._write_buffer_size += 1
        self._maybe_pause_protocol()
769331
def send_bytes(self):
        """"""
        data = b''.join(self.bytes_to_send)
        self.bytes_to_send = []
        return data
253568
async def Save(self, metadata):
        '''
        
        '''
        # map input types to rpc msg
        _params = dict()
        msg = dict(type='ImageMetadata',
                   request='Save',
                   version=2,
                   params=_params)
        _params['metadata'] = metadata
        reply = await self.rpc(msg)
        return reply
637142
def _to_meta_data(pif_obj, dataset_hit, mdf_acl):
    """"""
    pif = pif_obj.as_dictionary()
    dataset = dataset_hit.as_dictionary()
    mdf = {}
    try:
        if pif.get("names"):
            mdf["title"] = pif["names"][0]
        else:
            mdf["title"] = "Citrine PIF " + str(pif["uid"])

        if pif.get("chemicalFormula"):
            mdf["composition"] = pif["chemicalFormula"]
        elif pif.get("composition"):
            mdf["composition"] = ''.join([comp["element"] for comp in pif["composition"] if comp["element"]])
        if not mdf["composition"]:
            mdf.pop("composition")

        mdf["acl"] = mdf_acl
        mdf["source_name"] = _construct_new_key(dataset["name"])

        if pif.get("contacts"):
            mdf["data_contact"] = []
            for contact in pif["contacts"]:
                data_c = {
                    "given_name": contact["name"]["given"],  #REQ
                    "family_name": contact["name"]["family"]  #REQ
                    }
                if contact.get("email"):
                    data_c["email"] = contact.get("email", "")
                if contact.get("orcid"):
                    data_c["orcid"] = contact.get("orcid", "")
                mdf["data_contact"].append(data_c)
            if not mdf["data_contact"]:
                mdf.pop("data_contact")
        
        mdf["data_contributor"] = [{}] 
        if "owner" in dataset:
            name = dataset["owner"].split()
            contributor = {
                "given_name": name[0],
                "family_name": name[1],
                "email": dataset["email"]
            }
            mdf["data_contributor"] = [contributor]

        mdf["links"] = {
            "landing_page": "https://citrination.com/datasets/{}".format(dataset["id"]),
            "publication": []
            }
        if pif.get("references"):
            mdf["author"] = []
            mdf["citation"] = []
            for ref in pif["references"]:
                if ref.get("doi"):
                    mdf["citation"].append(ref["doi"]) #TODO: Make actual citation
                    mdf["links"]["publication"].append(ref["doi"])
                if ref.get("authors"):
                    for author in ref["authors"]:
                        if author.get("given") and author.get("family"):
                            mdf["author"].append({
                                "given_name": author["given"],
                                "family_name": author["family"]
                                })
            # Remove fields if blank
            if not mdf["author"]:
                mdf.pop("author")
            if not mdf["citation"]:
                mdf.pop("citation")
        if not mdf["links"]["publication"]:
            mdf["links"].pop("publication")

        if pif.get("licenses", [{}])[0].get("url"):
            mdf["license"] = pif["licenses"][0]["url"]
        if pif.get("tags"):
            mdf["tags"] = pif["tags"]

    # If required MDF metadata is missing from PIF, abort
    except KeyError as e:
        print("Error: Required MDF metadata", str(e), "not found in PIF", pif["uid"])
        return None

    return mdf
1202246
def add_tensor_as_parameter(pb, tensor):
    """"""
    p = pb.parameter.add()
    p.variable_name = tensor.name
    p.shape.dim.extend(tensor.dims)
    if tensor.data_type == TensorProto.FLOAT:
        # convert raw bytestream to floating points
        if tensor.raw_data:
            p.data.extend(np.fromstring(tensor.raw_data, dtype=np.float32))
        elif len(tensor.float_data) > 0:
            p.data.extend(tensor.float_data)
        else:
            raise ValueError("float data not found for {}".format(tensor.name))
    elif tensor.data_type == TensorProto.INT32:
        # convert raw bytestream to integer
        if tensor.raw_data:
            p.data.extend(np.fromstring(tensor.raw_data, dtype=np.int32))
        elif len(tensor.int32_data) > 0:
            p.data.extend(tensor.int32_data)
        else:
            raise ValueError("int32 data not found for {}".format(tensor.name))
    elif tensor.data_type == TensorProto.INT64:
        # convert raw bytestream to integer
        if tensor.raw_data:
            p.data.extend(np.fromstring(tensor.raw_data, dtype=np.int64))
        elif len(tensor.int64_data) > 0:
            p.data.extend(tensor.int64_data)
        else:
            raise ValueError("int64 data not found for {}".format(tensor.name))
    elif tensor.data_type == TensorProto.BOOL:
        if tensor.raw_data:
            p.data.extend(np.fromstring(tensor.raw_data, dtype=np.bool))
        else:
            raise ValueError("bool data not found for {}".format(tensor.name))

    else:
        raise ValueError("Unsupported tensor data type for {}: {}"
                         .format(tensor.name, tensor.data_type))
    p.need_grad = False
32198
def _send_update(self, data):
        """"""
        if isinstance(data, dict):
            data = json.dumps(data)
        self.update_callback(data)
1112245
def sendVY(self, vy):
        '''
        

        '''
        self.lock.acquire()
        self.data.vy = vy
        self.lock.release()
384669
def buildData(self, key, default=None):
        
        return self._buildData.get(nativestring(key), default)
836910
def canonical_dataarray_to_grib(
        data_var, file, grib_keys={}, default_grib_keys=DEFAULT_GRIB_KEYS, **kwargs
):
    # type: (T.IO[bytes], xr.DataArray, T.Dict[str, T.Any], T.Dict[str, T.Any], T.Any) -> None
    
    # validate Dataset keys, DataArray names, and attr keys/values
    detected_keys, suggested_keys = detect_grib_keys(data_var, default_grib_keys, grib_keys)
    merged_grib_keys = merge_grib_keys(grib_keys, detected_keys, suggested_keys)

    if 'gridType' not in merged_grib_keys:
        raise ValueError("required grib_key 'gridType' not passed nor auto-detected")

    template_message = make_template_message(merged_grib_keys, **kwargs)

    coords_names, data_var = expand_dims(data_var)

    header_coords_values = [data_var.coords[name].values.tolist() for name in coords_names]
    for items in itertools.product(*header_coords_values):
        select = {n: v for n, v in zip(coords_names, items)}
        field_values = data_var.sel(**select).values.flat[:]

        # Missing values handling
        invalid_field_values = np.logical_not(np.isfinite(field_values))

        # There's no need to save a message full of missing values
        if invalid_field_values.all():
            continue

        missing_value = merged_grib_keys.get('missingValue', 9999)
        field_values[invalid_field_values] = missing_value

        message = cfgrib.CfMessage.from_message(template_message)
        for coord_name, coord_value in zip(coords_names, items):
            if coord_name in ALL_TYPE_OF_LEVELS:
                coord_name = 'level'
            message[coord_name] = coord_value

        # OPTIMIZE: convert to list because Message.message_set doesn't support np.ndarray
        message['values'] = field_values.tolist()

        message.write(file)
1263780
def _data(self):
        
        self.wait_to_read()
        hdl = NDArrayHandle()
        check_call(_LIB.MXNDArrayGetDataNDArray(self.handle, ctypes.byref(hdl)))
        return NDArray(hdl)
1231153
def _set_data(self, data):
        """"""
        if type(data) == bytearray:
            self._data = data
        elif type(data) == str:
            if sys.version_info < (3,):
                self._data = bytearray(data)
            else:
                self._data = bytearray(data.encode('ISO-8859-1'))
        elif type(data) == list or type(data) == tuple:
            self._data = bytearray(data)
        elif sys.version_info >= (3,) and type(data) == bytes:
            self._data = bytearray(data)
        else:
            raise Exception('Data must be bytearray, string, list or tuple,'
                            ' not {}'.format(type(data)))