Python;AUSNAHME AUSHEIT KARTE ()
-
14-11-2019 - |
Frage
Ich mache etwas Pyblotting von generischen Daten und umwandeln Sie sie von einem Leistungswert auf einen dB-Wert. Aufgrund des Systems, das diese Werte stammen, wird 0 als "die nützlichen Daten endet hier" Indikator (der Art der Mathematik, kein definierter Wert).
meine übliche Art des Umgangs mit diesen ist die Umwicklung der Konvertierung in einem Versuch / außer, mit Ausnahme und zurücksenden "niedriger" Wert, z. B. generasacodicetagpre.
Dies ist in meinem Rahmen in Bezug auf 90% in meinem Rahmen, außer wenn es um die Grafik geeignet ist.
Ich bin faul, also was ich in der Minute tue, ist: generasacodicetagpre.
Dies zieht die Daten ordnungsgemäß an, und wenn die Daten enden, stürzen Sie sich von einer Klippe aus, was das erwartete Verhalten ist. Aber was ich gerne passieren würde, ist für das Graphen auf ende , wenn er auf eine ValueError-Ausnahme trifft und f () insgesamt trennen kann.
Andere, als den Karten in eine Schleife zu brechen, hat jeder brillante Ideen?
Updates:
Ich verwende Pylab / Matplotlib "Endpoint" ist Ausführungsabhängigkeit; Manchmal spielt das Obige keine Rolle, da es keine schlechten Werte gibt. Dies ist alles in einem Anstrengungsaufwand, um faul zu sein, und verwenden Sie die MATPLOTLIBS-Graph-Skalierung, anstatt dynamische Ylims basierend auf der Minute des YData zurückzusetzen (was ich nicht atm, nur Ylim (-140) in diesem Fall.)
vage Wichtiges Update auf Antwort: Die Antwort von Unutbu ist, was ich tatsächlich für meine Umsetzung verwenden werde, weil ich (nicht erwähnt in den Frageabhängigkeiten nicht erwähnt von diesen anderen Fällen in probieren Ausnahme; Manchmal macht -inf mehr Sinn, als Sie denken würden.
Danke an jeden, um freutlich zu sein, und ich entschuldige mich bei Unutbu für den FrageFail.
Lösung
Perhaps there's some trick in the plotting library, but a much better options seems not generating such data to begin with. It's not that map
saves you thirty lines of code...
Use itertools.takewhile(lambda y: y != NO_VALUE, (f(y) for y in yvals))
(and wrap it in a call to list
if the plotting library requires a list instead of an iterable).
Edit: I had an even better idea: In the wrapper, add
except ValueError:
raise StopIteration
That's the exception signaling "end of iterale", and map
respects it.
Andere Tipps
If you are using matplotlib
, then it implies you have numpy
installed.
Since you are converting to dB
, it sounds like you might be taking a log. In that case, np.log(0) = -inf
.
You can mask nans and infs with the numpy function np.ma.masked_invalid, and matplotlib
can plot masked arrays. For example,
import matplotlib.pyplot as plt
import numpy as np
xvals=np.arange(100)
yvals=np.cumsum(np.random.random(100))
yvals[-10:]=0
yvals=np.log(yvals)
print(yvals[-10:])
# [-Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf]
yvals=np.ma.masked_invalid(yvals)
plt.plot(xvals,yvals)
plt.show()
yields
Notice that the graph ends with xval
equal to 89, since the last 10 values of yval
are masked.
You're needlessly limiting yourself by refusing to use a looping construct.
In your situation you want to stop iterating over data when a certain value is reached, that is exactly the purpose of forloops
and breaks
yvals_ = []
for y in yvals:
y_ = f(y)
if y_ == -140:
break
else:
yvals_.append(y_)
p1.plot(xvals[:len(yvals_)],yvals_)
It seems like you have data, and you don't want to plot the last point. So what about not plotting it?
pl.plot(xvals[:-1], map(f, yvals)[:-1])