Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 18.21 KB | None | 0 0
  1. from openpyxl import load_workbook
  2. import matplotlib.pyplot as plt
  3. # Библиотека для работы с excel файлами
  4. import os
  5. import numpy as np
  6. class Answer:
  7.     def __init__(self, string):
  8.         self.name = string
  9.         self.valence = [0 for x in range(11)]
  10.         self.arousal = [0 for x in range(11)]
  11.         self.dominance = [0 for x in range(11)]
  12.         self.status = 0  # 0 not set,  1 good,  2 evil,  3 neutral
  13.  
  14.     def set_g(self, value):
  15.         self.valence[value] += 1
  16.  
  17.     def set_a(self, value):
  18.         self.arousal[value] += 1
  19.  
  20.     def set_s(self, value):
  21.         self.dominance[value] += 1
  22.  
  23.     def set_status(self, value):
  24.         self.status = value
  25.  
  26.     def printall(self):
  27.         print(self.name, self.status, self.valence, self.arousal, self.dominance)
  28.  
  29.     def get_g(self):
  30.         return self.valence
  31.  
  32.     def get_a(self):
  33.         return self.arousal
  34.  
  35.     def get_s(self):
  36.         return self.dominance
  37.  
  38.     def get_name(self):
  39.         return self.name
  40.  
  41.     def get_status(self):
  42.         if self.status == 1:
  43.             return 'good'
  44.         elif self.status == 2:
  45.             return 'evil'
  46.         elif self.status == 3:
  47.             return 'neutral'
  48.         elif self.status == 55:
  49.             return 'disgusting'
  50. def cumulativeLHist(hist1, i):
  51.     return sum(hist1[:i])
  52. def cumulativeRHist(hist1, i):
  53.     return sum(hist1[i:])
  54. def search(hist1, hist2):
  55.     err1 = 0
  56.     err2 = 0
  57.     err_relative = 0
  58.     Vstart = 1
  59.     cumulativeLHist1 = cumulativeLHist(hist1, Vstart)
  60.     cumulativeRHist2 = cumulativeRHist(hist2, Vstart)
  61.     mindif = 1000
  62.     cumulativeLHistMin = 0
  63.     cumulativeRHistMin = 0
  64.     Vmin = 0
  65.     while (cumulativeLHist1 != cumulativeRHist2):
  66.         Vstart += 1
  67.         print(cumulativeLHist1, cumulativeRHist2, Vstart, "debug")
  68.         cumulativeLHist1 = cumulativeLHist(hist1, Vstart)
  69.         cumulativeRHist2 = cumulativeRHist(hist2, Vstart)
  70.         err = (cumulativeLHist1 + cumulativeRHist2)/2
  71.         print("percentageerr",err/sum(hist1),abs(cumulativeRHist2 - cumulativeLHist1))
  72.         if ((abs(cumulativeRHist2 - cumulativeLHist1)) < mindif):
  73.             mindif = abs(cumulativeRHist2 - cumulativeLHist1)
  74.             Vmin = Vstart
  75.             cumulativeLHistMin = cumulativeLHist1
  76.             cumulativeRHistMin = cumulativeRHist2
  77.         if (Vstart > 9):
  78.             err1 = cumulativeLHistMin
  79.             err2 = cumulativeRHistMin
  80.             err = (err1 + err2) / 2
  81.             err_relative = err / sum(hist1)
  82.             print("errdebug,cumulative", Vmin,err1, err2,err,err_relative)
  83.             return mindif, Vmin, err, err_relative
  84.     return Vstart
  85. def HistWeightedSumm(hist1, hist2, weight=0.5):
  86.     hist3 = []
  87.     for index, value in enumerate(hist1):
  88.         print(index, value)
  89.         hist3.append(value + weight * hist2[index])
  90.     return hist3
  91. def Answer2Hist(Answer,par):
  92.     if (par == 'valence'):
  93.         print("valence")
  94.         return Answer.valence
  95.     elif (par == 'arousal'):
  96.         print("arousal")
  97.         return Answer.arousal
  98.     elif (par == 'dominance'):
  99.         print("dominance")
  100.         return Answer.dominance
  101.     print("Big Error")
  102. def ImageSelect(ImageName, Images):
  103.     for index, value in enumerate(Images):
  104.         if ImageName == value.name:
  105.             return index
  106. class Participant:
  107.     def __init__(self, name):
  108.         self.name = name
  109.         self.images = []
  110.         self.vec = []
  111.         self.meantime = 0
  112.         self.meandiff = 0
  113.     def image_add(self, image):
  114.         self.images.append(image)
  115.     def image_check(self, img_name):
  116.         for i in self.images:
  117.             if img_name == i.imgname_return():
  118.                 return 1
  119.         return False
  120.     def img_get(self):
  121.         return self.images
  122. class Image:
  123.     def __init__(self, name="***", good=1, active=1, strong=1):
  124.         self.name = name
  125.         self.good = good
  126.         self.active = active
  127.         self.strong = strong
  128.         self.binGood = -1
  129.         self.binActive = -1
  130.         self.binStrong = -1
  131.         self.vec = []
  132.     def imgname_return(self):
  133.         return self.name
  134. images = {}
  135. img_good = ['1811', '1920', '2055.2', '2208', '8120',  # cat1
  136.             '1340', '1750', '2040', '2154', '2791',  # cat2
  137.             '1500', '1720', '1810', '2550', '4574',  # cat3
  138.             '1440', '1463', '1722', '2050', '2650', ]  # cat4
  139. img_evil = ["1300", "1301", "1726", "1932", "2130",  # cat1
  140.             "1051", "1303", "6300", "6562", "9429",  # cat2
  141.             "2100", "2110", "6930", "9611", "9911",  # cat3
  142.             "2795", "6241", "6311", "6610", "9830"]  # cat4
  143. img_neutr = ["5260", "5470", "5593", "5623", "7590",  # cat1
  144.              "2381", "5410", "5626", "5629", "5900",  # cat2
  145.              "2200", "5300", "5395", "5471", "7500",  # cat3
  146.              "2441", "2500", "5030", "7004", "7058"  # cat4
  147.              ]
  148. img_active = ["1811", "1920", "2055.2", "2208", "8120",
  149.               "1340", "1750", "2040", "2154", "2791",
  150.               "1300", "1301", "1726", "1932", "2130",
  151.               "1051", "1303", "6300", "6562", "9429",
  152.               "5260", "5470", "5593", "5623", "7590"
  153.               "2381", "5410", "5626", "5629", "5900"
  154.               ]
  155. img_passive =[
  156. "1500","1720","1810","2550","4574",
  157. "1440","1463","1722","2050","2650",
  158. "2100","2110","6930","9611","9911",
  159. "2795","6241","6311","6610","9830",
  160. "2200","5300","5395","5471","7500"
  161. "2441","2500","5030","7004","7058"]
  162. img_strong =[
  163. "1811","1920","2055.2","2208","8120",
  164. "1500","1720","1810","2550","4574",
  165. "1300","1301","1726","1932","2130",
  166. "2100","2110","6930","9611","9911",
  167. "5260","5470","5593","5623","7590",
  168. "2200","5300","5395","5471","7500",
  169. ]
  170. img_weak =[
  171. "1340","1750","2040","2154","2791",
  172. "1440","1463","1722","2050","2650",
  173. "1051","1303","6300","6562","9429",
  174. "2795","6241","6311","6610","9830",
  175. "2381","5410","5626","5629","5900",
  176. "2441","2500","5030","7004","7058",
  177.  
  178. ]
  179. dir = 'Log'
  180. work_dir = ''
  181. log_folders = os.listdir(dir)
  182.  
  183.  
  184. valenceHistGood = Answer("Valence")  # Ответы по хорошим картинкам
  185. valenceHistNeutr = Answer("Valence")
  186. valenceHistEvil = Answer("Valence")
  187. arousalHistActive = Answer("Arousal")
  188. arousalHistPassive = Answer("Arousal")
  189. dominanceHistStrong = Answer("Dominance")
  190. dominanceHistWeak = Answer("Dominance")
  191.  
  192.  
  193. participants = [] #Участники
  194.  
  195.  
  196. for counter, i in enumerate(log_folders): #по всем папкам из директории Log
  197.     workdir = i
  198.     files = os.listdir(dir + '/' + workdir) #Все папки в папке log
  199.     for j in files: #по всем участникам
  200.         if j == files[1]:
  201.             break  # пропускаем первый файл, т.к он не содержит информацию по ответам
  202.         participants.append(Participant(j))
  203.         fileName = j
  204.         wb = load_workbook(dir + '/' + i + '/' + fileName)  # грузим xlsx
  205.         sheet1 = wb.get_sheet_by_name('Лист1')
  206.         for cellObj in sheet1['B1':'B195']:
  207.             for cell in cellObj:
  208.                 cNum = "C" + str(cell.row)  # Ячейка Ci
  209.                 aNum = "A" + str(cell.row)  # Ячейка Ai
  210.                 cValue = sheet1[cNum].value  # значение в ячейке Ci
  211.                 aValue = sheet1[aNum].value  # значение в ячейке Ai
  212.                 if not participants[counter].image_check(aValue):  # есть ли картинка в списке участника
  213.                     participants[counter].image_add(Image(aValue))
  214.                 imageIndex = ImageSelect(aValue, participants[counter].images) #Индекс картинки (не её номер)
  215.                 if str(aValue) in img_good:
  216.                     if (cell.value == 'Злой-Добрый'):
  217.                         valenceHistGood.set_g(cValue)  # увеличиваем значение гистограммы на 1
  218.                         participants[counter].images[imageIndex].good = cValue
  219.                 if str(aValue) in img_neutr:
  220.                     if (cell.value == 'Злой-Добрый'):
  221.                         valenceHistNeutr.set_g(cValue)
  222.                         participants[counter].images[imageIndex].good = cValue
  223.                 if str(aValue) in img_evil:
  224.                     if (cell.value == 'Злой-Добрый'):
  225.                         valenceHistEvil.set_g(cValue)
  226.                         participants[counter].images[imageIndex].good = cValue
  227.                 if str(aValue) in img_active:
  228.                     if (cell.value == 'Пассивный-Активный'):
  229.                         arousalHistActive.set_a(cValue)
  230.                         participants[counter].images[imageIndex].active = cValue
  231.                 if str(aValue) in img_passive:
  232.                     if (cell.value == 'Пассивный-Активный'):
  233.                         arousalHistPassive.set_a(cValue)
  234.                         participants[counter].images[imageIndex].active = cValue
  235.                 if str(aValue) in img_strong:
  236.                     if (cell.value == 'Маленький(слабый)-Большой(сильный)'):
  237.                         dominanceHistStrong.set_s(cValue)
  238.                         participants[counter].images[imageIndex].strong = cValue
  239.                 if str(aValue) in img_weak:
  240.                     if (cell.value == 'Маленький(слабый)-Большой(сильный)'):
  241.                         dominanceHistWeak.set_s(cValue)
  242.                         participants[counter].images[imageIndex].strong = cValue
  243. valence = [valenceHistGood, valenceHistNeutr, valenceHistEvil] # гистограммы по хорошим, нейтр, злым
  244. arousal = [arousalHistActive,arousalHistPassive]
  245. dominance = [dominanceHistStrong,dominanceHistWeak]
  246. VADList = [valence, arousal, dominance]
  247. statlist = []
  248. setterlist = []
  249. errlist = []
  250. for item in VADList:
  251.     for j, value in enumerate(item):
  252.         if (j > 0):
  253.             break
  254.         if (item == valence):
  255.             hist1 = Answer2Hist(value, "valence")
  256.             hist2 = Answer2Hist(item[j + 2], "valence")
  257.             hist3 = Answer2Hist(item[j + 1], "valence")
  258.             hist1w = HistWeightedSumm(hist1, hist3, 0.5)
  259.             hist2w = HistWeightedSumm(hist2, hist3, 0.5)
  260.             good_hist = hist1
  261.             evil_hist = hist2
  262.             diff1, ValueOfBorder, err1, err1_relative = search(hist1[1:], hist2[1:])
  263.             diff2, ValueOfBorderWeighted, err1w, err1w_relative = search(hist1w[1:], hist2w[1:])
  264.             setterlist = [ValueOfBorder, ValueOfBorderWeighted]
  265.         if (item == arousal):
  266.             hist1 = Answer2Hist(value, "arousal")
  267.             hist2 = Answer2Hist(item[j + 1], "arousal")
  268.             active_hist = hist1
  269.             passive_hist = hist2
  270.             diff1, ValueOfBorder, err1, err1_relative = search(hist1[1:], hist2[1:])
  271.         if (item == dominance):
  272.             hist1 = Answer2Hist(value, "dominance")
  273.             hist2 = Answer2Hist(item[j + 1], "dominance")
  274.             strong_hist = hist1
  275.             weak_hist = hist2
  276.             diff1, ValueOfBorder, err1, err1_relative = search(hist1[1:], hist2[1:])
  277.         hist1=hist1[1:]
  278.         hist2=hist2[2:]
  279.         # setterlist = [diff1,valence_1,diff2,valence_2]
  280.         print("ValNum",ValueOfBorder,ValueOfBorderWeighted, err1, err1_relative)
  281.         errlist.append([err1, err1_relative])
  282.         statlist.append(setterlist)
  283.  
  284. elem = statlist[0] #valence
  285. elem1 = statlist[1] #arousal
  286. elem2 = statlist[2] #dominance
  287.  
  288. for i in statlist:
  289.     for j in i:
  290.         print('statlist,debug',j)
  291.  
  292. for index, item in enumerate(participants):
  293.     print(item.name)
  294.     for i, j in enumerate(item.images):
  295.         if j.good >= elem[0]:
  296.             # print("before",j.name, j.good, j.active, j.strong,elem[0])
  297.             participants[index].images[i].binGood = 1
  298.             # print("after",j.name, j.good, j.active, j.strong)
  299.         else:
  300.             participants[index].images[i].binGood = 0
  301.         if j.active >= elem1[0]:
  302.             # print("before",j.name, j.good, j.active, j.strong,elem1[0])
  303.             participants[index].images[i].binActive = 1
  304.             # print("after",j.name, j.good, j.active, j.strong)
  305.         else:
  306.             participants[index].images[i].binActive = 0
  307.         if j.strong >= elem2[0]:
  308.             # print("before", j.name, j.good, j.active, j.strong, elem2[0])
  309.             participants[index].images[i].binStrong = 1
  310.             # print("after", j.name, j.good, j.active, j.strong)
  311.         else:
  312.             participants[index].images[i].binStrong = 0
  313.         print(j.name, "Dec", j.good, j.active, j.strong, "Bin", j.binGood, j.binActive, j.binStrong, "means", elem[0],
  314.               elem1[0], elem2[0])
  315. MeanGood = 0
  316. MeanGoodCounter = 0
  317. MeanEvil = 0
  318. MeanEvilCounter = 0
  319. MeanActive = 0
  320. MeanActiveCounter = 0
  321. MeanPassive = 0
  322. MeanPassiveCounter = 0
  323. MeanStrong = 0
  324. MeanStrongCounter = 0
  325. MeanWeak = 0
  326. MeanWeakCounter = 0
  327. for i in participants:
  328.     for j in i.images:
  329.         print(j.name, j.binGood)
  330.         if (str(j.name) in img_good) and (j.binGood == 1):
  331.             MeanGood += j.good
  332.             MeanGoodCounter += 1
  333.         if (str(j.name) in img_evil) and (j.binGood == 0):
  334.             MeanEvil += j.good
  335.             MeanEvilCounter += 1
  336.         if (str(j.name) in img_active) and (j.binActive== 1):
  337.             MeanActive += j.active
  338.             MeanActiveCounter += 1
  339.         if (str(j.name) in img_passive) and (j.binActive == 0):
  340.             MeanPassive += j.active
  341.             MeanPassiveCounter += 1
  342.         if (str(j.name) in img_strong) and (j.binStrong== 1):
  343.             MeanStrong += j.strong
  344.             MeanStrongCounter += 1
  345.         if (str(j.name) in img_weak) and (j.binStrong == 0):
  346.             MeanWeak += j.strong
  347.             MeanWeakCounter += 1
  348.  
  349. MeanGood = MeanGood / MeanGoodCounter
  350. MeanEvil = MeanEvil / MeanEvilCounter
  351. MeanActive = MeanActive/MeanActiveCounter
  352. MeanPassive = MeanPassive/MeanPassiveCounter
  353. MeanStrong = MeanStrong/MeanStrongCounter
  354. MeanWeak = MeanWeak/MeanWeakCounter
  355.  
  356. for i in participants: #вектор характеристик по каждой картинке
  357.     for j in i.images:
  358.       # print("anoterDebug,MeanGood,MeanEvi\t", MeanGood, MeanEvil, j.good)
  359.         j.vec.append(j.good - MeanGood)
  360.         j.vec.append(j.good - MeanEvil)
  361.         j.vec.append(j.active - MeanActive)
  362.         j.vec.append(j.active - MeanPassive)
  363.         j.vec.append(j.strong - MeanStrong)
  364.         j.vec.append(j.strong - MeanWeak)
  365.         print(j.vec)
  366. for i in participants:
  367.     MeanVecGood = 0
  368.     MeanVecGoodCounter = 0
  369.     MeanVecEvil = 0
  370.     MeanVecEvilCounter = 0
  371.     MeanVecActive = 0
  372.     MeanVecActiveCounter = 0
  373.     MeanVecPassive = 0
  374.     MeanVecPassiveCounter = 0
  375.     MeanVecStrong = 0
  376.     MeanVecStrongCounter = 0
  377.     MeanVecWeak = 0
  378.     MeanVecWeakCounter = 0
  379.     for j in i.images:
  380.         if str(j.name) in img_good:
  381.             MeanVecGood += j.vec[0]
  382.             MeanVecGoodCounter += 1
  383.         if str(j.name) in img_evil:
  384.             MeanVecEvil += j.vec[1]
  385.             MeanVecEvilCounter += 1
  386.         if str(j.name) in img_active:
  387.             MeanVecActive+= j.vec[2]
  388.             MeanVecActiveCounter += 1
  389.         if str(j.name) in img_passive:
  390.             MeanVecPassive += j.vec[3]
  391.             MeanVecPassiveCounter += 1
  392.         if str(j.name) in img_strong:
  393.             MeanVecStrong += j.vec[4]
  394.             MeanVecStrongCounter += 1
  395.         if str(j.name) in img_weak:
  396.             MeanVecWeak += j.vec[5]
  397.             MeanVecWeakCounter += 1
  398.     i.vec.append(MeanVecGood / MeanVecGoodCounter)
  399.     i.vec.append(MeanVecEvil / MeanVecEvilCounter)
  400.     i.vec.append(MeanVecActive / MeanVecActiveCounter)
  401.     i.vec.append(MeanVecPassive / MeanVecPassiveCounter)
  402.     i.vec.append(MeanVecStrong / MeanVecStrongCounter)
  403.     i.vec.append(MeanVecWeak / MeanVecStrongCounter)
  404.     print('Participant_Vector', i.name, i.vec)
  405. print("Meangood = {0}, Meanevil = {1}, ErrGood = {2}, ErrEvil ={3}".format(MeanGood, MeanEvil, errlist[0], errlist[1]))
  406. print("good,evil,hists", good_hist, evil_hist)
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417. columns = ['K', 'M', 'O', 'Q', 'S', 'U', 'W', 'Y', 'AA', 'AC', 'AE', 'AG', 'AI']
  418.  
  419. for counter, i in enumerate(log_folders):  # по всем папкам из директории Log
  420.     workdir = i
  421.     files = os.listdir(dir + '/' + workdir)  # Все папки в папке log
  422.     for j in files[1:]:  # по всем участникам
  423.         print(j)
  424.         participants.append(Participant(j))
  425.         timecounter = 0
  426.         fileName = j
  427.         wb = load_workbook(dir + '/' + i + '/' + fileName, False)  # грузим xlsx
  428.         try:
  429.             sheet2 = wb.get_sheet_by_name('S2')
  430.             sheet2.title = 'Лист2'
  431.             sheet1 = sheet2
  432.         except:
  433.             sheet1 = wb.get_sheet_by_name('Лист2')
  434.         for col in columns:
  435.             for row in range(3, 18):
  436.                 cell = col + str(row)
  437.                 value = sheet1[cell].value
  438.                 try:
  439.                     participants[counter].meantime += value
  440.                     timecounter += 1
  441.                 except:
  442.                     pass
  443.         if timecounter == 0:
  444.             print(j)
  445.             break
  446.         participants[counter].meantime = participants[counter].meantime / timecounter
  447.         print(participants[counter].meantime)
  448. mean = 0
  449. timecounter = 0
  450. for i in participants:
  451.     if (i.meantime > 0):
  452.         mean += i.meantime
  453.         timecounter +=1
  454. mean = mean/timecounter
  455. for i in participants:
  456.     if(i.meantime==0):
  457.         i.meantime = mean
  458. for i in participants:
  459.     i.vec.append(mean - i.meantime)
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471. f = open('kkk.txt', 'w')
  472. for item in participants:
  473.     f.write("%s\n" % item.vec)
  474.  
  475. x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  476. width = 0.35
  477. v2 = statlist[0]
  478. v2errlist = errlist[1]
  479. v2e = v2errlist[1]
  480. vvv = "border Ao ="+str(v2[0] + 1) + " " + "err =" + str(v2e)
  481. basewidth = np.arange(11)
  482. plt.bar(basewidth, active_hist,width =0.5, color="green")
  483. plt.bar(basewidth + width, passive_hist,width =0.5, color="blue")
  484. plt.xticks(x)
  485. plt.title("Active- green , Passive - blue" + "  "+ str(vvv))
  486. print(vvv)
  487. print("cum_sum,",cumulativeRHist(evil_hist,7),
  488.                  cumulativeLHist(good_hist,7))
  489. plt.show()
  490. input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement