Python SNSData Class for Processing and Analyzing CSV Data
class SNSData:
def __init__(self, pandas_dir=""):
if os.path.isfile(pandas_dir):
self.path = pandas_dir
else:
raise FileNotFoundError('File not found:' + pandas_dir)
self.raw_data = pd.read_csv(self.path, encoding="utf-8")
self.regular_data = {'name': [], 'magnitude': [], 'phase': [], 'frequency': []}
self.freq_list = [1, 2, 3, 7, 11, 17, 23, 31, 43, 61, 89, 127, 179, 251, 349]
for it in self.freq_list:
self.regular_data['name'].extend(
self.raw_data['name'] + self.raw_data['name'].isin(['saline']) * self.raw_data[
'property']) # 盐水考虑浓度属性,脑脊液不考虑, 在新数据中命名为盐水+浓度
self.regular_data['magnitude'].extend(self.raw_data[str(it) + 'kHz_mag'])
self.regular_data['phase'].extend(self.raw_data[str(it) + 'kHz_ang'])
for i in range(self.raw_data.shape[0]):
self.regular_data['frequency'].append(it)
self.regular_data['logFreq'] = np.log10(self.regular_data['frequency']) # 将频率以对数形式展现
self.regular_data['R'] = np.array(self.regular_data['magnitude']) * np.cos(
np.array(self.regular_data['phase']) * 3.14 / 180) # np.cos默认弧度
self.regular_data['X'] = np.array(self.regular_data['magnitude']) * np.sin(
np.array(self.regular_data['phase']) * 3.14 / 180)
self.regular_data['logMag'] = np.log10(self.regular_data['magnitude']) # 在bode图中,幅度也是用对数表示的
'下面利用已知浓度盐水的R和电导率(conductivity)映射关系来得到待测物体的电导率'
# 创建电导率空列表保持行数一致
self.regular_data['conductivity'] = [0] * self.raw_data.shape[0] * len(self.freq_list)
self.df = pd.DataFrame(self.regular_data)
self.generate_conductivity()
'重新整理完数据后,整合至pandas数据帧,用get_df方法可以返回该pandas数据帧'
def generate_conductivity(self):
"""
映射关系如下
利用给出的盐水和电导率表自己先写个映射关系
"""
conductivity_list = [976, 1987, 3850, 5650, 7450, 9238] # 该电导率列表对应浓度为0.1,0.5的盐水
concentration_list = [0.05, 0.10, 0.2, 0.3, 0.4, 0.50]
saline_real_dict = {} # 每个频率,每个浓度的盐水的实部,其中同浓度对应同一个电导率
# 在这里,首先过滤出同一个频率下的所有盐水的real part,然后对同一个浓度的realpart求平均值,这里有七个浓度,将七个平均值打包为一个列表,作为子列表添加入saline_real_list
for it in self.freq_list: # 对每一个频率
one_freq_saline_real_list = [] # 打包用的子列表
for con in concentration_list:
# 这里依次为0.05, 0.1, 0.2, 0., 0.4, 0.6的盐水的real part平均值
saline_concentration = 'saline' + format(con, ".2f")
filted_df = self.df[
self.df['frequency'].isin([it]) * self.df['name'].isin([saline_concentration])]
avg = np.mean(filted_df['R']) # 求筛选出来的下R的平均值
one_freq_saline_real_list.append(avg)
saline_real_dict[it] = one_freq_saline_real_list # 每一个频率下都有了一个浓度对应的real part列表
# real part和电导率建立起映射关系,对于单个频率,one_freq_saline_real_list作为x轴,conductivity作为y轴,用np.interp方法, 寻找R对应的电导率
self.df['conductivity'] += self.df['frequency'].isin([it]) * np.interp(self.df['R'],
one_freq_saline_real_list[::-1],
conductivity_list[
::-1]) # interp要求x轴为递增序列,因此此处倒序输入
def get_df(self):
return self.df
print(os.getcwd())
To add a new column named 'property' to the snsdata.get_df() data, you need to modify the code within the __init__ method of the SNSData class. The modification involves adding a line after the following line of code:
self.regular_data['property'] = self.raw_data['property']
The specific line number will depend on where you want to insert the new column.
You then need to add another line before the return statement at the end of the __init__ method to ensure the newly added 'property' column is included in the DataFrame returned by the get_df method:
return self.df
Here's the updated code snippet with the modifications:
class SNSData:
def __init__(self, pandas_dir=""):
if os.path.isfile(pandas_dir):
self.path = pandas_dir
else:
raise FileNotFoundError('File not found:' + pandas_dir)
self.raw_data = pd.read_csv(self.path, encoding="utf-8")
self.regular_data = {'name': [], 'magnitude': [], 'phase': [], 'frequency': []}
self.freq_list = [1, 2, 3, 7, 11, 17, 23, 31, 43, 61, 89, 127, 179, 251, 349]
for it in self.freq_list:
self.regular_data['name'].extend(
self.raw_data['name'] + self.raw_data['name'].isin(['saline']) * self.raw_data[
'property']) # 盐水考虑浓度属性,脑脊液不考虑, 在新数据中命名为盐水+浓度
self.regular_data['magnitude'].extend(self.raw_data[str(it) + 'kHz_mag'])
self.regular_data['phase'].extend(self.raw_data[str(it) + 'kHz_ang'])
for i in range(self.raw_data.shape[0]):
self.regular_data['frequency'].append(it)
self.regular_data['logFreq'] = np.log10(self.regular_data['frequency']) # 将频率以对数形式展现
self.regular_data['R'] = np.array(self.regular_data['magnitude']) * np.cos(
np.array(self.regular_data['phase']) * 3.14 / 180) # np.cos默认弧度
self.regular_data['X'] = np.array(self.regular_data['magnitude']) * np.sin(
np.array(self.regular_data['phase']) * 3.14 / 180)
self.regular_data['logMag'] = np.log10(self.regular_data['magnitude']) # 在bode图中,幅度也是用对数表示的
'下面利用已知浓度盐水的R和电导率(conductivity)映射关系来得到待测物体的电导率'
# 创建电导率空列表保持行数一致
self.regular_data['conductivity'] = [0] * self.raw_data.shape[0] * len(self.freq_list)
self.df = pd.DataFrame(self.regular_data)
self.generate_conductivity()
'重新整理完数据后,整合至pandas数据帧,用get_df方法可以返回该pandas数据帧'
# Add the new 'property' column to the DataFrame
self.regular_data['property'] = self.raw_data['property']
self.df = pd.DataFrame(self.regular_data)
return self.df
def generate_conductivity(self):
"""
映射关系如下
利用给出的盐水和电导率表自己先写个映射关系
"""
conductivity_list = [976, 1987, 3850, 5650, 7450, 9238] # 该电导率列表对应浓度为0.1,0.5的盐水
concentration_list = [0.05, 0.10, 0.2, 0.3, 0.4, 0.50]
saline_real_dict = {} # 每个频率,每个浓度的盐水的实部,其中同浓度对应同一个电导率
# 在这里,首先过滤出同一个频率下的所有盐水的real part,然后对同一个浓度的realpart求平均值,这里有七个浓度,将七个平均值打包为一个列表,作为子列表添加入saline_real_list
for it in self.freq_list: # 对每一个频率
one_freq_saline_real_list = [] # 打包用的子列表
for con in concentration_list:
# 这里依次为0.05, 0.1, 0.2, 0., 0.4, 0.6的盐水的real part平均值
saline_concentration = 'saline' + format(con, ".2f")
filted_df = self.df[
self.df['frequency'].isin([it]) * self.df['name'].isin([saline_concentration])]
avg = np.mean(filted_df['R']) # 求筛选出来的下R的平均值
one_freq_saline_real_list.append(avg)
saline_real_dict[it] = one_freq_saline_real_list # 每一个频率下都有了一个浓度对应的real part列表
# real part和电导率建立起映射关系,对于单个频率,one_freq_saline_real_list作为x轴,conductivity作为y轴,用np.interp方法, 寻找R对应的电导率
self.df['conductivity'] += self.df['frequency'].isin([it]) * np.interp(self.df['R'],
one_freq_saline_real_list[::-1],
conductivity_list[
::-1]) # interp要求x轴为递增序列,因此此处倒序输入
def get_df(self):
return self.df
print(os.getcwd())
原文地址: https://www.cveoy.top/t/topic/jt49 著作权归作者所有。请勿转载和采集!