Skip to content

Commit

Permalink
Merge pull request #24 from praksharma/development
Browse files Browse the repository at this point in the history
Major improvements in geometry and sampling
  • Loading branch information
praksharma committed Jun 20, 2023
2 parents eaa749d + b07fcc8 commit 6f656ce
Show file tree
Hide file tree
Showing 7 changed files with 532 additions and 29 deletions.
13 changes: 6 additions & 7 deletions DeepINN/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

from . import backend
from . import config
# from . import callbacks
# from . import data
from . import geometry
# from . import gradients as grad
# from . import BC
# from . import nn
#from . import utils

from .geometry import spaces
from .geometry import domains
from .geometry import samplers


2 changes: 1 addition & 1 deletion DeepINN/geometry/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Make the subdirectory callable
from . import domains
from . import conditions
#from . import conditions
from . import samplers
from . import spaces
3 changes: 2 additions & 1 deletion DeepINN/geometry/domains/domainoperations/sampler_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import torch
import warnings

from torchphysics.problem.spaces.points import Points
from ....geometry.spaces.points import Points # some relative path fixing
#from torchphysics.problem.spaces.points import Points


def _inside_random_with_n(main_domain, domain_a, domain_b, n, params, invert, device):
Expand Down
56 changes: 39 additions & 17 deletions DeepINN/utils/plotting/scatter_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import matplotlib.pyplot as plt


def scatter(subspace, *samplers):
def scatter(subspace, *samplers, dpi=False, save=False):
"""Shows (one batch) of used points in the training. If the sampler is
static, the shown points will be the points for the training. If not
the points may vary, depending of the sampler.
Expand All @@ -19,22 +19,32 @@ def scatter(subspace, *samplers):
The diffrent samplers for which the points should be plotted.
The plot for each sampler will be created in the order there were
passed in.
dpi : dpi of the saved figure.
save : A flag to save figure.
In future, I might use a dictionary to add more functions.
Returns
-------
fig : matplotlib.pyplot.figure
The figure handle of the plot.
The figure handle of the plot. The Figure is created on the STDOUT, so actual return isn't required.
"""
assert subspace.dim <= 3, "Can only scatter points in dimensions <= 3."

fig, ax, scatter_fn = _choose_scatter_function(subspace.dim)
ax.grid()
#scatter_fn = _choose_scatter_function(subspace.dim)

for sampler in samplers:
points = sampler.sample_points()[:, list(subspace.keys())]
numpy_points = points.as_tensor.detach().cpu().numpy()
labels = _create_labels(subspace)
scatter_fn(ax, numpy_points, labels)
return fig
#scatter_fn(numpy_points, labels)

if subspace.dim == 1:
_scatter_1D(numpy_points, labels, dpi, save)
elif subspace.dim == 2:
_scatter_2D(numpy_points, labels, dpi, save)
else:
_scatter_3D(numpy_points, labels, dpi, save)

def _create_labels(subspace):
labels = []
Expand All @@ -47,31 +57,43 @@ def _create_labels(subspace):
return labels

def _choose_scatter_function(space_dim):
fig = plt.figure()

if space_dim == 1:
ax = fig.add_subplot()
return fig, ax, _scatter_1D
return _scatter_1D
elif space_dim == 2:
ax = fig.add_subplot()
return fig, ax, _scatter_2D
return _scatter_2D
else:
ax = fig.add_subplot(projection='3d')
return fig, ax, _scatter_3D
return _scatter_3D


def _scatter_1D(ax, points, labels):
def _scatter_1D(points, labels, dpi, save):
fig = plt.figure()
ax = fig.add_subplot()
ax.grid()
ax.scatter(points, np.zeros_like(points))
ax.set_xlabel(labels[0])
if save:
plt.savefig('geom.jpg', dpi = dpi,bbox_inches='tight',transparent=True)


def _scatter_2D(ax, points, labels):
def _scatter_2D(points, labels, dpi, save):
fig = plt.figure()
ax = fig.add_subplot()
ax.grid()
ax.scatter(points[:, 0], points[:, 1])
ax.set_xlabel(labels[0])
ax.set_ylabel(labels[1])
if save:
plt.savefig('geom.jpg', dpi = dpi,bbox_inches='tight',transparent=True)


def _scatter_3D(ax, points, labels):
def _scatter_3D(points, labels, dpi, save):
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.grid()
ax.scatter(points[:, 0], points[:, 1], points[:, 2])
ax.set_xlabel(labels[0])
ax.set_ylabel(labels[1])
ax.set_zlabel(labels[2])
ax.set_zlabel(labels[2])
if save:
plt.savefig('geom.jpg', dpi = dpi,bbox_inches='tight',transparent=True)
65 changes: 65 additions & 0 deletions Tutorials/Basic_domains.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# This is only valid when the package is not installed\n",
"import sys\n",
"sys.path.append('/home/hell/Desktop/repos/DeepINN/')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using default backend: PyTorch\n",
"Using Pytorch: 2.0.1+cu117\n"
]
}
],
"source": [
"import DeepINN as dp"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"T = dp.spaces.R1('t') # we need a one dimensional space\n",
"I = dp.domains.Interval(T,0,5 )"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 6f656ce

Please sign in to comment.