Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mtl test #129

Merged
merged 3 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 68 additions & 67 deletions fire/cli/mtl.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,56 +57,60 @@ def get_observation_strings(
spredning = fil[1]
if verbose:
print("Læser " + filnavn + " med spredning ", spredning)
with open(filnavn, "rt") as obsfil:
for line in obsfil:
if "#" != line[0]:
continue
line = line.lstrip("#").strip()

# Check at observationen er i et af de kendte formater
tokens = line.split(" ", 12)
assert len(tokens) in (9, 13, 14), (
"Malform input line: " + line + " i fil: " + filnavn
)

# Bring observationen på kanonisk 14-feltform.
for i in range(len(tokens), 13):
tokens.append(0)
if len(tokens) < 14:
tokens.append('""')
tokens[13] = tokens[13].lstrip('"').strip().rstrip('"')

# Korriger de rædsomme dato/tidsformater
tid = " ".join((tokens[kol.dato], tokens[kol.tid]))
try:
isotid = datetime.strptime(tid, "%d.%m.%Y %H.%M")
except ValueError:
sys.exit(
"Argh - ikke-understøttet datoformat: '"
+ tid
+ "' i fil: "
+ filnavn
try:
with open(filnavn, "rt") as obsfil:
tilgængelig = True
for line in obsfil:
if "#" != line[0]:
continue
line = line.lstrip("#").strip()

# Check at observationen er i et af de kendte formater
tokens = line.split(" ", 12)
assert len(tokens) in (9, 13, 14), (
"Malform input line: " + line + " i fil: " + filnavn
)

# Reorganiser søjler og omsæt numeriske data fra strengrepræsentation til tal
reordered = [
tokens[kol.journal],
tokens[kol.fra],
tokens[kol.til],
float(tokens[kol.dH]),
float(tokens[kol.L]),
int(tokens[kol.setups]),
spredning,
tokens[kol.kommentar],
isotid,
float(tokens[kol.T]),
int(tokens[kol.sky]),
int(tokens[kol.sol]),
int(tokens[kol.vind]),
int(tokens[kol.sigt]),
filnavn,
]
observationer.append(reordered)
# Bring observationen på kanonisk 14-feltform.
for i in range(len(tokens), 13):
tokens.append(0)
if len(tokens) < 14:
tokens.append('""')
tokens[13] = tokens[13].lstrip('"').strip().rstrip('"')

# Korriger de rædsomme dato/tidsformater
tid = " ".join((tokens[kol.dato], tokens[kol.tid]))
try:
isotid = datetime.strptime(tid, "%d.%m.%Y %H.%M")
except ValueError:
sys.exit(
"Argh - ikke-understøttet datoformat: '"
+ tid
+ "' i fil: "
+ filnavn
)

# Reorganiser søjler og omsæt numeriske data fra strengrepræsentation til tal
reordered = [
tokens[kol.journal],
tokens[kol.fra],
tokens[kol.til],
float(tokens[kol.dH]),
float(tokens[kol.L]),
int(tokens[kol.setups]),
spredning,
tokens[kol.kommentar],
isotid,
float(tokens[kol.T]),
int(tokens[kol.sky]),
int(tokens[kol.sol]),
int(tokens[kol.vind]),
int(tokens[kol.sigt]),
filnavn,
]
observationer.append(reordered)
except FileNotFoundError:
print("Kunne ikke læse filen '" + filnavn + "'")
return observationer


Expand Down Expand Up @@ -615,25 +619,27 @@ def go(**kwargs) -> None:
forbundne_punkter = find_forbundne_punkter(
observationer, alle_punkter, fastholdte_punkter
)
print(f"Forbundne punkter: {forbundne_punkter}")
estimerede = tuple(sorted(set(forbundne_punkter) - set(fastholdte_punkter)))
estimerede_punkter = tuple(sorted(set(forbundne_punkter) - set(fastholdte_punkter)))
print(f"Forbundne punkter: {forbundne_punkter}")
print(f"Estimerede punkter: {estimerede_punkter}")

if "Regn" in workflow:
# -----------------------------------------------------
# Opstil designmatrix, responsvektor og vægtvektor
# -----------------------------------------------------
(X, P, y) = designmatrix(
observationer, forbundne_punkter, estimerede, fastholdte, holdte
observationer, forbundne_punkter, estimerede_punkter, fastholdte, holdte
)

# -----------------------------------------------------
# Udfør beregning og rapporter i kort form
# Udfør beregning og rapportér i kort form
# -----------------------------------------------------

# Først en ikke-vægtet udjævning som sammenligningsgrundlag
model = sm.OLS(y, X)
result = model.fit()
print("Ikke-vægtet")
print(result.params)
# model = sm.OLS(y, X)
# result = model.fit()
# print("Ikke-vægtet")
# print(result.params)

# Se https://www.statsmodels.org/devel/examples/notebooks/generated/wls.html
model = sm.WLS(y, X, weights=P ** 2)
Expand All @@ -643,25 +649,20 @@ def go(**kwargs) -> None:
# Se https://www.statsmodels.org/stable/generated/statsmodels.regression.linear_model.RegressionResults.html
print(result.params)
print(result.HC0_se)
# print(result.summary())
print(result.summary2())
wlsparams = result.params
print(dir(result))
# print(dir(result))

# Geninstaller 'punkt'-søjlen som indexsøjle, så vi kan indicere fornuftigt
punktoversigt = punktoversigt.set_index("punkt")
for punkt, kote in zip(estimerede, wlsparams):
for punkt, kote in zip(estimerede_punkter, wlsparams):
punktoversigt.at[punkt, "ny"] = kote
punktoversigt = punktoversigt.reset_index()

# Ændring i millimeter
d = list(abs(punktoversigt["kote"] - punktoversigt["ny"]) * 1000)
print(d)
# Men vi ignorerer ændringer under mikrometerniveau
dd = [e if e > 0.001 else None for e in d]
print(dd)

print(dd)
punktoversigt["Δ"] = dd

X["P"] = np.floor(100 * P / max(P) + 0.5)
Expand All @@ -674,7 +675,7 @@ def go(**kwargs) -> None:
# https://www.marsja.se/pandas-excel-tutorial-how-to-read-and-write-excel-files
# https://pypi.org/project/XlsxWriter/
# -----------------------------------------------------
print("Skriver netoversigt")
print("Skriver resultater")

ark = [("Punktoversigt", punktoversigt)]
if "Regn" in workflow:
Expand All @@ -684,10 +685,10 @@ def go(**kwargs) -> None:
if "Observationer" in workflow:
ark += [("Observationer", observationer)]
print(f"Alle ark: {[a[0] for a in ark]}")
writer = pd.ExcelWriter("netoversigt.xlsx", engine="xlsxwriter")
writer = pd.ExcelWriter("resultat.xlsx", engine="xlsxwriter")
for a in ark:
print(f"Nu arker vi {a[0]}")
# print(f"Nu arker vi {a[0]}")
if a[1] is not None:
a[1].to_excel(writer, sheet_name=a[0], index=False)
writer.save()
print("Færdig - output kan ses i [netoversigt.xlsx]")
print("Færdig - output kan ses i 'resultat.xlsx'")
Binary file added test/mtl/projekt.xlsx
Binary file not shown.
Binary file added test/mtl/resultat_canonical.xlsx
Binary file not shown.
12 changes: 12 additions & 0 deletions test/mtl/ta04_observationer.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 96-04-09021 96-04-09020 20.01.2020 10.31 887.59 0.46483 259865.1 9.0 8 8 2 4 1000
# 96-04-09020 96-04-09039 20.01.2020 10.49 627.19 -2.95277 259866.1 8.0 5 8 2 4 1000
# 96-04-09039 96-04-09018 20.01.2020 11.35 1142.23 20.99977 259867.1 8.0 10 8 2 4 1000
# 96-04-09018 96-04-09039 20.01.2020 12.38 1136.72 -20.99842 259867.2 8.0 10 8 2 4 1000
# 96-04-09039 96-04-09020 20.01.2020 12.58 607.82 2.95354 259866.2 9.0 5 8 2 4 1000
# 96-04-09020 96-04-09021 20.01.2020 13.31 892.89 -0.46528 259865.2 9.0 8 8 2 4 1000
# 96-04-09021 96-04-00809 20.01.2020 13.48 36.29 2.69872 259868.1 10.0 2 6 1 4 1000
# 96-04-00809 96-04-09021 20.01.2020 13.51 36.29 -2.69882 259868.2 9.0 2 6 1 4 1000
# 96-04-09021 96-04-00809 20.01.2020 14.32 36.56 2.69892 259868.3 8.0 2 6 2 4 1000
# 96-04-00809 96-04-09021 20.01.2020 14.36 36.57 -2.69863 259868.4 8.0 2 8 2 4 1000
# 96-04-09015 96-04-09018 20.01.2020 15.35 449.93 -6.23623 259869.2 8.0 8 8 2 4 1000
# 96-04-09018 96-04-09015 20.01.2020 16.07 450.00 6.23669 259869.1 8.0 8 8 2 4 1000
26 changes: 26 additions & 0 deletions test/mtl/test_mtl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os

import click
import pytest

from click.testing import CliRunner
from fire.cli.mtl import mtl


def test_cli():
runner = CliRunner()

title = "go"
args = ["go", "bananas"]

click.echo("\nTest: " + title)
click.echo(" Emulating: fire mtl " + " ".join(args))

cwd = os.getcwd()
# As os.chdir(os.path.dirname(__file__)), but using the absolute path
os.chdir(os.path.dirname(os.path.abspath(__file__)))
result = runner.invoke(mtl, args)
os.remove("resultat.xlsx")
os.chdir(cwd)

assert result.exit_code == 0