Added function for conc_df generation
This commit is contained in:
95
fsl_mrs_mce/mc_in.py
Normal file
95
fsl_mrs_mce/mc_in.py
Normal file
@@ -0,0 +1,95 @@
|
||||
# mc_sim.py - Helpers for Monte-Carlo style simulations
|
||||
#
|
||||
# Author: Konstantin E Bosbach <konstantin.bosbach@mars.uni-freiburg.de>
|
||||
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from configparser import ConfigParser
|
||||
|
||||
|
||||
def load_config(input_folder="input/", section="default"):
|
||||
"""Loads configuration file.
|
||||
If given option section="": returns list of config sections"""
|
||||
# Information from config txt
|
||||
try:
|
||||
# Load txt
|
||||
configFilePath = str(input_folder + "config.txt")
|
||||
configParser = ConfigParser()
|
||||
configParser.read(configFilePath)
|
||||
try:
|
||||
if section == "":
|
||||
return configParser.sections()
|
||||
else:
|
||||
return configParser[section]
|
||||
except input:
|
||||
print(configParser.sections())
|
||||
print(f"No {section} section.")
|
||||
except input:
|
||||
print(f"No {configFilePath} provided.")
|
||||
|
||||
|
||||
def conc_df_from_file(input_path="input/", foundation="vivo_average"):
|
||||
sections = load_config(input_path, "")
|
||||
"""Generates concentration dataframe for mc method.
|
||||
Supports different variation types (absolute/delta/relative)
|
||||
Returns data_frame of different concentrations"""
|
||||
# Load all relevant sections
|
||||
list_parser_parameter = []
|
||||
|
||||
for i in sections:
|
||||
if i[0:20] == "generation_parameter":
|
||||
list_parser_parameter.append(load_config(input_path, i))
|
||||
|
||||
# Load foundation file, a line with e.g. in-vivo averages
|
||||
# The code expects a one-line-foundation.
|
||||
if foundation == "vivo_average":
|
||||
try:
|
||||
df_foundation = pd.read_csv(
|
||||
"basis/fit_conc_result.csv").mean().to_frame().transpose()
|
||||
except:
|
||||
print("No basis/fit_conc_result.csv file supplied")
|
||||
else:
|
||||
print("Only foundation mode vivo_average not chosen")
|
||||
|
||||
# Load values, dependent on type absolute/delta/relative
|
||||
list_parameter_values = []
|
||||
list_parameter_names = []
|
||||
for i in list_parser_parameter:
|
||||
string_values = i["amount"][1:-1]
|
||||
list_values = list(map(float, string_values.split(",")))
|
||||
if i["type"] == "absolute":
|
||||
# set absolute values
|
||||
None
|
||||
|
||||
elif i["type"] == "delta":
|
||||
# set absolute value offset from foundation
|
||||
list_values = np.add(list_values, df_foundation[i["param"]].mean())
|
||||
|
||||
elif i["type"] == "relative":
|
||||
# set relative value from foundation
|
||||
list_values = np.multiply(
|
||||
list_values, df_foundation[i["param"]].mean())
|
||||
|
||||
list_parameter_values.append(list_values)
|
||||
list_parameter_names.append(i["param"])
|
||||
|
||||
# Create parameter tensor
|
||||
mesh_parameter_values = np.meshgrid(*list_parameter_values)
|
||||
|
||||
# Create 'empty' standard data
|
||||
df_conc = pd.concat(
|
||||
[df_foundation]*int(np.array(mesh_parameter_values[0]).size),
|
||||
ignore_index=True
|
||||
)
|
||||
|
||||
# Write parameter values into dataframe
|
||||
i = 0
|
||||
while i <= len(list_parameter_values)-1:
|
||||
parameter_values = mesh_parameter_values[i].ravel()
|
||||
parameter_name = list_parameter_names[i]
|
||||
print(parameter_name, parameter_values)
|
||||
df_conc[parameter_name] = parameter_values
|
||||
i = i+1
|
||||
|
||||
return df_conc
|
||||
Reference in New Issue
Block a user