Skip to content

Commit

Permalink
add() method to assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomusy committed Sep 13, 2024
1 parent 98d6036 commit 2d1004d
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Changes that will break existing code and need active thinking and work to adapt
tests/issues/issue_1146.py
examples/advanced/spline_draw2.py
examples/volumetric/isosurfaces2.py
examples/pyplot/fit_curve2.py
```


Expand Down
2 changes: 1 addition & 1 deletion docs/examples_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,7 @@ vedo_example_db =
imgsrc: 'images/pyplot/fit_erf.png',
},
{
pyname: 'fit_curve',
pyname: 'fit_curve1',
kbd : '',
categ : 'plotting',
short : 'fit data w/ error bars',
Expand Down
File renamed without changes.
44 changes: 44 additions & 0 deletions examples/pyplot/fit_curve2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Create slider that changes the value of the k parameter in the function."""
import numpy as np
from vedo.pyplot import plot
from vedo import settings, Line, Text2D, Plotter


settings.default_font = "Brachium"
settings.remember_last_figure_format = True


def func(x, h, a, x0, k):
return h + a * (x-x0) * np.sin((x-x0)**2 / k)


def callback(w, e):
y = func(xdata, *true_params[:3], slider.value)
res = np.sum((ydata - y)**2 / 100)
txt2d.text(f"Residuals: {res:.3f}")
# remove previous fit line and insert the new one
line = Line(np.c_[xdata, y], c="green4", lw=3)
p.remove(p[2]).insert(line)


true_params = [20, 2, 8, 3]
xdata = np.linspace(3, 10, 100)
ydata_true = func(xdata, *true_params)
ydata = np.random.normal(ydata_true, 3, 100)

p = plot(
xdata,
ydata,
"o",
mc="blue2",
title="f = h + a*(x-x_0 )*sin((x-x_0 )**2 /k)",
label="Data",
)
p += plot(xdata, ydata_true, "-g", lw=2, label="Fit")
p.add_legend(pos="top-right")

txt2d = Text2D(pos="bottom-left", bg='yellow5', s=1.2)

plt = Plotter(size=(900, 650))
slider = plt.add_slider(callback, 1, 5, value=3, title="k-value")
plt.show(p, txt2d, __doc__, zoom=1.3, mode="2d")
59 changes: 55 additions & 4 deletions vedo/assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ def __str__(self):
return out.rstrip() + "\x1b[0m"

def __iadd__(self, obj):
"""
Add an object to the group
"""
"""Add an object to the group."""
if not vedo.utils.is_sequence(obj):
obj = [obj]
for a in obj:
Expand All @@ -144,7 +142,30 @@ def __iadd__(self, obj):
self.AddPart(a)
except TypeError:
self.AddPart(a.actor)
self.objcects.append(a)
self.objects.append(a)
return self

def __isub__(self, obj):
"""Remove an object to the group."""
if not vedo.utils.is_sequence(obj):
obj = [obj]
for a in obj:
if a:
try:
self.RemovePart(a)
except TypeError:
self.RemovePart(a.actor)
self.objects.append(a)
return self

def add(self, obj):
"""Add an object to the group."""
self.__iadd__(obj)
return self

def remove(self, obj):
"""Remove an object to the group."""
self.__isub__(obj)
return self

def _unpack(self):
Expand Down Expand Up @@ -402,6 +423,36 @@ def unpack_group(scalarbar):
self.pipeline = vedo.utils.OperationNode("add mesh", parents=[self, obj], c="#f08080")
return self

def __isub__(self, obj):
"""
Remove an object to the assembly.
"""
if not vedo.utils.is_sequence(obj):
obj = [obj]
for a in obj:
if a:
try:
self.RemovePart(a)
self.objects.remove(a)
except TypeError:
self.RemovePart(a.actor)
self.objects.remove(a)
return self

def add(self, obj):
"""
Add an object to the assembly.
"""
self.__add__(obj)
return self

def remove(self, obj):
"""
Remove an object to the assembly.
"""
self.__isub__(obj)
return self

def __contains__(self, obj):
"""Allows to use `in` to check if an object is in the `Assembly`."""
return obj in self.objects
Expand Down
2 changes: 1 addition & 1 deletion vedo/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
_version = '2024.5.2+dev10'
_version = '2024.5.2+dev11'

0 comments on commit 2d1004d

Please sign in to comment.