Skip to content

Commit

Permalink
Added data editing example
Browse files Browse the repository at this point in the history
Example added in which data is changed and saved again.
In connection with this example a bug was fixed that arrays could only be read.
  • Loading branch information
maxkrapp1 committed Nov 28, 2023
1 parent 5a8e626 commit eafd201
Show file tree
Hide file tree
Showing 15 changed files with 353 additions and 66 deletions.
198 changes: 198 additions & 0 deletions Examples/EditImpedanceData/EditImpedanceData.ipynb

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions Examples/EditImpedanceData/EditImpedanceData.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from zahner_analysis.file_import.ism_import import IsmImport
from zahner_analysis.plotting.impedance_plot import bodePlotter
import matplotlib.pyplot as plt
from zahner_analysis.file_export.ism_export import IsmExport
import numpy as np

impedanceData = IsmImport("li-ion-battery.ism")

frequencies = impedanceData.getFrequencyArray()
impedanceAbsolut = impedanceData.getImpedanceArray()
phase = impedanceData.getPhaseArray()

(fig1, (impedanceAxis1, phaseAxis1)) = bodePlotter(
frequencies=frequencies,
impedanceAbsolute=impedanceAbsolut,
phase=phase,
zTogetherPhase=False)
fig1.set_size_inches(18, 10)
plt.show()

maxFrequency = 1000
try:
index = next(i for i, freq in enumerate(frequencies) if freq > maxFrequency)
except:
index = len(frequencies)

frequenciesEdit = frequencies[:index]
impedanceAbsolutEdit = impedanceAbsolut[:index] * 2.0
impedanceAbsolutEdit = np.clip(impedanceAbsolutEdit, 0.08, 1)
phaseEdit = phase[:index] * 1.2

impedanceAbsolutEdit[5] = 4
impedanceAbsolutEdit[10] = 2

frequenciesEdit = np.insert(frequenciesEdit, 0, 10e-6)
impedanceAbsolutEdit = np.insert(impedanceAbsolutEdit, 0, impedanceAbsolutEdit[0])
phaseEdit = np.insert(phaseEdit, 0, phaseEdit[0])

(fig2, (impedanceAxis1, phaseAxis1)) = bodePlotter(
frequencies=frequencies,
impedanceAbsolute=impedanceAbsolut,
phase=phase,
zTogetherPhase=False)
(fig2, (impedanceAxis2, phaseAxis2)) = bodePlotter(
axes=(impedanceAxis1, phaseAxis1),
frequencies=frequenciesEdit,
impedanceAbsolute=impedanceAbsolutEdit,
phase=phaseEdit,
zTogetherPhase=False,
argsImpedanceAxis={"linestyle": "solid", "marker": None},
argsPhaseAxis={"linestyle": "solid", "marker": None})

impedanceAxis2.legend(["Original Data", "Edited Data"])
phaseAxis2.legend(["Original Data", "Edited Data"])

fig2.set_size_inches(18, 10)
plt.show()


exportFile = IsmExport(frequency=frequenciesEdit,impedance=impedanceAbsolutEdit,phase=phaseEdit)
exportFile.writeToFile("li-ion-battery-edited.ism")

Binary file not shown.
Binary file added Examples/EditImpedanceData/li-ion-battery.ism
Binary file not shown.
2 changes: 1 addition & 1 deletion zahner_analysis/analysis_tools/analysis_connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
r"""
____ __ __ __ __ _ __
/_ / ___ _/ / ___ ___ ___________ / /__ / /__/ /_____(_) /__
/ /_/ _ `/ _ \/ _ \/ -_) __/___/ -_) / -_) '_/ __/ __/ / '_/
Expand Down
2 changes: 1 addition & 1 deletion zahner_analysis/analysis_tools/eis_fitting.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
r"""
____ __ __ __ __ _ __
/_ / ___ _/ / ___ ___ ___________ / /__ / /__/ /_____(_) /__
/ /_/ _ `/ _ \/ _ \/ -_) __/___/ -_) / -_) '_/ __/ __/ / '_/
Expand Down
2 changes: 1 addition & 1 deletion zahner_analysis/analysis_tools/error.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
r"""
____ __ __ __ __ _ __
/_ / ___ _/ / ___ ___ ___________ / /__ / /__/ /_____(_) /__
/ /_/ _ `/ _ \/ _ \/ -_) __/___/ -_) / -_) '_/ __/ __/ / '_/
Expand Down
19 changes: 17 additions & 2 deletions zahner_analysis/file_export/ism_export.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
r"""
____ __ __ __ __ _ __
/_ / ___ _/ / ___ ___ ___________ / /__ / /__/ /_____(_) /__
/ /_/ _ `/ _ \/ _ \/ -_) __/___/ -_) / -_) '_/ __/ __/ / '_/
Expand Down Expand Up @@ -36,7 +36,7 @@ class IsmExport:
With this class only impedances to different frequency points can be stored.
However, the ism files are not complete and can no longer be loaded in Thales, nor do they contain any meta data.
ACQ data is also lost.
ACQ and DC data is also lost.
:param frequency: Array with the frequency data.
:param impedance: Array with the impedance data.
Expand Down Expand Up @@ -80,6 +80,21 @@ def __init__(
self._binaryFileContent += self.tmpPhase.tobytes()
self._binaryFileContent += self.tmpTime.tobytes()
self._binaryFileContent += self.tmpSig.tobytes()

#add empty strings
self._binaryFileContent += int.to_bytes(0,length=2,byteorder="big")
self._binaryFileContent += int.to_bytes(0,length=2,byteorder="big")
self._binaryFileContent += int.to_bytes(0,length=2,byteorder="big")
self._binaryFileContent += int.to_bytes(0,length=2,byteorder="big")
self._binaryFileContent += int.to_bytes(0,length=2,byteorder="big")
self._binaryFileContent += int.to_bytes(0,length=2,byteorder="big")
self._binaryFileContent += int.to_bytes(0,length=2,byteorder="big")
self._binaryFileContent += int.to_bytes(0,length=2,byteorder="big")
self._binaryFileContent += int.to_bytes(0,length=2,byteorder="big")
self._binaryFileContent += int.to_bytes(0,length=2,byteorder="big")
self._binaryFileContent += int.to_bytes(0,length=2,byteorder="big")
self._binaryFileContent += int.to_bytes(0,length=2,byteorder="big")

self._binaryFileContent += metaData
return

Expand Down
2 changes: 1 addition & 1 deletion zahner_analysis/file_import/impedance_model_import.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
r"""
____ __ __ __ __ _ __
/_ / ___ _/ / ___ ___ ___________ / /__ / /__/ /_____(_) /__
/ /_/ _ `/ _ \/ _ \/ -_) __/___/ -_) / -_) '_/ __/ __/ / '_/
Expand Down
2 changes: 1 addition & 1 deletion zahner_analysis/file_import/isc_import.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
r"""
____ __ __ __ __ _ __
/_ / ___ _/ / ___ ___ ___________ / /__ / /__/ /_____(_) /__
/ /_/ _ `/ _ \/ _ \/ -_) __/___/ -_) / -_) '_/ __/ __/ / '_/
Expand Down
74 changes: 40 additions & 34 deletions zahner_analysis/file_import/ism_import.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
r"""
____ __ __ __ __ _ __
/_ / ___ _/ / ___ ___ ___________ / /__ / /__/ /_____(_) /__
/ /_/ _ `/ _ \/ _ \/ -_) __/___/ -_) / -_) '_/ __/ __/ / '_/
Expand Down Expand Up @@ -65,39 +65,45 @@ def __init__(self, filename: Union[str, bytes, bytearray]):
)
self.significance = readI2ArrayFromFile(ismFile, self.numberOfSamples)

self.acqChannels = dict()

self.measurementDate = readZahnerDate(ismFile)

self.system = readZahnerStringFromFile(ismFile)
self.potential = readZahnerStringFromFile(ismFile)
self.current = readZahnerStringFromFile(ismFile)
self.temperature = readZahnerStringFromFile(ismFile)
self.time = readZahnerStringFromFile(ismFile)
self.comment_1 = readZahnerStringFromFile(ismFile)
self.comment_2 = readZahnerStringFromFile(ismFile)
self.comment_3 = readZahnerStringFromFile(ismFile)
self.comment_4 = readZahnerStringFromFile(ismFile)

self.areaForCurrentDensity = readZahnerStringFromFile(ismFile)
serialQuantityStuff = readZahnerStringFromFile(ismFile)
acquisition_flag = readI2FromFile(ismFile)

kValues = readF8ArrayFromFile(ismFile, 32)

k_value_27 = int(kValues[27])

if acquisition_flag > 256 and (k_value_27 & 32768) == 32768:
self.acqChannels["Voltage/V"] = np.ndarray(
shape=(self.numberOfSamples,), dtype=">f8"
)
self.acqChannels["Current/A"] = np.ndarray(
shape=(self.numberOfSamples,), dtype=">f8"
)

for index in range(self.numberOfSamples):
self.acqChannels["Voltage/V"][index] = readF8FromFile(ismFile)
self.acqChannels["Current/A"][index] = readF8FromFile(ismFile)
try:
"""
optional file contents
"""
self.acqChannels = dict()

self.measurementDate = readZahnerDate(ismFile)

self.system = readZahnerStringFromFile(ismFile)
self.potential = readZahnerStringFromFile(ismFile)
self.current = readZahnerStringFromFile(ismFile)
self.temperature = readZahnerStringFromFile(ismFile)
self.time = readZahnerStringFromFile(ismFile)
self.comment_1 = readZahnerStringFromFile(ismFile)
self.comment_2 = readZahnerStringFromFile(ismFile)
self.comment_3 = readZahnerStringFromFile(ismFile)
self.comment_4 = readZahnerStringFromFile(ismFile)

self.areaForCurrentDensity = readZahnerStringFromFile(ismFile)
serialQuantityStuff = readZahnerStringFromFile(ismFile)
acquisition_flag = readI2FromFile(ismFile)

kValues = readF8ArrayFromFile(ismFile, 32)

k_value_27 = int(kValues[27])

if acquisition_flag > 256 and (k_value_27 & 32768) == 32768:
self.acqChannels["Voltage/V"] = np.ndarray(
shape=(self.numberOfSamples,), dtype=">f8"
)
self.acqChannels["Current/A"] = np.ndarray(
shape=(self.numberOfSamples,), dtype=">f8"
)

for index in range(self.numberOfSamples):
self.acqChannels["Voltage/V"][index] = readF8FromFile(ismFile)
self.acqChannels["Current/A"][index] = readF8FromFile(ismFile)
except:
pass

self._metaData = bytearray(ismFile.read())
ismFile.close()
Expand Down
2 changes: 1 addition & 1 deletion zahner_analysis/file_import/iss_import.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
r"""
____ __ __ __ __ _ __
/_ / ___ _/ / ___ ___ ___________ / /__ / /__/ /_____(_) /__
/ /_/ _ `/ _ \/ _ \/ -_) __/___/ -_) / -_) '_/ __/ __/ / '_/
Expand Down
2 changes: 1 addition & 1 deletion zahner_analysis/file_import/seqtxt_import.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
r"""
____ __ __ __ __ _ __
/_ / ___ _/ / ___ ___ ___________ / /__ / /__/ /_____(_) /__
/ /_/ _ `/ _ \/ _ \/ -_) __/___/ -_) / -_) '_/ __/ __/ / '_/
Expand Down
44 changes: 25 additions & 19 deletions zahner_analysis/file_import/thales_file_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
r"""
____ __ __ __ __ _ __
/_ / ___ _/ / ___ ___ ___________ / /__ / /__/ /_____(_) /__
/ /_/ _ `/ _ \/ _ \/ -_) __/___/ -_) / -_) '_/ __/ __/ / '_/
Expand Down Expand Up @@ -36,7 +36,7 @@ def readF8FromFile(file: io.BufferedReader) -> float:


def readF8ArrayFromFile(file: io.BufferedReader, length: int) -> np.ndarray:
return np.ndarray(shape=(length,), dtype=">f8", buffer=file.read(8 * length))
return np.array(np.ndarray(shape=(length,), dtype=">f8", buffer=file.read(8 * length)))


def readI2FromFile(file: io.BufferedReader) -> int:
Expand All @@ -48,7 +48,7 @@ def peekI2FromFile(file: io.BufferedReader) -> int:


def readI2ArrayFromFile(file: io.BufferedReader, length: int) -> np.ndarray:
return np.ndarray(shape=(length,), dtype=">i2", buffer=file.read(2 * length))
return np.array(np.ndarray(shape=(length,), dtype=">i2", buffer=file.read(2 * length)))


def readI6FromFile(file: io.BufferedReader) -> int:
Expand All @@ -75,23 +75,29 @@ def readZahnerStringFromFile(file: io.BufferedReader) -> str:
return content.decode("ASCII").swapcase()


def readZahnerDate(file: io.BufferedReader) -> datetime.datetime:
def readZahnerDate(file: io.BufferedReader) -> datetime.datetime:
dateString = readZahnerStringFromFile(file)
date = dateString[0:6]

day = int(date[0:2])
month = int(date[2:4])
year = int(date[4:6])

"""
Only the last two digits of the date are saved.
It is assumed that the measurement was carried out between 1970 and 2070.
A software update is necessary in the year 2070 at the latest.
"""
if year < 70:
year += 2000
else:
year += 1900
try:
date = dateString[0:6]

day = int(date[0:2])
month = int(date[2:4])
year = int(date[4:6])

"""
Only the last two digits of the date are saved.
It is assumed that the measurement was carried out between 1970 and 2070.
A software update is necessary in the year 2070 at the latest.
"""
if year < 70:
year += 2000
else:
year += 1900
except:
# fallback date
day = 1
month = 1
year = 1970

return datetime.datetime(year, month, day)

Expand Down
8 changes: 4 additions & 4 deletions zahner_analysis/plotting/impedance_plot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
r"""
____ __ __ __ __ _ __
/_ / ___ _/ / ___ ___ ___________ / /__ / /__/ /_____(_) /__
/ /_/ _ `/ _ \/ _ \/ -_) __/___/ -_) / -_) '_/ __/ __/ / '_/
Expand Down Expand Up @@ -138,7 +138,7 @@ def bodePlotter(

impedanceAxis.loglog(frequencies, np.abs(impedanceAbsolute), **argsImpedanceAxis)
impedanceAxis.xaxis.set_major_formatter(EngFormatter(unit="Hz"))
impedanceAxis.yaxis.set_major_formatter(EngFormatter(unit="$\Omega$"))
impedanceAxis.yaxis.set_major_formatter(EngFormatter(unit=r"$\Omega$"))
impedanceAxis.set_xlabel(r"f")
impedanceAxis.set_ylabel(r"|Z|")
if zTogetherPhase:
Expand Down Expand Up @@ -279,8 +279,8 @@ def nyquistPlotter(
nyquistAxis.plot(np.real(Z), np.imag(Z), **argsNyquistAxis)
nyquistAxis.grid(which="both", linestyle="dashed", linewidth=0.5)
nyquistAxis.set_aspect("equal")
nyquistAxis.xaxis.set_major_formatter(EngFormatter(unit="$\Omega$"))
nyquistAxis.yaxis.set_major_formatter(EngFormatter(unit="$\Omega$"))
nyquistAxis.xaxis.set_major_formatter(EngFormatter(unit=r"$\Omega$"))
nyquistAxis.yaxis.set_major_formatter(EngFormatter(unit=r"$\Omega$"))
nyquistAxis.set_xlabel(r"$Z_{\rm re}$")

if minusNyquist:
Expand Down

0 comments on commit eafd201

Please sign in to comment.