def read_sheet(sheet: Worksheet, *, header=True, index_col: Union[int, Iterable] = None) -> pd.DataFrame: """ Read a openpyxl Worksheet object and return the sheet's data >>> wb = pxl.load_workbook("test.xlsx") >>> df = read_sheet(wb.active, index_col=0) :param Worksheet sheet: the sheet object :param bool header: if the sheet's first row represent the header, if header is True then colnames are converted to upper case strings :param Union[int, Iterable] index_col: the cols used as index, default no columns used as index """ data: Iterable = sheet.values col_names = [str(col_name).upper() for col_name in next(data)] if header else None data = list(data) df = pd.DataFrame(data, columns=col_names) if isiterable(index_col): index_header = [col_names[i] for i in index_col] if header else index_col df.set_index(index_header, inplace=True) elif index_col is not None: index_header = col_names[index_col] if header else index_col df.set_index(index_header, inplace=True) else: pass return df
def _validate_model_args( args: Union[_ServableType, List[_ServableType], Tuple[_ServableType, ...], Dict[str, _ServableType]] ) -> None: """Validator for machine learning models. Parameters ---------- args model args passed into ``__init__`` of ``ModelComponent`` Raises ------ ValueError If an empty iterable is passed as the model argument TypeError If the args do not contain properly formatted model refences """ if isiterable(args) and len(args) == 0: raise ValueError(f"Iterable args={args} must have length >= 1") if isinstance(args, (list, tuple)): if not all(isinstance(x, _Servable_t) for x in args): raise TypeError( f"One of arg in args={args} is not type {_Servable_t}") elif isinstance(args, dict): if not all(isinstance(x, str) for x in args.keys()): raise TypeError( f"One of keys in args={args.keys()} is not type {str}") if not all(isinstance(x, _Servable_t) for x in args.values()): raise TypeError( f"One of values in args={args} is not type {_Servable_t}") elif not isinstance(args, _Servable_t): raise TypeError( f"Args must be instance, list/tuple, or mapping of {_Servable_t}")
def make_model_dict(modules, default_key=None): assert modules is None or isiterable(modules) if isinstance(modules, dict): return ModuleDict(modules, default_key=default_key) elif modules is not None: modules = dict([(p.context, p) for p in modules]) return ModuleDict(modules, default_key) else: return ModuleDict(default_key=default_key)
def __init__(self, *clause, name=None, **kwargs): """ :param clause: clause like objects including :class:`Clause`, :class:`ParallelGroup`, :class:`SequentialGroup` etc :param name: a optional name of this group """ if len(clause) == 1 and isiterable(clause[0]): clause = clause[0] d = OrderedDict(((n, cl) for cl, n in zip(clause, self._get_dict_name(clause)))) super().__init__(d) self.name = name
def zspin_projector(n, sz=0, stype="csr"): """Construct the projector onto spin-z subpspaces. Parameters ---------- n : int Total size of spin system. sz : float or sequence of floats Spin-z value(s) subspace(s) to find projector for. stype : str Sparse format of the output matrix. Returns ------- immutable sparse matrix The (non-square) projector onto the specified subspace(s). Examples -------- >>> zspin_projector(2, 0).A array([[ 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j], [ 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j]]) """ if not isiterable(sz): sz = (sz,) p = 0 all_perms = [] for s in sz: # Number of 'up' spins k = n / 2 + s if not k.is_integer(): raise ValueError("{} is not a valid spin half subspace for " "{} spins.".format(s, n)) k = int(round(k)) # Size of subspace p += int(round(cmbn(n, k))) # Find all computational basis states with correct number of 0s and 1s base_perm = '0' * (n - k) + '1' * k all_perms += [uniq_perms(base_perm)] # Coordinates cis = tuple(range(p)) # arbitrary basis cjs = tuple(int("".join(perm), 2) for perm in concat(all_perms)) # Construct matrix which prjects only on to these basis states prj = sp.coo_matrix((np.ones(p, dtype=complex), (cis, cjs)), shape=(p, 2**n), dtype=complex) prj = qu(prj, stype=stype) make_immutable(prj) return prj
def _parse_cases(cases): """ """ # cases = {'a': 1, 'b': 2, 'c': 3} --> ({'a': 1, 'b': 2, 'c': 3},) if isinstance(cases, dict): return (cases,) cases = tuple(cases) # e.g. if fn_args = ('a',) and cases = (1, 10, 100) # we want cases --> ((1,), (10,), (100,)) if isinstance(cases[0], str) or not isiterable(cases[0]): cases = tuple((c,) for c in cases) return cases
def run(self, ns, kernels=None, **harvest_opts): """Run the benchmarks. Each run accumulates rather than overwriting the results. Parameters ---------- ns : sequence of int or int The sizes to run the benchmarks with. kernels : sequence of str, optional If given, only run the kernels with these names. harvest_opts Supplied to :meth:`~xyzpy.Harvester.harvest_combos`. """ if not isiterable(ns): ns = (ns,) if kernels is None: kernels = self.names combos = {'n': ns, 'kernel': kernels} self.harvester.harvest_combos(combos, **harvest_opts)
def run(self, ns, kernels=None, **harvest_opts): """Run the benchmarks. Each run accumulates rather than overwriting the results. Parameters ---------- ns : sequence of int or int The sizes to run the benchmarks with. kernels : sequence of str, optional If given, only run the kernels with these names. harvest_opts Supplied to :meth:`~xyzpy.Harvester.harvest_combos`. """ if not isiterable(ns): ns = (ns, ) if kernels is None: kernels = self.names combos = {'n': ns, 'kernel': kernels} self.harvester.harvest_combos(combos, **harvest_opts)
def zspin_projector(n, sz=0, stype="csr", dtype=float): """Construct the projector onto spin-z subpspaces. Parameters ---------- n : int Total size of spin system. sz : float or sequence of floats Spin-z value(s) subspace(s) to find projector for. stype : str Sparse format of the output operator. dtype : {float, complex}, optional The data type of the operator to generate. Returns ------- prj : immutable sparse operator, shape (2**n, D) The (non-square) projector onto the specified subspace(s). The subspace size ``D`` is given by ``n choose (n / 2 + s)`` for each ``s`` specified in ``sz``. Examples -------- >>> zspin_projector(n=2, sz=0).A array([[0., 0.], [1., 0.], [0., 1.], [0., 0.]] Project a 9-spin Heisenberg-Hamiltonian into its spin-1/2 subspace: >>> H = ham_heis(9, sparse=True) >>> H.shape (512, 512) >>> P = zspin_projector(n=9, sz=1 / 2) >>> H0 = P.T @ H @ P >>> H0.shape (126, 126) """ if not isiterable(sz): sz = (sz,) p = 0 all_perms = [] for s in sz: # Number of 'up' spins k = n / 2 + s if not k.is_integer(): raise ValueError(f"{s} is not a valid spin half subspace for {n} " "spins.") k = int(round(k)) # Size of subspace p += comb(n, k, exact=True) # Find all computational basis states with correct number of 0s and 1s base_perm = '0' * (n - k) + '1' * k all_perms += [uniq_perms(base_perm)] # Coordinates cis = tuple(range(p)) # arbitrary basis cjs = tuple(int("".join(perm), 2) for perm in concat(all_perms)) # Construct matrix which projects only on to these basis states prj = sp.coo_matrix((np.ones(p, dtype=dtype), (cjs, cis)), shape=(2**n, p), dtype=dtype) prj = qu(prj, stype=stype, dtype=dtype) make_immutable(prj) return prj
def __init__(self, name, ratio_tables, base, t_offset, *, mth_converter=None, prob_tables=None, default_context='DEFAULT', virtual=False, contexts_exclude=()): """ :param str name: name of the clause :param ratio_tables: input can be both A dict and a single module. if the input is a dict, the keys should be contexts. if the input is a single module, the `default_context` is treated as the key if the input is a float, then it will convert to a module using :class:`~util.Lambda`, acting like a constant multiplier. :type ratio_tables: Module or dict[str, Module] or float :param base: base of the clause, calculate the value with the ratio will multiply to. input can be a :class:`BaseConverter` or integer. if the input is a integer, the base of the clause will be a :class:`BaseSelector`. :type base: int or BaseConverter :param float t_offset: time offset :param mth_converter: input can be both A module and a integer. if `monthly_converter` is integer or None then :class:`~util.CF2M` is used. you can use :class:`~util.Lambda` wrap a callable :type mth_converter: Module or int :param prob_tables: input can be both A dict and a single module. if the input is a dict, the keys should be contexts. if the input is a single module, the `default_context` is treated as the key :type prob_tables: Module or dict[str, Module] :param str default_context: default context, Default 'DEFAULT' :param bool virtual: default `False` :param contexts_exclude: contexts under which the cash flow of this clause should be excluded. Input can be a string, an iterable or a callable. - str: can be a single context or contexts separated by comma. if the string is started with `~`, the semantics is *'exclude except'*. - iterable: contexts of the iterable is excluded - callable: returns `True` if the cash flow should be excluded under the input context. :type contexts_exclude: str or Iterable[str] or Callable """ super().__init__() self.name = name self.default_context = default_context self.t_offset = t_offset self.virtual = virtual if isinstance(contexts_exclude, str): self._context_exclude_repr = contexts_exclude if contexts_exclude.startswith('~'): _context_exclude = set((x.strip() for x in contexts_exclude[1:].split(','))) self.contexts_exclude = lambda x: x not in _context_exclude else: _context_exclude = set((x.strip() for x in contexts_exclude.split(','))) self.contexts_exclude = lambda x: x in _context_exclude elif isiterable(contexts_exclude): _context_exclude = set(contexts_exclude) self._context_exclude_repr = ','.join((str(x) for x in contexts_exclude)) self.contexts_exclude = lambda x: x in _context_exclude else: assert callable(contexts_exclude) self._context_exclude_repr = repr(contexts_exclude) self.contexts_exclude = contexts_exclude # set up base modules & params if isinstance(base, int): self.base = BaseSelector(base) elif isinstance(base, BaseConverter): self.base = base else: raise TypeError(base) # set up ratio tables if not isiterable(ratio_tables): if isinstance(ratio_tables, float) or isinstance(ratio_tables, int): _r = float(ratio_tables) ratio_tables = Lambda(lambda mp_idx, _: _.new([_r]).expand(_.shape[0], MAX_YR_LEN), repre=ratio_tables) ratio_tables = {self.default_context: ratio_tables} self.ratio_tables = make_model_dict(ratio_tables, self.default_context) # set up probabilities if not isiterable(prob_tables): prob_tables = {self.default_context: prob_tables} self.prob_tables = make_model_dict(prob_tables, self.default_context) # set up mth converter if mth_converter is None or isinstance(mth_converter, int): self.mth_converter = CF2M(mth_converter) else: self.mth_converter = mth_converter self._setup_clause_ref() # dictionary meta information of the clause self.meta_data = { 'class': self.__class__.__name__, 'name': name, 'ratio_tables': str(ratio_tables), 'base': str(base), 't_off_set': t_offset, 'mth_converter': str(mth_converter), 'prob_tables': str(prob_tables), 'default_context': default_context, 'virtual': virtual, 'context_exclude': str(contexts_exclude), }
def pluck(self, ind): if cytoolz.isiterable(ind): return self.__class__(itertools.imap(flist, cytoolz.pluck(ind, self))) else: return self.__class__(cytoolz.pluck(ind, self))