SlideTransition in Kivy - slide direction fails - python-3.x

I am having issues with the slide direction using SlideTransition in Kivy. When implementing a button-specific slide direction, the direction seems to work fine only occasionally. However, it fails in other circumstances.
To reproduce the problem, please feel free to use the following code:
main.py:
from kivy.app import App # Use of fields and methods of Kivy
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
class Screen1(Screen):
pass
class Screen2(Screen):
pass
class Screen3(Screen):
pass
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("style.kv")
class MyApp(App):
def build(self):
return presentation
myApp = MyApp()
myApp.run()
style.kv:
#: import SlideTransition kivy.uix.screenmanager.SlideTransition
ScreenManagement:
transition: SlideTransition()
Screen1:
Screen2:
Screen3:
<Screen1>:
name: "screen1"
FloatLayout:
Label:
text: "Screen 1"
pos_hint: {"top": 1.0}
Button:
on_release:
app.root.current = "screen2"
app.root.transition = SlideTransition(direction="left")
size_hint: 0.5, 0.8/3
pos_hint: {"bottom": 0.8, "left": 1}
text: "Screen 2 (slide left)"
font_size: 30
<Screen2>:
name: "screen2"
FloatLayout:
Label:
text: "Screen 2"
pos_hint: {"top": 1.0}
Button:
on_release:
app.root.current = "screen1"
app.root.transition = SlideTransition(direction="right")
size_hint: 0.5, 0.8/3
pos_hint: {"top": 0.8, "right": 1}
text: "Screen 1 (slide right)"
font_size: 30
Button:
on_release:
app.root.current = "screen3"
app.root.transition = SlideTransition(direction="left")
size_hint: 0.5, 0.8/3
pos_hint: {"top": 0.8, "left": 1}
text: "Screen 3 (slide left)"
font_size: 30
<Screen3>:
name: "screen3"
FloatLayout:
Label:
text: "Screen 3"
pos_hint: {"top": 1.0}
Button:
on_release:
app.root.current = "screen1"
app.root.transition = SlideTransition(direction="right")
size_hint: 0.5, 0.8/3
pos_hint: {"top": 0.8, "left": 1}
text: "Screen 1 (slide right)"
font_size: 30
I am experiencing the following problem: Upon clicking on "Slide 2" in the first slide and "Slide 3" in the second slide, both disappearing to the left as specified, clicking "Slide 1" in the third slide results in slide direction "left", although the specified direction is "right". Something seems to override the slide direction?
Did anyone experience similar problems and does know how to solve them? Thanks in advance!

Problem
The screen was displayed before the given direction. Therefore, it used the previous Slide Transition direction i.e. left. You have the same problem in a couple of places.
Button:
on_release:
app.root.current = "screen1"
app.root.transition = SlideTransition(direction="right")
Solution
Set the Slide Transition direction first, and then set current to the desire screen name. Please refer to the example for details.
Button:
on_release:
app.root.transition = SlideTransition(direction="right")
app.root.current = "screen1"
Example
style.kv
#:import SlideTransition kivy.uix.screenmanager.SlideTransition
ScreenManagement:
transition: SlideTransition()
Screen1:
Screen2:
Screen3:
<Screen1>:
name: "screen1"
FloatLayout:
Label:
text: "Screen 1"
pos_hint: {"top": 1.0}
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen2"
size_hint: 0.5, 0.8/3
pos_hint: {"bottom": 0.8, "left": 1}
text: "Screen 2 (slide left)"
font_size: 30
<Screen2>:
name: "screen2"
FloatLayout:
Label:
text: "Screen 2"
pos_hint: {"top": 1.0}
Button:
on_release:
app.root.transition = SlideTransition(direction="right")
app.root.current = "screen1"
size_hint: 0.5, 0.8/3
pos_hint: {"top": 0.8, "right": 1}
text: "Screen 1 (slide right)"
font_size: 30
Button:
on_release:
app.root.transition = SlideTransition(direction="left")
app.root.current = "screen3"
size_hint: 0.5, 0.8/3
pos_hint: {"top": 0.8, "left": 1}
text: "Screen 3 (slide left)"
font_size: 30
<Screen3>:
name: "screen3"
FloatLayout:
Label:
text: "Screen 3"
pos_hint: {"top": 1.0}
Button:
on_release:
app.root.transition = SlideTransition(direction="right")
app.root.current = "screen1"
size_hint: 0.5, 0.8/3
pos_hint: {"top": 0.8, "left": 1}
text: "Screen 1 (slide right)"
font_size: 30

Related

How to add a widget in GridLayout?(kivy+python)

How do I add a widget via python? What is my mistake?
I want to add a widget using the button.How to apply correctly?
In python format:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from kivy.core.window import Window
Window.size = (320, 720)
Builder.load_file('my.kv')
class Container(FloatLayout):
def minus(self):
self.fp.add_widget(TextInput(text="1103"))
print("ok")
class MyApp(App):
def build(self):
return Container()
if __name__ == "__main__":
MyApp().run()
In kiwy format:
<Container>
fp: fp
FloatLayout:
orientation: "vertical"
Button:
text : "Системы"
size_hint : (0.3, 0.1)
pos_hint : {'x':0.05, 'y':0.9}
font_size : "20sp"
Button:
text : "Локодром"
size_hint : (0.3, 0.1)
pos_hint : {'x':0.35, 'y':0.9}
font_size : "20sp"
Button:
text : "Задачи"
size_hint : (0.3, 0.1)
pos_hint : {'x':0.65, 'y':0.9}
font_size : "20sp"
BoxLayout:
orientation: "horizontal"
cols: 3
size_hint: 0.9,0.975
pos_hint: {'center_x':.5}
Button:
text : "<-"
size_hint : (0.05, 0.05)
font_size : "20sp"
pos_hint: {'top': 0.91}
Label:
text : "19.11.2022"
size_hint : (0.2, 0.05)
font_size : "20sp"
pos_hint: {'top': 0.91}
Button:
text : "->"
size_hint : (0.05, 0.05)
font_size : "20sp"
pos_hint: {'top': 0.91}
TextInput:
text : ""
size_hint:(0.47, 0.05)
pos_hint:{'x': 0.05, 'y': 0.78}
font_size : "18sp"
Button:
text : "Поиск"
size_hint:(0.2, 0.05)
pos_hint:{'x': 0.536, 'y': 0.78}
font_size : "15sp"
Button:
text : "Убрать"
size_hint : (0.2, 0.05)
pos_hint : {'x':0.75, 'y':0.78}
font_size : "15sp"
Label:
text : "S"
size_hint : (0.1, 0.05)
pos_hint : {'x': 0.03, 'y': 0.72}
font_size : "20sp"
Label:
text : "№"
size_hint : (0.15, 0.05)
pos_hint : {'x': 0.12, 'y': 0.72}
font_size : "20sp"
Label:
text : "Описание"
size_hint : (0.53, 0.05)
pos_hint : {'x': 0.25, 'y': 0.72}
font_size : "20sp"
Label:
text : "Прим."
size_hint : (0.18, 0.05)
pos_hint : {'x': 0.77, 'y': 0.72}
font_size : "20sp"
BoxLayout:
orientation: 'vertical'
Label:
text: ""
size_hint : (0.18, 0.53)
ScrollView:
size_hint: 0.9,1
pos_hint: {'center_x':.5}
GridLayout:
id:fp
size_hint_y:None
height: self.minimum_height
cols: 4
cols_minimum: {0: 10, 1: 30, 2: 140, 3: 50}
TextInput:
text: 'Filler'
size_hint:1,None
height:40
Label:
text: ''
size_hint : (0.18, 0.2)
BoxLayout:
orientation: "horizontal"
cols: 3
padding: 20
Button:
text : "Удалить"
size_hint:(0.29, 0.07)
pos_hint: {'top': 0.07}
font_size : "15sp"
on_press: root.minus()
Button:
text : "Добавить"
size_hint : (0.29, 0.07)
pos_hint: {'top': 0.07}
font_size : "15sp"
Button:
text : "Сохранить"
size_hint : (0.29, 0.07)
pos_hint: {'top': 0.07}
font_size : "15sp"
I thought that by referring to the id I would get a new widget
self.fp.add_widget(TextInput(text="1103"))
Но получаю enter image description here
Help please
You can do it this way, see the kivy doc (https://kivy.org/doc/stable/guide/widgets.html):
layout = BoxLayout(padding=10)
button = Button(text='My first button')
layout.add_widget(button)

KivyMD: How to change the row height on MDList with ScrollView

I have created a Screen with a Scrollview and a MDList on it, with some TwoLineListItems.
Is there a way to reduce the row height on the MDList to fit the font size of the TwoLineListItem?
Part of my kv file:
<SelectionList>:
name: "Selection"
MDBoxLayout:
orientation: 'vertical'
size: root.width, root.height
padding: 5
ScrollView:
size_hint: (1, 1)
bar_color: "#ff0066"
bar_width: 4
MDList:
TwoLineListItem:
id: name_list
text: "[size=10]Name: [/size]"
secondary_text: "[size=12]XXXXXX[/size]"
TwoLineListItem:
id: surname_list
text: "[size=10]Surname: [/size]"
secondary_text: "[size=12]XXXXXX[/size]"
TwoLineListItem:
id: phone_list
text: "[size=10]Phone: [/size]"
secondary_text: "[size=12]XXXXXX[/size]"
...
GridLayout:
cols: 2
size_hint: None, None
height: self.minimum_height
padding: 10
col_force_default: True
col_default_width: root.width/2
MDFloatingActionButton:
type: "small"
icon_size: "30sp"
icon: "arrow-left-bold-circle"
icon_color: "#2d5986"
md_bg_color: "white"
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
MDFloatingActionButton:
type: "small"
icon_size: "30sp"
icon: "home-circle"
icon_color: "#2d5986"
md_bg_color: "white"
pos_hint: {'center_x': 0.8, 'center_y': 0.5}
improve coding on kivymd

Draw a shadow border on ModalView with kivy Vertex Instruction?

I am trying to draw a shadow for a ModalView using the Canvas Line vertex instructions. I.e. the ModalView bottom and left sides should have a slight shadowy overlay when open. I have tried calling the ModalView property overlay_color with no effect and Canvas Line vertex instructions do not create the right effect. But I cannot seem to only draw a bottom and left border that gives the shadowy effect.
<PopScrollModal>
on_open: app.root._is_modal_open = True
on_dismiss: app.root._is_modal_open = False
id: popscroll
auto_dismiss: True
orientation: 'vertical'
size_hint: (0.94, 0.41)
border: [50, 50, 16, 16]
overlay_color: [0.1, 0.1, 0.1, 0.4]
pos_hint: {'top': 0.72}
background_normal: ''
background_color: (1, 1, 1, 0)
background: 'white.png'
canvas:
Color:
rgba: app.theme_cls.bg_dark
RoundedRectangle:
size: self.size
pos: self.pos
radius: [7,]
canvas.after:
Color:
rgba: (0.2, 0.2, 0.2, 0.4)
Line:
width: 1.
rounded_rectangle: (self.x, self.y, self.width, self.height, 7)
RecycleView:
id: view_popscroll
viewclass: 'PopScrollBut'
pos_hint: {'top': 1}
size_hint: [1, 0.99]
do_scroll_y: True
RecycleGridLayout:
cols: 1
spacing: 1
default_size: None, 70
default_size_hint: 1, None
size_hint: 1, None
size: self.minimum_size
This line instruction draws on the bottom but does not adhere to the radius of the canvas:
canvas.after:
Color:
rgba: (0.2, 0.2, 0.2, 0.4)
Line:
width: 1.
close: False
points: self.pos[0], self.pos[1], self.pos[0] + self.size[0], self.pos[1]]
The Line instruction only draws a line around the ModalView.
Can somebody help to understand how to set the Points so they only appear left and bottom or set the overlay_color in the same way?
You can do that using BorderImage. It is not well documented and difficult to understand how it is actually intended to work. But here is an example that mostly does what you want:
class MyPop(Popup):
pass
kv = '''
<MyPop>:
canvas.before:
BorderImage:
source: 'shadow32.png'
border: 30, 0, 0, 30
pos: self.x - 30, self.y - 30
size: self.width + 60, self.height + 60
'''
And here is the shadow32.png that is used above:

Kivy : childeren GridLayouts of a ScrollView are overlapping with each other

I have kivy 1.11.1, Ubuntu 20.04 LTS, python 3.8 running on my laptop . I am beginner to kivy and currently working on kivy project, I am stuck on this problem of children gridlayout of scrollview getting overlapped. I have a scroll view with GridLayout and some labels as its children. GridLayout have one Label and a StackLayout which can have different number of entries for different row. Here is source code main.py.
from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.effects.scroll import ScrollEffect
kv = '''
#:import ScrollEffect kivy.effects.scroll.ScrollEffect
ScrollApp:
<ScrollApp>:
ScrollView:
size_hint: 1.0, 0.90
pos_hint: {'center_y':0.5}
padding: 22, 0, 22, 50
spacing: 50
effect_cls: ScrollEffect
GridLayout:
cols: 1
# id: streak_zone
size_hint_y: None
height: self.minimum_height
row_force_default: True
row_default_height: 400
canvas:
Color:
rgba: .15, .15, .15, .9
Rectangle:
size: self.size
pos: self.pos
Button:
size_hint: None, None
width: 100
height: 100
on_press: print('This button does not overlap the menu above')
# "ScrollViews containers"
Custom
Custom
Custom
Custom
Custom
Custom
BoxLayout:
size_hint: 1, 0.05
pos_hint: {'bottom':1.0}
Button:
on_press: print("This menu at the bottom is not affected by the problem that occurs with the top one")
BoxLayout:
size_hint: 1, 0.05
pos_hint: {'top':1.0}
Button:
text: 'Fixed Menu'
on_press: print('This button stops working if there is a horizontal scrollview "behind"')
<Custom#GridLayout>:
cols: 1
# id: streak_zone
size_hint_y: None
height: self.minimum_height
row_force_default: True
row_default_height: 60
Label:
id: label
font_size: 20
text: 'Teste'
text_size: self.width, None
size_hint_y: None
halign: "center"
valign: "middle"
canvas:
Color:
rgba: (0, 1, 0, .5) # DarkOliveGreen
Rectangle:
size: self.size
pos: self.pos
StackLayout:
id : grid
size_hint_y: None
ToggleButton:
text:'1'
size_hint:1/3, 1
group:'ttt'
ToggleButton:
text:'2'
size_hint:1/3, 1
group:'ttt'
ToggleButton:
text:'3'
size_hint:1/3, 1
group:'ttt'
ToggleButton:
text:'4'
size_hint:1/3, 1
group:'ttt'
ToggleButton:
text:'5'
size_hint:1/3, 1
group:'ttt'
ToggleButton:
text:'6'
size_hint:1/3, 1
group:'ttt'
ToggleButton:
text:'7'
size_hint:1/3, 1
group:'ttt'
ToggleButton:
text:'8'
size_hint:1/3, 1
group:'ttt'
'''
class ScrollApp(FloatLayout):
pass
class Test(App):
def build(self):
kv1 = Builder.load_string(kv)
print("jhe ",kv1.height)
return kv1
return ScrollApp()
Test().run()
I am getting this layout after running it.
enter image description here
I have read many articles on GridLayout and ScrollView but still unable to fix this issue. What am I missing ?
Updated code:
from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.effects.scroll import ScrollEffect
kv = '''
#:import ScrollEffect kivy.effects.scroll.ScrollEffect
ScrollApp:
<ScrollApp>:
ScrollView:
size_hint: 1.0, 0.90
pos_hint: {'center_y':0.5}
padding: 22, 0, 22, 50
spacing: 50
effect_cls: ScrollEffect
GridLayout:
cols: 1
# id: streak_zone
size_hint_y: None
height: self.minimum_height
#row_force_default: True
#row_default_height: 400
canvas:
Color:
rgba: .15, .15, .15, .9
Rectangle:
size: self.size
pos: self.pos
Button:
size_hint: None, None
width: 100
height: 100
on_press: print('This button does not overlap the menu above')
# "ScrollViews containers"
Custom
Custom
Custom
Custom
Custom
Custom
BoxLayout:
size_hint: 1, 0.05
pos_hint: {'bottom':1.0}
Button:
on_press: print("This menu at the bottom is not affected by the problem that occurs with the top one")
BoxLayout:
size_hint: 1, 0.05
pos_hint: {'top':1.0}
Button:
text: 'Fixed Menu'
on_press: print('This button stops working if there is a horizontal scrollview "behind"')
<Custom#GridLayout>:
cols: 1
# id: streak_zone
size_hint_y: None
height: self.minimum_height
#row_force_default: True
#row_default_height: 60
Label:
id: label
font_size: 20
text: 'Teste'
text_size: self.width, None
size_hint_y: None
halign: "center"
valign: "middle"
canvas:
Color:
rgba: (0, 1, 0, .5) # DarkOliveGreen
Rectangle:
size: self.size
pos: self.pos
StackLayout:
id : grid
size_hint_y: None
ToggleButton:
text:'1'
size_hint:1/3, 1
group:'ttt'
ToggleButton:
text:'2'
size_hint:1/3, 1
group:'ttt'
ToggleButton:
text:'3'
size_hint:1/3, 1
group:'ttt'
ToggleButton:
text:'4'
size_hint:1/3, 1
group:'ttt'
ToggleButton:
text:'5'
size_hint:1/3, 1
group:'ttt'
ToggleButton:
text:'6'
size_hint:1/3, 1
group:'ttt'
ToggleButton:
text:'7'
size_hint:1/3, 1
group:'ttt'
ToggleButton:
text:'8'
size_hint:1/3, 1
group:'ttt'
'''
class ScrollApp(FloatLayout):
pass
class Test(App):
def build(self):
kv1 = Builder.load_string(kv)
print("jhe ",kv1.height)
return kv1
return ScrollApp()
Test().run()
I think the problem is that whenever you use the minimum_height property of GridLayout, you must make sure that the size of its children are well defined, and you can't use size_hint_y for those children.
So here is a slightly modified version of your kv that specifies more height properties:
#:import ScrollEffect kivy.effects.scroll.ScrollEffect
ScrollApp:
<ScrollApp>:
ScrollView:
size_hint: 1.0, 0.90
pos_hint: {'center_y':0.5}
padding: 22, 0, 22, 50
spacing: 50
effect_cls: ScrollEffect
GridLayout:
cols: 1
# id: streak_zone
size_hint_y: None
height: self.minimum_height
#row_force_default: True
#row_default_height: 400
canvas:
Color:
rgba: .15, .15, .15, .9
Rectangle:
size: self.size
pos: self.pos
Button:
size_hint: None, None
width: 100
height: 100
on_press: print('This button does not overlap the menu above')
# "ScrollViews containers"
Custom
Custom
Custom
Custom
Custom
Custom
BoxLayout:
size_hint: 1, 0.05
pos_hint: {'bottom':1.0}
Button:
on_press: print("This menu at the bottom is not affected by the problem that occurs with the top one")
BoxLayout:
size_hint: 1, 0.05
pos_hint: {'top':1.0}
Button:
text: 'Fixed Menu'
on_press: print('This button stops working if there is a horizontal scrollview "behind"')
<Custom#GridLayout>:
cols: 1
# id: streak_zone
size_hint_y: None
height: self.minimum_height
#row_force_default: True
#row_default_height: 60
Label:
id: label
font_size: 20
text: 'Teste'
text_size: self.width, None
size_hint_y: None
height: 50
halign: "center"
valign: "middle"
canvas:
Color:
rgba: (0, 1, 0, .5) # DarkOliveGreen
Rectangle:
size: self.size
pos: self.pos
StackLayout:
id : grid
size_hint_y: None
height: self.minimum_height
ToggleButton:
text:'1'
size_hint:1/3, None
height: 50
group:'ttt'
ToggleButton:
text:'2'
size_hint:1/3, None
height: 50
group:'ttt'
ToggleButton:
text:'3'
size_hint:1/3, None
height: 50
group:'ttt'
ToggleButton:
text:'4'
size_hint:1/3, None
height: 50
group:'ttt'
ToggleButton:
text:'5'
size_hint:1/3, None
height: 50
group:'ttt'
ToggleButton:
text:'6'
size_hint:1/3, None
height: 50
group:'ttt'
ToggleButton:
text:'7'
size_hint:1/3, None
height: 50
group:'ttt'
ToggleButton:
text:'8'
size_hint:1/3, None
height: 50
group:'ttt'

How to find similar events of a signal in python

I have one signal and now I want to find the similar events happened multiple times in that signal. For example the below picture we can see there are three events.
I have created this using below values:
import matplotlib.pyplot as plt
y = [
0, 1, 2,
.3, .4, .4, .6, .5, .4, .3, .4, .5, .6, .7, .6, .5, .4, .3, .5, .7, .8, .6, .3, .4, .5, .6, .5, .4, .3, #1
.3, .3, .3, .3, .3, 5, 4,
0, 1, 3, 4, 8, 9, 13, 10, 7, 4, 6, 3, 4, 3, #2
.3, 4, 4.4, 4.3, 3, 3.4, 3.2, 4, 3.8, 4, 6, 6, 5, 4, 1,
.3, .4, .5, .6, .5, .4, .3, .4, .5, .6, .7, .6, .3, .4, .3, .5, .7, .8, .6, .3, .4, .6, .6, .5, .4, .3, # 1
0, 1, 3, 4, 6, 9, 13.5, 9.5, 7, 4, 6, 3, 4, 3, #2
.3, .4, .5, .4, .5, .4, .3, .4, .5, .6, .7, .6, .5, .4, .3, .5, .7, .8, .6, .3, .4, .5, .6, .5, .4, .3, # 1
0, 1, 3, 4, 8, 9, 14, 10, 7, 4, 6, 3, 4, 3, #2
.3, 2, 1, 1,
2, 3, 4, 4.5, 4, 3, 2, 3, 4, 4, 4, 3, 2, #3
1,2,2,1,1,
2, 3, 4, 4, 4, 3, 2, 3, 4, 5, 4, 3, 2, #3
1,2,3, .2, .1, 0
]
plt.plot(y)
plt.show()
First event:
Second event:
Third event:
First event happend 3 times, second event 3 times and third event 2 times. They are almost similar. In real situation the similarity will be little bit less than shown in the above picture.
Now i want to find:
1. similar type events
2. how similar they are
3. where it happend.
For example first event occured again in:
timestamp | similarity
04 - 22 | 100%
60 - 90 | 95%
110 - 130 | 96%
I want to do it in python. How can i do this? Is there any signal processing libraries for this kind of task?
Using numpy you could differentiate using numpy.diff which could give you a clue based on a certain threshold where your events happen. After you get those events, you can use numpy.correlate between your events and the input signal to find the most likely places where the events occur.
correlation
differentiation
Follow-up
If you want to completely automate it for any arbitrary signal, there's gonna have to be some machine learning in there somewhere. I won't give you a machine learning algo outright. I will, however, give you pointers as to what parameters could be considered as factors in your algo.
Take a look at the following
import matplotlib.pyplot as plt
import numpy as np
y = [0, 1, 2,
.3, .4, .4, .6, .5, .4, .3, .4, .5, .6, .7, .6, .5, .4, .3, .5, .7, .8, .6, .3, .4, .5, .6, .5, .4, .3,
.3, .3, .3, .3, .3, 5, 4,
0, 1, 3, 4, 8, 9, 13, 10, 7, 4, 6, 3, 4, 3,
.3, 4, 4.4, 4.3, 3, 3.4, 3.2, 4, 3.8, 4, 6, 6, 5, 4, 1,
.3, .4, .5, .6, .5, .4, .3, .4, .5, .6, .7, .6, .3, .4, .3, .5, .7, .8, .6, .3, .4, .6, .6, .5, .4, .3,
0, 1, 3, 4, 6, 9, 13.5, 9.5, 7, 4, 6, 3, 4, 3,
.3, .4, .5, .4, .5, .4, .3, .4, .5, .6, .7, .6, .5, .4, .3, .5, .7, .8, .6, .3, .4, .5, .6, .5, .4, .3,
0, 1, 3, 4, 8, 9, 14, 10, 7, 4, 6, 3, 4, 3,
.3, 2, 1, 1,
2, 3, 4, 4.5, 4, 3, 2, 3, 4, 4, 4, 3, 2,
1, 2, 2, 1, 1,
2, 3, 4, 4, 4, 3, 2, 3, 4, 5, 4, 3, 2,
1, 2, 3, .2, .1, 0
]
x = 5 #Mean Convolution Window Width
v = np.full(x,1/x) #Convolution vector
y=np.convolve(y,v) #Apply X-Width Convolution on signal to get average between X samples
diff_y = np.abs(np.diff(y))
plt.figure()
plt.plot(diff_y)
plt.show()
diff_y_filtered=diff_y
diff_y_filtered[diff_y < 0.20*np.nanmax(diff_y)] = 0 #Differientiate the signal to look for large variations
plt.figure()
plt.plot(y)
plt.plot(diff_y_filtered)
plt.show()
diff_y_peaks = np.diff(diff_y_filtered) #Second derivative
diff_y_peaks = np.convolve(diff_y_peaks,[-1/8,-1/4,-1/2,1.75,-1/2,-1/4,-1/8]) #You may wanna tweak the filter
plt.figure()
plt.plot(diff_y_peaks)
plt.show()
diff_y_peaks[diff_y_peaks < 0.5] = 0; #This is a parameter that would be tweaked
diff_y_peaks[diff_y_peaks > 0]=np.nanmax(y) #Make things pretty
plt.figure()
plt.stem(diff_y_peaks)
plt.plot(y)
plt.show()
First and second derivatives come to mind because you'd be looking for large variations, I presume, in the signal. Even if events contain large variations themselves, this shouldn't be a problem because of a factor I will point out later on.
I first smoothed the signal to avoid having large variations within events. Then I applied the first derivative to find large variations. I then cut small peaks with a threshold. That threshold's value could be a parameter in your model.
I then filter a second time and convolve again. You can tweak that filter to give you better results. Some filters find more possible cutoff points, some less. It's not a big deal if you get many cutoff points next to each other, because you can train your model to find event lengths that are too small, or you can directly specify that, say, 2 cutoff points 5 samples apart don't constitute an event.
Personally, I believe you might want to look at getting more events that are contained within a bigger event, that will probably get you the best results.

Resources