camera_analyze/main.py
2025-04-24 17:44:30 +02:00

138 lines
3.7 KiB
Python

from typing import Any
from numpy import array, diff, mean, median
from numpy.linalg import norm
from rich.console import Console
from pathlib import Path
from matplotlib.pyplot import figure, show
from utils import diff_matrix, Directions, Axis, Vector
from framel import frgetvect
console = Console()
def frgetvectN(
filename: str | list[str],
channels: list[str],
start: int = -1,
span: int = -1,
verbose: bool = False,
) -> list[Any]:
return [frgetvect(filename, channel, start, span, verbose) for channel in channels]
def main(
file: Path,
start: int = 15,
duration: int = 2 * 5 * 49,
factors: list = [1.00, 0.90, 1.95, 1.19],
step: int = 50,
_show: bool = False,
):
factors = array(factors)
indexes = diff_matrix(3)
steps = step * factors
channels_name = ["FitPosY", "FitPosX"]
channels = [
"V1:Camera_Scatter_{}".format(channel_name) for channel_name in channels_name
]
data = frgetvectN(str(file), channels)
if _show:
Figure = figure()
for i in range(len(data)):
Figure.gca().plot(
data[i][0][start : start + duration], label=channels_name[i]
)
Figure.gca().legend()
show()
medians = [
median(datum[0][start : start + duration].reshape((-1, 10)), axis=1)
for datum in data
]
positions = [
[diff(median)[index] for index in indexes] for median in medians
] # liste de mouvements associé à un axe pour une direction
means: Any = list()
for i in range(len(positions[0])):
if i < 2:
means.append(mean(positions[0][i] / steps[i]))
else:
means.append(mean(positions[1][i] / steps[i]))
means = array(means)
temps = list()
for i in range(len(positions[0])):
temp = list()
for j in range(len(positions)):
temp.append(mean(positions[j][i]) / steps[i])
temps.append(Vector(x=temp[0], y=temp[1]))
directions = Directions(
x=Axis(pos=temps[0], neg=temps[1]),
y=Axis(pos=temps[2], neg=temps[3]),
)
i, k = 1, norm(directions.x.pos) / norm(directions.y.pos)
j = (
directions.y.neg.x * (-k * directions.y.pos.y - i * directions.x.pos.y)
+ k * directions.y.neg.y * directions.y.pos.x
+ i * directions.x.pos.x * directions.y.neg.y
) / (
directions.x.neg.y * directions.y.neg.x
- directions.x.neg.x * directions.y.neg.y
)
l = -(
directions.x.neg.x * (-k * directions.y.pos.y - i * directions.x.pos.y)
+ k * directions.x.neg.y * directions.y.pos.x
+ i * directions.x.neg.y * directions.x.pos.x
) / (
directions.x.neg.y * directions.y.neg.x
- directions.x.neg.x * directions.y.neg.y
)
if _show:
Figure = figure()
namess = ["X", "Y"]
names = ["X+", "X-", "Y+", "Y-"]
for i in range(len(positions)):
position = positions[i]
name = namess[i]
for j in range(len(position)):
name_ = names[j]
Figure.gca().plot(position[j], label="{} {}".format(name, name_))
Figure.gca().legend()
show()
new_factors = array([i, j, k, l])
console.print("movement for one step:")
console.print(directions)
console.print("factors are: {}".format(new_factors))
console.print("-------------------")
console.print(new_factors * means)
console.print(factors * means)
main(
Path(
"/home/demagny/data/backscattermeter/camera/snail_N3_SXp50_old_1-1429443262-102.gwf"
),
)
main(
Path(
"/home/demagny/data/backscattermeter/camera/snail_N3_SXp50_3-1429442495-124.gwf"
),
96,
2 * 5 * 49,
[1.00, 0.90, 1.95, 1.404],
)