python 调用 R语言实现认知网络(ENA)数据处理

# -*- coding: UTF-8 -*-
# https://rpy2.github.io/doc/latest/html/vector.html#rpy2.robjects.vectors.DataFrame.iter_row
import time

import pandas as pd
import rpy2.robjects as robjects
from rpy2.robjects import r, pandas2ri, Formula
from rpy2.robjects.packages import importr
import rpy2.robjects as ro
from rpy2.robjects.conversion import localconverter
rENA = importr('rENA')

rsdata = pd.read_csv("rsdata.csv")

pandas2ri.activate()  # makes some conversions automatic

pr = ro.r("print")
colMeans = ro.r("colMeans")
rescale = ro.r("scales::rescale")
rmax = ro.r("max")

# load your file

units = rsdata[['Condition','UserName']]
conversation = rsdata[['Condition','GroupName']]
codes = rsdata[['Data','Technical.Constraints','Performance.Parameters','Client.and.Consultant.Requests','Design.Reasoning','Collaboration']]
meta = rsdata[["CONFIDENCE.Change","CONFIDENCE.Pre","CONFIDENCE.Post","C.Change"]]

accum = rENA.ena_accumulate_data(units, conversation, codes, meta)

set = rENA.ena_make_set(
  enadata=accum
)

points = (set.rx2('points'))
# print(points)
# print(type(points))  # 输出类型
# print(points.colnames) # 输出列名
# print(points.rownames)
# print(len(points)) #  有多少列
# print(points[0]) #  取第1列
# print(points.ncol)
# print(points.nrow)
# print(points[1]) #  取第1列
# print(points[1] == 'FirstGame') #  取第1列
# points.iter_column()
df_points1 = robjects.DataFrame({})
df_points2 = robjects.DataFrame({})
for i in points.iter_row(): # iter_column  iter_row
  if(i[1] == 'FirstGame'):
    df_points1 = i
  else:
    print(2222)

# plot_first = rENA.ena_plot(set, title ="FirstGame")
# # plot_first = rENA.ena_plot_network(plot_first, network = first_game_mean)
# pr(plot_first)
# 使用R的索引语法来筛选行
# df_points1 = robjects.r[points][points.rx2('Condition') == 'FirstGame', ]
df_points1 = points.loc[points['Condition'] == "FirstGame"]
df_points2 = points.loc[points['Condition'] == "SecondGame"]
# 假设 points 是原始 R DataFrame
# df_points1 = robjects.r[points][points.rx2('Condition') == 'FirstGame', ]
# df_points2 = robjects.r['points'][robjects.r['points']['Condition'] == 'SecondGame', ]
### Subset rotated points for the first condition
# 删除前面非编码的数据,只留下编码数据 SVD1-SVD15
first_game_points = df_points1.drop(columns=['ENA_UNIT','Condition','UserName', 'CONFIDENCE.Change', 'CONFIDENCE.Pre', 'CONFIDENCE.Post', "C.Change"])
### Subset rotated points for the second condition
second_game_points = df_points2.drop(columns=['ENA_UNIT','Condition','UserName',  'CONFIDENCE.Change', 'CONFIDENCE.Pre', 'CONFIDENCE.Post', "C.Change"])




plot = rENA.ena_plot(set, scale_to ="network", title ="Groups of Units")
plot = rENA.ena_plot_points(plot, points = first_game_points, confidence_interval ="box", colors = ("blue"))
plot = rENA.ena_plot_points(plot, points = second_game_points, confidence_interval ="box", colors = ("red"))
pr(plot)



plot = rENA.ena_plot(set, scale_to = [-1, 0, 1], title ="Groups and Means")
plot = rENA.ena_plot_points(plot, points = first_game_points, confidence_interval ="box", colors = ("blue"))
plot = rENA.ena_plot_points(plot, points = second_game_points, confidence_interval ="box", colors = ("red"))
plot = rENA.ena_plot_group(plot, first_game_points, colors = ("red"), confidence_interval ="box")
plot = rENA.ena_plot_group(plot, second_game_points, colors =("blue"), confidence_interval ="box")
pr(plot)

line_weights = (set.rx2('line.weights'))

### Subset lineweights for SecondGame and Calculate the colMeans
first_game_lineweights = line_weights.loc[points['Condition'] == "FirstGame"]
second_game_lineweights = line_weights.loc[points['Condition'] == "SecondGame"]

first_game_lineweights = first_game_lineweights.drop(columns=['ENA_UNIT','Condition','UserName', 'CONFIDENCE.Change', 'CONFIDENCE.Pre', 'CONFIDENCE.Post', "C.Change"])
### Subset rotated points for the second condition
second_game_lineweights = second_game_lineweights.drop(columns=['ENA_UNIT','Condition','UserName',  'CONFIDENCE.Change', 'CONFIDENCE.Pre', 'CONFIDENCE.Post', "C.Change"])



first_game_mean = colMeans(first_game_lineweights)
second_game_mean = colMeans(second_game_lineweights)

### Subtract the two sets of means, resulting in a vector with negative values
### indicatinag a stronger connection with the SecondGame, and positive values
### a stronger FirstGame connection
subtracted_mean = first_game_mean - second_game_mean

#> [1]  0.0769062510 -0.0409819566  0.0204737523  0.0008708646  0.0373163091
#Plot subtracted network only
plot_first = rENA.ena_plot(set, title ="FirstGame")
plot_first = rENA.ena_plot_network(plot_first, network = first_game_mean)
pr(plot_first)


plot_second = rENA.ena_plot(set, title ="SecondGame")
plot_second = rENA.ena_plot_network(plot_second, network = second_game_mean, colors = ("blue"))
pr(plot_second)




plot_sub = rENA.ena_plot(set, title ="Subtracted")
plot_sub = rENA.ena_plot_network(plot_sub, network = subtracted_mean)
pr(plot_sub)


rotation = (set.rx2('rotation'))
nodes = rotation.rx2('nodes')

# Scale the nodes to match that of the network, for better viewing
first_game_points_max = rmax(first_game_points)
second_game_points_max = rmax(second_game_points)
if(first_game_points_max> second_game_points_max):
  point_max = first_game_points_max
else:
  point_max = second_game_points_max

nodes = nodes.drop(columns=['code'])
max_nodes = rmax(nodes)

with localconverter(ro.default_converter + pandas2ri.converter):
  first_game_points = ro.conversion.py2rpy(first_game_points)
print(type(first_game_points))

first_game_scaled = first_game_points
second_game_scaled = second_game_points

plot = rENA.ena_plot(set, title ="Plot with Units and Network", font_family ="Times")
plot = rENA.ena_plot_points(plot, points = first_game_scaled, colors = ("red"))
plot = rENA.ena_plot_points(plot, points = second_game_scaled, colors = ("blue"))
plot = rENA.ena_plot_group(plot, point = first_game_scaled, colors =("red"), confidence_interval ="box")
plot = rENA.ena_plot_group(plot, point = second_game_scaled, colors =("blue"), confidence_interval ="box")
plot = rENA.ena_plot_network(plot, network = subtracted_mean)
pr(plot)

time.sleep(10)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值