Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from openpyxl import load_workbook
- import matplotlib.pyplot as plt
- # Библиотека для работы с excel файлами
- import os
- import numpy as np
- class Answer:
- def __init__(self, string):
- self.name = string
- self.valence = [0 for x in range(11)]
- self.arousal = [0 for x in range(11)]
- self.dominance = [0 for x in range(11)]
- self.status = 0 # 0 not set, 1 good, 2 evil, 3 neutral
- def set_g(self, value):
- self.valence[value] += 1
- def set_a(self, value):
- self.arousal[value] += 1
- def set_s(self, value):
- self.dominance[value] += 1
- def set_status(self, value):
- self.status = value
- def printall(self):
- print(self.name, self.status, self.valence, self.arousal, self.dominance)
- def get_g(self):
- return self.valence
- def get_a(self):
- return self.arousal
- def get_s(self):
- return self.dominance
- def get_name(self):
- return self.name
- def get_status(self):
- if self.status == 1:
- return 'good'
- elif self.status == 2:
- return 'evil'
- elif self.status == 3:
- return 'neutral'
- elif self.status == 55:
- return 'disgusting'
- def cumulativeLHist(hist1, i):
- return sum(hist1[:i])
- def cumulativeRHist(hist1, i):
- return sum(hist1[i:])
- def search(hist1, hist2):
- err1 = 0
- err2 = 0
- err_relative = 0
- Vstart = 1
- cumulativeLHist1 = cumulativeLHist(hist1, Vstart)
- cumulativeRHist2 = cumulativeRHist(hist2, Vstart)
- mindif = 1000
- cumulativeLHistMin = 0
- cumulativeRHistMin = 0
- Vmin = 0
- while (cumulativeLHist1 != cumulativeRHist2):
- Vstart += 1
- print(cumulativeLHist1, cumulativeRHist2, Vstart, "debug")
- cumulativeLHist1 = cumulativeLHist(hist1, Vstart)
- cumulativeRHist2 = cumulativeRHist(hist2, Vstart)
- err = (cumulativeLHist1 + cumulativeRHist2)/2
- print("percentageerr",err/sum(hist1),abs(cumulativeRHist2 - cumulativeLHist1))
- if ((abs(cumulativeRHist2 - cumulativeLHist1)) < mindif):
- mindif = abs(cumulativeRHist2 - cumulativeLHist1)
- Vmin = Vstart
- cumulativeLHistMin = cumulativeLHist1
- cumulativeRHistMin = cumulativeRHist2
- if (Vstart > 9):
- err1 = cumulativeLHistMin
- err2 = cumulativeRHistMin
- err = (err1 + err2) / 2
- err_relative = err / sum(hist1)
- print("errdebug,cumulative", Vmin,err1, err2,err,err_relative)
- return mindif, Vmin, err, err_relative
- return Vstart
- def HistWeightedSumm(hist1, hist2, weight=0.5):
- hist3 = []
- for index, value in enumerate(hist1):
- print(index, value)
- hist3.append(value + weight * hist2[index])
- return hist3
- def Answer2Hist(Answer,par):
- if (par == 'valence'):
- print("valence")
- return Answer.valence
- elif (par == 'arousal'):
- print("arousal")
- return Answer.arousal
- elif (par == 'dominance'):
- print("dominance")
- return Answer.dominance
- print("Big Error")
- def ImageSelect(ImageName, Images):
- for index, value in enumerate(Images):
- if ImageName == value.name:
- return index
- class Participant:
- def __init__(self, name):
- self.name = name
- self.images = []
- self.vec = []
- self.meantime = 0
- self.meandiff = 0
- def image_add(self, image):
- self.images.append(image)
- def image_check(self, img_name):
- for i in self.images:
- if img_name == i.imgname_return():
- return 1
- return False
- def img_get(self):
- return self.images
- class Image:
- def __init__(self, name="***", good=1, active=1, strong=1):
- self.name = name
- self.good = good
- self.active = active
- self.strong = strong
- self.binGood = -1
- self.binActive = -1
- self.binStrong = -1
- self.vec = []
- def imgname_return(self):
- return self.name
- images = {}
- img_good = ['1811', '1920', '2055.2', '2208', '8120', # cat1
- '1340', '1750', '2040', '2154', '2791', # cat2
- '1500', '1720', '1810', '2550', '4574', # cat3
- '1440', '1463', '1722', '2050', '2650', ] # cat4
- img_evil = ["1300", "1301", "1726", "1932", "2130", # cat1
- "1051", "1303", "6300", "6562", "9429", # cat2
- "2100", "2110", "6930", "9611", "9911", # cat3
- "2795", "6241", "6311", "6610", "9830"] # cat4
- img_neutr = ["5260", "5470", "5593", "5623", "7590", # cat1
- "2381", "5410", "5626", "5629", "5900", # cat2
- "2200", "5300", "5395", "5471", "7500", # cat3
- "2441", "2500", "5030", "7004", "7058" # cat4
- ]
- img_active = ["1811", "1920", "2055.2", "2208", "8120",
- "1340", "1750", "2040", "2154", "2791",
- "1300", "1301", "1726", "1932", "2130",
- "1051", "1303", "6300", "6562", "9429",
- "5260", "5470", "5593", "5623", "7590"
- "2381", "5410", "5626", "5629", "5900"
- ]
- img_passive =[
- "1500","1720","1810","2550","4574",
- "1440","1463","1722","2050","2650",
- "2100","2110","6930","9611","9911",
- "2795","6241","6311","6610","9830",
- "2200","5300","5395","5471","7500"
- "2441","2500","5030","7004","7058"]
- img_strong =[
- "1811","1920","2055.2","2208","8120",
- "1500","1720","1810","2550","4574",
- "1300","1301","1726","1932","2130",
- "2100","2110","6930","9611","9911",
- "5260","5470","5593","5623","7590",
- "2200","5300","5395","5471","7500",
- ]
- img_weak =[
- "1340","1750","2040","2154","2791",
- "1440","1463","1722","2050","2650",
- "1051","1303","6300","6562","9429",
- "2795","6241","6311","6610","9830",
- "2381","5410","5626","5629","5900",
- "2441","2500","5030","7004","7058",
- ]
- dir = 'Log'
- work_dir = ''
- log_folders = os.listdir(dir)
- valenceHistGood = Answer("Valence") # Ответы по хорошим картинкам
- valenceHistNeutr = Answer("Valence")
- valenceHistEvil = Answer("Valence")
- arousalHistActive = Answer("Arousal")
- arousalHistPassive = Answer("Arousal")
- dominanceHistStrong = Answer("Dominance")
- dominanceHistWeak = Answer("Dominance")
- participants = [] #Участники
- for counter, i in enumerate(log_folders): #по всем папкам из директории Log
- workdir = i
- files = os.listdir(dir + '/' + workdir) #Все папки в папке log
- for j in files: #по всем участникам
- if j == files[1]:
- break # пропускаем первый файл, т.к он не содержит информацию по ответам
- participants.append(Participant(j))
- fileName = j
- wb = load_workbook(dir + '/' + i + '/' + fileName) # грузим xlsx
- sheet1 = wb.get_sheet_by_name('Лист1')
- for cellObj in sheet1['B1':'B195']:
- for cell in cellObj:
- cNum = "C" + str(cell.row) # Ячейка Ci
- aNum = "A" + str(cell.row) # Ячейка Ai
- cValue = sheet1[cNum].value # значение в ячейке Ci
- aValue = sheet1[aNum].value # значение в ячейке Ai
- if not participants[counter].image_check(aValue): # есть ли картинка в списке участника
- participants[counter].image_add(Image(aValue))
- imageIndex = ImageSelect(aValue, participants[counter].images) #Индекс картинки (не её номер)
- if str(aValue) in img_good:
- if (cell.value == 'Злой-Добрый'):
- valenceHistGood.set_g(cValue) # увеличиваем значение гистограммы на 1
- participants[counter].images[imageIndex].good = cValue
- if str(aValue) in img_neutr:
- if (cell.value == 'Злой-Добрый'):
- valenceHistNeutr.set_g(cValue)
- participants[counter].images[imageIndex].good = cValue
- if str(aValue) in img_evil:
- if (cell.value == 'Злой-Добрый'):
- valenceHistEvil.set_g(cValue)
- participants[counter].images[imageIndex].good = cValue
- if str(aValue) in img_active:
- if (cell.value == 'Пассивный-Активный'):
- arousalHistActive.set_a(cValue)
- participants[counter].images[imageIndex].active = cValue
- if str(aValue) in img_passive:
- if (cell.value == 'Пассивный-Активный'):
- arousalHistPassive.set_a(cValue)
- participants[counter].images[imageIndex].active = cValue
- if str(aValue) in img_strong:
- if (cell.value == 'Маленький(слабый)-Большой(сильный)'):
- dominanceHistStrong.set_s(cValue)
- participants[counter].images[imageIndex].strong = cValue
- if str(aValue) in img_weak:
- if (cell.value == 'Маленький(слабый)-Большой(сильный)'):
- dominanceHistWeak.set_s(cValue)
- participants[counter].images[imageIndex].strong = cValue
- valence = [valenceHistGood, valenceHistNeutr, valenceHistEvil] # гистограммы по хорошим, нейтр, злым
- arousal = [arousalHistActive,arousalHistPassive]
- dominance = [dominanceHistStrong,dominanceHistWeak]
- VADList = [valence, arousal, dominance]
- statlist = []
- setterlist = []
- errlist = []
- for item in VADList:
- for j, value in enumerate(item):
- if (j > 0):
- break
- if (item == valence):
- hist1 = Answer2Hist(value, "valence")
- hist2 = Answer2Hist(item[j + 2], "valence")
- hist3 = Answer2Hist(item[j + 1], "valence")
- hist1w = HistWeightedSumm(hist1, hist3, 0.5)
- hist2w = HistWeightedSumm(hist2, hist3, 0.5)
- good_hist = hist1
- evil_hist = hist2
- diff1, ValueOfBorder, err1, err1_relative = search(hist1[1:], hist2[1:])
- diff2, ValueOfBorderWeighted, err1w, err1w_relative = search(hist1w[1:], hist2w[1:])
- setterlist = [ValueOfBorder, ValueOfBorderWeighted]
- if (item == arousal):
- hist1 = Answer2Hist(value, "arousal")
- hist2 = Answer2Hist(item[j + 1], "arousal")
- active_hist = hist1
- passive_hist = hist2
- diff1, ValueOfBorder, err1, err1_relative = search(hist1[1:], hist2[1:])
- if (item == dominance):
- hist1 = Answer2Hist(value, "dominance")
- hist2 = Answer2Hist(item[j + 1], "dominance")
- strong_hist = hist1
- weak_hist = hist2
- diff1, ValueOfBorder, err1, err1_relative = search(hist1[1:], hist2[1:])
- hist1=hist1[1:]
- hist2=hist2[2:]
- # setterlist = [diff1,valence_1,diff2,valence_2]
- print("ValNum",ValueOfBorder,ValueOfBorderWeighted, err1, err1_relative)
- errlist.append([err1, err1_relative])
- statlist.append(setterlist)
- elem = statlist[0] #valence
- elem1 = statlist[1] #arousal
- elem2 = statlist[2] #dominance
- for i in statlist:
- for j in i:
- print('statlist,debug',j)
- for index, item in enumerate(participants):
- print(item.name)
- for i, j in enumerate(item.images):
- if j.good >= elem[0]:
- # print("before",j.name, j.good, j.active, j.strong,elem[0])
- participants[index].images[i].binGood = 1
- # print("after",j.name, j.good, j.active, j.strong)
- else:
- participants[index].images[i].binGood = 0
- if j.active >= elem1[0]:
- # print("before",j.name, j.good, j.active, j.strong,elem1[0])
- participants[index].images[i].binActive = 1
- # print("after",j.name, j.good, j.active, j.strong)
- else:
- participants[index].images[i].binActive = 0
- if j.strong >= elem2[0]:
- # print("before", j.name, j.good, j.active, j.strong, elem2[0])
- participants[index].images[i].binStrong = 1
- # print("after", j.name, j.good, j.active, j.strong)
- else:
- participants[index].images[i].binStrong = 0
- print(j.name, "Dec", j.good, j.active, j.strong, "Bin", j.binGood, j.binActive, j.binStrong, "means", elem[0],
- elem1[0], elem2[0])
- MeanGood = 0
- MeanGoodCounter = 0
- MeanEvil = 0
- MeanEvilCounter = 0
- MeanActive = 0
- MeanActiveCounter = 0
- MeanPassive = 0
- MeanPassiveCounter = 0
- MeanStrong = 0
- MeanStrongCounter = 0
- MeanWeak = 0
- MeanWeakCounter = 0
- for i in participants:
- for j in i.images:
- print(j.name, j.binGood)
- if (str(j.name) in img_good) and (j.binGood == 1):
- MeanGood += j.good
- MeanGoodCounter += 1
- if (str(j.name) in img_evil) and (j.binGood == 0):
- MeanEvil += j.good
- MeanEvilCounter += 1
- if (str(j.name) in img_active) and (j.binActive== 1):
- MeanActive += j.active
- MeanActiveCounter += 1
- if (str(j.name) in img_passive) and (j.binActive == 0):
- MeanPassive += j.active
- MeanPassiveCounter += 1
- if (str(j.name) in img_strong) and (j.binStrong== 1):
- MeanStrong += j.strong
- MeanStrongCounter += 1
- if (str(j.name) in img_weak) and (j.binStrong == 0):
- MeanWeak += j.strong
- MeanWeakCounter += 1
- MeanGood = MeanGood / MeanGoodCounter
- MeanEvil = MeanEvil / MeanEvilCounter
- MeanActive = MeanActive/MeanActiveCounter
- MeanPassive = MeanPassive/MeanPassiveCounter
- MeanStrong = MeanStrong/MeanStrongCounter
- MeanWeak = MeanWeak/MeanWeakCounter
- for i in participants: #вектор характеристик по каждой картинке
- for j in i.images:
- # print("anoterDebug,MeanGood,MeanEvi\t", MeanGood, MeanEvil, j.good)
- j.vec.append(j.good - MeanGood)
- j.vec.append(j.good - MeanEvil)
- j.vec.append(j.active - MeanActive)
- j.vec.append(j.active - MeanPassive)
- j.vec.append(j.strong - MeanStrong)
- j.vec.append(j.strong - MeanWeak)
- print(j.vec)
- for i in participants:
- MeanVecGood = 0
- MeanVecGoodCounter = 0
- MeanVecEvil = 0
- MeanVecEvilCounter = 0
- MeanVecActive = 0
- MeanVecActiveCounter = 0
- MeanVecPassive = 0
- MeanVecPassiveCounter = 0
- MeanVecStrong = 0
- MeanVecStrongCounter = 0
- MeanVecWeak = 0
- MeanVecWeakCounter = 0
- for j in i.images:
- if str(j.name) in img_good:
- MeanVecGood += j.vec[0]
- MeanVecGoodCounter += 1
- if str(j.name) in img_evil:
- MeanVecEvil += j.vec[1]
- MeanVecEvilCounter += 1
- if str(j.name) in img_active:
- MeanVecActive+= j.vec[2]
- MeanVecActiveCounter += 1
- if str(j.name) in img_passive:
- MeanVecPassive += j.vec[3]
- MeanVecPassiveCounter += 1
- if str(j.name) in img_strong:
- MeanVecStrong += j.vec[4]
- MeanVecStrongCounter += 1
- if str(j.name) in img_weak:
- MeanVecWeak += j.vec[5]
- MeanVecWeakCounter += 1
- i.vec.append(MeanVecGood / MeanVecGoodCounter)
- i.vec.append(MeanVecEvil / MeanVecEvilCounter)
- i.vec.append(MeanVecActive / MeanVecActiveCounter)
- i.vec.append(MeanVecPassive / MeanVecPassiveCounter)
- i.vec.append(MeanVecStrong / MeanVecStrongCounter)
- i.vec.append(MeanVecWeak / MeanVecStrongCounter)
- print('Participant_Vector', i.name, i.vec)
- print("Meangood = {0}, Meanevil = {1}, ErrGood = {2}, ErrEvil ={3}".format(MeanGood, MeanEvil, errlist[0], errlist[1]))
- print("good,evil,hists", good_hist, evil_hist)
- columns = ['K', 'M', 'O', 'Q', 'S', 'U', 'W', 'Y', 'AA', 'AC', 'AE', 'AG', 'AI']
- for counter, i in enumerate(log_folders): # по всем папкам из директории Log
- workdir = i
- files = os.listdir(dir + '/' + workdir) # Все папки в папке log
- for j in files[1:]: # по всем участникам
- print(j)
- participants.append(Participant(j))
- timecounter = 0
- fileName = j
- wb = load_workbook(dir + '/' + i + '/' + fileName, False) # грузим xlsx
- try:
- sheet2 = wb.get_sheet_by_name('S2')
- sheet2.title = 'Лист2'
- sheet1 = sheet2
- except:
- sheet1 = wb.get_sheet_by_name('Лист2')
- for col in columns:
- for row in range(3, 18):
- cell = col + str(row)
- value = sheet1[cell].value
- try:
- participants[counter].meantime += value
- timecounter += 1
- except:
- pass
- if timecounter == 0:
- print(j)
- break
- participants[counter].meantime = participants[counter].meantime / timecounter
- print(participants[counter].meantime)
- mean = 0
- timecounter = 0
- for i in participants:
- if (i.meantime > 0):
- mean += i.meantime
- timecounter +=1
- mean = mean/timecounter
- for i in participants:
- if(i.meantime==0):
- i.meantime = mean
- for i in participants:
- i.vec.append(mean - i.meantime)
- f = open('kkk.txt', 'w')
- for item in participants:
- f.write("%s\n" % item.vec)
- x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- width = 0.35
- v2 = statlist[0]
- v2errlist = errlist[1]
- v2e = v2errlist[1]
- vvv = "border Ao ="+str(v2[0] + 1) + " " + "err =" + str(v2e)
- basewidth = np.arange(11)
- plt.bar(basewidth, active_hist,width =0.5, color="green")
- plt.bar(basewidth + width, passive_hist,width =0.5, color="blue")
- plt.xticks(x)
- plt.title("Active- green , Passive - blue" + " "+ str(vvv))
- print(vvv)
- print("cum_sum,",cumulativeRHist(evil_hist,7),
- cumulativeLHist(good_hist,7))
- plt.show()
- input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement