Skip to content

Commit

Permalink
Merge branch 'release/v0.2.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
wpk committed Apr 6, 2023
2 parents 3d727db + 65bdcc4 commit 1f28060
Show file tree
Hide file tree
Showing 24 changed files with 3,311 additions and 2,234 deletions.
15 changes: 8 additions & 7 deletions .cruft.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"template": "https://github.com/wpk-nist-gov/cookiecutter-pypackage.git",
"commit": "54f802e17db2a2f922595fc6a9401288c18ed509",
"commit": "c209d4c07a6142de87d6b1600f7319ebc69729cd",
"checkout": "feature/markdown",
"skip": [
"src/thermoextrap/__init__.py",
"environment.yaml",
"HISTORY.md",
"AUTHORS.md",
"MANIFEST.in",
"docs/spelling_wordlist.txt",
"environment/",
"environment.yaml",
"HISTORY.md",
"AUTHORS.md",
"MANIFEST.in",
"README.md",
"docs/spelling_wordlist.txt",
"environment/"
],
"context": {
"cookiecutter": {
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ repos:
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
- id: black-jupyter
- repo: https://github.com/adamchainz/blacken-docs
rev: "1.13.0" # replace with latest tag on GitHub
hooks:
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,16 @@ environment-files: environment/dev.yaml environment/docs.yaml environment/test.y
.PHONY: mamba-env mamba-dev mamba-env-update mamba-dev-update

mamba-env: environment.yaml ## create base environment
mamba env create -f environment.yaml
mamba env create -f $<

mamba-env-update: environment.yaml ## update base environment
mamba env update -f environment.yml
mamba env update -f $<

mamba-dev: environment/dev.yaml ## create development environment
mamba env create -f environment-dev.yaml
mamba env create -f $<

mamba-dev-update: environment/dev.yaml ## update development environment
mamba env update -f environment-dev.yml
mamba env update -f $<

################################################################################
# TOX
Expand Down
84 changes: 40 additions & 44 deletions docs/notebooks/Customized_Derivatives.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
"\n",
" @staticmethod\n",
" def create_deriv_func(order):\n",
"\n",
" def func(W, xW, dxdq, volume, ndim=1):\n",
" \"\"\"\n",
" dxdq is <sum_{i=1}^N dy/dx_i x_i>\n",
Expand All @@ -137,7 +136,7 @@
" deriv_val = (-xW[0] * W[1] + xW[1] + dxdq) / (volume * ndim)\n",
" return deriv_val\n",
"\n",
" return func\n"
" return func"
]
},
{
Expand Down Expand Up @@ -172,19 +171,19 @@
"# Define our reference size to extrapolated from\n",
"volume_ref = volumes[9]\n",
"\n",
"npart = 1000 #Number of particles (in single configuration)\n",
"nconfig = 100_000 #Number of configurations\n",
"npart = 1000 # Number of particles (in single configuration)\n",
"nconfig = 100_000 # Number of configurations\n",
"\n",
"# And generate new data at this reference size, using a beta of 1.0 for simplicity\n",
"xdatavol, udatavol = idealgas.generate_data((nconfig, npart), beta=beta, vol=volume_ref)\n",
"\n",
"# Wrap with xarray\n",
"xdatavol = xr.DataArray(xdatavol, dims=['rec'])\n",
"udatavol = xr.DataArray(udatavol, dims=['rec'])\n",
"xdatavol = xr.DataArray(xdatavol, dims=[\"rec\"])\n",
"udatavol = xr.DataArray(udatavol, dims=[\"rec\"])\n",
"\n",
"#For the IG model, the virial W is the same as the negative of the number of particles multiplied by the average position\n",
"#(for a=1)\n",
"#And we have to multiply by beta to make the virial dimensionless\n",
"# For the IG model, the virial W is the same as the negative of the number of particles multiplied by the average position\n",
"# (for a=1)\n",
"# And we have to multiply by beta to make the virial dimensionless\n",
"wdatavol = -1.0 * npart * xdatavol"
]
},
Expand Down Expand Up @@ -218,22 +217,18 @@
"from thermoextrap.data import DataCallbackABC\n",
"import attrs\n",
"\n",
"\n",
"@attrs.define\n",
"class VolumeDataCallback(DataCallbackABC):\n",
" \"\"\"\n",
" object to handle callbacks of metadata\n",
" \"\"\"\n",
" \n",
" volume: float = attrs.field(\n",
" validator=attrs.validators.instance_of(float)\n",
" )\n",
"\n",
" volume: float = attrs.field(validator=attrs.validators.instance_of(float))\n",
" dxdqv: xr.DataArray = attrs.field(\n",
" validator=attrs.validators.instance_of(xr.DataArray)\n",
" )\n",
" ndim: int = attrs.field(\n",
" default=3, \n",
" validator=attrs.validators.instance_of(int)\n",
" )\n",
" ndim: int = attrs.field(default=3, validator=attrs.validators.instance_of(int))\n",
"\n",
" def check(self, data):\n",
" pass\n",
Expand All @@ -254,7 +249,7 @@
" self.dxdq(rec_dim=data.rec_dim, skipna=data.skipna),\n",
" self.volume,\n",
" self.ndim,\n",
" )\n"
" )"
]
},
{
Expand Down Expand Up @@ -296,7 +291,7 @@
" # dxdqv = single observation of sum(dx/dq_i q_i) where q_i is the ith coordinate\n",
" # for ideal gas, this is just xdata\n",
" dxdqv=xdatavol,\n",
" ndim=1\n",
" ndim=1,\n",
")"
]
},
Expand Down Expand Up @@ -401,19 +396,19 @@
}
],
"source": [
"#Check the computation of derivatives\n",
"# Check the computation of derivatives\n",
"print(\"Model parameters (derivatives):\")\n",
"print(xemv.derivs(norm=False).values)\n",
"print('\\n')\n",
"print(\"\\n\")\n",
"\n",
"#Finally, look at predictions\n",
"# Finally, look at predictions\n",
"print(\"Model predictions:\")\n",
"print(xemv.predict(volumes[:4]).values)\n",
"print('\\n')\n",
"print(\"\\n\")\n",
"\n",
"#And bootstrapped uncertainties\n",
"# And bootstrapped uncertainties\n",
"print(\"Bootstrapped uncertainties in predictions:\")\n",
"print(xemv.resample(nrep=100).predict(volumes[:4]).std('rep').values)"
"print(xemv.resample(nrep=100).predict(volumes[:4]).std(\"rep\").values)"
]
},
{
Expand Down Expand Up @@ -455,43 +450,44 @@
"source": [
"fig, ax = plt.subplots()\n",
"\n",
"nsampvals = np.array((10.0*np.ones(5))**np.arange(1,6), dtype=int)\n",
"nsampcolors = plt.cm.viridis(np.arange(0.0, 1.0, float(1.0/len(nsampvals))))\n",
"nsampvals = np.array((10.0 * np.ones(5)) ** np.arange(1, 6), dtype=int)\n",
"nsampcolors = plt.cm.viridis(np.arange(0.0, 1.0, float(1.0 / len(nsampvals))))\n",
"\n",
"#First plot the analytical result\n",
"ax.plot(volumes, idealgas.x_ave(beta, volumes), 'k--', linewidth=2.0)\n",
"# First plot the analytical result\n",
"ax.plot(volumes, idealgas.x_ave(beta, volumes), \"k--\", linewidth=2.0)\n",
"\n",
"#And the infinite sampling results for first order extrapolation\n",
"# And the infinite sampling results for first order extrapolation\n",
"trueExtrap, trueDerivs = idealgas.x_vol_extrap(1, volume_ref, volumes, beta=beta)\n",
"ax.plot(volumes, trueExtrap, 'r-', zorder=0)\n",
"print('True extrapolation coefficients: {}'.format(trueDerivs))\n",
"ax.plot(volumes, trueExtrap, \"r-\", zorder=0)\n",
"print(\"True extrapolation coefficients: {}\".format(trueDerivs))\n",
"\n",
"for i, n in enumerate(nsampvals):\n",
" thisinds = np.random.choice(len(xdatavol), size=n, replace=False)\n",
" \n",
" #Get parameters for extrapolation model with this data by training it - the parameters are the derivatives\n",
"\n",
" # Get parameters for extrapolation model with this data by training it - the parameters are the derivatives\n",
" xemv = xtrap.volume.factory_extrapmodel(\n",
" volume=volume_ref,\n",
" # this is acually w\n",
" uv=wdatavol[thisinds],\n",
" xv=xdatavol[thisinds],\n",
" dxdqv=xdatavol[thisinds],\n",
" ndim=1\n",
" ndim=1,\n",
" )\n",
" \n",
"\n",
" out = xemv.predict(volumes)\n",
" print('\\t With N_configs = %6i: %s'%(n, str(xemv.derivs(norm=False).values)))\n",
" \n",
" out.plot(marker='s', ms=4, color=nsampcolors[i], ls='None', label='N={}'.format(n), \n",
" ax=ax)\n",
" print(\"\\t With N_configs = %6i: %s\" % (n, str(xemv.derivs(norm=False).values)))\n",
"\n",
" out.plot(\n",
" marker=\"s\", ms=4, color=nsampcolors[i], ls=\"None\", label=\"N={}\".format(n), ax=ax\n",
" )\n",
"\n",
"ax.set_ylabel(r'$\\langle x \\rangle$')\n",
"ax.set_xlabel(r'$L$')\n",
"ax.set_ylabel(r\"$\\langle x \\rangle$\")\n",
"ax.set_xlabel(r\"$L$\")\n",
"\n",
"ax.annotate('O(1) Extrapolation', xy=(0.4, 0.7), xycoords='axes fraction', fontsize=9)\n",
"ax.annotate(\"O(1) Extrapolation\", xy=(0.4, 0.7), xycoords=\"axes fraction\", fontsize=9)\n",
"\n",
"ax.set_ylim((-1.0, 4.0))\n",
"ax.yaxis.set_major_locator(mpl.ticker.MaxNLocator(nbins=4, prune='both'))\n",
"ax.yaxis.set_major_locator(mpl.ticker.MaxNLocator(nbins=4, prune=\"both\"))\n",
"\n",
"fig.tight_layout()\n",
"fig.subplots_adjust(hspace=0.0)\n",
Expand Down
74 changes: 45 additions & 29 deletions docs/notebooks/Data_Organization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@
},
"outputs": [],
"source": [
"#Import idealgas module\n",
"# Import idealgas module\n",
"from thermoextrap import idealgas\n",
"\n",
"#Define reference beta\n",
"# Define reference beta\n",
"beta_ref = 5.6\n",
"\n",
"#And maximum order\n",
"# And maximum order\n",
"order = 2\n",
"\n",
"npart = 1000 #Number of particles (in single configuration)\n",
"nconfig = 100_000 #Number of configurations\n",
"npart = 1000 # Number of particles (in single configuration)\n",
"nconfig = 100_000 # Number of configurations\n",
"\n",
"#Generate all the data we could want\n",
"# Generate all the data we could want\n",
"xdata, udata = idealgas.generate_data((nconfig, npart), beta_ref)"
]
},
Expand Down Expand Up @@ -148,8 +148,8 @@
},
"outputs": [],
"source": [
"xdata = xr.DataArray(xdata, dims=['rec'])\n",
"udata = xr.DataArray(udata, dims=['rec'])"
"xdata = xr.DataArray(xdata, dims=[\"rec\"])\n",
"udata = xr.DataArray(udata, dims=[\"rec\"])"
]
},
{
Expand All @@ -176,9 +176,9 @@
},
"outputs": [],
"source": [
"data = xtrap.DataCentralMomentsVals.from_vals(order=order, \n",
" rec_dim='rec',\n",
" xv=xdata, uv=udata, central=True)"
"data = xtrap.DataCentralMomentsVals.from_vals(\n",
" order=order, rec_dim=\"rec\", xv=xdata, uv=udata, central=True\n",
")"
]
},
{
Expand Down Expand Up @@ -598,7 +598,7 @@
}
],
"source": [
"data.xv\n"
"data.xv"
]
},
{
Expand Down Expand Up @@ -3875,7 +3875,9 @@
}
],
"source": [
"data_noboot = xtrap.DataCentralMoments.from_vals(xv=xdata, uv=udata, rec_dim='rec', central=True, order=order)\n",
"data_noboot = xtrap.DataCentralMoments.from_vals(\n",
" xv=xdata, uv=udata, rec_dim=\"rec\", central=True, order=order\n",
")\n",
"xr.testing.assert_allclose(data_noboot.values, data.values)\n",
"data_noboot.values"
]
Expand Down Expand Up @@ -3916,7 +3918,7 @@
"try:\n",
" data_noboot.resample(nrep=3).values\n",
"except ValueError as e:\n",
" print('caught error!')\n",
" print(\"caught error!\")\n",
" print(e)"
]
},
Expand Down Expand Up @@ -4380,8 +4382,8 @@
"outputs": [],
"source": [
"# Make 100 averaged observations\n",
"xx = xr.DataArray(xdata.values.reshape(100, -1), dims=['rec', 'block'])\n",
"uu = xr.DataArray(udata.values.reshape(100, -1), dims=['rec', 'block'])"
"xx = xr.DataArray(xdata.values.reshape(100, -1), dims=[\"rec\", \"block\"])\n",
"uu = xr.DataArray(udata.values.reshape(100, -1), dims=[\"rec\", \"block\"])"
]
},
{
Expand Down Expand Up @@ -4902,7 +4904,9 @@
"# Create directly from values of moments - notice that this is DataCentralMoments, not DataCentralMomentsVals\n",
"# Effectively just means that the 'rec' dim will not be collapsed when using data for extrapolation, etc.\n",
"# Behaves similarly to the 'rep' dim when resampling\n",
"data_fv = xtrap.DataCentralMoments.from_vals(xv=xx, uv=uu, dim='block', order=order, central=True)\n",
"data_fv = xtrap.DataCentralMoments.from_vals(\n",
" xv=xx, uv=uu, dim=\"block\", order=order, central=True\n",
")\n",
"# So 'rec' is for each separate block average\n",
"data_fv.values"
]
Expand Down Expand Up @@ -5361,10 +5365,12 @@
"outputs": [],
"source": [
"# Compute moments of U, i.e., averages to integer powers up to maximum order desired\n",
"mom_u = xr.DataArray(np.arange(order + 1), dims=['umom'])\n",
"uave = (uu ** mom_u).mean('block')\n",
"xuave = (xx * uu ** mom_u).mean('block')\n",
"data_fa = xtrap.DataCentralMoments.from_ave_raw(u=uave, xu=xuave, central=True, w=xx.sizes['block'])\n",
"mom_u = xr.DataArray(np.arange(order + 1), dims=[\"umom\"])\n",
"uave = (uu**mom_u).mean(\"block\")\n",
"xuave = (xx * uu**mom_u).mean(\"block\")\n",
"data_fa = xtrap.DataCentralMoments.from_ave_raw(\n",
" u=uave, xu=xuave, central=True, w=xx.sizes[\"block\"]\n",
")\n",
"\n",
"xr.testing.assert_allclose(data_fv.values, data_fa.values)"
]
Expand Down Expand Up @@ -5404,10 +5410,14 @@
"outputs": [],
"source": [
"# Extrapolate both average x and average x**2\n",
"x_xsq_data = xr.DataArray(np.vstack([xdata.values, xdata.values**2]).T, dims=['rec', 'vals'], coords={'vals': ['x','xsq']})\n",
"data_vec = xtrap.DataCentralMomentsVals.from_vals(order=order, \n",
" rec_dim='rec',\n",
" xv=x_xsq_data, uv=udata, central=True)"
"x_xsq_data = xr.DataArray(\n",
" np.vstack([xdata.values, xdata.values**2]).T,\n",
" dims=[\"rec\", \"vals\"],\n",
" coords={\"vals\": [\"x\", \"xsq\"]},\n",
")\n",
"data_vec = xtrap.DataCentralMomentsVals.from_vals(\n",
" order=order, rec_dim=\"rec\", xv=x_xsq_data, uv=udata, central=True\n",
")"
]
},
{
Expand Down Expand Up @@ -6302,9 +6312,15 @@
},
"outputs": [],
"source": [
"xx_xsqxsq = xr.DataArray(x_xsq_data.values.reshape(100, -1, 2), dims=['rec', 'block', 'vals'], coords={'vals': ['x','xsq']})\n",
"x_xsq_uave = (xx_xsqxsq * uu ** mom_u).mean('block')\n",
"data_fa_vec = xtrap.DataCentralMoments.from_ave_raw(u=uave, xu=x_xsq_uave, central=True, w = xx_xsqxsq.sizes['block'])"
"xx_xsqxsq = xr.DataArray(\n",
" x_xsq_data.values.reshape(100, -1, 2),\n",
" dims=[\"rec\", \"block\", \"vals\"],\n",
" coords={\"vals\": [\"x\", \"xsq\"]},\n",
")\n",
"x_xsq_uave = (xx_xsqxsq * uu**mom_u).mean(\"block\")\n",
"data_fa_vec = xtrap.DataCentralMoments.from_ave_raw(\n",
" u=uave, xu=x_xsq_uave, central=True, w=xx_xsqxsq.sizes[\"block\"]\n",
")"
]
},
{
Expand Down Expand Up @@ -6711,7 +6727,7 @@
}
],
"source": [
"data_fa_vec.reduce('rec').values"
"data_fa_vec.reduce(\"rec\").values"
]
},
{
Expand Down
Loading

0 comments on commit 1f28060

Please sign in to comment.