def save_to_db(self, update = True): r""" Saves ```self``` to the database, i.e. save the meta record and the file in the gridfs file system. """ import pymongo fs = self._files self.authorize() file_key = self.file_key_dict() coll = self._file_collection if fs.exists(file_key): emf_logger.debug("File exists with key={0}".format(file_key)) if not update: return True else: fid = coll.find_one(file_key, projection=['_id'])['_id'] fs.delete(fid) emf_logger.debug("Deleted file with fid={0}".format(fid)) # insert s = dumps(self.fs_dict()) if not self._use_separate_db: file_key.update(self.db_dict()) try: t = fs.put(s, **file_key) emf_logger.debug("Inserted file t={0}, filekey={1}".format(t,file_key)) except Exception, e: emf_logger.debug("Could not insert file s={0}, filekey={1}".format(s,file_key)) emf_logger.warn("Error inserting record: {0}".format(e))
def save_to_db(self, update=True): r""" Saves ```self``` to the database, i.e. save the meta record and the file in the gridfs file system. """ import pymongo fs = self._files self.authorize() file_key = self.file_key_dict() coll = self._file_collection if fs.exists(file_key): emf_logger.debug("File exists with key={0}".format(file_key)) if not update: return True else: fid = coll.find_one(file_key, projection=['_id'])['_id'] fs.delete(fid) emf_logger.debug("Deleted file with fid={0}".format(fid)) # insert s = dumps(self.fs_dict()) if not self._use_separate_db: file_key.update(self.db_dict()) try: t = fs.put(s, **file_key) emf_logger.debug("Inserted file t={0}, filekey={1}".format( t, file_key)) except Exception, e: emf_logger.debug("Could not insert file s={0}, filekey={1}".format( s, file_key)) emf_logger.warn("Error inserting record: {0}".format(e))
def print_list_of_coefficients(info): r""" Print a table of Fourier coefficients in the requested format """ level = my_get(info, 'level', -1, int) weight = my_get(info, 'weight', -1, int) prec = my_get(info, 'prec', 12, int) # number of digits bitprec = my_get(info, 'bitprec', 12, int) # number of digits character = my_get(info, 'character', '', str) # int(info.get('weight',0)) fmt = info.get("format", "q_expansion") if character == '': character = '1' label = info.get('label', '') if character.isalnum(): character = int(character) else: return "The character '{0}' is not well-defined!".format(character) print "--------------" if label == '' or level == -1 or weight == -1: return "Need to specify a modular form completely!!" number = int(info['number']) + 1 if 'number' in info else 20 emf_logger.debug("number = {}".format(number)) F = WebNewForm(level=level, weight=weight, character=character, label=label, prec=number) if not F.has_updated(): return "" if not 'number' in info: F.prec = number = max(F.parent.sturm_bound + 1, 20) F.update_from_db() shead = "Cusp forms of weight " + str(weight) + "on \(" + latex( F.parent.group) + "\)" s = "" if ((character is not None) and (character > 0)): shead = shead + " and character \( \chi_{" + str(character) + "}\)" # s="<table><tr><td>" coefs = "" if fmt == "sage": res = [] if number > F.max_available_prec(): raise IndexError, "The database does not contain this many ({0}) coefficients for this modular form! We only have {1}".format( number, F.max_available_prec()) if fmt == "sage": qe = F.coefficients(range(number)) res.append(qe) else: coefs += print_coefficients_for_one_form(F, number, info['format'], bitprec=bitprec) if not fmt == "sage": return s + "\n" + coefs else: if len(res) == 1: res = res[0] #print "res=",res return dumps(res)
def insert_into_db(self): r""" Insert a dictionary of data for self into the database collection WebNewforms.files """ wmf_logger.debug("inserting self into db! name={0}".format(self._name)) C = connect_to_modularforms_db('WebNewforms.files') fs = get_files_from_gridfs('WebNewforms') s = {'name':self._name,'version':float(self._version)} rec = C.find_one(s) if rec: id = rec.get('_id') else: id = None if id<>None: wmf_logger.debug("Removing self from db with id={0}".format(id)) fs.delete(id) fname = "webnewform-{0:0>5}-{1:0>3}-{2:0>3}-{3}".format(self._N,self._k,self._chi,self._label) d = self.to_dict() d.pop('_ap',None) d.pop('_character',None) d.pop('_as_factor',None) id = fs.put(dumps(d),filename=fname,N=int(self._N),k=int(self._k),chi=int(self._chi),label=self._label,name=self._name,version=float(self._version)) wmf_logger.debug("inserted :{0}".format(id))
def append(self, x): s = dumps(x) with open(self._index, 'ab') as f: f.write(struct.pack(_INTSYMB, os.path.getsize(self._data))) with open(self._sizes, 'ab') as f: f.write(struct.pack(_INTSYMB, len(s))) with open(self._data, 'ab') as f: f.write(s)
def close(self): # Flush data file and write metadata. if not self._file.closed: self._file.close() with open(self._meta_filename, 'wb') as f: f.write(dumps( {'cand': self._cand, 'critical': self._critical, 'count': self._count} ))
def print_list_of_coefficients(info): r""" Print a table of Fourier coefficients in the requested format """ level = my_get(info, "level", -1, int) weight = my_get(info, "weight", -1, int) bitprec = my_get(info, "bitprec", 12, int) # number of digits character = my_get(info, "character", "", str) # int(info.get('weight',0)) fmt = info.get("format", "q_expansion") if character == "": character = "1" label = info.get("label", "") if character.isalnum(): character = int(character) else: return "The character '{0}' is not well-defined!".format(character) print "--------------" if label == "" or level == -1 or weight == -1: return "Need to specify a modular form completely!!" number = int(info["number"]) + 1 if "number" in info else 20 emf_logger.debug("number = {}".format(number)) F = WebNewForm(level=level, weight=weight, character=character, label=label, prec=number) if not F.has_updated(): return "" if not "number" in info: F.prec = number = max(F.parent.sturm_bound + 1, 20) F.update_from_db() shead = "Cusp forms of weight " + str(weight) + "on \(" + latex(F.parent.group) + "\)" s = "" if (character is not None) and (character > 0): shead = shead + " and character \( \chi_{" + str(character) + "}\)" # s="<table><tr><td>" coefs = "" if fmt == "sage": res = [] if number > F.max_available_prec(): raise IndexError, "The database does not contain this many ({0}) coefficients for this modular form! We only have {1}".format( number, F.max_available_prec() ) if fmt == "sage": qe = F.coefficients(range(number)) res.append(qe) else: coefs += print_coefficients_for_one_form(F, number, info["format"], bitprec=bitprec) if not fmt == "sage": return s + "\n" + coefs else: if len(res) == 1: res = res[0] # print "res=",res return dumps(res)
def save(self, obj, key=None, compress=None): """Save Python object obj to the grid file system self.gridfs. If key is None, the file is stored by MongoDB assigned ObjectID, and that id is returned. """ from sage.all import dumps data = dumps(obj, compress=compress) if key is not None: self.gridfs.put(data, filename=key) return key else: # store by MongoDB assigned _id only, and return that id. return self.gridfs.put(data)
def generate_fundom_plots(N, group='Gamma0'): gps = C['SL2Zsubgroups']['groups'] if group == 'Gamma0': G = Gamma0(N) grouptype = int(0) else: G = Gamma1(N) grouptype = int(1) dom = draw_fundamental_domain(N, group) filename = 'domain' + str(N) + '.png' save(dom, filename) data = open(filename).read() idins = gps.insert({'level': int(N), 'index': int(G.index( )), 'G': pymongo.binary.Binary(dumps(G)), 'domain': pymongo.binary.Binary(data), 'type': grouptype}) print "inserted: ", N, " ", idins
def command(self): r""" Return command line arguments that can be used to recreate this surface. EXAMPLES:: >>> from flatsurvey.surfaces import Ngon >>> Ngon((1, 1, 1), 'exact-real').command() ['pickle', '--base64', '...'] """ from base64 import b64encode from sage.all import dumps return ["pickle", "--base64", b64encode(dumps(self)).decode("ASCII").strip()]
def save_to_db(self, update=True): r""" Saves ```self``` to the database, i.e. save the meta record and the file in the gridfs file system. """ import pymongo from pymongo.errors import OperationFailure fs = self._files try: self.authorize() except OperationFailure: emf_logger.critical( "Authentication failed. You are not authorized to save data to the database!" ) return False if self._use_gridfs: file_key = self.file_key_dict() coll = self._file_collection if fs.exists(file_key): emf_logger.debug("File exists with key={0}".format(file_key)) if not update: return True else: fid = coll.find_one(file_key, projection=['_id'])['_id'] fs.delete(fid) emf_logger.debug("Deleted file with fid={0}".format(fid)) # insert s = dumps(self.fs_dict()) if not self._use_separate_db: file_key.update(self.db_dict()) try: t = fs.put(s, **file_key) emf_logger.debug("Inserted file with filekey={1}".format( t, file_key)) except Exception, e: emf_logger.debug( "Could not insert file with filekey={1}".format( s, file_key)) emf_logger.warn("Error inserting record: {0}".format(e)) #fid = coll.find_one(key)['_id'] # insert extended record if not self._use_separate_db: self.logout() return True
def generate_fundom_plots(N, group='Gamma0'): gps = C['SL2Zsubgroups']['groups'] if group == 'Gamma0': G = Gamma0(N) grouptype = int(0) else: G = Gamma1(N) grouptype = int(1) dom = draw_fundamental_domain(N, group) filename = 'domain' + str(N) + '.png' save(dom, filename) data = open(filename).read() idins = gps.insert({ 'level': int(N), 'index': int(G.index()), 'G': pymongo.binary.Binary(dumps(G)), 'domain': pymongo.binary.Binary(data), 'type': grouptype }) print "inserted: ", N, " ", idins
def download_web_modform(info): r""" Return a dump of a WebNewForm object. """ emf_logger.debug("IN GET_WEB_MODFORM!!! info={0}".format(info)) level = my_get(info, 'level', -1, int) weight = my_get(info, 'weight', -1, int) character = my_get(info, 'character', 0, int) # int(info.get('weight',0)) emf_logger.debug("info={0}".format(info)) if character == '': character = 0 label = info.get('label', '') # we only want one form or one embedding if label != '': if format == 'sage': if character != 0: D = DirichletGroup(level) x = D[character] X = Newforms(x, weight, names='a') else: X = Newforms(level, weight, names='a') else: # format=='web_new': X = WebNewForm(level=level, weight=weight, character=character, label=label) s = dumps(X) name = "{0}-{1}-{2}-{3}-web_newform.sobj".format(weight, level, character, label) emf_logger.debug("name={0}".format(name)) info['filename'] = name strIO = StringIO.StringIO() strIO.write(s) strIO.seek(0) try: return send_file(strIO, attachment_filename=info["filename"], as_attachment=True) except IOError: info['error'] = "Could not send file!"
def generate_fundom_plots(N, group="Gamma0"): gps = C["SL2Zsubgroups"]["groups"] if group == "Gamma0": G = Gamma0(N) grouptype = int(0) else: G = Gamma1(N) grouptype = int(1) dom = draw_fundamental_domain(N, group) filename = "domain" + str(N) + ".png" save(dom, filename) data = open(filename).read() idins = gps.insert( { "level": int(N), "index": int(G.index()), "G": pymongo.binary.Binary(dumps(G)), "domain": pymongo.binary.Binary(data), "type": grouptype, } ) print "inserted: ", N, " ", idins
def insert_into_db(self): r""" Insert a dictionary of data for self into the collection WebModularforms.files """ wmf_logger.debug("inserting self into db! name={0}".format(self._name)) db = connect_to_modularforms_db('WebModformspace.files') fs = get_files_from_gridfs('WebModformspace') s = {'name':self._name,'version':emf_version} rec = db.find_one(s) if rec: id = rec.get('_id') else: id = None if id<>None: wmf_logger.debug("Removing self from db with id={0}".format(id)) fs.delete(id) fname = "webmodformspace-{0:0>5}-{1:0>3}-{2:0>3}".format(self._N,self._k,self._chi) d = self.to_dict() d.pop('_ap',None) # Since the ap's are already in the database we don't need them here id = fs.put(dumps(d),filename=fname,N=int(self._N),k=int(self._k),chi=int(self._chi),name=self._name,version=emf_version) wmf_logger.debug("inserted :{0}".format(id))
def save_to_db(self, update = True): r""" Saves ```self``` to the database, i.e. save the meta record and the file in the gridfs file system. """ import pymongo from pymongo.errors import OperationFailure fs = self._files try: self.authorize() except OperationFailure: emf_logger.critical("Authentication failed. You are not authorized to save data to the database!") return False if self._use_gridfs: file_key = self.file_key_dict() coll = self._file_collection if fs.exists(file_key): emf_logger.debug("File exists with key={0}".format(file_key)) if not update: return True else: fid = coll.find_one(file_key, projection=['_id'])['_id'] fs.delete(fid) emf_logger.debug("Deleted file with fid={0}".format(fid)) # insert s = dumps(self.fs_dict()) if not self._use_separate_db: file_key.update(self.db_dict()) try: t = fs.put(s, **file_key) emf_logger.debug("Inserted file with filekey={1}".format(t,file_key)) except Exception, e: emf_logger.debug("Could not insert file with filekey={1}".format(s,file_key)) emf_logger.warn("Error inserting record: {0}".format(e)) #fid = coll.find_one(key)['_id'] # insert extended record if not self._use_separate_db: self.logout() return True
def download_web_modform(info): r""" Return a dump of a WebNewForm object. """ emf_logger.debug("IN GET_WEB_MODFORM!!! info={0}".format(info)) level = my_get(info, 'level', -1, int) weight = my_get(info, 'weight', -1, int) character = my_get(info, 'character',0, int) # int(info.get('weight',0)) emf_logger.debug("info={0}".format(info)) if character == '': character = 0 label = info.get('label', '') # we only want one form or one embedding if label != '': if format == 'sage': if character != 0: D = DirichletGroup(level) x = D[character] X = Newforms(x, weight, names='a') else: X = Newforms(level, weight, names='a') else: # format=='web_new': X = WebNewForm(level=level, weight=weight, character=character, label=label) s = dumps(X) name = "{0}-{1}-{2}-{3}-web_newform.sobj".format(weight, level, character, label) emf_logger.debug("name={0}".format(name)) info['filename'] = name strIO = StringIO.StringIO() strIO.write(s) strIO.seek(0) try: return send_file(strIO, attachment_filename=info["filename"], as_attachment=True) except IOError: info['error'] = "Could not send file!"
def render_one_maass_waveform(maass_id, **kwds): info = get_args_mwf(**kwds) info['maass_id'] = bson.objectid.ObjectId(maass_id) mwf_logger.debug("in_render_one_maass_form: info={0}".format(info)) if info.get('download', '') == 'coefficients1': DB = connect_db() fname = "" f = DB.get_maassform_by_id({"_id": info['maass_id']}, filename='True') dim = f.get('dim', 0) res = DB.get_coefficients({"_id": info['maass_id']}, filename='True') if isinstance(res, list): if isinstance(res, list): res = res[0][0] else: res = res[0] try: C, fname = res except: # ValueError: C = res mwf_logger.debug("res={0}".format(res)) filename = fname + '.txt' mwf_logger.debug("filename={0}".format(filename)) s = dumps(C) strIO = StringIO.StringIO() strIO.write(s) strIO.seek(0) try: return send_file(strIO, attachment_filename=filename, as_attachment=True) except IOError: info['error'] = "Could not send file!" else: # mwf_logger.debug("id1={0}".format(id)) return render_one_maass_waveform_wp(info)
def to_fs(self): return dumps(self._value)
def _test(self): from sage.all import QuadraticField, loads, dumps K = QuadraticField(-1, 'i') if loads(dumps(K.maximal_order())) is not K.maximal_order(): raise Exception("#24934 has not been fixed")
def dump_coefficients(self, prec): if prec is None: prec = self.prec return dumps(self.coefficients(range(prec)))
def print_list_of_coefficients(info): r""" Print a table of Fourier coefficients in the requested format """ level = my_get(info, 'level', -1, int) weight = my_get(info, 'weight', -1, int) prec = my_get(info, 'prec', 12, int) # number of digits bitprec = my_get(info, 'bitprec', 12, int) # number of digits character = my_get(info, 'character', '', str) # int(info.get('weight',0)) fmt = info.get("format","q_expansion") if character == '': character = '1' label = info.get('label', '') if character.isalnum(): character = int(character) else: return "The character '{0}' is not well-defined!".format(character) print "--------------" if label == '' or level == -1 or weight == -1: return "Need to specify a modular form completely!!" WMFS = WebModFormSpace(level= level, weight = weight, cuspidal=True,character = character) if not WMFS: return "" if('number' in info): number = int(info['number']) + 1 else: number = max(WMFS.sturm_bound + 1, 20) FS = list() f = WMFS.hecke_orbits.get(label) if f is not None: FS.append(f) else: for label in WMFS.hecke_orbits: FS.append(WMFS.f(label)) shead = "Cusp forms of weight " + str(weight) + "on \(" + latex(WMFS.group) + "\)" s = "" if((character is not None) and (character > 0)): shead = shead + " and character \( \chi_{" + str(character) + "}\)" # s="<table><tr><td>" coefs = "" if fmt == "sage": res = [] for F in FS: if number > F.max_cn(): raise IndexError,"The database does not contain this many ({0}) coefficients for this modular form! We only have {1}".format(number,F.max_cn()) if len(FS) > 1: if info['format'] == 'html': coefs += F.label() else: coefs += F.label() if fmt == "sage": qe = F.coefficients(range(number)) res.append(qe) else: coefs += print_coefficients_for_one_form(F, number, info['format'],bitprec=bitprec) if not fmt == "sage": return s+"\n"+coefs else: if len(res)==1: res = res[0] #print "res=",res return dumps(res)
def print_list_of_coefficients(info): r""" Print a table of Fourier coefficients in the requested format """ level = my_get(info, 'level', -1, int) weight = my_get(info, 'weight', -1, int) prec = my_get(info, 'prec', 12, int) # number of digits bitprec = my_get(info, 'bitprec', 12, int) # number of digits character = my_get(info, 'character', '', str) # int(info.get('weight',0)) fmt = info.get("format", "q_expansion") if character == '': character = '1' label = info.get('label', '') if character.isalnum(): character = int(character) else: return "The character '{0}' is not well-defined!".format(character) print "--------------" if label == '' or level == -1 or weight == -1: return "Need to specify a modular form completely!!" WMFS = WebModFormSpace(level=level, weight=weight, cuspidal=True, character=character) if not WMFS: return "" if ('number' in info): number = int(info['number']) + 1 else: number = max(WMFS.sturm_bound + 1, 20) FS = list() f = WMFS.hecke_orbits.get(label) if f is not None: FS.append(f) else: for label in WMFS.hecke_orbits: FS.append(WMFS.f(label)) shead = "Cusp forms of weight " + str(weight) + "on \(" + latex( WMFS.group) + "\)" s = "" if ((character is not None) and (character > 0)): shead = shead + " and character \( \chi_{" + str(character) + "}\)" # s="<table><tr><td>" coefs = "" if fmt == "sage": res = [] for F in FS: if number > F.max_cn(): raise IndexError, "The database does not contain this many ({0}) coefficients for this modular form! We only have {1}".format( number, F.max_cn()) if len(FS) > 1: if info['format'] == 'html': coefs += F.label() else: coefs += F.label() if fmt == "sage": qe = F.coefficients(range(number)) res.append(qe) else: coefs += print_coefficients_for_one_form(F, number, info['format'], bitprec=bitprec) if not fmt == "sage": return s + "\n" + coefs else: if len(res) == 1: res = res[0] #print "res=",res return dumps(res)