def connect_matlab(matlab_session, first_connect=False): # pragma: matlab r"""Connect to Matlab engine. Args: matlab_session (str): Name of the Matlab session that should be connected to. first_connect (bool, optional): If True, this is the first time Python is connecting to the Matlab shared engine and certain environment variables should be set. Defaults to False. Returns: MatlabEngine: Matlab engine that was connected. """ matlab_engine = matlab.engine.connect_matlab(matlab_session) matlab_engine.eval('clear classes;', nargout=0) err = backwards.StringIO() try: matlab_engine.eval("YggInterface('YGG_MSG_MAX');", nargout=0, stderr=err) except BaseException: matlab_engine.addpath(_top_dir, nargout=0) matlab_engine.addpath(_incl_interface, nargout=0) matlab_engine.eval("os = py.importlib.import_module('os');", nargout=0) if not first_connect: if backwards.PY2: matlab_engine.eval("py.reload(os);", nargout=0) else: matlab_engine.eval("py.importlib.reload(os);", nargout=0) return matlab_engine
def array_to_table(arrs, fmt_str, use_astropy=False): r"""Serialize an array as an ASCII table. Args: arrs (np.ndarray, list, tuple): Structured array or list/tuple of arrays that contain table information. fmt_str (str, bytes): Format string that should be used to structure the ASCII array. use_astropy (bool, optional): If True, astropy will be used to format the table if it is installed. Defaults to False. Returns: bytes: ASCII table. """ if not _use_astropy: use_astropy = False dtype = cformat2nptype(fmt_str) info = format2table(fmt_str) comment = info.get('comment', None) if comment is not None: fmt_str = fmt_str.split(comment, 1)[-1] arr1 = consolidate_array(arrs, dtype=dtype) if use_astropy: fd = backwards.StringIO() table = apy_Table(arr1) delimiter = info['delimiter'] delimiter = backwards.as_str(delimiter) apy_ascii.write(table, fd, delimiter=delimiter, format='no_header') out = backwards.as_bytes(fd.getvalue()) else: fd = backwards.BytesIO() for ele in arr1: line = format_message(ele.tolist(), backwards.as_bytes(fmt_str)) fd.write(line) # fmt = fmt_str.split(info['newline'])[0] # np.savetxt(fd, arr1, # fmt=fmt, delimiter=info['delimiter'], # newline=info['newline'], header='') out = fd.getvalue() fd.close() return out
def func_serialize(self, args): r"""Serialize a message. Args: args (obj): Python object to be serialized. Returns: bytes, str: Serialized message. """ if not isinstance(args, pandas.DataFrame): raise TypeError(("Pandas DataFrame required. Invalid type " + "of '%s' provided.") % type(args)) fd = backwards.StringIO() if backwards.PY2: # pragma: Python 2 args_ = args else: # pragma: Python 3 # For Python 3 and higher, bytes need to be encoded args_ = copy.deepcopy(args) for c in args.columns: if isinstance(args_[c][0], backwards.bytes_type): args_[c] = args_[c].apply(lambda s: s.decode('utf-8')) if self.field_names is None: self.field_names = self.get_field_names() args_ = self.apply_field_names(args_, self.field_names) args_.to_csv( fd, index=False, # Not in pandas <0.24 # line_terminator=backwards.as_str(self.newline), sep=backwards.as_str(self.delimiter), mode='wb', encoding='utf8', header=(not self.dont_write_header)) if self.write_header_once: self.dont_write_header = True out = fd.getvalue() fd.close() # Required to change out \r\n for \n on windows out = out.replace(backwards.match_stype(out, platform._newline), backwards.match_stype(out, self.newline)) return backwards.as_bytes(out)
def load_yaml(fname): r"""Parse a yaml file defining a run. Args: fname (str, file, dict): Path to a YAML file, an open file descriptor to a file containing a YAML, or a loaded YAML document. Returns: dict: Contents of yaml file. """ opened = False if isinstance(fname, dict): yamlparsed = copy.deepcopy(fname) yamlparsed.setdefault('working_dir', os.getcwd()) return yamlparsed elif isinstance(fname, str): fname = os.path.realpath(fname) if not os.path.isfile(fname): raise IOError("Unable locate yaml file %s" % fname) fd = open(fname, 'r') opened = True else: fd = fname if (hasattr(fd, 'name') and (not fd.name.startswith('<'))): fname = fd.name else: fname = os.path.join(os.getcwd(), 'stream') # Mustache replace vars yamlparsed = fd.read() yamlparsed = pystache.render( backwards.StringIO(yamlparsed).getvalue(), dict(os.environ)) yamlparsed = yaml.safe_load(yamlparsed) if not isinstance(yamlparsed, dict): # pragma: debug raise ValueError("Loaded yaml is not a dictionary.") yamlparsed['working_dir'] = os.path.dirname(fname) if opened: fd.close() return yamlparsed
def load_yaml(fname): r"""Parse a yaml file defining a run. Args: fname (str): Path to the yaml file. Returns: dict: Contents of yaml file. """ fname = os.path.realpath(fname) if not os.path.isfile(fname): raise IOError("Unable locate yaml file %s" % fname) # Open file and parse yaml with open(fname, 'r') as f: # Mustache replace vars yamlparsed = f.read() yamlparsed = pystache.render( backwards.StringIO(yamlparsed).getvalue(), dict(os.environ)) yamlparsed = yaml.safe_load(yamlparsed) if not isinstance(yamlparsed, dict): # pragma: debug raise ValueError("Loaded yaml is not a dictionary.") yamlparsed['working_dir'] = os.path.dirname(fname) return yamlparsed