hno-hammerstein 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Daniel Freitas de Jesus and Luiz Alberto Luz de Almeida
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,77 @@
1
+ Metadata-Version: 2.4
2
+ Name: hno-hammerstein
3
+ Version: 0.1.0
4
+ Summary: Hammerstein Neural Operator with Latent Dynamics for Magnetic Hysteresis
5
+ Author: Daniel Freitas de Jesus and Luiz Alberto Luz de Almeida
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/seu-usuario/hno-hammerstein
8
+ Requires-Python: >=3.9
9
+ Description-Content-Type: text/markdown
10
+ License-File: LICENSE
11
+ Requires-Dist: torch>=2.0.0
12
+ Requires-Dist: numpy>=1.21.0
13
+ Dynamic: license-file
14
+
15
+ # HNO-Hammerstein
16
+
17
+ **Hammerstein Neural Operator with Latent Dynamics for Magnetic Hysteresis**
18
+
19
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
20
+ [![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/release/python-390/)
21
+
22
+ This package implements a Neural Operator based on the classic Hammerstein structure, designed to model non-linear dynamic systems with memory, such as magnetic hysteresis.
23
+
24
+ Unlike traditional neural networks (DeepONet, FNO), the HNO architecture explicitly separates the static non-linearity (input) from the latent dynamics (memory), ensuring physical interpretability and generalization across different temporal resolutions.
25
+
26
+ ---
27
+
28
+ ## Arquitetura / Architecture
29
+
30
+ ![Arquitetura HNO-Hammerstein](https://raw.githubusercontent.com/seu-usuario/hno-hammerstein/main/docs/arquitetura.png)
31
+
32
+ O fluxo de dados é:
33
+ 1. **Não Linearidade Estática (Static Non-linearity):** `z(t) = N_theta(H(t))`
34
+ 2. **Contexto Global (Global Context):** `c = E_phi(z(t))`
35
+ 3. **Dinâmica Latente (Latent Dynamics):** `s(t) = Integrate(f_psi(s, z, c))`
36
+ 4. **Saída (Output):** `B(t) = G_omega(s(t), z(t))`
37
+
38
+ ---
39
+
40
+ ## Instalação / Installation
41
+
42
+ ```bash
43
+ pip install hno-hammerstein
44
+
45
+ #Uso Rápido / Quick Start
46
+ import torch
47
+ from hno_hammerstein import HNOHammerstein
48
+
49
+ # Instancia a rede / Instantiate the network
50
+ model = HNOHammerstein(
51
+ nonlinear_dim=32,
52
+ context_dim=32,
53
+ state_dim=64,
54
+ hidden_dim=64,
55
+ dropout=0.05
56
+ )
57
+
58
+ # Cria uma entrada fictícia / Create a dummy input: (batch=1, Nt=198, 1)
59
+ h = torch.randn(1, 198, 1)
60
+
61
+ # Predição / Prediction
62
+ B_pred = model(h)
63
+ print(B_pred.shape) # torch.Size([1, 198, 1])
64
+
65
+
66
+ #Licença / License
67
+ Este projeto está sob a licença MIT. Veja o arquivo LICENSE para mais detalhes.
68
+ This project is licensed under the MIT License. See the LICENSE file for details.
69
+
70
+ #Citação / Citation
71
+ bibtex
72
+ @mastersthesis{freitas2026hno,
73
+ title={Hammerstein Neural Operator (HNO): Um Novo Operador Neural para Modelagem de Histerese Magnética},
74
+ authors={Freitas, Daniel, Almeida, Luiz}
75
+ school={Universidade Federal do ABC (UFABC)},
76
+ year={2026}
77
+ }
@@ -0,0 +1,63 @@
1
+ # HNO-Hammerstein
2
+
3
+ **Hammerstein Neural Operator with Latent Dynamics for Magnetic Hysteresis**
4
+
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+ [![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/release/python-390/)
7
+
8
+ This package implements a Neural Operator based on the classic Hammerstein structure, designed to model non-linear dynamic systems with memory, such as magnetic hysteresis.
9
+
10
+ Unlike traditional neural networks (DeepONet, FNO), the HNO architecture explicitly separates the static non-linearity (input) from the latent dynamics (memory), ensuring physical interpretability and generalization across different temporal resolutions.
11
+
12
+ ---
13
+
14
+ ## Arquitetura / Architecture
15
+
16
+ ![Arquitetura HNO-Hammerstein](https://raw.githubusercontent.com/seu-usuario/hno-hammerstein/main/docs/arquitetura.png)
17
+
18
+ O fluxo de dados é:
19
+ 1. **Não Linearidade Estática (Static Non-linearity):** `z(t) = N_theta(H(t))`
20
+ 2. **Contexto Global (Global Context):** `c = E_phi(z(t))`
21
+ 3. **Dinâmica Latente (Latent Dynamics):** `s(t) = Integrate(f_psi(s, z, c))`
22
+ 4. **Saída (Output):** `B(t) = G_omega(s(t), z(t))`
23
+
24
+ ---
25
+
26
+ ## Instalação / Installation
27
+
28
+ ```bash
29
+ pip install hno-hammerstein
30
+
31
+ #Uso Rápido / Quick Start
32
+ import torch
33
+ from hno_hammerstein import HNOHammerstein
34
+
35
+ # Instancia a rede / Instantiate the network
36
+ model = HNOHammerstein(
37
+ nonlinear_dim=32,
38
+ context_dim=32,
39
+ state_dim=64,
40
+ hidden_dim=64,
41
+ dropout=0.05
42
+ )
43
+
44
+ # Cria uma entrada fictícia / Create a dummy input: (batch=1, Nt=198, 1)
45
+ h = torch.randn(1, 198, 1)
46
+
47
+ # Predição / Prediction
48
+ B_pred = model(h)
49
+ print(B_pred.shape) # torch.Size([1, 198, 1])
50
+
51
+
52
+ #Licença / License
53
+ Este projeto está sob a licença MIT. Veja o arquivo LICENSE para mais detalhes.
54
+ This project is licensed under the MIT License. See the LICENSE file for details.
55
+
56
+ #Citação / Citation
57
+ bibtex
58
+ @mastersthesis{freitas2026hno,
59
+ title={Hammerstein Neural Operator (HNO): Um Novo Operador Neural para Modelagem de Histerese Magnética},
60
+ authors={Freitas, Daniel, Almeida, Luiz}
61
+ school={Universidade Federal do ABC (UFABC)},
62
+ year={2026}
63
+ }
@@ -0,0 +1,21 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "hno-hammerstein"
7
+ version = "0.1.0"
8
+ description = "Hammerstein Neural Operator with Latent Dynamics for Magnetic Hysteresis"
9
+ readme = "README.md"
10
+ requires-python = ">=3.9"
11
+ license = {text = "MIT"}
12
+ authors = [
13
+ {name = "Daniel Freitas de Jesus and Luiz Alberto Luz de Almeida"}
14
+ ]
15
+ dependencies = [
16
+ "torch>=2.0.0",
17
+ "numpy>=1.21.0"
18
+ ]
19
+
20
+ [project.urls]
21
+ Homepage = "https://github.com/seu-usuario/hno-hammerstein"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1 @@
1
+ from .model import HNOHammerstein
@@ -0,0 +1,196 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ HNO-Hammerstein (Variante H2) — Hammerstein Neural Operator with Latent Dynamics.
4
+
5
+ Fluxo estrito de Hammerstein:
6
+ H(t) --> N_theta --> z(t) --> Contexto c(z)
7
+ --> Estado s(t) (dinâmica causal + contexto)
8
+ --> Saída B(t) = G_omega(s(t), z(t))
9
+
10
+ A dinâmica vê z(t) e c(z), mas NUNCA vê H(t) cru.
11
+ A saída vê s(t) e z(t), mas NUNCA vê H(t) cru.
12
+ """
13
+
14
+ import torch
15
+ import torch.nn as nn
16
+
17
+
18
+ # ============================================================
19
+ # 1. BLOCO NÃO LINEAR ESTÁTICO (N_theta)
20
+ # ============================================================
21
+ class NonlinearStaticOperator(nn.Module):
22
+ def __init__(self, nonlinear_dim=32, hidden_dim=64, dropout=0.05):
23
+ super().__init__()
24
+ self.network = nn.Sequential(
25
+ nn.Linear(1, hidden_dim),
26
+ nn.GELU(),
27
+ nn.Linear(hidden_dim, hidden_dim),
28
+ nn.GELU(),
29
+ nn.Dropout(dropout),
30
+ nn.Linear(hidden_dim, nonlinear_dim),
31
+ nn.Tanh(), # Limita z(t) entre -1 e 1
32
+ )
33
+
34
+ def forward(self, h):
35
+ # h: (batch, Nt, 1) -> z: (batch, Nt, nonlinear_dim)
36
+ return self.network(h)
37
+
38
+
39
+ # ============================================================
40
+ # 2. ENCODER GLOBAL (E_phi): c = E_phi(z(t))
41
+ # ============================================================
42
+ class GlobalContextEncoder(nn.Module):
43
+ """
44
+ Aprende o contexto global a partir de z(t).
45
+ NÃO vê H(t) cru.
46
+ """
47
+ def __init__(self, nonlinear_dim=32, context_dim=32, hidden_dim=64):
48
+ super().__init__()
49
+ self.conv = nn.Sequential(
50
+ nn.Conv1d(nonlinear_dim, 32, kernel_size=5, padding=2),
51
+ nn.GELU(),
52
+ nn.Conv1d(32, 64, kernel_size=5, padding=2),
53
+ nn.GELU(),
54
+ )
55
+ self.projection = nn.Sequential(
56
+ nn.Linear(64 * 2, hidden_dim),
57
+ nn.GELU(),
58
+ nn.Linear(hidden_dim, context_dim),
59
+ nn.Tanh(),
60
+ )
61
+
62
+ def forward(self, z):
63
+ # z: (batch, Nt, nonlinear_dim)
64
+ x = z.transpose(1, 2) # (batch, nonlinear_dim, Nt)
65
+ features = self.conv(x)
66
+ avg = features.mean(dim=-1) # (batch, 64)
67
+ maximum = features.amax(dim=-1) # (batch, 64)
68
+ combined = torch.cat([avg, maximum], dim=-1) # (batch, 128)
69
+ return self.projection(combined) # (batch, context_dim)
70
+
71
+
72
+ # ============================================================
73
+ # 3. DINÂMICA LATENTE (f_psi): ds/dt = f(s, z, c, tau)
74
+ # ============================================================
75
+ class LatentDynamicOperator(nn.Module):
76
+ """
77
+ Estado latente evoluindo no tempo.
78
+ Entrada: z(t), contexto c, estado anterior s, tempo tau.
79
+ NÃO vê H(t) cru.
80
+ """
81
+ def __init__(self, nonlinear_dim=32, context_dim=32, state_dim=64, hidden_dim=64, dropout=0.05):
82
+ super().__init__()
83
+ self.state_dim = state_dim
84
+
85
+ # Estado inicial condicionado por c e z(0)
86
+ self.initial_state = nn.Sequential(
87
+ nn.Linear(context_dim + nonlinear_dim, hidden_dim),
88
+ nn.GELU(),
89
+ nn.Linear(hidden_dim, state_dim),
90
+ nn.Tanh(),
91
+ )
92
+
93
+ # Dinâmica: entrada = [s, z, c, tau] -> derivada
94
+ self.state_dynamics = nn.Sequential(
95
+ nn.Linear(state_dim + nonlinear_dim + context_dim + 1, hidden_dim),
96
+ nn.GELU(),
97
+ nn.Dropout(dropout),
98
+ nn.Linear(hidden_dim, hidden_dim),
99
+ nn.GELU(),
100
+ nn.Linear(hidden_dim, state_dim),
101
+ nn.Tanh(),
102
+ )
103
+
104
+ self.state_gain = nn.Parameter(torch.tensor(1.0))
105
+
106
+ def forward(self, z, context):
107
+ # z: (batch, Nt, nonlinear_dim)
108
+ # context: (batch, context_dim)
109
+ batch, nt, _ = z.shape
110
+ dtau = 1.0 / (nt - 1)
111
+
112
+ s = self.initial_state(torch.cat([context, z[:, 0, :]], dim=-1))
113
+ states = []
114
+
115
+ for k in range(nt):
116
+ z_k = z[:, k, :]
117
+ tau_k = torch.full((batch, 1), k * dtau, device=z.device, dtype=z.dtype)
118
+ dynamic_input = torch.cat([s, z_k, context, tau_k], dim=-1)
119
+ derivative = self.state_dynamics(dynamic_input)
120
+
121
+ states.append(s)
122
+ if k < nt - 1:
123
+ s = s + dtau * self.state_gain * derivative
124
+
125
+ return torch.stack(states, dim=1) # (batch, Nt, state_dim)
126
+
127
+
128
+ # ============================================================
129
+ # 4. OPERADOR DE SAÍDA (G_omega): B(t) = G(s(t), z(t))
130
+ # ============================================================
131
+ class OutputOperator(nn.Module):
132
+ """
133
+ Reconstrói B(t) a partir do estado s(t) e do sinal não linear z(t).
134
+ NÃO vê H(t) cru.
135
+ """
136
+ def __init__(self, state_dim=64, nonlinear_dim=32, hidden_dim=64, dropout=0.05):
137
+ super().__init__()
138
+ input_dim = state_dim + nonlinear_dim
139
+ self.decoder = nn.Sequential(
140
+ nn.Linear(input_dim, hidden_dim),
141
+ nn.GELU(),
142
+ nn.Dropout(dropout),
143
+ nn.Linear(hidden_dim, hidden_dim // 2),
144
+ nn.GELU(),
145
+ nn.Linear(hidden_dim // 2, 1),
146
+ )
147
+
148
+ def forward(self, states, z):
149
+ decoder_input = torch.cat([states, z], dim=-1)
150
+ return self.decoder(decoder_input)
151
+
152
+
153
+ # ============================================================
154
+ # 5. HNO-HAMMERSTEIN COMPLETO (Variante H2)
155
+ # ============================================================
156
+ class HNOHammerstein(nn.Module):
157
+ """
158
+ Estrutura:
159
+ z = N(H) (não linearidade estática)
160
+ c = E(z) (contexto global)
161
+ s = Dinâmica(z,c) (estado latente)
162
+ B = G(s, z) (saída)
163
+ """
164
+ def __init__(
165
+ self,
166
+ nonlinear_dim=32,
167
+ context_dim=32,
168
+ state_dim=64,
169
+ hidden_dim=64,
170
+ dropout=0.05,
171
+ ):
172
+ super().__init__()
173
+ self.static_operator = NonlinearStaticOperator(nonlinear_dim, hidden_dim, dropout)
174
+ self.context_encoder = GlobalContextEncoder(nonlinear_dim, context_dim, hidden_dim)
175
+ self.dynamic_operator = LatentDynamicOperator(nonlinear_dim, context_dim, state_dim, hidden_dim, dropout)
176
+ self.output_operator = OutputOperator(state_dim, nonlinear_dim, hidden_dim, dropout)
177
+
178
+ def forward(self, h):
179
+ if h.ndim != 3 or h.shape[-1] != 1:
180
+ raise ValueError("h deve ter formato (batch, Nt, 1).")
181
+ if h.shape[1] < 2:
182
+ raise ValueError("Nt precisa ser >= 2.")
183
+
184
+ # 1. H -> z (não linearidade estática)
185
+ z = self.static_operator(h)
186
+
187
+ # 2. z -> c (contexto global)
188
+ context = self.context_encoder(z)
189
+
190
+ # 3. z, c -> s (dinâmica com memória)
191
+ states = self.dynamic_operator(z, context)
192
+
193
+ # 4. s, z -> B (saída)
194
+ B_hat = self.output_operator(states, z)
195
+
196
+ return B_hat
@@ -0,0 +1,77 @@
1
+ Metadata-Version: 2.4
2
+ Name: hno-hammerstein
3
+ Version: 0.1.0
4
+ Summary: Hammerstein Neural Operator with Latent Dynamics for Magnetic Hysteresis
5
+ Author: Daniel Freitas de Jesus and Luiz Alberto Luz de Almeida
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/seu-usuario/hno-hammerstein
8
+ Requires-Python: >=3.9
9
+ Description-Content-Type: text/markdown
10
+ License-File: LICENSE
11
+ Requires-Dist: torch>=2.0.0
12
+ Requires-Dist: numpy>=1.21.0
13
+ Dynamic: license-file
14
+
15
+ # HNO-Hammerstein
16
+
17
+ **Hammerstein Neural Operator with Latent Dynamics for Magnetic Hysteresis**
18
+
19
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
20
+ [![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/release/python-390/)
21
+
22
+ This package implements a Neural Operator based on the classic Hammerstein structure, designed to model non-linear dynamic systems with memory, such as magnetic hysteresis.
23
+
24
+ Unlike traditional neural networks (DeepONet, FNO), the HNO architecture explicitly separates the static non-linearity (input) from the latent dynamics (memory), ensuring physical interpretability and generalization across different temporal resolutions.
25
+
26
+ ---
27
+
28
+ ## Arquitetura / Architecture
29
+
30
+ ![Arquitetura HNO-Hammerstein](https://raw.githubusercontent.com/seu-usuario/hno-hammerstein/main/docs/arquitetura.png)
31
+
32
+ O fluxo de dados é:
33
+ 1. **Não Linearidade Estática (Static Non-linearity):** `z(t) = N_theta(H(t))`
34
+ 2. **Contexto Global (Global Context):** `c = E_phi(z(t))`
35
+ 3. **Dinâmica Latente (Latent Dynamics):** `s(t) = Integrate(f_psi(s, z, c))`
36
+ 4. **Saída (Output):** `B(t) = G_omega(s(t), z(t))`
37
+
38
+ ---
39
+
40
+ ## Instalação / Installation
41
+
42
+ ```bash
43
+ pip install hno-hammerstein
44
+
45
+ #Uso Rápido / Quick Start
46
+ import torch
47
+ from hno_hammerstein import HNOHammerstein
48
+
49
+ # Instancia a rede / Instantiate the network
50
+ model = HNOHammerstein(
51
+ nonlinear_dim=32,
52
+ context_dim=32,
53
+ state_dim=64,
54
+ hidden_dim=64,
55
+ dropout=0.05
56
+ )
57
+
58
+ # Cria uma entrada fictícia / Create a dummy input: (batch=1, Nt=198, 1)
59
+ h = torch.randn(1, 198, 1)
60
+
61
+ # Predição / Prediction
62
+ B_pred = model(h)
63
+ print(B_pred.shape) # torch.Size([1, 198, 1])
64
+
65
+
66
+ #Licença / License
67
+ Este projeto está sob a licença MIT. Veja o arquivo LICENSE para mais detalhes.
68
+ This project is licensed under the MIT License. See the LICENSE file for details.
69
+
70
+ #Citação / Citation
71
+ bibtex
72
+ @mastersthesis{freitas2026hno,
73
+ title={Hammerstein Neural Operator (HNO): Um Novo Operador Neural para Modelagem de Histerese Magnética},
74
+ authors={Freitas, Daniel, Almeida, Luiz}
75
+ school={Universidade Federal do ABC (UFABC)},
76
+ year={2026}
77
+ }
@@ -0,0 +1,10 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/hno_hammerstein/__init__.py
5
+ src/hno_hammerstein/model.py
6
+ src/hno_hammerstein.egg-info/PKG-INFO
7
+ src/hno_hammerstein.egg-info/SOURCES.txt
8
+ src/hno_hammerstein.egg-info/dependency_links.txt
9
+ src/hno_hammerstein.egg-info/requires.txt
10
+ src/hno_hammerstein.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ torch>=2.0.0
2
+ numpy>=1.21.0
@@ -0,0 +1 @@
1
+ hno_hammerstein