Skip to content

Commit

Permalink
deleted pssession file because it was very big before pull request
Browse files Browse the repository at this point in the history
[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
  • Loading branch information
gahorwitz authored and BenjaminCharmes committed Jul 29, 2024
1 parent c3cc272 commit 97312b2
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 58 deletions.
138 changes: 80 additions & 58 deletions pydatalab/pydatalab/apps/PSTrace/PalmSensReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"""



import pandas as pd
import os.path, glob
import csv
import re
from io import StringIO
import numpy as np

filename = (
"PalmSense_test_datalab.csv" # file with experimetnal data as exported "as csv" from PSTrace
)

<<<<<<< HEAD
# The following lines adjust the granularity of reporting.
Expand All @@ -20,6 +20,7 @@
#pd.options.display.float_format = "{:.1f}".format
<<<<<<< HEAD

<<<<<<< HEAD

<<<<<<< HEAD
<<<<<<< HEAD
Expand Down Expand Up @@ -64,6 +65,9 @@ def getdata(filename, file_encoding='utf-16 LE', verbose = False):

def getdata(filename, file_encoding='utf-16 LE', verbose = False ):
>>>>>>> 2d1266c (Formated EIS part of the output files)
=======
def getdata(filename, file_encoding="utf-16 LE", verbose=False):
>>>>>>> 2542e1a ([pre-commit.ci] auto fixes from pre-commit.com hooks)
"""
Loads experimental data from a CSV file, splits the DataFrame based on a specified keyword,
and returns a dictionary containing the resulting DataFrames.
Expand All @@ -77,9 +81,8 @@ def getdata(filename, file_encoding='utf-16 LE', verbose = False ):
it is normally any type of measurement in the first df, and EIS in the following ones
"""


# Open the file to get the number of columns in each line
with open(filename, 'r', encoding=file_encoding) as temp_f:
with open(filename, "r", encoding=file_encoding) as temp_f:
# Get the number of columns in each line
col_count = [len(l.split(",")) for l in temp_f.readlines()]

Expand All @@ -92,15 +95,14 @@ def getdata(filename, file_encoding='utf-16 LE', verbose = False ):

# Find the locations of the keyword "Measurement" in any column. The file onlyhas that when an EIS mesurmment starts
mask = df.apply(lambda row: row.astype(str).str.contains("Measurement"), axis=1)
mask['Any'] = mask.any(axis=1)
mask["Any"] = mask.any(axis=1)
groups = mask["Any"].cumsum()

# Split the DataFrame based on the keyword occurrences and drop columns with all NaN values
split_dfs = {group: df[group == groups].dropna(axis=1, how='all')
for group in groups.unique()}

split_dfs = {group: df[group == groups].dropna(axis=1, how="all") for group in groups.unique()}

# Display the split DataFrames if verbose=True, default value is False
if verbose:
if verbose:
for key, split_df in split_dfs.items():
print(f"DataFrame for splitting keyword = Measurement occurrence {key}:")
print(split_df)
Expand All @@ -109,6 +111,7 @@ def getdata(filename, file_encoding='utf-16 LE', verbose = False ):
return split_dfs

<<<<<<< HEAD
<<<<<<< HEAD

<<<<<<< HEAD
<<<<<<< HEAD
Expand Down Expand Up @@ -200,19 +203,23 @@ def formatdata(split_dfs):
=======
>>>>>>> 4fa42bc (code reads all parts of csv output file from PSTrace)
=======
=======

>>>>>>> 2542e1a ([pre-commit.ci] auto fixes from pre-commit.com hooks)
def find_row(df, keyword):
"""This function finds the index of a row containiing a keyword
Args:
Args:
- df : DataFrame where we are searching
- keyword (str): the keyword it searches"""

return df[df.apply(lambda row: row.astype(str).str.contains(keyword)).any(axis=1)].index[0]
>>>>>>> 3ca1a83 (cleaned up a bit)


def format_impedance_data(split_dfs):
"""
Formats the data extracted from split_dfs, filtering the data related to Impedance measurements.
Args:
- split_dfs (dict): A dictionary containing values as dataframes for different measurements,
that are the result of the function getdata(filename)
Expand All @@ -221,50 +228,57 @@ def format_impedance_data(split_dfs):
- impedance_dfs (dict): A dictionary storing dataframes related to Impedance measurements,
indexed by their respective keys, each containing the measurement name, date, and the actual data.
"""

# Initialize a list to store keys of dataframes with 'freq / Hz'
dfs_with_freq = []

# Create a dictionary to hold dataframes with impedance measurements
impedance_dfs = {}

# Check if 'freq / Hz' exists in any dataframe, and append the key to a list
for key, df in split_dfs.items():

if df.apply(lambda row: row.astype(str).str.contains('freq / Hz')).any().any():
if df.apply(lambda row: row.astype(str).str.contains("freq / Hz")).any().any():
# Reset the index of the dataframe
df = split_dfs[key].reset_index(drop=True)
df = split_dfs[key].reset_index(drop=True)
dfs_with_freq.append(key)

# Find the row index that contains 'Measurement' to find the name to use
name_row = find_row(df, "Measurement")
new_name = df.iloc[name_row][1]

# Find the row index that contains 'Date and time' for the date and time
date_row = find_row(df, 'Date and time')
date_row = find_row(df, "Date and time")
date_time = df.iloc[date_row][1]
# Find the index of the row containing the string 'freq / Hz' to use as df header
freq_row = find_row(df, 'freq / Hz' )

# Find the index of the row containing the string 'freq / Hz' to use as df header
freq_row = find_row(df, "freq / Hz")
df.columns = df.iloc[freq_row]

# Remove the row that contains 'freq / Hz' and rows before it
df = df.drop(freq_row).drop(index=range(0, freq_row))

# Store the extracted information and data in the impedance_dfs dictionary
impedance_dfs[f"EIS measurement {key}"] = {"Name" : new_name, "Date and Time": date_time, "Data": df}

impedance_dfs[f"EIS measurement {key}"] = {
"Name": new_name,
"Date and Time": date_time,
"Data": df,
}

# Check if there are dataframes with 'freq / Hz'
if dfs_with_freq:
n = len(dfs_with_freq)
print(f"There are {n} Impedance measurements")
else:
print("The are no Impedance measurements")
<<<<<<< HEAD

<<<<<<< HEAD
>>>>>>> 2d1266c (Formated EIS part of the output files)

=======
=======

>>>>>>> 2542e1a ([pre-commit.ci] auto fixes from pre-commit.com hooks)
return impedance_dfs
<<<<<<< HEAD

Expand All @@ -276,56 +290,62 @@ def format_impedance_data(split_dfs):
def format_DC_data(split_dfs):
"""
Extracts and formats DC (direct current) measurement data from a collection of DataFrames.
Args:
split_dfs (dict): A dictionary containing DataFrames to process,
split_dfs (dict): A dictionary containing DataFrames to process,
comes from the funtion getdata(filename)
Returns:
dict: Dictionary of formatted DC measurement data.
"""

dfs_DC_meas = []
DC_data = False
# Process each DataFrame in the input dictionary
for key, df in split_dfs.items():
# Check for the presence of 'Date and time measurement:', only present in dataframes of DC measurements
if df.apply(lambda row: row.astype(str).str.contains('Date and time measurement:')).any().any():
if (
df.apply(lambda row: row.astype(str).str.contains("Date and time measurement:"))
.any()
.any()
):
DC_data = True
# Reset the index of the dataframe
df = split_dfs[key].reset_index(drop=True)


# Find and remove the row index containing 'File date:' because it belongs to EIS measurements
# it is an artifact of how the different dataframes were split by getdata(filename)
row_filedate = find_row(df, 'File date:')
row_filedate = find_row(df, "File date:")
df = df.drop(df.index[row_filedate])

# Create a dictionary of DataFrames with two columns each
# Create a dictionary of DataFrames with two columns each
# (each DC measurements only consists on 2 columns that can change in the magnitude measured
# possible magnitudes: time(s), Voltage (V), Currrent (microA)
DC_dfs = {f"DC measurement {int(i/2)}": df.iloc[:, i:i+2] for i in range(0, df.shape[1], 2)}
DC_dfs = {
f"DC measurement {int(i/2)}": df.iloc[:, i : i + 2]
for i in range(0, df.shape[1], 2)
}
if DC_data:
# Select the first DataFrame 'DC measurement 0' as example to find rows
df = DC_dfs["DC measurement 0"]

# Find the row index for date/time of measurement, name of measurement and units
date_row = find_row(df, 'Date and time measurement:')
# Find the row index for date/time of measurement, name of measurement and units
date_row = find_row(df, "Date and time measurement:")
name_row = date_row - 1
units_row = date_row + 1

# Process each DataFrame in the generated dictionary
for key, df in DC_dfs.items():
dfs_DC_meas.append(key)
# Extract date and time information
date_time = df.iloc[date_row, 1]
new_name = df.iloc[name_row, 0].split(":")[0]

# Set column headers as the units row
df.columns = df.iloc[units_row]
df = df.drop(units_row).drop(index=range(0, units_row))
df.dropna(how='all', inplace=True)
df.dropna(how="all", inplace=True)

# Store the extracted information and data in the 'DC_dfs' dictionary
DC_dfs[key] = {"Name": new_name, "Date and Time": date_time, "Data": df}
new_key = f"DC measurement ({key})"
Expand All @@ -337,6 +357,7 @@ def format_DC_data(split_dfs):
print("There are no direct current measurements")

<<<<<<< HEAD
<<<<<<< HEAD



Expand All @@ -347,21 +368,22 @@ def format_DC_data(split_dfs):
>>>>>>> 3ca1a83 (cleaned up a bit)
def main():
""" Main program """
=======
>>>>>>> 2542e1a ([pre-commit.ci] auto fixes from pre-commit.com hooks)

def main():
"""Main program"""

split_dfs = getdata(filename, verbose=False)

split_dfs = getdata(filename, verbose = False)

eis_data = format_impedance_data(split_dfs)

DC_data = format_DC_data(split_dfs)

print(eis_data)
print (DC_data)


if __name__ == "__main__":



print(eis_data)
print(DC_data)

#Call main program
main()

if __name__ == "__main__":
# Call main program
main()
Binary file modified pydatalab/pydatalab/apps/PSTrace/PalmSense_test_datalab.csv
Binary file not shown.
Binary file modified pydatalab/pydatalab/apps/PSTrace/PalmSense_test_datalab2.csv
Binary file not shown.
Binary file modified pydatalab/pydatalab/apps/PSTrace/PalmSense_test_datalab_EIS.csv
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified pydatalab/pydatalab/apps/PSTrace/test_EIS.csv
Binary file not shown.
Binary file modified pydatalab/pydatalab/apps/PSTrace/test_chronopot.csv
Binary file not shown.
Binary file not shown.

0 comments on commit 97312b2

Please sign in to comment.