Skip to content

Commit

Permalink
Finally working
Browse files Browse the repository at this point in the history
  • Loading branch information
grigsos committed Mar 28, 2023
1 parent 934b391 commit 6aa3d0b
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 35 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 11 additions & 3 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@ def main():
logging.info("Program started")
start_time = time.time()

#initialisation
mainFolderPath,configFileMain,hashL, xlsxList = initialisation.main()
levelListM,inputsSubListM, inputsDimListM, inputsInterDimensionalListM, kripSimplifiedListM=data_import.main(mainFolderPath,configFileMain, xlsxList)

#get data
levelListM,inputsSubListM, inputsDimListM, inputsInterDimensionalListM, kripSimpleListOutMain, kripInputListOutMain=data_import.main(mainFolderPath,configFileMain, xlsxList)

#process data
outputDataframeMain, DimConsistencyListMain, DimWeightListMain=data_processing.main(inputsSubListM, inputsDimListM)

#report generation
from src.methods import report_generator # to make global variable work
report_generator.main(mainFolderPath,configFileMain,len(xlsxList),levelListM,inputsSubListM, inputsDimListM,
inputsInterDimensionalListM, kripSimplifiedListM,outputDataframeMain,
DimConsistencyListMain,DimWeightListMain)
inputsInterDimensionalListM,outputDataframeMain,
DimConsistencyListMain,DimWeightListMain,
kripSimpleListOutMain, kripInputListOutMain)


logging.info("Program ended")
logging.info("Program runtime: " + f"{(time.time() - start_time):.2f}" + " seconds")
Expand Down
36 changes: 28 additions & 8 deletions src/methods/data_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ def main(inputFolder, configFile,xlList):

worksheetList = get_first_X_worksheet_names(xlList[0], len(subCompNumList))

levelList,weightList, dimensionList, interDimensionalList, kripSimplifiedList=start_import(xlList, worksheetList,
levelList,weightList, dimensionDF, interDimensionalList=start_import(xlList, worksheetList,
cC, sR,subCompNumList,cLG,lC,special_sheets_list)
#print(x)
kripSimpleListOut, kripInputListOut = iterate_through_simple_krip(weightList, 17, 3,dimensionDF)


logging.info("M - Data import finished successfully")
return levelList,weightList, dimensionList, interDimensionalList, kripSimplifiedList
return levelList,weightList, dimensionDF, interDimensionalList, kripSimpleListOut, kripInputListOut


def number_of_comparisions(n):
Expand All @@ -53,14 +53,12 @@ def start_import(pathList, worksheetList, comparisonColumn, startingRow,subCompy

lList,wList, dimList, intDimList = get_data(pathList, worksheetList, levelColumn, comparisonColumn, startingRow,subCompyList,comparisonLevelGap,SSList)

#print(get_krippendorff_DF(pd.concat(wList,axis=0)))
kripSimpleList = []



#verticalAdd= pd.concat(wList,axis=0) #join all dataframes vertically
#print(get_krippendorff_DF(verticalAdd))
#print(get_krippendorff_DF(pd.concat(lList,axis=0)))
return lList , wList, dimList, intDimList, kripSimpleList
return lList , wList, dimList, intDimList

def concat_dataframes(list_of_lists):
concatenated_dfs = []
Expand Down Expand Up @@ -154,4 +152,26 @@ def replace_with_lists(num,odd_groups,my_wantedList): #replace with lists
for sublist in odd_groups:
if num in sublist:
return my_wantedList[odd_groups.index(sublist)]
raise ValueError(f"Number {num} not found in any sublist")
raise ValueError(f"Number {num} not found in any sublist")

def iterate_through_simple_krip(weightList, nMax, nMin,dimDF):
kripSimpleList = []
kripInputList = []

for x in range(nMin,nMax+1,2):
workInputList=[]
workSimpleList=[]
mWL,oG=simplify_krip(x)
onlyDimensionSImplified=dimDF.applymap(lambda x: replace_with_lists(x, oG, mWL))
workInputList.append(onlyDimensionSImplified)
workSimpleList.append(get_krippendorff_DF(onlyDimensionSImplified))

for i,df in enumerate(weightList):
dimensionSimplifiedListWork=df.applymap(lambda x: replace_with_lists(x, oG, mWL))
workInputList.append(dimensionSimplifiedListWork)
workSimpleList.append(get_krippendorff_DF(dimensionSimplifiedListWork))

kripSimpleList.append(workSimpleList)
kripInputList.append(workInputList)

return kripSimpleList, kripInputList
44 changes: 30 additions & 14 deletions src/methods/report_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

##############Main function##############
def main(mFP,cFM,nList,levelListSM,weightListSM, dimensionListSM,
interDimensionalListSM, kripSimplifiedListSM, outputDataframeSM,
subDimConsistencyListSM,DimWeightListSM):
interDimensionalListSM, outputDataframeSM,
subDimConsistencyListSM,DimWeightListSM,
kripSimpleListOut, kripInputListOut):
logging.info("M - Report generation started")
outFolderPath = check_output_folder(cFM.get("Settings", "outputFolder"),mFP) #check if output folder exists
listDimension,combinedList=get_subsection(cFM) #get subsections and dimensions from config file
Expand All @@ -24,13 +25,11 @@ def main(mFP,cFM,nList,levelListSM,weightListSM, dimensionListSM,
csv_interdim_comparisions_input(interDimensionalListSM,sOF) #generates csv file with interdimensional comparisions
csv_calculated_weights(outputDataframeSM,sOF,combinedList,DimWeightListSM,listDimension) #generates csv file with calculated weights
csv_consistency_indexes(subDimConsistencyListSM,sOF,listDimension) #generates csv file with consistency indexes

csv_krip_inputs(kripInputListOut,sOF) #generates csv file with krippendorff inputs
csv_krip_outputs(kripSimpleListOut,sOF,listDimension) #generates csv file with krippendorff outputs
logging.info("M - Report generation finished successfully")
return None




##############Sub functions##############

#File that generates the report
Expand Down Expand Up @@ -62,7 +61,7 @@ def make_folder(oFP,num_participants):

if not os.path.exists(os.path.join(oFP, folder_name)):
os.makedirs(os.path.join(oFP, folder_name))
logging.info("Directory created successfully!")
logging.info("Report folder created successfully!")
else:
logging.info("Directory already exists!")
logging.log(logging.CRITICAL, "Program aborted, Program run twice in single second")
Expand Down Expand Up @@ -94,10 +93,15 @@ def csv_levels(df,sOF,combL):
verticalAdd.to_csv(outPath, index=True)
return None

def csv_kripke_simplified(kSL,sOF): #doesn't work
verticalAdd= pd.concat(kSL,axis=0)
outPath=os.path.join(sOF, "kripke_simplified.csv")
verticalAdd.to_csv(outPath, index=True)
def csv_krip_inputs(kripInputListOut,sOF): #from most convoluting to least (aka higher alpha to lower)
finalDF = pd.DataFrame()
for i in range(len(kripInputListOut)):
workingDF = pd.DataFrame()
for x in range(len(kripInputListOut[i])):
workingDF = pd.concat([workingDF,kripInputListOut[i][x]],axis=0)
finalDF = pd.concat([finalDF,workingDF],axis=1)
outPath=os.path.join(sOF, "inputs_krip.csv")
finalDF.to_csv(outPath, index=True)
return None

def csv_calculated_weights(df,sOF,combL,DimWeightListSMFunc,listDimensionFunc):
Expand All @@ -120,11 +124,23 @@ def csv_consistency_indexes(sDCL,sOF,listDimensionFunc):
sDCL.to_csv(outPath, index=True)
return None


def csv_interdim_comparisions_input(iDL,sOF):
outPath=os.path.join(sOF, "inputs_interdimensions.csv")
iDL.to_csv(outPath, index=True)
return None

def csv_base_math_model():
return None
def csv_krip_outputs(kripSimpleListOut, sOF, listDimensionFunc):
indexToUse = ["Dimensions"] + listDimensionFunc
cols = list(range(3, len(kripSimpleListOut)*2+2, 2)) #list of columns to drop
print(cols)

finalDF = pd.DataFrame()
for i in range(len(kripSimpleListOut)):
col_name=cols[i]
df=pd.DataFrame({col_name: kripSimpleListOut[i]})
finalDF=pd.concat([finalDF,df],axis=1)

finalDF.index=indexToUse
outPath=os.path.join(sOF, "outputs_krip.csv")
finalDF.to_csv(outPath, index=True)
return None
37 changes: 27 additions & 10 deletions tests/generic_module_practice.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
import ast
import configparser
import krippendorff
import pandas as pd
import numpy as np

config = configparser.ConfigParser()
config.read('config.ini')
def test_krippendorff_alpha():
df = pd.DataFrame({'rater1': ["N/A", "N/A", "N/A", "N/A", "N/A",3,4,1,2,1,1,3,3,"N/A",3],
'rater2': [1, "N/A", 2, 1, 3,3,4,3,"N/A","N/A","N/A","N/A","N/A","N/A","N/A"],
'rater3': ["N/A", "N/A", 2, 1, 3,4,4,"N/A",2,1,1,3,3,"N/A",4]})

# Create an empty list to store the dictionaries
special_sheets_list = []
data = df.T.values.tolist()

# Loop over all items in the special sheets dictionary and convert the string values to their appropriate data types using ast.literal_eval()
for key in config['Special_sheets']:
special_sheets_list.append(eval(config['Special_sheets'][key]))
data_tuple = tuple(' '.join(map(str, row)) for row in data)

reliability_data_str = (
"3 0.5 0.5 1 0.5 0.25 0.25 0.25 0.25 0.3333 2 0.5 3 2 0.3333", # coder A
"1 0.5 0.3333 0.5 1 0.5 3 1 1 2 3 4 1 1 1", # coder B
"2 0.3333 0.1667 1 2 2 0.2 0.3333 0.3333 0.3333 1 0.3333 3 3 1", # coder C
)

print(reliability_data_str)
print(data_tuple)
newlistconvert =[[np.nan if (v == "*" or v=="N/A") else v for v in coder.split()] for coder in data_tuple]
reliability_data = [[np.nan if (v == "*" or v=="N/A") else v for v in coder.split()] for coder in reliability_data_str]


#assert newlistconvert ==reliability_data

print("Krippendorff's alpha for nominal metric: ", krippendorff.alpha(reliability_data=reliability_data,
level_of_measurement="nominal"))
#assert (krippendorff.alpha(reliability_data=newlistconvert,level_of_measurement="nominal"))== (krippendorff.alpha(reliability_data=reliability_data, level_of_measurement="nominal"))

print(special_sheets_list)

0 comments on commit 6aa3d0b

Please sign in to comment.