emoge 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.
- emoge-0.1.0/LICENSE +12 -0
- emoge-0.1.0/PKG-INFO +28 -0
- emoge-0.1.0/README.md +15 -0
- emoge-0.1.0/pyproject.toml +63 -0
- emoge-0.1.0/pyproject.toml.orig +61 -0
- emoge-0.1.0/src/emoge/__init__.py +1 -0
- emoge-0.1.0/src/emoge/fuzzy_color/__init__.py +75 -0
- emoge-0.1.0/src/emoge/fuzzy_color/lut.py +111 -0
- emoge-0.1.0/src/emoge/fuzzy_color/math/__init__.py +9 -0
- emoge-0.1.0/src/emoge/fuzzy_color/math/membership.py +164 -0
- emoge-0.1.0/src/emoge/fuzzy_color/math/norms.py +110 -0
- emoge-0.1.0/src/emoge/fuzzy_color/model.py +181 -0
- emoge-0.1.0/src/emoge/fuzzy_color/models/__init__.py +5 -0
- emoge-0.1.0/src/emoge/fuzzy_color/models/colibri.py +61 -0
- emoge-0.1.0/src/emoge/fuzzy_color/partitions.py +102 -0
- emoge-0.1.0/src/emoge/fuzzy_color/plots.py +211 -0
- emoge-0.1.0/src/emoge/fuzzy_color/sets.py +82 -0
- emoge-0.1.0/src/emoge/fuzzy_color/shapes.py +104 -0
- emoge-0.1.0/src/emoge/fuzzy_color/spaces/__init__.py +26 -0
- emoge-0.1.0/src/emoge/fuzzy_color/spaces/hsi.py +65 -0
- emoge-0.1.0/src/emoge/fuzzy_color/spaces/hsv.py +57 -0
- emoge-0.1.0/src/emoge/fuzzy_color/spaces/rgb.py +51 -0
- emoge-0.1.0/src/emoge/fuzzy_color/spaces/space.py +51 -0
- emoge-0.1.0/src/emoge/fuzzy_color/variables.py +70 -0
- emoge-0.1.0/src/emoge/py.typed +0 -0
emoge-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Copyright (C) 2026 by coredegon
|
|
2
|
+
|
|
3
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
4
|
+
purpose with or without fee is hereby granted.
|
|
5
|
+
|
|
6
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
7
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
8
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
9
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
10
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
11
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
12
|
+
PERFORMANCE OF THIS SOFTWARE.
|
emoge-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: emoge
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Research packages: fuzzy color models, and more to come.
|
|
5
|
+
Author: coredegon
|
|
6
|
+
Author-email: coredegon <valikalik15@gmail.com>
|
|
7
|
+
License-Expression: 0BSD
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: matplotlib>=3.9
|
|
10
|
+
Requires-Dist: numpy>=2.0
|
|
11
|
+
Requires-Python: >=3.12
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
# emoge
|
|
15
|
+
|
|
16
|
+
emoge is a package for research purposes: the analysis of images and the
|
|
17
|
+
emotions they evoke.
|
|
18
|
+
|
|
19
|
+
## Documentation
|
|
20
|
+
|
|
21
|
+
| Package | What it is for | Guide |
|
|
22
|
+
|---|---|---|
|
|
23
|
+
| `emoge.fuzzy_color` | Fuzzy color models: the degree to which every pixel belongs to every named color, and the color composition of an image | [docs/fuzzy_color](docs/fuzzy_color/) |
|
|
24
|
+
|
|
25
|
+
## Disclaimer
|
|
26
|
+
|
|
27
|
+
Not everything may be implemented yet. Missing parts can be added on your own,
|
|
28
|
+
or suggested in an issue or a pull request.
|
emoge-0.1.0/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# emoge
|
|
2
|
+
|
|
3
|
+
emoge is a package for research purposes: the analysis of images and the
|
|
4
|
+
emotions they evoke.
|
|
5
|
+
|
|
6
|
+
## Documentation
|
|
7
|
+
|
|
8
|
+
| Package | What it is for | Guide |
|
|
9
|
+
|---|---|---|
|
|
10
|
+
| `emoge.fuzzy_color` | Fuzzy color models: the degree to which every pixel belongs to every named color, and the color composition of an image | [docs/fuzzy_color](docs/fuzzy_color/) |
|
|
11
|
+
|
|
12
|
+
## Disclaimer
|
|
13
|
+
|
|
14
|
+
Not everything may be implemented yet. Missing parts can be added on your own,
|
|
15
|
+
or suggested in an issue or a pull request.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "emoge"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Research packages: fuzzy color models, and more to come."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "0BSD"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.12"
|
|
9
|
+
dependencies = [
|
|
10
|
+
"matplotlib>=3.9",
|
|
11
|
+
"numpy>=2.0",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
[[project.authors]]
|
|
15
|
+
name = "coredegon"
|
|
16
|
+
email = "valikalik15@gmail.com"
|
|
17
|
+
|
|
18
|
+
[build-system]
|
|
19
|
+
requires = ["uv_build>=0.13.7,<0.14.0"]
|
|
20
|
+
build-backend = "uv_build"
|
|
21
|
+
|
|
22
|
+
[dependency-groups]
|
|
23
|
+
dev = [
|
|
24
|
+
"pytest>=9.2.0",
|
|
25
|
+
"ruff>=0.17.5",
|
|
26
|
+
"ty>=0.0.104",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[tool.ruff.lint]
|
|
30
|
+
select = [
|
|
31
|
+
"E",
|
|
32
|
+
"W",
|
|
33
|
+
"F",
|
|
34
|
+
"I",
|
|
35
|
+
"N",
|
|
36
|
+
"D",
|
|
37
|
+
"UP",
|
|
38
|
+
"B",
|
|
39
|
+
"C4",
|
|
40
|
+
"SIM",
|
|
41
|
+
"PT",
|
|
42
|
+
"NPY",
|
|
43
|
+
"PERF",
|
|
44
|
+
"RUF",
|
|
45
|
+
]
|
|
46
|
+
ignore = [
|
|
47
|
+
"D105",
|
|
48
|
+
"D107",
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
[tool.ruff.lint.pydocstyle]
|
|
52
|
+
convention = "google"
|
|
53
|
+
|
|
54
|
+
[tool.ruff.lint.per-file-ignores]
|
|
55
|
+
"tests/*" = ["D"]
|
|
56
|
+
"docs/**" = ["D"]
|
|
57
|
+
|
|
58
|
+
[tool.pytest]
|
|
59
|
+
minversion = "9.0"
|
|
60
|
+
strict = true
|
|
61
|
+
testpaths = ["tests"]
|
|
62
|
+
addopts = ["--import-mode=importlib"]
|
|
63
|
+
filterwarnings = ["error::RuntimeWarning"]
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "emoge"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Research packages: fuzzy color models, and more to come."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "0BSD"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "coredegon", email = "valikalik15@gmail.com" }
|
|
10
|
+
]
|
|
11
|
+
requires-python = ">=3.12"
|
|
12
|
+
dependencies = [
|
|
13
|
+
"matplotlib>=3.9",
|
|
14
|
+
"numpy>=2.0",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
[build-system]
|
|
18
|
+
requires = ["uv_build>=0.13.7,<0.14.0"]
|
|
19
|
+
build-backend = "uv_build"
|
|
20
|
+
|
|
21
|
+
[dependency-groups]
|
|
22
|
+
dev = [
|
|
23
|
+
"pytest>=9.2.0",
|
|
24
|
+
"ruff>=0.17.5",
|
|
25
|
+
"ty>=0.0.104",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[tool.ruff.lint]
|
|
29
|
+
select = [
|
|
30
|
+
"E", "W", # pycodestyle
|
|
31
|
+
"F", # pyflakes
|
|
32
|
+
"I", # import order
|
|
33
|
+
"N", # naming
|
|
34
|
+
"D", # docstrings
|
|
35
|
+
"UP", # modern syntax
|
|
36
|
+
"B", # likely bugs
|
|
37
|
+
"C4", # comprehensions
|
|
38
|
+
"SIM", # simplifications
|
|
39
|
+
"PT", # pytest style
|
|
40
|
+
"NPY", # numpy
|
|
41
|
+
"PERF", # performance
|
|
42
|
+
"RUF", # ruff's own
|
|
43
|
+
]
|
|
44
|
+
ignore = [
|
|
45
|
+
"D105", # operators are documented by their class
|
|
46
|
+
"D107", # and so is __init__
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
[tool.ruff.lint.pydocstyle]
|
|
50
|
+
convention = "google"
|
|
51
|
+
|
|
52
|
+
[tool.ruff.lint.per-file-ignores]
|
|
53
|
+
"tests/*" = ["D"]
|
|
54
|
+
"docs/**" = ["D"]
|
|
55
|
+
|
|
56
|
+
[tool.pytest]
|
|
57
|
+
minversion = "9.0"
|
|
58
|
+
strict = true
|
|
59
|
+
testpaths = ["tests"]
|
|
60
|
+
addopts = ["--import-mode=importlib"] # test files in different folders may share a name
|
|
61
|
+
filterwarnings = ["error::RuntimeWarning"] # a leaked overflow or 0/0 is a bug
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Research packages: import each one by itself, as `from emoge import fuzzy_color`."""
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""Fuzzy color models: every pixel belongs to every named color to a degree.
|
|
2
|
+
|
|
3
|
+
```python
|
|
4
|
+
import emoge.fuzzy_color as fc
|
|
5
|
+
```
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from emoge.fuzzy_color.lut import lookup_table
|
|
9
|
+
from emoge.fuzzy_color.math import membership, norms
|
|
10
|
+
from emoge.fuzzy_color.model import ColorModel
|
|
11
|
+
from emoge.fuzzy_color.models import COLIBRI
|
|
12
|
+
from emoge.fuzzy_color.partitions import partition
|
|
13
|
+
from emoge.fuzzy_color.plots import (
|
|
14
|
+
plot_palette,
|
|
15
|
+
plot_swatches,
|
|
16
|
+
plot_terms,
|
|
17
|
+
plot_variable,
|
|
18
|
+
)
|
|
19
|
+
from emoge.fuzzy_color.sets import FuzzySet
|
|
20
|
+
from emoge.fuzzy_color.shapes import (
|
|
21
|
+
falling,
|
|
22
|
+
gaussian,
|
|
23
|
+
generalized_bell,
|
|
24
|
+
pi_shape,
|
|
25
|
+
piecewise_linear,
|
|
26
|
+
rectangle,
|
|
27
|
+
rising,
|
|
28
|
+
s_shape,
|
|
29
|
+
sigmoid,
|
|
30
|
+
sigmoid_difference,
|
|
31
|
+
sigmoid_product,
|
|
32
|
+
singleton,
|
|
33
|
+
trapezoid,
|
|
34
|
+
triangle,
|
|
35
|
+
two_sided_gaussian,
|
|
36
|
+
z_shape,
|
|
37
|
+
)
|
|
38
|
+
from emoge.fuzzy_color.spaces import HSI, HSV, RGB, Channel, Space
|
|
39
|
+
from emoge.fuzzy_color.variables import Variable
|
|
40
|
+
|
|
41
|
+
__all__ = [
|
|
42
|
+
"COLIBRI",
|
|
43
|
+
"HSI",
|
|
44
|
+
"HSV",
|
|
45
|
+
"RGB",
|
|
46
|
+
"Channel",
|
|
47
|
+
"ColorModel",
|
|
48
|
+
"FuzzySet",
|
|
49
|
+
"Space",
|
|
50
|
+
"Variable",
|
|
51
|
+
"falling",
|
|
52
|
+
"gaussian",
|
|
53
|
+
"generalized_bell",
|
|
54
|
+
"lookup_table",
|
|
55
|
+
"membership",
|
|
56
|
+
"norms",
|
|
57
|
+
"partition",
|
|
58
|
+
"pi_shape",
|
|
59
|
+
"piecewise_linear",
|
|
60
|
+
"plot_palette",
|
|
61
|
+
"plot_swatches",
|
|
62
|
+
"plot_terms",
|
|
63
|
+
"plot_variable",
|
|
64
|
+
"rectangle",
|
|
65
|
+
"rising",
|
|
66
|
+
"s_shape",
|
|
67
|
+
"sigmoid",
|
|
68
|
+
"sigmoid_difference",
|
|
69
|
+
"sigmoid_product",
|
|
70
|
+
"singleton",
|
|
71
|
+
"trapezoid",
|
|
72
|
+
"triangle",
|
|
73
|
+
"two_sided_gaussian",
|
|
74
|
+
"z_shape",
|
|
75
|
+
]
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"""`lookup_table`: the strongest color of every 8-bit pixel, worked out once."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
import numpy as np
|
|
11
|
+
from numpy.typing import NDArray
|
|
12
|
+
|
|
13
|
+
from emoge.fuzzy_color.model import ColorModel
|
|
14
|
+
|
|
15
|
+
# The strongest color of every pixel: its index, or its index and degree.
|
|
16
|
+
type Entries = NDArray[np.unsignedinteger | np.void]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def lookup_table(
|
|
20
|
+
model: ColorModel,
|
|
21
|
+
path: str | os.PathLike[str] | None = None,
|
|
22
|
+
*,
|
|
23
|
+
degree: bool = False,
|
|
24
|
+
) -> Entries:
|
|
25
|
+
"""Return `model.classify` of every 8-bit pixel, as `table[red, green, blue]`.
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
table = lookup_table(COLIBRI, "colibri.npz") # built and saved, or loaded
|
|
29
|
+
table[image[..., 0], image[..., 1], image[..., 2]] # COLIBRI.classify(image)
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
With `path`, the table is loaded from that compressed .npz file, or built
|
|
33
|
+
in seconds and saved there when there is none yet. A file that disagrees
|
|
34
|
+
with the model on a sample of pixels is refused: delete it to build anew.
|
|
35
|
+
|
|
36
|
+
A color takes the smallest unsigned integer that holds `len(colors)`, the
|
|
37
|
+
index of no color: a byte up to 255 colors. With `degree=True` every entry
|
|
38
|
+
also keeps the degree of its color, in steps of 1/255, the byte it takes in
|
|
39
|
+
a file: `table["color"]` and `table["degree"]`.
|
|
40
|
+
"""
|
|
41
|
+
if path is None:
|
|
42
|
+
return _build(model, degree)
|
|
43
|
+
path = Path(path)
|
|
44
|
+
if path.suffix != ".npz":
|
|
45
|
+
path = path.with_name(path.name + ".npz") # as np.savez names it
|
|
46
|
+
if not path.exists():
|
|
47
|
+
table = _build(model, degree)
|
|
48
|
+
_save(path, table)
|
|
49
|
+
return table
|
|
50
|
+
table = _load(path)
|
|
51
|
+
sample = np.arange(0, 256, 17, dtype=np.uint8) # 16 levels a channel
|
|
52
|
+
red, green, blue = np.meshgrid(sample, sample, sample, indexing="ij")
|
|
53
|
+
expected = _entries(model, np.stack([red, green, blue], axis=-1), degree)
|
|
54
|
+
if (
|
|
55
|
+
table.dtype != expected.dtype
|
|
56
|
+
or table.shape != (256, 256, 256)
|
|
57
|
+
or not np.array_equal(table[red, green, blue], expected)
|
|
58
|
+
):
|
|
59
|
+
raise ValueError(f"{path} holds a table of another model or kind")
|
|
60
|
+
return table
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _build(model: ColorModel, degree: bool) -> Entries:
|
|
64
|
+
levels = np.arange(256, dtype=np.uint8)
|
|
65
|
+
green, blue = np.meshgrid(levels, levels, indexing="ij")
|
|
66
|
+
|
|
67
|
+
def level(red: int) -> Entries: # one red level: 65536 pixels
|
|
68
|
+
pixels = np.stack([np.full_like(green, red), green, blue], axis=-1)
|
|
69
|
+
return _entries(model, pixels, degree)
|
|
70
|
+
|
|
71
|
+
# NumPy lets go of the GIL while it computes, so threads share the levels;
|
|
72
|
+
# past 4 of them memory, not the cores, holds the work back.
|
|
73
|
+
with ThreadPoolExecutor(min(4, os.cpu_count() or 1)) as pool:
|
|
74
|
+
return np.stack(list(pool.map(level, range(256))))
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _entries(model: ColorModel, pixels: NDArray[np.uint8], degree: bool) -> Entries:
|
|
78
|
+
"""The strongest color of every pixel, and its degree if it is kept."""
|
|
79
|
+
color, color_degree = model.strongest(pixels)
|
|
80
|
+
color = color.astype(np.min_scalar_type(len(model.colors)))
|
|
81
|
+
if not degree:
|
|
82
|
+
return color
|
|
83
|
+
return _join(color, np.round(color_degree * 255.0) / 255.0)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _join(color: Entries, degree: NDArray[np.floating]) -> Entries:
|
|
87
|
+
table = np.empty(
|
|
88
|
+
color.shape, dtype=[("color", color.dtype), ("degree", np.float16)]
|
|
89
|
+
)
|
|
90
|
+
table["color"] = color
|
|
91
|
+
table["degree"] = degree
|
|
92
|
+
return table
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# A file keeps the color and the degree apart, the degree in a byte, which
|
|
96
|
+
# compresses far better than a float16.
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _save(path: Path, table: NDArray[Any]) -> None:
|
|
100
|
+
if table.dtype.names is None:
|
|
101
|
+
np.savez_compressed(path, color=table)
|
|
102
|
+
return
|
|
103
|
+
in_bytes = np.round(table["degree"].astype(np.float32) * 255.0).astype(np.uint8)
|
|
104
|
+
np.savez_compressed(path, color=table["color"], degree=in_bytes)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _load(path: Path) -> Entries:
|
|
108
|
+
with np.load(path) as file:
|
|
109
|
+
if "degree" not in file.files:
|
|
110
|
+
return file["color"]
|
|
111
|
+
return _join(file["color"], file["degree"] / 255.0)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""The mathematics of fuzzy sets as plain NumPy functions; nothing here knows colors.
|
|
2
|
+
|
|
3
|
+
- `membership`: the shapes of fuzzy sets;
|
|
4
|
+
- `norms`: t-norms and s-norms, ways to join degrees.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from emoge.fuzzy_color.math import membership, norms
|
|
8
|
+
|
|
9
|
+
__all__ = ["membership", "norms"]
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"""Membership functions: the shapes of fuzzy sets, as plain NumPy functions.
|
|
2
|
+
|
|
3
|
+
Every function takes an array of values `x` first, then the parameters of its
|
|
4
|
+
shape, and returns the degree of every value: in [0, 1], in the shape of `x`.
|
|
5
|
+
`FuzzySet(function, parameters)` makes a set of one.
|
|
6
|
+
|
|
7
|
+
Parameters that mark places on the axis ascend. Equal neighbours make a
|
|
8
|
+
vertical edge, where the higher degree holds.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import itertools
|
|
14
|
+
import math
|
|
15
|
+
from collections.abc import Sequence
|
|
16
|
+
|
|
17
|
+
import numpy as np
|
|
18
|
+
from numpy.typing import NDArray
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _require_ascending(*parameters: float) -> None:
|
|
22
|
+
if not all(map(math.isfinite, parameters)) or list(parameters) != sorted(
|
|
23
|
+
parameters
|
|
24
|
+
):
|
|
25
|
+
raise ValueError(f"parameters must be finite and ascending, got {parameters}")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _require_positive(**parameters: float) -> None:
|
|
29
|
+
for name, value in parameters.items():
|
|
30
|
+
if not (math.isfinite(value) and value > 0.0):
|
|
31
|
+
raise ValueError(f"{name} must be positive and finite, got {value}")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
# --- straight lines -------------------------------------------------------------------
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def rising(x: NDArray[np.float64], a: float, b: float) -> NDArray[np.float64]:
|
|
38
|
+
"""0 up to `a`, a straight line, 1 from `b` on."""
|
|
39
|
+
_require_ascending(a, b)
|
|
40
|
+
if a == b:
|
|
41
|
+
return (x >= a).astype(np.float64)
|
|
42
|
+
return np.clip((x - a) / (b - a), 0.0, 1.0)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def falling(x: NDArray[np.float64], a: float, b: float) -> NDArray[np.float64]:
|
|
46
|
+
"""1 up to `a`, a straight line, 0 from `b` on."""
|
|
47
|
+
_require_ascending(a, b)
|
|
48
|
+
if a == b:
|
|
49
|
+
return (x <= a).astype(np.float64)
|
|
50
|
+
return np.clip((b - x) / (b - a), 0.0, 1.0)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def triangle(
|
|
54
|
+
x: NDArray[np.float64], a: float, b: float, c: float
|
|
55
|
+
) -> NDArray[np.float64]:
|
|
56
|
+
"""Rises from `a` to the peak at `b`, falls to `c`."""
|
|
57
|
+
_require_ascending(a, b, c)
|
|
58
|
+
return np.minimum(rising(x, a, b), falling(x, b, c))
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def trapezoid(
|
|
62
|
+
x: NDArray[np.float64], a: float, b: float, c: float, d: float
|
|
63
|
+
) -> NDArray[np.float64]:
|
|
64
|
+
"""Rises from `a` to `b`, is 1 until `c`, falls to `d`."""
|
|
65
|
+
_require_ascending(a, b, c, d)
|
|
66
|
+
return np.minimum(rising(x, a, b), falling(x, c, d))
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def rectangle(x: NDArray[np.float64], a: float, b: float) -> NDArray[np.float64]:
|
|
70
|
+
"""1 on `[a, b]`, 0 elsewhere."""
|
|
71
|
+
return trapezoid(x, a, a, b, b)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def singleton(x: NDArray[np.float64], a: float) -> NDArray[np.float64]:
|
|
75
|
+
"""1 at `a`, 0 elsewhere."""
|
|
76
|
+
return trapezoid(x, a, a, a, a)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def piecewise_linear(
|
|
80
|
+
x: NDArray[np.float64], points: Sequence[tuple[float, float]]
|
|
81
|
+
) -> NDArray[np.float64]:
|
|
82
|
+
"""Straight lines through `(value, degree)` points, flat before and after them.
|
|
83
|
+
|
|
84
|
+
The values strictly ascend.
|
|
85
|
+
"""
|
|
86
|
+
values = [value for value, _ in points]
|
|
87
|
+
degrees = [degree for _, degree in points]
|
|
88
|
+
if not values or any(left >= right for left, right in itertools.pairwise(values)):
|
|
89
|
+
raise ValueError(f"the values of the points must strictly ascend, got {values}")
|
|
90
|
+
if not all(0.0 <= degree <= 1.0 for degree in degrees):
|
|
91
|
+
raise ValueError(f"the degrees of the points must lie in [0, 1], got {degrees}")
|
|
92
|
+
return np.interp(x, values, degrees)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# --- smooth curves --------------------------------------------------------------------
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def gaussian(
|
|
99
|
+
x: NDArray[np.float64], center: float, sigma: float
|
|
100
|
+
) -> NDArray[np.float64]:
|
|
101
|
+
"""`exp(-(x - center)**2 / (2 * sigma**2))`."""
|
|
102
|
+
_require_positive(sigma=sigma)
|
|
103
|
+
return np.exp(-((x - center) ** 2) / (2.0 * sigma**2))
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def two_sided_gaussian(
|
|
107
|
+
x: NDArray[np.float64], center1: float, sigma1: float, center2: float, sigma2: float
|
|
108
|
+
) -> NDArray[np.float64]:
|
|
109
|
+
"""A gaussian up to `center1`, 1 until `center2`, a gaussian after it."""
|
|
110
|
+
_require_positive(sigma1=sigma1, sigma2=sigma2)
|
|
111
|
+
left = np.minimum(x, center1) - center1 # 0 from center1 on
|
|
112
|
+
right = np.maximum(x, center2) - center2 # 0 up to center2
|
|
113
|
+
return np.exp(-(left**2) / (2.0 * sigma1**2) - right**2 / (2.0 * sigma2**2))
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def generalized_bell(
|
|
117
|
+
x: NDArray[np.float64], center: float, width: float, slope: float
|
|
118
|
+
) -> NDArray[np.float64]:
|
|
119
|
+
"""`1 / (1 + |(x - center) / width|**(2 * slope))`."""
|
|
120
|
+
_require_positive(width=width, slope=slope)
|
|
121
|
+
return 1.0 / (1.0 + np.abs((x - center) / width) ** (2.0 * slope))
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def sigmoid(x: NDArray[np.float64], center: float, slope: float) -> NDArray[np.float64]:
|
|
125
|
+
"""`1 / (1 + exp(-slope * (x - center)))`; a negative slope makes it fall."""
|
|
126
|
+
with np.errstate(over="ignore"): # exp overflows to inf, which gives 0
|
|
127
|
+
return 1.0 / (1.0 + np.exp(-slope * (x - center)))
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def sigmoid_difference(
|
|
131
|
+
x: NDArray[np.float64], center1: float, slope1: float, center2: float, slope2: float
|
|
132
|
+
) -> NDArray[np.float64]:
|
|
133
|
+
"""`|sigmoid(center1, slope1) - sigmoid(center2, slope2)|`."""
|
|
134
|
+
return np.abs(sigmoid(x, center1, slope1) - sigmoid(x, center2, slope2))
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def sigmoid_product(
|
|
138
|
+
x: NDArray[np.float64], center1: float, slope1: float, center2: float, slope2: float
|
|
139
|
+
) -> NDArray[np.float64]:
|
|
140
|
+
"""`sigmoid(center1, slope1) * sigmoid(center2, slope2)`."""
|
|
141
|
+
return sigmoid(x, center1, slope1) * sigmoid(x, center2, slope2)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def s_shape(x: NDArray[np.float64], a: float, b: float) -> NDArray[np.float64]:
|
|
145
|
+
"""Smooth rise from 0 at `a` to 1 at `b`: `2t**2`, then `1 - 2(1 - t)**2`."""
|
|
146
|
+
_require_ascending(a, b)
|
|
147
|
+
if a == b:
|
|
148
|
+
raise ValueError(f"a smooth rise needs a < b, got {a} and {b}")
|
|
149
|
+
t = np.clip((x - a) / (b - a), 0.0, 1.0)
|
|
150
|
+
lower_half = 2.0 * np.minimum(t, 1.0 - t) ** 2
|
|
151
|
+
return np.where(t <= 0.5, lower_half, 1.0 - lower_half)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def z_shape(x: NDArray[np.float64], a: float, b: float) -> NDArray[np.float64]:
|
|
155
|
+
"""Smooth fall from 1 at `a` to 0 at `b`: `1 - s_shape(a, b)`."""
|
|
156
|
+
return 1.0 - s_shape(x, a, b)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def pi_shape(
|
|
160
|
+
x: NDArray[np.float64], a: float, b: float, c: float, d: float
|
|
161
|
+
) -> NDArray[np.float64]:
|
|
162
|
+
"""Smooth rise from `a` to `b`, 1 until `c`, smooth fall to `d`."""
|
|
163
|
+
_require_ascending(a, b, c, d)
|
|
164
|
+
return s_shape(x, a, b) * z_shape(x, c, d)
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""Norms: ways to join two degrees into one.
|
|
2
|
+
|
|
3
|
+
A t-norm joins degrees for "and", an s-norm for "or". Every norm takes two
|
|
4
|
+
broadcastable arrays of degrees and returns the joined degrees.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from collections.abc import Callable
|
|
10
|
+
|
|
11
|
+
import numpy as np
|
|
12
|
+
from numpy.typing import NDArray
|
|
13
|
+
|
|
14
|
+
type Norm = Callable[[NDArray[np.float64], NDArray[np.float64]], NDArray[np.float64]]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# --- t-norms: "and" -------------------------------------------------------------------
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def minimum(a: NDArray[np.float64], b: NDArray[np.float64]) -> NDArray[np.float64]:
|
|
21
|
+
"""`min(a, b)`: the Goedel t-norm, the largest of all."""
|
|
22
|
+
return np.minimum(a, b)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def product(a: NDArray[np.float64], b: NDArray[np.float64]) -> NDArray[np.float64]:
|
|
26
|
+
"""`a * b`: the product t-norm."""
|
|
27
|
+
return a * b
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def bounded_difference(
|
|
31
|
+
a: NDArray[np.float64], b: NDArray[np.float64]
|
|
32
|
+
) -> NDArray[np.float64]:
|
|
33
|
+
"""`max(a + b - 1, 0)`: the Lukasiewicz t-norm."""
|
|
34
|
+
return np.maximum(a + b - 1.0, 0.0)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def drastic_product(
|
|
38
|
+
a: NDArray[np.float64], b: NDArray[np.float64]
|
|
39
|
+
) -> NDArray[np.float64]:
|
|
40
|
+
"""`b` if `a` is 1, `a` if `b` is 1, else 0: the smallest of all t-norms."""
|
|
41
|
+
return np.where(a == 1.0, b, np.where(b == 1.0, a, 0.0))
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def nilpotent_minimum(
|
|
45
|
+
a: NDArray[np.float64], b: NDArray[np.float64]
|
|
46
|
+
) -> NDArray[np.float64]:
|
|
47
|
+
"""`min(a, b)` if `a + b > 1`, else 0."""
|
|
48
|
+
return np.where(a + b > 1.0, np.minimum(a, b), 0.0)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def hamacher_product(
|
|
52
|
+
a: NDArray[np.float64], b: NDArray[np.float64]
|
|
53
|
+
) -> NDArray[np.float64]:
|
|
54
|
+
"""`a * b / (a + b - a * b)`, and 0 where both are 0."""
|
|
55
|
+
joined = a * b
|
|
56
|
+
divisor = a + b - joined
|
|
57
|
+
return np.divide(joined, divisor, out=np.zeros_like(divisor), where=divisor > 0.0)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def einstein_product(
|
|
61
|
+
a: NDArray[np.float64], b: NDArray[np.float64]
|
|
62
|
+
) -> NDArray[np.float64]:
|
|
63
|
+
"""`a * b / (2 - (a + b - a * b))`."""
|
|
64
|
+
return a * b / (2.0 - (a + b - a * b))
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
# --- s-norms: "or" --------------------------------------------------------------------
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def maximum(a: NDArray[np.float64], b: NDArray[np.float64]) -> NDArray[np.float64]:
|
|
71
|
+
"""`max(a, b)`: the Goedel s-norm, the smallest of all."""
|
|
72
|
+
return np.maximum(a, b)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def probabilistic_sum(
|
|
76
|
+
a: NDArray[np.float64], b: NDArray[np.float64]
|
|
77
|
+
) -> NDArray[np.float64]:
|
|
78
|
+
"""`a + b - a * b`: the s-norm of the product."""
|
|
79
|
+
return a + b - a * b
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def bounded_sum(a: NDArray[np.float64], b: NDArray[np.float64]) -> NDArray[np.float64]:
|
|
83
|
+
"""`min(a + b, 1)`: the Lukasiewicz s-norm."""
|
|
84
|
+
return np.minimum(a + b, 1.0)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def drastic_sum(a: NDArray[np.float64], b: NDArray[np.float64]) -> NDArray[np.float64]:
|
|
88
|
+
"""`b` if `a` is 0, `a` if `b` is 0, else 1: the largest of all s-norms."""
|
|
89
|
+
return np.where(a == 0.0, b, np.where(b == 0.0, a, 1.0))
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def nilpotent_maximum(
|
|
93
|
+
a: NDArray[np.float64], b: NDArray[np.float64]
|
|
94
|
+
) -> NDArray[np.float64]:
|
|
95
|
+
"""`max(a, b)` if `a + b < 1`, else 1."""
|
|
96
|
+
return np.where(a + b < 1.0, np.maximum(a, b), 1.0)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def hamacher_sum(a: NDArray[np.float64], b: NDArray[np.float64]) -> NDArray[np.float64]:
|
|
100
|
+
"""`(a + b - 2ab) / (1 - ab)`, and 1 where both are 1."""
|
|
101
|
+
joined = a * b
|
|
102
|
+
divisor = 1.0 - joined
|
|
103
|
+
return np.divide(
|
|
104
|
+
a + b - 2.0 * joined, divisor, out=np.ones_like(divisor), where=divisor > 0.0
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def einstein_sum(a: NDArray[np.float64], b: NDArray[np.float64]) -> NDArray[np.float64]:
|
|
109
|
+
"""`(a + b) / (1 + a * b)`."""
|
|
110
|
+
return (a + b) / (1.0 + a * b)
|