| Download
Project: admcycles
Views: 724Visibility: Unlisted (only visible to those who know the link)
Image: ubuntu2004r"""1Cache of computations of top xi evaluations and various evaluation of tautological classes.2"""34from ast import literal_eval5import os, os.path67from sage.env import DOT_SAGE8import sage.misc.persist910from admcycles.diffstrata.adm_eval_cache import ADM_EVAL_CACHE11from admcycles.diffstrata.xi_cache import XI_TOP_CACHE1213class Cache(dict):14r"""15Dictionary with synchronization in a ``.sobj`` file.1617TESTS:1819The synchronization files stores only diffs::2021sage: from admcycles.diffstrata.cache import Cache22sage: filename = tmp_filename(ext='.sobj')23sage: C = Cache({2: 'two'}, filename=filename)24sage: C[3] = 'three'25sage: D = Cache(filename=filename)26sage: D27{3: 'three'}2829An example without synchronization file::3031sage: C = Cache({2: 'two'})32sage: C[3] = 'three'33sage: C34{2: 'two', 3: 'three'}35"""36def __init__(self, values=None, filename=None):37self._filename = filename38if values is None:39self._default = {}40dict.__init__(self)41else:42self._default = values.copy()43dict.__init__(self, values)44self.update_from_sobj_file(self._filename)4546def reset_default(self):47r"""48Reset the content of the dictionary to the set of default values.4950TESTS::5152sage: from admcycles.diffstrata.cache import Cache53sage: C = Cache({1: 'ein', 2: 'zwei'})54sage: C[3] = 'drei'55sage: sorted(C.keys())56[1, 2, 3]57sage: C.reset_default()58sage: sorted(C.keys())59[1, 2]60"""61self.clear()62self.update(self._default)6364def update_from_sobj_file(self, filename=None):65r"""66Update the values of this dictionary from a ``.sobj`` file.6768Missing or empty files are ignored. The cache file is synchronized.6970EXAMPLES::7172sage: from admcycles.diffstrata.cache import Cache73sage: filename1 = tmp_filename(ext='.sobj')74sage: filename2 = tmp_filename(ext='.sobj')75sage: C1 = Cache({2: 'two'}, filename=filename1)76sage: C1[3] = 'three'77sage: C2 = Cache(filename=filename2)78sage: C279{}80sage: C2.update_from_sobj_file(filename1)81sage: Cache(filename=filename2)82{3: 'three'}83"""84if filename is None:85filename = self._filename86if filename is not None and os.path.isfile(filename) and os.stat(filename).st_size:87self.update(sage.misc.persist.load(filename))88if filename != self._filename:89self.save_to_sobj_file()9091def diff(self):92cache = self.copy()93for key in self._default:94del cache[key]95return cache9697def save_to_sobj_file(self, filename=None, only_diff=True):98r"""99Save the values of this dictionary to a ``.sobj`` file.100"""101if filename is None:102filename = self._filename103if filename is not None:104cache = self.diff() if only_diff else self.copy()105if cache:106sage.misc.persist.save(cache, filename)107108def __setitem__(self, key, value):109dict.__setitem__(self, key, value)110self.save_to_sobj_file()111112class FakeCache(Cache):113r"""114A cache where ``__setitem__`` does not do anything.115116This class is intended for benchmarking without caching.117118TESTS::119120sage: from admcycles.diffstrata.cache import FakeCache121sage: C = FakeCache()122sage: C[3] = 'trois'123sage: C124{}125"""126def __setitem__(self, key, value):127pass128129ADM_EVALS_FILENAME = os.path.join(DOT_SAGE, 'adm_evals.sobj')130TOP_XIS_FILENAME = os.path.join(DOT_SAGE, 'top_xis.sobj')131132ADM_EVALS = Cache(ADM_EVAL_CACHE, ADM_EVALS_FILENAME)133TOP_XIS = Cache(XI_TOP_CACHE, TOP_XIS_FILENAME)134135def list_top_xis(xi_dict=TOP_XIS):136"""137A generator for decyphering LevelStratum.dict_key138139By default, the xi cache is used, alternatively a different140cache dictionary may be specified.141142Args:143xi_dict (dict, optional): cache dictionary. Defaults to ``TOP_XIS``.144145Yields:146tuple: signature list, residue conditions, top xi evaluation147"""148for key in sorted(xi_dict):149sig_list, rcs = key150yield (sig_list, rcs, xi_dict[key])151152def print_top_xis(xi_dict=TOP_XIS):153"""154The top xi powers in the cache are printed in human-readable form.155156Alternatively, any valid xi cache dictionary may be specified.157158The dimensions of the table might have to be adapted to console size and159complexity of the involved strata.160161Args:162xi_dict (dict, optional): xi cache dictionary. Defaults to ``TOP_XIS``.163164EXAMPLES::165166sage: from admcycles.diffstrata import print_top_xis167sage: from admcycles.diffstrata.cache import TOP_XIS168sage: TOP_XIS.reset_default()169sage: print_top_xis()170Stratum | Residue Conditions | xi^dim171--------------------------------------------------------------------------------------------------172(-8, -2, 8) | [(0, 0), (0, 1)] | 1173(-8, 0, 6) | [(0, 0)] | 1174(-8, 1, 1, 2, 2) | [(0, 0)] | 49175...176(3, 3) | () | 0177(4,) | () | 305/580608178(6,) | () | -87983/199065600179(8,) | () | 339849/504627200180"""181LEFT = 40182MIDDLE = 45183RIGHT = 13184print('{:<{width}}'.format('Stratum', width=LEFT) + '|' +185'{:<{width}}'.format(' Residue Conditions', width=MIDDLE) + '|' +186'{:<{width}}'.format(' xi^dim', width=RIGHT))187print('-'*(LEFT+MIDDLE+RIGHT))188for sig_list, res_conds, xi in list_top_xis(xi_dict):189if len(sig_list) == 1:190sig = sig_list[0]191else:192sig = list(sig_list)193if not res_conds:194rcs = tuple()195else:196if len(res_conds) == 1:197rcs = list(res_conds[0])198else:199rcs = [list(c) for c in res_conds]200print('{:<{width}}'.format(str(sig), width=LEFT) + '| ' +201'{:<{width}}'.format(str(rcs), width=MIDDLE-1) + '| ' +202'{:<{width}}'.format(str(xi), width=RIGHT-1))203204def print_adm_evals(adm_dict=ADM_EVALS):205"""206The cached evaluations are printed in human-readable form.207208Alternatively, any valid adm cache dictionary may be specified (see load_adm_evals209for details on the format).210211The dimensions of the table might have to be adapted to console size and212complexity of the involved strata.213214Args:215adm_dict (dict, optional): evaluations cache dictionary. Defaults to None.216217EXAMPLES::218219sage: from admcycles.diffstrata import print_adm_evals220sage: from admcycles.diffstrata.cache import ADM_EVALS221sage: ADM_EVALS.reset_default()222sage: print_adm_evals()223Stratum | Psis | eval224--------------------------------------------------------------------------------------------------225...226(2,) | {1: 3} | 1/1920227(2, 2) | {1: 1, 2: 5} | 71/322560228(2, 2) | {1: 2, 2: 4} | 13/53760229(2, 2) | {1: 3, 2: 3} | 13/53760230(2, 2) | {1: 6} | 43/322560231(4,) | {1: 5} | 13/580608232(6,) | {1: 7} | 281/199065600233"""234LEFT = 40235MIDDLE = 45236RIGHT = 13237print('{:<{width}}'.format('Stratum', width=LEFT) + '|' +238'{:<{width}}'.format(' Psis', width=MIDDLE) + '|' +239'{:<{width}}'.format(' eval', width=RIGHT))240print('-'*(LEFT+MIDDLE+RIGHT))241for key in sorted(adm_dict): # sort by stratum242sig, psis = key243value = adm_dict[key]244dpsis = dict(psis)245print('{:<{width}}'.format(str(sig), width=LEFT) + '| ' +246'{:<{width}}'.format(str(dpsis), width=MIDDLE-1) + '| ' +247'{:<{width}}'.format(str(value), width=RIGHT-1))248249def jsonify_dict(d):250json_dict = {}251for k, v in d.items():252json_dict[str(k)] = v253return json_dict254255def unjsonify_dict(json_dict):256restored_dict = {}257for k, v in json_dict.items():258restored_dict[literal_eval(k)] = v259return restored_dict260261def import_adm_evals(filename):262"""263Import a dictionary of the form adm_key : value into the cache.264265Args:266filename (String): sobj filename267"""268ADM_EVALS.update_from_sobj_file(filename)269270def import_top_xis(filename):271"""272Import a dictionary of the form LevelStratum.dict_key : value into the cache.273274Args:275filename (String): sobj filename276"""277TOP_XIS.update_from_sobj_file(filename)278279def load_adm_evals():280"""281The cache dictionary for the admcycles evaluations (via GeneralisedStratum.adm_evaluate)282283The cache dictionary is of the form:284285adm_key -> GeneralisedStratum.adm_evaluate(adm_key) (a rational number)286287Returns:288dict: cache dictionary289"""290return ADM_EVALS291292def load_xis():293"""294The cache dictionary for the top xi powers (evaluated)295296The cache dictionary is of the form:297298L.dict_key -> (L.xi)^L.dim().evaluate() (a rational number)299300for a LevelStratum L.301302Returns:303dict: cache dictionary304"""305return TOP_XIS306307308