Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 18.95 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.         self.meangoodtime = 0
  114.         self.meanactivetime = 0
  115.         self.meanstrongtime = 0
  116.     def image_add(self, image):
  117.         self.images.append(image)
  118.     def image_check(self, img_name):
  119.         for i in self.images:
  120.             if img_name == i.imgname_return():
  121.                 return 1
  122.         return False
  123.     def img_get(self):
  124.         return self.images
  125. class Image:
  126.     def __init__(self, name="***", good=1, active=1, strong=1):
  127.         self.name = name
  128.         self.good = good
  129.         self.active = active
  130.         self.strong = strong
  131.         self.binGood = -1
  132.         self.binActive = -1
  133.         self.binStrong = -1
  134.         self.vec = []
  135.         self.goodDuration = 0
  136.         self.activeDuration = 0
  137.         self.strongDuration = 0
  138.         self.category = "net"
  139.     def imgname_return(self):
  140.         return self.name
  141. images = {}
  142. img_good = ['1811', '1920', '2055.2', '2208', '8120',  # cat1
  143.             '1340', '1750', '2040', '2154', '2791',  # cat2
  144.             '1500', '1720', '1810', '2550', '4574',  # cat3
  145.             '1440', '1463', '1722', '2050', '2650', ]  # cat4
  146. img_evil = ["1300", "1301", "1726", "1932", "2130",  # cat1
  147.             "1051", "1303", "6300", "6562", "9429",  # cat2
  148.             "2100", "2110", "6930", "9611", "9911",  # cat3
  149.             "2795", "6241", "6311", "6610", "9830"]  # cat4
  150. img_neutr = ["5260", "5470", "5593", "5623", "7590",  # cat1
  151.              "2381", "5410", "5626", "5629", "5900",  # cat2
  152.              "2200", "5300", "5395", "5471", "7500",  # cat3
  153.              "2441", "2500", "5030", "7004", "7058"  # cat4
  154.              ]
  155. img_active = ["1811", "1920", "2055.2", "2208", "8120",
  156.               "1340", "1750", "2040", "2154", "2791",
  157.               "1300", "1301", "1726", "1932", "2130",
  158.               "1051", "1303", "6300", "6562", "9429",
  159.               "5260", "5470", "5593", "5623", "7590"
  160.               "2381", "5410", "5626", "5629", "5900"
  161.               ]
  162. img_passive =[
  163. "1500","1720","1810","2550","4574",
  164. "1440","1463","1722","2050","2650",
  165. "2100","2110","6930","9611","9911",
  166. "2795","6241","6311","6610","9830",
  167. "2200","5300","5395","5471","7500"
  168. "2441","2500","5030","7004","7058"]
  169. img_strong =[
  170. "1811","1920","2055.2","2208","8120",
  171. "1500","1720","1810","2550","4574",
  172. "1300","1301","1726","1932","2130",
  173. "2100","2110","6930","9611","9911",
  174. "5260","5470","5593","5623","7590",
  175. "2200","5300","5395","5471","7500",
  176. ]
  177. img_weak =[
  178. "1340","1750","2040","2154","2791",
  179. "1440","1463","1722","2050","2650",
  180. "1051","1303","6300","6562","9429",
  181. "2795","6241","6311","6610","9830",
  182. "2381","5410","5626","5629","5900",
  183. "2441","2500","5030","7004","7058",
  184.  
  185. ]
  186. dir = 'Last_logs'
  187. work_dir = ''
  188. log_folders = os.listdir(dir)
  189.  
  190.  
  191. valenceHistGood = Answer("Valence")  # Ответы по хорошим картинкам
  192. valenceHistNeutr = Answer("Valence")
  193. valenceHistEvil = Answer("Valence")
  194. arousalHistActive = Answer("Arousal")
  195. arousalHistPassive = Answer("Arousal")
  196. dominanceHistStrong = Answer("Dominance")
  197. dominanceHistWeak = Answer("Dominance")
  198.  
  199.  
  200. participants = [] #Участники
  201. numerator = -1
  202.  
  203. for counter, i in enumerate(log_folders): #по всем файлам из директории Log
  204.     if counter % 2 == 0: continue
  205.     numerator +=1
  206.     workdir = i
  207.     print (i)
  208.     participants.append(Participant(i))
  209.     fileName = i
  210.     wb = load_workbook(dir + '/'  + fileName)  # грузим xlsx
  211.     try:
  212.         sheet1 = wb.get_sheet_by_name('S2+S3')
  213.     except:
  214.         sheet1 = wb.get_sheet_by_name('Лист2')
  215.     for cellObj in sheet1['A2':'A196']:
  216.         for cell in cellObj:
  217.  
  218.  
  219.             cNum = "C" + str(cell.row)  # Ячейка Ci
  220.             eNum = "E" + str(cell.row)  # Ячейка Ai
  221.             bNum = 'B' + str(cell.row)
  222.             eValue = sheet1[eNum].value
  223.             bValue = sheet1[bNum].value
  224.             cValue = sheet1[cNum].value
  225.             aValue = cell.value  # значение в ячейке ei
  226. ###проверка наличия картинки у участника
  227.             if bValue == "Otvr":
  228.                 continue
  229.             if not participants[numerator].image_check(cValue):  # есть ли картинка в списке участника
  230.                 participants[numerator].image_add(Image(cValue))
  231.             imageIndex = ImageSelect(cValue, participants[numerator].images)  # Индекс картинки (не её номер)
  232.  
  233.             if str(cValue) in img_good:
  234.                 if (cell.value == 'Злой-Добрый'):
  235.                     participants[numerator].images[imageIndex].goodDuration = eValue
  236.             if str(cValue) in img_neutr:
  237.                 if (cell.value == 'Злой-Добрый'):
  238.                     participants[numerator].images[imageIndex].goodDuration = eValue
  239.                 if (cell.value == 'Пассивный-Активный'):
  240.                     participants[numerator].images[imageIndex].activeDuration = eValue
  241.                 if (cell.value == 'Маленький(слабый)-Большой(сильный)'):
  242.                     participants[numerator].images[imageIndex].strongDuration = eValue
  243.             if str(cValue) in img_evil:
  244.                 if (cell.value == 'Злой-Добрый'):
  245.                     participants[numerator].images[imageIndex].goodDuration = eValue
  246.             if str(cValue) in img_active:
  247.                 if (cell.value == 'Пассивный-Активный'):
  248.                     participants[numerator].images[imageIndex].activeDuration = eValue
  249.             if str(cValue) in img_passive:
  250.                 if (cell.value == 'Пассивный-Активный'):
  251.                     participants[numerator].images[imageIndex].activeDuration = eValue
  252.             if str(cValue) in img_strong:
  253.                 if (cell.value == 'Маленький(слабый)-Большой(сильный)'):
  254.                     participants[numerator].images[imageIndex].strongDuration = eValue
  255.             if str(cValue) in img_weak:
  256.                 if (cell.value == 'Маленький(слабый)-Большой(сильный)'):
  257.                     participants[numerator].images[imageIndex].strongDuration = eValue
  258. for i in participants:
  259.     meanG = 0
  260.     meanA = 0
  261.     meanS = 0
  262.     for itter, j in enumerate(i.images):
  263.         if j.activeDuration == None:
  264.             j.activeDuration = 0
  265.         if j.goodDuration == None:
  266.             j.goodDuration = 0
  267.         if j.strongDuration == None:
  268.             j.strongDuration = 0
  269.         meanG += j.goodDuration
  270.         meanA += j.activeDuration
  271.         meanS += j.strongDuration
  272.     meanG = meanG / itter
  273.     meanA = meanA / itter
  274.     meanS = meanS / itter
  275.     print(i.name, meanG, meanA, meanS)
  276.     i.meangoodtime = meanG
  277.     i.meanactivetime = meanA
  278.     i.meanstrongtime = meanS
  279. input()
  280.  
  281. valence = [valenceHistGood, valenceHistNeutr, valenceHistEvil] # гистограммы по хорошим, нейтр, злым
  282. arousal = [arousalHistActive,arousalHistPassive]
  283. dominance = [dominanceHistStrong,dominanceHistWeak]
  284. VADList = [valence, arousal, dominance]
  285. statlist = []
  286. setterlist = []
  287. errlist = []
  288. for item in VADList:
  289.     for j, value in enumerate(item):
  290.         if (j > 0):
  291.             break
  292.         if (item == valence):
  293.             hist1 = Answer2Hist(value, "valence")
  294.             hist2 = Answer2Hist(item[j + 2], "valence")
  295.             hist3 = Answer2Hist(item[j + 1], "valence")
  296.             hist1w = HistWeightedSumm(hist1, hist3, 0.5)
  297.             hist2w = HistWeightedSumm(hist2, hist3, 0.5)
  298.             good_hist = hist1
  299.             evil_hist = hist2
  300.             diff1, ValueOfBorder, err1, err1_relative = search(hist1[1:], hist2[1:])
  301.             diff2, ValueOfBorderWeighted, err1w, err1w_relative = search(hist1w[1:], hist2w[1:])
  302.             setterlist = [ValueOfBorder, ValueOfBorderWeighted]
  303.         if (item == arousal):
  304.             hist1 = Answer2Hist(value, "arousal")
  305.             hist2 = Answer2Hist(item[j + 1], "arousal")
  306.             active_hist = hist1
  307.             passive_hist = hist2
  308.             diff1, ValueOfBorder, err1, err1_relative = search(hist1[1:], hist2[1:])
  309.         if (item == dominance):
  310.             hist1 = Answer2Hist(value, "dominance")
  311.             hist2 = Answer2Hist(item[j + 1], "dominance")
  312.             strong_hist = hist1
  313.             weak_hist = hist2
  314.             diff1, ValueOfBorder, err1, err1_relative = search(hist1[1:], hist2[1:])
  315.         hist1=hist1[1:]
  316.         hist2=hist2[2:]
  317.         # setterlist = [diff1,valence_1,diff2,valence_2]
  318.         print("ValNum",ValueOfBorder,ValueOfBorderWeighted, err1, err1_relative)
  319.         errlist.append([err1, err1_relative])
  320.         statlist.append(setterlist)
  321.  
  322. elem = statlist[0] #valence
  323. elem1 = statlist[1] #arousal
  324. elem2 = statlist[2] #dominance
  325.  
  326. for i in statlist:
  327.     for j in i:
  328.         print('statlist,debug',j)
  329.  
  330. for index, item in enumerate(participants):
  331.     print(item.name)
  332.     for i, j in enumerate(item.images):
  333.         if j.good >= elem[0]:
  334.             # print("before",j.name, j.good, j.active, j.strong,elem[0])
  335.             participants[index].images[i].binGood = 1
  336.             # print("after",j.name, j.good, j.active, j.strong)
  337.         else:
  338.             participants[index].images[i].binGood = 0
  339.         if j.active >= elem1[0]:
  340.             # print("before",j.name, j.good, j.active, j.strong,elem1[0])
  341.             participants[index].images[i].binActive = 1
  342.             # print("after",j.name, j.good, j.active, j.strong)
  343.         else:
  344.             participants[index].images[i].binActive = 0
  345.         if j.strong >= elem2[0]:
  346.             # print("before", j.name, j.good, j.active, j.strong, elem2[0])
  347.             participants[index].images[i].binStrong = 1
  348.             # print("after", j.name, j.good, j.active, j.strong)
  349.         else:
  350.             participants[index].images[i].binStrong = 0
  351.         print(j.name, "Dec", j.good, j.active, j.strong, "Bin", j.binGood, j.binActive, j.binStrong, "means", elem[0],
  352.               elem1[0], elem2[0])
  353. MeanGood = 0
  354. MeanGoodCounter = 0
  355. MeanEvil = 0
  356. MeanEvilCounter = 0
  357. MeanActive = 0
  358. MeanActiveCounter = 0
  359. MeanPassive = 0
  360. MeanPassiveCounter = 0
  361. MeanStrong = 0
  362. MeanStrongCounter = 0
  363. MeanWeak = 0
  364. MeanWeakCounter = 0
  365. for i in participants:
  366.     for j in i.images:
  367.         print(j.name, j.binGood)
  368.         if (str(j.name) in img_good) and (j.binGood == 1):
  369.             MeanGood += j.good
  370.             MeanGoodCounter += 1
  371.         if (str(j.name) in img_evil) and (j.binGood == 0):
  372.             MeanEvil += j.good
  373.             MeanEvilCounter += 1
  374.         if (str(j.name) in img_active) and (j.binActive== 1):
  375.             MeanActive += j.active
  376.             MeanActiveCounter += 1
  377.         if (str(j.name) in img_passive) and (j.binActive == 0):
  378.             MeanPassive += j.active
  379.             MeanPassiveCounter += 1
  380.         if (str(j.name) in img_strong) and (j.binStrong== 1):
  381.             MeanStrong += j.strong
  382.             MeanStrongCounter += 1
  383.         if (str(j.name) in img_weak) and (j.binStrong == 0):
  384.             MeanWeak += j.strong
  385.             MeanWeakCounter += 1
  386.  
  387. MeanGood = MeanGood / MeanGoodCounter
  388. MeanEvil = MeanEvil / MeanEvilCounter
  389. MeanActive = MeanActive/MeanActiveCounter
  390. MeanPassive = MeanPassive/MeanPassiveCounter
  391. MeanStrong = MeanStrong/MeanStrongCounter
  392. MeanWeak = MeanWeak/MeanWeakCounter
  393.  
  394. for i in participants: #вектор характеристик по каждой картинке
  395.     for j in i.images:
  396.       # print("anoterDebug,MeanGood,MeanEvi\t", MeanGood, MeanEvil, j.good)
  397.         j.vec.append(j.good - MeanGood)
  398.         j.vec.append(j.good - MeanEvil)
  399.         j.vec.append(j.active - MeanActive)
  400.         j.vec.append(j.active - MeanPassive)
  401.         j.vec.append(j.strong - MeanStrong)
  402.         j.vec.append(j.strong - MeanWeak)
  403.         print(j.vec)
  404. for i in participants:
  405.     MeanVecGood = 0
  406.     MeanVecGoodCounter = 0
  407.     MeanVecEvil = 0
  408.     MeanVecEvilCounter = 0
  409.     MeanVecActive = 0
  410.     MeanVecActiveCounter = 0
  411.     MeanVecPassive = 0
  412.     MeanVecPassiveCounter = 0
  413.     MeanVecStrong = 0
  414.     MeanVecStrongCounter = 0
  415.     MeanVecWeak = 0
  416.     MeanVecWeakCounter = 0
  417.     for j in i.images:
  418.         if str(j.name) in img_good:
  419.             MeanVecGood += j.vec[0]
  420.             MeanVecGoodCounter += 1
  421.         if str(j.name) in img_evil:
  422.             MeanVecEvil += j.vec[1]
  423.             MeanVecEvilCounter += 1
  424.         if str(j.name) in img_active:
  425.             MeanVecActive+= j.vec[2]
  426.             MeanVecActiveCounter += 1
  427.         if str(j.name) in img_passive:
  428.             MeanVecPassive += j.vec[3]
  429.             MeanVecPassiveCounter += 1
  430.         if str(j.name) in img_strong:
  431.             MeanVecStrong += j.vec[4]
  432.             MeanVecStrongCounter += 1
  433.         if str(j.name) in img_weak:
  434.             MeanVecWeak += j.vec[5]
  435.             MeanVecWeakCounter += 1
  436.     i.vec.append(MeanVecGood / MeanVecGoodCounter)
  437.     i.vec.append(MeanVecEvil / MeanVecEvilCounter)
  438.     i.vec.append(MeanVecActive / MeanVecActiveCounter)
  439.     i.vec.append(MeanVecPassive / MeanVecPassiveCounter)
  440.     i.vec.append(MeanVecStrong / MeanVecStrongCounter)
  441.     i.vec.append(MeanVecWeak / MeanVecStrongCounter)
  442.     print('Participant_Vector', i.name, i.vec)
  443. print("Meangood = {0}, Meanevil = {1}, ErrGood = {2}, ErrEvil ={3}".format(MeanGood, MeanEvil, errlist[0], errlist[1]))
  444. print("good,evil,hists", good_hist, evil_hist)
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455. columns = ['K', 'M', 'O', 'Q', 'S', 'U', 'W', 'Y', 'AA', 'AC', 'AE', 'AG', 'AI']
  456.  
  457. for counter, i in enumerate(log_folders):  # по всем папкам из директории Log
  458.     workdir = i
  459.     files = os.listdir(dir + '/' + workdir)  # Все папки в папке log
  460.     for j in files[1:]:  # по всем участникам
  461.         print(j)
  462.         participants.append(Participant(j))
  463.         timecounter = 0
  464.         fileName = j
  465.         wb = load_workbook(dir + '/' + i + '/' + fileName, False)  # грузим xlsx
  466.         try:
  467.             sheet2 = wb.get_sheet_by_name('S2')
  468.             sheet2.title = 'Лист2'
  469.             sheet1 = sheet2
  470.         except:
  471.             sheet1 = wb.get_sheet_by_name('Лист2')
  472.         for col in columns:
  473.             for row in range(3, 18):
  474.                 cell = col + str(row)
  475.                 value = sheet1[cell].value
  476.                 try:
  477.                     participants[counter].meantime += value
  478.                     timecounter += 1
  479.                 except:
  480.                     pass
  481.         if timecounter == 0:
  482.             print(j)
  483.             break
  484.         participants[counter].meantime = participants[counter].meantime / timecounter
  485.         print(participants[counter].meantime)
  486. mean = 0
  487. timecounter = 0
  488. for i in participants:
  489.     if (i.meantime > 0):
  490.         mean += i.meantime
  491.         timecounter +=1
  492. mean = mean/timecounter
  493. for i in participants:
  494.     if(i.meantime==0):
  495.         i.meantime = mean
  496. for i in participants:
  497.     i.vec.append(mean - i.meantime)
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509. f = open('kkk.txt', 'w')
  510. for item in participants:
  511.     f.write("%s\n" % item.vec)
  512.  
  513. x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  514. width = 0.35
  515. v2 = statlist[0]
  516. v2errlist = errlist[1]
  517. v2e = v2errlist[1]
  518. vvv = "border Ao ="+str(v2[0] + 1) + " " + "err =" + str(v2e)
  519. basewidth = np.arange(11)
  520. plt.bar(basewidth, active_hist,width =0.5, color="green")
  521. plt.bar(basewidth + width, passive_hist,width =0.5, color="blue")
  522. plt.xticks(x)
  523. plt.title("Active- green , Passive - blue" + "  "+ str(vvv))
  524. print(vvv)
  525. print("cum_sum,",cumulativeRHist(evil_hist,7),
  526.                  cumulativeLHist(good_hist,7))
  527. plt.show()
  528. input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement