Skip to content

Commit

Permalink
cache sqlite connection
Browse files Browse the repository at this point in the history
use the proper matrix mult syntax
  • Loading branch information
segasai committed Nov 29, 2024
1 parent 7317b57 commit e0b7acb
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions py/rvspecfit/read_grid.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from __future__ import print_function
import argparse
import itertools
import functools
import warnings
import glob
import sys
import os
Expand All @@ -7,9 +10,6 @@
import scipy.stats
import scipy.sparse
import numpy as np
import argparse
import itertools
import warnings


def gau_integrator(A, B, x1, x2, l1, l2, s):
Expand Down Expand Up @@ -244,6 +244,12 @@ def makedb(prefix='/PHOENIX-ACES-AGSS-COND-2011/',
DB.commit()


@functools.lru_cache(None)
def _get_dbconn(dbfile):
conn = sqlite3.connect(dbfile)
return conn


def get_spec(params, dbfile=None, prefix=None, wavefile=None):
""" Returns individual spectra for a given spectral parameters
Expand Down Expand Up @@ -282,7 +288,7 @@ def get_spec(params, dbfile=None, prefix=None, wavefile=None):
query += ' and '
query += (f' {k} between {v1} and {v2} ')

conn = sqlite3.connect(dbfile)
conn = _get_dbconn(dbfile)
cur = conn.cursor()
cur.execute(query)
fnames = cur.fetchall()
Expand All @@ -292,9 +298,9 @@ def get_spec(params, dbfile=None, prefix=None, wavefile=None):
raise Exception('No spectra found')

dat = pyfits.getdata(prefix + '/' + fnames[0][0])
speclen = len(dat)
lams = np.arange(speclen) * 1.
dat = dat.astype(dat.dtype.newbyteorder('=')) # convert to native
lams = pyfits.getdata(wavefile)
lams = lams.astype(lams.dtype.newbyteorder('='))
print('Using', fnames[0][0])
return lams, dat

Expand Down Expand Up @@ -409,7 +415,7 @@ def make_rebinner(lam00,


def apply_rebinner(mat, spec0):
ret = np.array(spec0 * mat)
ret = np.array(spec0 @ mat)
return ret


Expand Down

0 comments on commit e0b7acb

Please sign in to comment.