def groupingAnalysis():

    groupDiet = dietActInfoRetrv.getGroups(labelsDietType)
    groupAct = dietActInfoRetrv.getGroups(labelsActType)
    print groupAct
    print groupDiet

    dd = {}
    for key1 in groupDiet:
        dd[key1] = {}
        for key2 in groupAct:
            dd[key1][key2] = 0
            for item in groupDiet[key1]:
                if item in groupAct[key2]:
                    dd[key1][key2] += 1
    print dd

    dd = {}
    for key1 in groupAct:
        dd[key1] = {}
        for key2 in groupDiet:
            dd[key1][key2] = 0
            for item in groupAct[key1]:
                if item in groupDiet[key2]:
                    dd[key1][key2] += 1
    print dd
def buildSubAveInfo():
    workbookW = xlwt.Workbook()
    ws = workbookW.add_sheet('AveInfo')

    groupAct = dietActInfoRetrv.getGroups(labelsActType)
    groupDiet = dietActInfoRetrv.getGroups(labelsDietType)

    Age, Gender, Height, Weight, BMI, FatFree, FatMass, PercFat, Vo2max = slpInfoRetrv.getDemoGInfo(
    )
    SlpHours = slpInfoRetrv.getSlpHours()
    MedianHR = slpInfoRetrv.getMedianHR()
    MedianHRBefore = slpInfoRetrv.getMedianHRBefore()
    MedianHRAfter = slpInfoRetrv.getMedianHRAfter()

    titles = [
        'SubjId', 'ActGroup', 'DietGroup', 'HoursSleep', 'MedianHR',
        'MedianHRBefore', 'MedianHRAfter', 'age', 'gender', 'height', 'weight',
        'BMI', 'FatFreeMass', 'FatMass', 'PercFat', 'vo2max'
    ]

    for i in range(len(titles)):
        ws.write(0, i, titles[i])

    rowW = 1

    for index in range(len(sleep_list)):
        ws.write(rowW, 0, sleep_list[index])

        for key in groupAct:
            if sleep_list[index] in groupAct[key]:
                ws.write(rowW, 1, key)
                break

        for key in groupDiet:
            if sleep_list[index] in groupDiet[key]:
                ws.write(rowW, 2, key)
                break

        ws.write(rowW, 1 + 2, SlpHours[index])
        ws.write(rowW, 2 + 2, MedianHR[index])
        ws.write(rowW, 3 + 2, MedianHRBefore[index])
        ws.write(rowW, 4 + 2, MedianHRAfter[index])
        ws.write(rowW, 5 + 2, Age[index])
        ws.write(rowW, 6 + 2, Gender[index])
        ws.write(rowW, 7 + 2, Height[index])
        ws.write(rowW, 8 + 2, Weight[index])
        ws.write(rowW, 9 + 2, BMI[index])
        ws.write(rowW, 10 + 2, FatFree[index])
        ws.write(rowW, 11 + 2, FatMass[index])
        ws.write(rowW, 12 + 2, PercFat[index])
        ws.write(rowW, 13 + 2, Vo2max[index])
        rowW += 1

    ws2 = workbookW.add_sheet('DietTF')

    row_labels = utilise.itemDict2list(dataGen4DietAct.genDietTypeDict())

    X = utilise.normArray(dataGen4DietAct.genDietTypeTFArray())

    ws2.write(0, 0, 'SubjId')
    ws2.write(0, 1, 'DietGroup')

    for i in range(len(row_labels)):
        ws2.write(0, i + 2, row_labels[i])

    rowW = 1
    for index in range(len(available_list)):
        ws2.write(rowW, 0, available_list[index])

        for key in groupDiet:
            if available_list[index] in groupDiet[key]:
                ws2.write(rowW, 1, key)
                break

        for i in range(len(row_labels)):
            ws2.write(rowW, i + 2, X[index][i])

        rowW += 1

    ws3 = workbookW.add_sheet('ActTF')

    row_labels = utilise.itemDict2list(dataGen4DietAct.genActTypeDict())

    X = utilise.normArray(dataGen4DietAct.genActTypeTFArray())

    ws3.write(0, 0, 'SubjId')
    ws3.write(0, 1, 'ActGroup')

    for i in range(len(row_labels)):
        ws3.write(0, i + 2, row_labels[i])

    rowW = 1
    for index in range(len(available_list)):
        ws3.write(rowW, 0, available_list[index])

        for key in groupAct:
            if available_list[index] in groupAct[key]:
                ws3.write(rowW, 1, key)
                break

        for i in range(len(row_labels)):
            ws3.write(rowW, i + 2, X[index][i])

        rowW += 1

    workbookW.save('SubAveInfo.xls')
def genDemoInfoDietGroups():
    groupDiet = dietActInfoRetrv.getGroups(labelsDietType)
    Age, Gender, Height, Weight, BMI, FatFree, FatMass, PercFat, Vo2max = slpInfoRetrv.getDemoGInfo(
    )
    SlpHours = slpInfoRetrv.getSlpHours()
    MedianHR = slpInfoRetrv.getMedianHR()
    MedianHRBefore = slpInfoRetrv.getMedianHRBefore()
    MedianHRAfter = slpInfoRetrv.getMedianHRAfter()

    # write the info to excel file
    workbookW = xlwt.Workbook()
    ws = workbookW.add_sheet('sheet1')
    rowW = 0

    titles = [
        'age', 'men', 'women', 'height', 'weight', 'BMI', 'fat_free',
        'fat_mass', 'perc_fat', 'vo2max', 'slpHours', 'medianHR',
        'medianHRBefore', 'medianHRAfter'
    ]
    col = 0
    for item in titles:
        ws.write(rowW, col, item)
        col += 1
    rowW += 1

    demoDict = {}

    for key in groupDiet:

        demoDict[key] = {}

        temp_Age = []
        temp_Gender = []
        temp_Height = []
        temp_Weight = []
        temp_BMI = []
        temp_FatFree = []
        temp_FatMass = []
        temp_PercFat = []
        temp_Vo2max = []
        temp_slpHours = []
        temp_MedianHR = []
        temp_MedianHRBefore = []
        temp_MedianHRAfter = []

        for index in range(len(sleep_list)):
            if sleep_list[index] in groupDiet[key]:
                temp_Age.append(Age[index])
                temp_Gender.append(Gender[index])
                temp_Height.append(Height[index])
                temp_Weight.append(Weight[index])
                temp_BMI.append(BMI[index])
                temp_FatFree.append(FatFree[index])
                temp_FatMass.append(FatMass[index])
                temp_PercFat.append(PercFat[index])
                temp_Vo2max.append(Vo2max[index])
                temp_slpHours.append(SlpHours[index])
                temp_MedianHR.append(MedianHR[index])
                temp_MedianHRBefore.append(MedianHRBefore[index])
                temp_MedianHRAfter.append(MedianHRAfter[index])

        demoDict[key]['age'] = sum(temp_Age) / float(len(temp_Age))
        demoDict[key]['men'] = temp_Gender.count(1.0)
        demoDict[key]['women'] = temp_Gender.count(0.0)
        demoDict[key]['height'] = sum(temp_Height) / float(len(temp_Height))
        demoDict[key]['weight'] = sum(temp_Weight) / float(len(temp_Weight))
        demoDict[key]['BMI'] = sum(temp_BMI) / float(len(temp_BMI))
        demoDict[key]['fat_free'] = sum(temp_FatFree) / float(
            len(temp_FatFree))
        demoDict[key]['fat_mass'] = sum(temp_FatMass) / float(
            len(temp_FatMass))
        demoDict[key]['perc_fat'] = sum(temp_PercFat) / float(
            len(temp_PercFat))
        demoDict[key]['vo2max'] = sum(temp_Vo2max) / float(len(temp_Vo2max))
        demoDict[key]['slpHours'] = sum(temp_slpHours) / float(
            len(temp_slpHours))
        demoDict[key]['medianHR'] = sum(temp_MedianHR) / float(
            len(temp_MedianHR))
        demoDict[key]['medianHRBefore'] = sum(temp_MedianHRBefore) / float(
            len(temp_MedianHRBefore))
        demoDict[key]['medianHRAfter'] = sum(temp_MedianHRAfter) / float(
            len(temp_MedianHRAfter))

        col = 0
        for item in titles:
            if item in demoDict[key]:
                ws.write(rowW, col, demoDict[key][item])
                col += 1
        rowW += 1

    workbookW.save('tempDietGroupDemo.xls')
    print demoDict