Skip to content

Commit

Permalink
fix repr/pickle
Browse files Browse the repository at this point in the history
  • Loading branch information
cmbant committed Jan 8, 2024
1 parent ecbe756 commit 856f237
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions camb/baseconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def AllocatableObjectArray(cls=None):


class AllocatableArrayInt(_AllocatableArray):
_dtype = int
_dtype = np.int32
_ctype = c_int


Expand Down Expand Up @@ -646,7 +646,7 @@ def __getstate__(self) -> dict:
state = {}
for field_name, field_type in self.get_all_fields():
obj = getattr(self, field_name)
if isinstance(obj, (ctypes.Array, np.ndarray)):
if isinstance(obj, (ctypes.Array, np.ndarray, FortranAllocatable)):
state[field_name] = list(obj[:len(obj)])
elif isinstance(obj, ctypes.c_void_p):
if obj.value is not None:
Expand Down
5 changes: 5 additions & 0 deletions camb/tests/camb_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,18 @@ def testSave(self):
pars = camb.set_params(H0=67.5, ombh2=0.022, omch2=0.122, As=2e-9, ns=0.95,
redshifts=[0.4, 31.5], kmax=0.1)
pars.set_dark_energy(w=-0.7, wa=0.2, dark_energy_model='ppf')
from camb.sources import GaussianSourceWindow
pars.SourceWindows = [GaussianSourceWindow(), GaussianSourceWindow(redshift=1)]
s = repr(pars)
pars2 = eval(s)
assert (repr(pars2) == s)
assert "DarkEnergyPPF" in str(pars2)
b = pickle.dumps(pars)
pars2 = pickle.loads(b)
assert (repr(pars2) == s)
pars2.InitPower = initialpower.SplinedInitialPower()
with self.assertRaises(TypeError):
repr(pars2)

def testSigmaR(self):
pars = camb.CAMBparams()
Expand Down
7 changes: 4 additions & 3 deletions docs/CAMBdemo.html
Original file line number Diff line number Diff line change
Expand Up @@ -14640,7 +14640,7 @@


<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain">
<pre>Using CAMB 1.5.1 installed at c:\work\dist\git\camb\camb
<pre>Using CAMB 1.5.3 installed at c:\work\dist\git\camb\camb
</pre>
</div>
</div>
Expand Down Expand Up @@ -15193,6 +15193,7 @@
<span class="n">pars</span> <span class="o">=</span> <span class="n">camb</span><span class="o">.</span><span class="n">read_ini</span><span class="p">(</span><span class="s1">&#39;https://raw.githubusercontent.com/cmbant/CAMB/master/inifiles/planck_2018.ini&#39;</span><span class="p">)</span>
<span class="c1"># for a local github installation you can just do </span>
<span class="c1"># pars=camb.read_ini(os.path.join(camb_path,&#39;inifiles&#39;,&#39;planck_2018.ini&#39;))</span>
<span class="c1"># view parameter objects using print(), or use pickle or repr to save and restore</span>
<span class="nb">print</span><span class="p">(</span><span class="n">pars</span><span class="p">)</span>
</pre></div>

Expand Down Expand Up @@ -16498,7 +16499,7 @@
<span class="k">for</span> <span class="n">z</span> <span class="ow">in</span> <span class="n">zplot</span><span class="p">:</span>
<span class="n">plt</span><span class="o">.</span><span class="n">loglog</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">PK</span><span class="o">.</span><span class="n">P</span><span class="p">(</span><span class="n">z</span><span class="p">,</span><span class="n">k</span><span class="p">))</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlim</span><span class="p">([</span><span class="mf">1e-4</span><span class="p">,</span><span class="n">kmax</span><span class="p">])</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">&#39;k Mpc&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;k Mpc&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">&#39;$P_\Psi\, Mpc^{-3}$&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">([</span><span class="s1">&#39;z=</span><span class="si">%s</span><span class="s1">&#39;</span><span class="o">%</span><span class="k">z</span> for z in zplot]);
</pre></div>
Expand Down Expand Up @@ -18763,7 +18764,7 @@ <h3 id="Animations">Animations<a class="anchor-link" href="#Animations">&#182;</


<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="application/vnd.jupyter.stderr">
<pre>C:\Users\Antony\AppData\Local\Temp\ipykernel_29556\455262666.py:12: UserWarning: Data has no positive values, and therefore cannot be log-scaled.
<pre>C:\Users\Antony\AppData\Local\Temp\ipykernel_43884\455262666.py:12: UserWarning: Data has no positive values, and therefore cannot be log-scaled.
ax.set_xlim(k[0], k[-1])
</pre>
</div>
Expand Down

0 comments on commit 856f237

Please sign in to comment.