Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Load permeability data in the mdb."""

import logging

from pathlib import Path
Expand All @@ -13,24 +14,27 @@
logger = logging.getLogger(__name__)

# configure logging to show terminal output
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)


# - - - - - - - - - - - - - - - -
# Define paths and load probe data
# - - - - - - - - - - - - - - - -


def create_paths_to_LEA_MTB_measurements_from_config():
"""
Create paths to permeability measurements from config file.

:return: path2processed_measurement_data and path2probe_database
"""
# Path to the directory, where the measurement data from the LEA Magnetics Testbench are located
path2measurement_data = Path(mdb.get_user_paths().external_material_data).joinpath("LEA_MTB")
path2measurement_data = Path(mdb.get_user_paths().external_material_data).joinpath(
"LEA_MTB"
)

# Path to the directory of the processed measurement data
path2processed_measurement_data = path2measurement_data.joinpath("permeability_temporary").joinpath("processed")
path2processed_measurement_data = path2measurement_data.joinpath("permeability")

# Path to the file, where all probes' data is stored centrally
path2probe_database = path2measurement_data.joinpath("probe_database.csv")
Expand All @@ -39,22 +43,28 @@ def create_paths_to_LEA_MTB_measurements_from_config():


# Paths to the measurement data of the LEA Magnetics testbench
path2processed_measurement_data, path2probe_database = create_paths_to_LEA_MTB_measurements_from_config()
path2processed_measurement_data, path2probe_database = (
create_paths_to_LEA_MTB_measurements_from_config()
)

# Read in the probe database as a pd.DataFrame
df_probe_database = pd.read_csv(path2probe_database)
print(df_probe_database)

# Path to the directory, where the data shall be stored inside the materialdatabase
path2mdb_data = Path(mdb.get_user_paths().material_data).joinpath("complex_permeability").joinpath("LEA_MTB")
path2mdb_data = (
Path(mdb.get_user_paths().material_data)
.joinpath("complex_permeability")
.joinpath("LEA_MTB")
)


# - - - - - - - - - - - - - - - -
# Iterate over materials:
# - - - - - - - - - - - - - - - -

# Name of the ferrite material to be processed
material_name = Material._3F46.value
material_name = Material.N49.value

# Path to the directory, where the processed measurement data of the specified material is located
path2processed_material_data = path2processed_measurement_data.joinpath(material_name)
Expand All @@ -65,29 +75,40 @@ def create_paths_to_LEA_MTB_measurements_from_config():

else:
# Find the directories inside the folder where the material data is stored
directories_in_path2processed_data = [f for f in listdir(path2processed_material_data) if isdir(path2processed_material_data.joinpath(str(f)))]
logger.info(f"Found {len(directories_in_path2processed_data)} directories (probe names) in {path2processed_material_data} : "
f"{directories_in_path2processed_data}")
directories_in_path2processed_data = [
f
for f in listdir(path2processed_material_data)
if isdir(path2processed_material_data.joinpath(str(f)))
]
logger.info(
f"Found {len(directories_in_path2processed_data)} directories (probe names) in {path2processed_material_data} : "
f"{directories_in_path2processed_data}"
)

# initialize an empty dataframe
df_material = pd.DataFrame()

# Iterate over probes
for directory in directories_in_path2processed_data:

# Check whether the directory name matches any entry in the probe_database.csv
if directory in df_probe_database["probe_code"].values:
probe_code = directory
else:
logger.warning(f"The directory name {directory} does not match any probe name in the probe_database.csv \n"
f"-> ({path2probe_database})")
logger.warning(
f"The directory name {directory} does not match any probe name in the probe_database.csv \n"
f"-> ({path2probe_database})"
)
break

# create path to the directory of the probe's measurement data
path2processed_probe_data = path2processed_material_data.joinpath(probe_code)

# Get all files in given directory.
files_in_path2processed_probe_data = [f for f in listdir(path2processed_probe_data) if isfile(path2processed_probe_data.joinpath(str(f)))]
files_in_path2processed_probe_data = [
f
for f in listdir(path2processed_probe_data)
if isfile(path2processed_probe_data.joinpath(str(f)))
]

# If only a single file is in the specified path, assume that it contains the correct measurement:
if len(files_in_path2processed_probe_data) != 1:
Expand All @@ -97,7 +118,12 @@ def create_paths_to_LEA_MTB_measurements_from_config():
logger.warning("Too many files in path", path2processed_probe_data)
else:
# read in the measurement data
df_probe = pd.read_csv(path2processed_probe_data.joinpath(str(files_in_path2processed_probe_data[0])), encoding="latin1")
df_probe = pd.read_csv(
path2processed_probe_data.joinpath(
str(files_in_path2processed_probe_data[0])
),
encoding="latin1",
)

# add probe column (since it's not in the CSV)
df_probe["probe"] = probe_code
Expand All @@ -108,11 +134,15 @@ def create_paths_to_LEA_MTB_measurements_from_config():
# append to material dataframe
df_material = pd.concat([df_material, df_probe], ignore_index=True)

logger.info(f"Write permeability data of material {material_name} measured on probe {probe_code} into the materialdatabase.")
logger.info(
f"Write permeability data of material {material_name} measured on probe {probe_code} into the materialdatabase."
)

# Check if reading the data was successful
if df_material.empty:
logger.warning('DataFrame is empty!')
logger.warning("DataFrame is empty!")
else:
# Store processed data in csv file
df_material.to_csv(path2mdb_data.joinpath(f"{material_name}.csv"), index=False)
df_material.to_csv(
path2mdb_data.joinpath(f"{material_name}.csv"), index=False
)
Loading
Loading