Skip to content

Commit 0e91fb8

Browse files
make flex and lintsampler optional
1 parent dd6b209 commit 0e91fb8

5 files changed

Lines changed: 48 additions & 10 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
*__pycache__/
22
*.DS_Store
33

4+
src/discmodel.egg*
5+
46
.coverage
57
docs/build/
68
dist/

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,22 @@ git clone https://github.com/michael-petersen/discmodel.git
1616
pip install .
1717
```
1818

19+
## Quickstart example
20+
21+
```
22+
import discmodel
23+
24+
N= 1_000_000
25+
a = 1.0
26+
M = 1.0
27+
rmax = 5.0
28+
nbins = 100
29+
mmax,nmax = 2,8
30+
31+
# generate N distributed points
32+
D = discmodel.DiscGalaxy(N=N, a=a, M=M)
33+
D.generate_image(rmax=rmax, nbins=nbins)
34+
35+
# now you have
36+
D.img
37+
```

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ requires = [
88
build-backend = "setuptools.build_meta"
99

1010
[project]
11-
name = "flex"
11+
name = "discmodel"
1212
authors = [
1313
{ name="Michael S. Petersen", email="michael.petersen@roe.ac.uk" },
1414
]

src/discmodel/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
See README.md for details
77
"""
88
from .discmodel import DiscGalaxy
9+
from importlib.metadata import version
910

1011
__version__ = version("discmodel")
1112
__all__ = ["DiscGalaxy"]

src/discmodel/discmodel.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@
77
# for interpolation
88
from scipy import interpolate
99

10-
# for resampling technology
11-
#from lintsampler import LintSampler
12-
#import lintsampler
10+
# check if lintsampler is available
11+
try:
12+
import lintsampler
13+
HAS_LINTSAMPLER = True
14+
except ImportError:
15+
HAS_LINTSAMPLER = False
16+
17+
# check if flex is available
18+
try:
19+
import flex
20+
HAS_FLEX = True
21+
except ImportError:
22+
HAS_FLEX = False
1323

14-
# if you leave LaguerreAmplitudes in a different file
15-
from FLEXbase import LaguerreAmplitudes
1624

1725

1826

@@ -205,6 +213,9 @@ def generate_image(self,rmax,nbins,noiselevel=-1.0):
205213

206214
def make_expansion(self,mmax,nmax,rscl,xmax=10000.,noisy=False):
207215

216+
if not HAS_FLEX:
217+
raise ImportError("flex is not available. Please install flex to use this method.")
218+
208219
try:
209220
snapshot = self.img
210221
except:
@@ -230,24 +241,29 @@ def make_expansion(self,mmax,nmax,rscl,xmax=10000.,noisy=False):
230241
#phi[gvals] = np.nan
231242
snapshotflat[gvals] = np.nan
232243

233-
laguerre = LaguerreAmplitudes(rscl,mmax,nmax,rval,phi,snapshotflat)
244+
laguerre = flex.FLEX(rscl,mmax,nmax,rval,phi,mass=snapshotflat)
234245

235246
return laguerre
236247

237248
def make_particle_expansion(self,mmax,nmax,rscl,xmax=10000.,noisy=False):
238249

239-
# no guards here yet, please add one!
250+
if not HAS_FLEX:
251+
raise ImportError("flex is not available. Please install flex to use this method.")
240252

241253
rval = np.sqrt(self.x**2+self.y**2)
242254
phi = np.arctan2(self.y,self.x)
243255
mass = (self.M/self.N)*np.ones(rval.size) # this assumes equal weights; the sqrt avoids double counting
244256

245257
# this assumes equal weights
246-
laguerre = LaguerreAmplitudes(rscl,mmax,nmax,rval,phi,mass=mass)
258+
laguerre = flex.FLEX(rscl,mmax,nmax,rval,phi,mass=mass)
247259

248260
return laguerre
249261

250262
def resample_expansion(self,E):
263+
264+
if not HAS_LINTSAMPLER:
265+
raise ImportError("lintsampler is not available. Please install lintsampler to use this method.")
266+
251267
def rndmpdf(X): return np.random.uniform()
252268
g = lintsampler.DensityGrid((self.x_centers,self.x_centers), rndmpdf)
253269

@@ -256,7 +272,7 @@ def rndmpdf(X): return np.random.uniform()
256272

257273
g.masses = g._calculate_faverages() * g._calculate_volumes()
258274
g._total_mass = np.sum(g.masses)
259-
pos = LintSampler(g).sample(self.N)
275+
pos = lintsampler.LintSampler(g).sample(self.N)
260276
return pos
261277

262278
def compute_a1(self,E):

0 commit comments

Comments
 (0)