-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Blob labels returned #37
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
47ed576
test git hooks
gregordecristoforo 74f0193
remove previous changes
gregordecristoforo a0e5562
docstrings of Model class updated
gregordecristoforo e1e80df
docstrings of Model class updated with README description
gregordecristoforo 80a6cad
blob labels returned as separate variable
gregordecristoforo b9054bd
tests for blob lables implemented
gregordecristoforo 213591a
make_realization() split in multiple methods
gregordecristoforo 55cf9cf
reduced code duplication in Model class
gregordecristoforo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from blobmodel import Model, show_model | ||
import numpy as np | ||
|
||
# here you can define your custom parameter distributions | ||
|
||
bm = Model( | ||
Nx=100, | ||
Ny=100, | ||
Lx=20, | ||
Ly=20, | ||
dt=0.1, | ||
T=20, | ||
periodic_y=True, | ||
blob_shape="gauss", | ||
num_blobs=10, | ||
t_drain=1e10, | ||
) | ||
|
||
# create data | ||
ds = bm.make_realization(speed_up=True, error=1e-2, labels=True) | ||
|
||
print(ds) | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
ds.n.isel(t=-1).plot() | ||
plt.figure() | ||
ds.blob_labels.isel(t=-1).plot() | ||
|
||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
from blobmodel import Model, BlobFactory, Blob | ||
import numpy as np | ||
import warnings | ||
|
||
|
||
# here you can define your custom parameter distributions | ||
class CustomBlobFactory(BlobFactory): | ||
def __init__(self) -> None: | ||
pass | ||
|
||
def sample_blobs( | ||
self, Ly: float, T: float, num_blobs: int, blob_shape: str, t_drain: float | ||
) -> list[Blob]: | ||
|
||
# set custom parameter distributions | ||
__amp = np.ones(num_blobs) | ||
__width = np.ones(num_blobs) | ||
__vx = np.ones(num_blobs) | ||
__vy = np.zeros(num_blobs) | ||
|
||
__posx = np.zeros(num_blobs) | ||
__posy = np.ones(num_blobs) * Ly / 2 | ||
__t_init = np.ones(num_blobs) * 0 | ||
|
||
return [ | ||
Blob( | ||
id=i, | ||
blob_shape=blob_shape, | ||
amplitude=__amp[i], | ||
width_prop=__width[i], | ||
width_perp=__width[i], | ||
v_x=__vx[i], | ||
v_y=__vy[i], | ||
pos_x=__posx[i], | ||
pos_y=__posy[i], | ||
t_init=__t_init[i], | ||
t_drain=t_drain, | ||
) | ||
for i in range(num_blobs) | ||
] | ||
|
||
|
||
def test_bloblabels_speedup(): | ||
warnings.filterwarnings("ignore") | ||
bf = CustomBlobFactory() | ||
bm = Model( | ||
Nx=5, | ||
Ny=1, | ||
Lx=5, | ||
Ly=5, | ||
dt=1, | ||
T=5, | ||
periodic_y=True, | ||
blob_shape="gauss", | ||
num_blobs=1, | ||
blob_factory=bf, | ||
t_drain=1e10, | ||
) | ||
ds = bm.make_realization(speed_up=True, error=1e-2, labels=True) | ||
correct_labels = np.array( | ||
[ | ||
[ | ||
[1.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 1.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 1.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 1.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 1.0], | ||
] | ||
] | ||
) | ||
diff = ds["blob_labels"].values - correct_labels | ||
assert np.max(diff) < 0.00001 | ||
|
||
|
||
def test_bloblabels(): | ||
warnings.filterwarnings("ignore") | ||
bf = CustomBlobFactory() | ||
bm = Model( | ||
Nx=5, | ||
Ny=1, | ||
Lx=5, | ||
Ly=5, | ||
dt=1, | ||
T=5, | ||
periodic_y=True, | ||
blob_shape="gauss", | ||
num_blobs=1, | ||
blob_factory=bf, | ||
t_drain=1e10, | ||
) | ||
ds = bm.make_realization(speed_up=False, labels=True) | ||
correct_labels = np.array( | ||
[ | ||
[ | ||
[1.0, 0.0, 0.0, 0.0, 0.0], | ||
[0.0, 1.0, 0.0, 0.0, 0.0], | ||
[0.0, 0.0, 1.0, 0.0, 0.0], | ||
[0.0, 0.0, 0.0, 1.0, 0.0], | ||
[0.0, 0.0, 0.0, 0.0, 1.0], | ||
] | ||
] | ||
) | ||
diff = ds["blob_labels"].values - correct_labels | ||
assert np.max(diff) < 0.00001 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why create __tmp? Seems unnecessary:
__labels_field[:, :, __start:__stop][__single_blob >= __max_amplitudes * label_border] = 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find another way of implementing this since I am not aware of a simpler way of applying a condition only on a slice of the array.
i.e.
__label_field[:, :, __start:__stop && __single_blob >= __max_amplitudes * label_border]
@Sosnowsky do you know a simpler way in python? Otherwise I would keep the current implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't what I wrote work?
__labels_field[:, :, __start:__stop][__single_blob >= __max_amplitudes * label_border] = 1
Creating the tmp array is time-comsuming, so in general you would prefer to avoid doing it if it is not necessary.