unlisted
ubuntu2004r"""1Cache of computations of top xi evaluations and various evaluation of tautological classes.2"""34from ast import literal_eval5import os6import os.path78from sage.env import DOT_SAGE9import sage.misc.persist1011from admcycles.diffstrata.adm_eval_cache import ADM_EVAL_CACHE12from admcycles.diffstrata.xi_cache import XI_TOP_CACHE131415class Cache(dict):16r"""17Dictionary with synchronization in a ``.sobj`` file.1819TESTS:2021The synchronization files stores only diffs::2223sage: from admcycles.diffstrata.cache import Cache24sage: filename = tmp_filename(ext='.sobj')25sage: C = Cache({2: 'two'}, filename=filename)26sage: C[3] = 'three'27sage: D = Cache(filename=filename)28sage: D29{3: 'three'}3031An example without synchronization file::3233sage: C = Cache({2: 'two'})34sage: C[3] = 'three'35sage: C36{2: 'two', 3: 'three'}37"""3839def __init__(self, values=None, filename=None):40self._filename = filename41if values is None:42self._default = {}43dict.__init__(self)44else:45self._default = values.copy()46dict.__init__(self, values)47self.update_from_sobj_file(self._filename)4849def reset_default(self):50r"""51Reset the content of the dictionary to the set of default values.5253TESTS::5455sage: from admcycles.diffstrata.cache import Cache56sage: C = Cache({1: 'ein', 2: 'zwei'})57sage: C[3] = 'drei'58sage: sorted(C.keys())59[1, 2, 3]60sage: C.reset_default()61sage: sorted(C.keys())62[1, 2]63"""64self.clear()65self.update(self._default)6667def update_from_sobj_file(self, filename=None):68r"""69Update the values of this dictionary from a ``.sobj`` file.7071Missing or empty files are ignored. The cache file is synchronized.7273EXAMPLES::7475sage: from admcycles.diffstrata.cache import Cache76sage: filename1 = tmp_filename(ext='.sobj')77sage: filename2 = tmp_filename(ext='.sobj')78sage: C1 = Cache({2: 'two'}, filename=filename1)79sage: C1[3] = 'three'80sage: C2 = Cache(filename=filename2)81sage: C282{}83sage: C2.update_from_sobj_file(filename1)84sage: Cache(filename=filename2)85{3: 'three'}86"""87if filename is None:88filename = self._filename89if filename is not None and os.path.isfile(90filename) and os.stat(filename).st_size:91self.update(sage.misc.persist.load(filename))92if filename != self._filename:93self.save_to_sobj_file()9495def diff(self):96cache = self.copy()97for key in self._default:98del cache[key]99return cache100101def save_to_sobj_file(self, filename=None, only_diff=True):102r"""103Save the values of this dictionary to a ``.sobj`` file.104"""105if filename is None:106filename = self._filename107if filename is not None:108cache = self.diff() if only_diff else self.copy()109if cache:110sage.misc.persist.save(cache, filename)111112def __setitem__(self, key, value):113dict.__setitem__(self, key, value)114self.save_to_sobj_file()115116117class FakeCache(Cache):118r"""119A cache where ``__setitem__`` does not do anything.120121This class is intended for benchmarking without caching.122123TESTS::124125sage: from admcycles.diffstrata.cache import FakeCache126sage: C = FakeCache()127sage: C[3] = 'trois'128sage: C129{}130"""131132def __setitem__(self, key, value):133pass134135136ADM_EVALS_FILENAME = os.path.join(DOT_SAGE, 'adm_evals.sobj')137TOP_XIS_FILENAME = os.path.join(DOT_SAGE, 'top_xis.sobj')138139ADM_EVALS = Cache(ADM_EVAL_CACHE, ADM_EVALS_FILENAME)140TOP_XIS = Cache(XI_TOP_CACHE, TOP_XIS_FILENAME)141142143def list_top_xis(xi_dict=TOP_XIS):144"""145A generator for decyphering LevelStratum.dict_key146147By default, the xi cache is used, alternatively a different148cache dictionary may be specified.149150Args:151xi_dict (dict, optional): cache dictionary. Defaults to ``TOP_XIS``.152153Yields:154tuple: signature list, residue conditions, top xi evaluation155"""156for key in sorted(xi_dict):157sig_list, rcs = key158yield (sig_list, rcs, xi_dict[key])159160161def print_top_xis(xi_dict=TOP_XIS):162"""163The top xi powers in the cache are printed in human-readable form.164165Alternatively, any valid xi cache dictionary may be specified.166167The dimensions of the table might have to be adapted to console size and168complexity of the involved strata.169170Args:171xi_dict (dict, optional): xi cache dictionary. Defaults to ``TOP_XIS``.172173EXAMPLES::174175sage: from admcycles.diffstrata import print_top_xis176sage: from admcycles.diffstrata.cache import TOP_XIS177sage: TOP_XIS.reset_default()178sage: print_top_xis()179Stratum | Residue Conditions | xi^dim180--------------------------------------------------------------------------------------------------181(-8, -2, 8) | [(0, 0), (0, 1)] | 1182(-8, 0, 6) | [(0, 0)] | 1183(-8, 1, 1, 2, 2) | [(0, 0)] | 49184...185(3, 3) | () | 0186(4,) | () | 305/580608187(6,) | () | -87983/199065600188(8,) | () | 339849/504627200189"""190LEFT = 40191MIDDLE = 45192RIGHT = 13193print('{:<{width}}'.format('Stratum', width=LEFT) + '|' +194'{:<{width}}'.format(' Residue Conditions', width=MIDDLE) + '|' +195'{:<{width}}'.format(' xi^dim', width=RIGHT))196print('-' * (LEFT + MIDDLE + RIGHT))197for sig_list, res_conds, xi in list_top_xis(xi_dict):198if len(sig_list) == 1:199sig = sig_list[0]200else:201sig = list(sig_list)202if not res_conds:203rcs = tuple()204else:205if len(res_conds) == 1:206rcs = list(res_conds[0])207else:208rcs = [list(c) for c in res_conds]209print('{:<{width}}'.format(str(sig), width=LEFT) + '| ' +210'{:<{width}}'.format(str(rcs), width=MIDDLE - 1) + '| ' +211'{:<{width}}'.format(str(xi), width=RIGHT - 1))212213214def print_adm_evals(adm_dict=ADM_EVALS):215"""216The cached evaluations are printed in human-readable form.217218Alternatively, any valid adm cache dictionary may be specified (see load_adm_evals219for details on the format).220221The dimensions of the table might have to be adapted to console size and222complexity of the involved strata.223224Args:225adm_dict (dict, optional): evaluations cache dictionary. Defaults to None.226227EXAMPLES::228229sage: from admcycles.diffstrata import print_adm_evals230sage: from admcycles.diffstrata.cache import ADM_EVALS231sage: ADM_EVALS.reset_default()232sage: print_adm_evals()233Stratum | Psis | eval234--------------------------------------------------------------------------------------------------235...236(2,) | {1: 3} | 1/1920237(2, 2) | {1: 1, 2: 5} | 71/322560238(2, 2) | {1: 2, 2: 4} | 13/53760239(2, 2) | {1: 3, 2: 3} | 13/53760240(2, 2) | {1: 6} | 43/322560241(4,) | {1: 5} | 13/580608242(6,) | {1: 7} | 281/199065600243"""244LEFT = 40245MIDDLE = 45246RIGHT = 13247print('{:<{width}}'.format('Stratum', width=LEFT) + '|' +248'{:<{width}}'.format(' Psis', width=MIDDLE) + '|' +249'{:<{width}}'.format(' eval', width=RIGHT))250print('-' * (LEFT + MIDDLE + RIGHT))251for key in sorted(adm_dict): # sort by stratum252sig, psis = key253value = adm_dict[key]254dpsis = dict(psis)255print('{:<{width}}'.format(str(sig), width=LEFT) + '| ' +256'{:<{width}}'.format(str(dpsis), width=MIDDLE - 1) + '| ' +257'{:<{width}}'.format(str(value), width=RIGHT - 1))258259260def jsonify_dict(d):261json_dict = {}262for k, v in d.items():263json_dict[str(k)] = v264return json_dict265266267def unjsonify_dict(json_dict):268restored_dict = {}269for k, v in json_dict.items():270restored_dict[literal_eval(k)] = v271return restored_dict272273274def import_adm_evals(filename):275"""276Import a dictionary of the form adm_key : value into the cache.277278Args:279filename (String): sobj filename280"""281ADM_EVALS.update_from_sobj_file(filename)282283284def import_top_xis(filename):285"""286Import a dictionary of the form LevelStratum.dict_key : value into the cache.287288Args:289filename (String): sobj filename290"""291TOP_XIS.update_from_sobj_file(filename)292293294def load_adm_evals():295"""296The cache dictionary for the admcycles evaluations (via GeneralisedStratum.adm_evaluate)297298The cache dictionary is of the form:299300adm_key -> GeneralisedStratum.adm_evaluate(adm_key) (a rational number)301302Returns:303dict: cache dictionary304"""305return ADM_EVALS306307308def load_xis():309"""310The cache dictionary for the top xi powers (evaluated)311312The cache dictionary is of the form:313314L.dict_key -> (L.xi)^L.dim().evaluate() (a rational number)315316for a LevelStratum L.317318Returns:319dict: cache dictionary320"""321return TOP_XIS322323324