Beispiel #1
0
def raw_mr_apa_table_excel(mod_raw_data_df, output_df):
    mod_output_df = output_df[[
        "Variable", "B", "95% CI B", "beta", "t", "adjusted_pvalues"
    ]]

    pd.options.mode.chained_assignment = None
    mod_output_df[["B", "beta", "t"]] = mod_output_df[[
        "B", "beta", "t"
    ]].applymap(lambda x: "{:.2f}".format(x))

    mod_output_df["adjusted_pvalues"] = mod_output_df["adjusted_pvalues"].map(
        helper_funcs.pvalue_formatting)

    mod_output_df.rename(columns={
        "95% CI B": "95% CI",
        "adjusted_pvalues": "p"
    },
                         inplace=True)
    mod_output_df.loc[
        0,
        "beta"] = ""  # removes the beta value for constant as it is always 0

    wb = Workbook()
    ws = wb.active

    ws.append(list(mod_output_df.columns))

    for row in dataframe_to_rows(mod_output_df, index=False, header=False):
        ws.append(row)

    for cell in ws[1]:
        cell.font = global_vars.font_header

    for cell in ws[1] + ws[len(mod_output_df) + 1]:
        cell.border = Border(top=global_vars.border_APA,
                             bottom=global_vars.border_APA)
    for cell in ws[len(mod_output_df) + 1]:
        cell.border = Border(bottom=global_vars.border_APA)

    for row in range(1, len(mod_output_df) + 2):
        for cell in ws[row]:
            cell.alignment = global_vars.alignment_center

    table_notes = [
        "R squared adjusted = {R}".format(
            R="{:.2f}".format(output_df["R2adj"][0])),
        "Dependent Variable: {DV}".format(DV=global_vars.raw_mr_outcomevar)
    ]
    helper_funcs.add_table_notes(ws, table_notes)

    helper_funcs.savefile(wb=wb)
Beispiel #2
0
def pvalues_table(mod_raw_data_df, output_df):
	wb = Workbook()
	ws = wb.active

	for row in dataframe_to_rows(output_df, index=False, header=True):
		ws.append(row)

	for row in range(1, len(output_df)+2):
		for cell in ws[row]:
			cell.alignment = global_vars.alignment_center

	for cell in ws[1]:
		cell.font = global_vars.font_bold

	for cell in ws[1] + ws[len(output_df)+1]:
		cell.border = Border(bottom=global_vars.border_APA)

	helper_funcs.add_table_notes(ws, [])

	wb.save(filename=global_vars.output_filename + ".xlsx")
Beispiel #3
0
def spss_mr_apa_table_excel(mod_raw_data_df, output_df):
    output_df.drop(columns=["pvalues"], inplace=True)
    pd.options.mode.chained_assignment = None
    output_df["adjusted_pvalues"] = output_df["adjusted_pvalues"].map(
        helper_funcs.pvalue_formatting)
    output_df.rename(columns={"adjusted_pvalues": "p"}, inplace=True)

    wb = Workbook()
    ws = wb.active

    for row in dataframe_to_rows(output_df, index=False, header=True):
        ws.append(row)

    for cell in ws[1]:
        cell.font = global_vars.font_header

    for row in range(1, len(output_df) + 2):
        for cell in ws[row]:
            cell.alignment = global_vars.alignment_center

    for cell in ws[1]:
        cell.border = Border(bottom=global_vars.border_APA,
                             top=global_vars.border_APA)

    for cell in ws[len(output_df) + 1]:
        cell.border = Border(bottom=global_vars.border_APA)

    DV_cell = mod_raw_data_df[mod_raw_data_df.columns[0]][
        len(mod_raw_data_df[mod_raw_data_df.columns[0]]) - 1]
    DV = DV_cell[DV_cell.find(":") + 2:]
    table_notes = [
        "R squared adjusted = X.XX", "Dependent variable: {}".format(DV)
    ]
    if output_df["95% CI"][0] == "[,]":
        table_notes.append(
            "Confidence intervals were not found in the SPSS table. Please add them to your SPSS table and re-run the program or add them manually."
        )

    helper_funcs.add_table_notes(ws, table_notes)

    helper_funcs.savefile(wb=wb)
Beispiel #4
0
def raw_pairttest_apa_table_excel(mod_raw_data_df, output_df):
    output_df.drop(columns=["pvalues"], inplace=True)
    apa_table_df = output_df[[
        "Variable", "Time1_Mean", "Time1_SD", "Time2_Mean", "Time2_SD",
        "Degrees of Freedom", "t", global_vars.effect_size_choice,
        "adjusted_pvalues"
    ]]

    pd.options.mode.chained_assignment = None
    apa_table_df[list(apa_table_df.columns)[1:-1]] = apa_table_df[list(
        apa_table_df.columns)[1:-1]].applymap(lambda x: "{:.2f}".format(x))

    apa_table_df["adjusted_pvalues"] = apa_table_df["adjusted_pvalues"].map(
        helper_funcs.pvalue_formatting)
    pd.options.mode.chained_assignment = "warn"

    wb = Workbook()
    ws = wb.active

    ws.cell(row=1, column=1).value = "Variable"
    ws.merge_cells("A1:A2")
    ws.cell(row=1, column=1).font = global_vars.font_header

    ws.cell(row=1, column=2).value = "Time 1"
    ws.merge_cells("B1:C1")

    ws.cell(row=1, column=4).value = "Time 2"
    ws.merge_cells("D1:E1")

    ws.cell(row=1, column=6).value = "df"
    ws.merge_cells("F1:F2")
    ws.cell(row=1, column=6).font = global_vars.font_header

    ws.cell(row=1, column=7).value = "t"
    ws.merge_cells("G1:G2")
    ws.cell(row=1, column=7).font = global_vars.font_header

    ws.cell(row=1, column=8).value = global_vars.effect_size_choice
    ws.merge_cells("H1:H2")
    ws.cell(row=1, column=8).font = global_vars.font_header

    ws.cell(row=1, column=9).value = "p"
    ws.merge_cells("I1:I2")
    ws.cell(row=1, column=9).font = global_vars.font_header

    for col in range(2, 5, 2):
        ws.cell(row=2, column=col).value = "M"
        ws.cell(row=2, column=col).font = global_vars.font_header
        ws.cell(row=2, column=col + 1).value = "SD"
        ws.cell(row=2, column=col + 1).font = global_vars.font_header

    for row in dataframe_to_rows(apa_table_df, index=False, header=False):
        ws.append(row)

    for row in range(1, len(apa_table_df) + 3):
        for cell in ws[row]:
            cell.alignment = global_vars.alignment_center

    for cell in ws[2] + ws[len(apa_table_df) + 2]:
        cell.border = Border(bottom=global_vars.border_APA)

    for cell in ws[1]:
        cell.border = Border(top=global_vars.border_APA)

    if global_vars.effect_size_choice == "None":
        ws.delete_cols(8)

    helper_funcs.add_table_notes(ws, [])

    helper_funcs.savefile(wb=wb)
def spss_corr_apa_table_excel(mod_raw_data_df, output_df):
    # could very easily use the summ_corr_apa_table function here as I pass identical data - seperate is preferred, however, as might be updated/adjusted in the future
    correlation_label = mod_raw_data_df.iloc[0, 1]
    variables_list = list(mod_raw_data_df.columns)[2:]

    if global_vars.corr_table_triangle == "Upper triangle":
        header_cols = variables_list[1:]
        header_rows = variables_list[:-1]
    elif global_vars.corr_table_triangle == "Lower triangle":
        header_cols = variables_list[:-1]
        header_rows = variables_list[1:]
    elif global_vars.corr_table_triangle == "Both":
        header_cols, header_rows = variables_list, variables_list

    wb = Workbook()
    ws = wb.active

    ws.append([""] + header_cols)
    for ind, var in enumerate(header_rows):
        ws.cell(row=ind + 2, column=1).value = var

    inside_loop_ind_start = 2
    for outside_loop_ind in range(2, len(header_rows) + 2):

        if global_vars.corr_table_triangle == "Upper triangle" or global_vars.corr_table_triangle == "Both":
            outside_loop_var = ws.cell(row=outside_loop_ind, column=1).value
        elif global_vars.corr_table_triangle == "Lower triangle":
            outside_loop_var = ws.cell(row=1, column=outside_loop_ind).value

        for inside_loop_ind in range(inside_loop_ind_start,
                                     len(header_cols) + 2):

            if global_vars.corr_table_triangle == "Upper triangle" or global_vars.corr_table_triangle == "Both":
                inside_loop_var = ws.cell(row=1, column=inside_loop_ind).value
            elif global_vars.corr_table_triangle == "Lower triangle":
                inside_loop_var = ws.cell(row=inside_loop_ind, column=1).value

            if outside_loop_var == inside_loop_var:
                ws.cell(row=outside_loop_ind, column=inside_loop_ind).value = 1
            else:
                # here query method is not preferred as it is not only slower as it is much smaller dataset but also cannot refer to two different vaiables (colname and val)
                df_filtered = output_df[(
                    (output_df["var1"] == outside_loop_var) &
                    (output_df["var2"] == inside_loop_var)) | (
                        (output_df["var1"] == inside_loop_var) &
                        (output_df["var2"] == outside_loop_var))].iloc[0]
                r = df_filtered[correlation_label]
                p = df_filtered["adjusted_pvalues"]
                r = helper_funcs.correlations_format_val(r, p)

                if global_vars.corr_table_triangle == "Upper triangle" or global_vars.corr_table_triangle == "Both":
                    ws.cell(row=outside_loop_ind,
                            column=inside_loop_ind).value = r
                elif global_vars.corr_table_triangle == "Lower triangle":
                    ws.cell(row=inside_loop_ind,
                            column=outside_loop_ind).value = r

        if global_vars.corr_table_triangle != "Both":
            inside_loop_ind_start += 1

    for row in range(1, len(header_rows) + 2):
        for cell in ws[row]:
            cell.alignment = global_vars.alignment_center

    for cell in ws[1]:
        cell.font = global_vars.font_header

    for row in range(2, len(header_rows) + 2):
        ws.cell(row=row, column=1).font = global_vars.font_header

    for cell in ws[len(header_rows) + 1]:
        cell.border = Border(bottom=global_vars.border_APA)

    for cell in ws[1]:
        cell.border = Border(top=global_vars.border_APA,
                             bottom=global_vars.border_APA)

    table_notes = ["**p < 0.01", "*p < {}".format(global_vars.alpha_threshold)]
    table_notes.append(
        "Correlation Coefficient used: {}".format(correlation_label))
    helper_funcs.add_table_notes(ws, table_notes)

    helper_funcs.savefile(wb=wb)
Beispiel #6
0
def raw_corr_apa_table_noCIs_excel(mod_raw_data_df, output_df):
    variables_list = list(
        mod_raw_data_df.columns
    )  #local var and .columns preferred to minimize use of global scope

    wb = Workbook()
    ws = wb.active

    if global_vars.corr_table_triangle == "Upper triangle":
        header_cols = variables_list[1:]
        header_rows = variables_list[:-1]
    elif global_vars.corr_table_triangle == "Lower triangle":
        header_cols = variables_list[:-1]
        header_rows = variables_list[1:]
    elif global_vars.corr_table_triangle == "Both":
        header_cols = [x for x in variables_list]
        header_rows = [x for x in variables_list]

    ws.append([""] + header_cols)
    for ind, var in enumerate(header_rows):
        ws.cell(row=ind + 2, column=1).value = var

    inside_loop_ind_start = 2
    for outside_loop_ind in range(2, len(header_rows) + 2):

        if global_vars.corr_table_triangle == "Upper triangle" or global_vars.corr_table_triangle == "Both":
            outside_loop_var = ws.cell(row=outside_loop_ind, column=1).value
        elif global_vars.corr_table_triangle == "Lower triangle":
            outside_loop_var = ws.cell(row=1, column=outside_loop_ind).value

        for inside_loop_ind in range(inside_loop_ind_start,
                                     len(header_cols) + 2):

            if global_vars.corr_table_triangle == "Upper triangle" or global_vars.corr_table_triangle == "Both":
                inside_loop_var = ws.cell(row=1, column=inside_loop_ind).value
            elif global_vars.corr_table_triangle == "Lower triangle":
                inside_loop_var = ws.cell(row=inside_loop_ind, column=1).value

            if outside_loop_var == inside_loop_var:
                ws.cell(row=outside_loop_ind, column=inside_loop_ind).value = 1
            else:
                # here query method is not preferred as it is not only slower as it is much smaller dataset but also cannot refer to two different vaiables (colname and val)
                df_filtered = output_df[(
                    (output_df["Variable1"] == outside_loop_var) &
                    (output_df["Variable2"] == inside_loop_var)) | (
                        (output_df["Variable1"] == inside_loop_var) &
                        (output_df["Variable2"] == outside_loop_var))].iloc[0]
                r = df_filtered["Correlation_Coefficient"]
                p = df_filtered["adjusted_pvalues"]
                r = helper_funcs.correlations_format_val(r, p)

                if global_vars.corr_table_triangle == "Upper triangle" or global_vars.corr_table_triangle == "Both":
                    ws.cell(row=outside_loop_ind,
                            column=inside_loop_ind).value = r
                elif global_vars.corr_table_triangle == "Lower triangle":
                    ws.cell(row=inside_loop_ind,
                            column=outside_loop_ind).value = r

        if global_vars.corr_table_triangle != "Both":
            inside_loop_ind_start += 1

    for row in range(1, len(header_rows) + 2):
        for cell in ws[row]:
            cell.alignment = global_vars.alignment_center

    for cell in ws[1] + ws["A"]:
        cell.font = global_vars.font_header

    for cell in ws[1]:
        cell.border = Border(top=global_vars.border_APA,
                             bottom=global_vars.border_APA)
    for cell in ws[len(header_rows) + 1]:
        cell.border = Border(bottom=global_vars.border_APA)

    table_notes = [
        "Correlation coefficient used: {}".format(
            list(global_vars.master_dict.keys())[list(
                global_vars.master_dict.values()).index(
                    global_vars.raw_corr_type)])
    ]
    table_notes.append("**p < 0.01")
    table_notes.append("*p < {}".format(global_vars.alpha_threshold))
    helper_funcs.add_table_notes(ws, table_notes)

    helper_funcs.savefile(wb=wb)
Beispiel #7
0
def raw_corr_apa_table_withCIs_excel(mod_raw_data_df, output_df):
    variables_list = list(
        mod_raw_data_df.columns
    )  # local var and .columns preferred to minimize use of global scope

    wb = Workbook()
    ws = wb.active

    if global_vars.corr_table_triangle == "Upper triangle":
        header_cols = variables_list[1:]
        header_rows = variables_list[:-1]
    elif global_vars.corr_table_triangle == "Lower triangle":
        header_cols = variables_list[:-1]
        header_rows = variables_list[1:]
    elif global_vars.corr_table_triangle == "Both":
        header_cols = [x for x in variables_list]
        header_rows = [x for x in variables_list]
    # this adds an empty row where the CIs will be
    # the code looks slightly wrong as it return a None array but it works
    [header_rows.insert(x, "") for x in range(1, len(header_rows) * 2, 2)]

    ws.append([""] + header_cols)
    for ind, var in enumerate(header_rows):
        ws.cell(row=ind + 2, column=1).value = var

    if global_vars.corr_table_triangle == "Upper triangle":
        inside_loop_ind_start = 2
        for outside_loop_ind in range(2, len(header_rows) + 2, 2):
            outside_loop_var = ws.cell(row=outside_loop_ind, column=1).value
            for inside_loop_ind in range(inside_loop_ind_start,
                                         len(header_cols) + 2):
                inside_loop_var = ws.cell(row=1, column=inside_loop_ind).value
                # here query method is not preferred as it is not only slower as it is much smaller dataset but also cannot refer to two different vaiables (colname and val)
                df_filtered = output_df[(
                    (output_df["Variable1"] == outside_loop_var) &
                    (output_df["Variable2"] == inside_loop_var)) | (
                        (output_df["Variable1"] == inside_loop_var) &
                        (output_df["Variable2"] == outside_loop_var))].iloc[0]
                r = df_filtered["Correlation_Coefficient"]
                p = df_filtered["adjusted_pvalues"]
                r = helper_funcs.correlations_format_val(r, p)
                ci_low, ci_high = helper_funcs.correlations_format_val(
                    df_filtered["CI_low"]
                ), helper_funcs.correlations_format_val(df_filtered["CI_high"])
                ws.cell(row=outside_loop_ind, column=inside_loop_ind).value = r
                ws.cell(row=outside_loop_ind + 1, column=inside_loop_ind
                        ).value = "[" + ci_low + ", " + ci_high + "]"
            inside_loop_ind_start += 1
    elif global_vars.corr_table_triangle == "Lower triangle":
        inside_loop_ind_start = 2
        for outside_loop_ind in range(2, len(header_cols) + 2):
            outside_loop_var = ws.cell(row=1, column=outside_loop_ind).value
            for inside_loop_ind in range(inside_loop_ind_start,
                                         len(header_rows) + 2, 2):
                inside_loop_var = ws.cell(row=inside_loop_ind, column=1).value
                # here query method is not preferred as it is not only slower as it is much smaller dataset but also cannot refer to two different vaiables (colname and val)
                df_filtered = output_df[(
                    (output_df["Variable1"] == outside_loop_var) &
                    (output_df["Variable2"] == inside_loop_var)) | (
                        (output_df["Variable1"] == inside_loop_var) &
                        (output_df["Variable2"] == outside_loop_var))].iloc[0]
                r = df_filtered["Correlation_Coefficient"]
                p = df_filtered["adjusted_pvalues"]
                r = helper_funcs.correlations_format_val(r, p)
                ci_low, ci_high = helper_funcs.correlations_format_val(
                    df_filtered["CI_low"]
                ), helper_funcs.correlations_format_val(df_filtered["CI_high"])
                ws.cell(row=inside_loop_ind, column=outside_loop_ind).value = r
                ws.cell(row=inside_loop_ind + 1, column=outside_loop_ind
                        ).value = "[" + ci_low + ", " + ci_high + "]"
            inside_loop_ind_start += 2
    elif global_vars.corr_table_triangle == "Both":
        inside_loop_ind_start = 2
        for outside_loop_ind in range(2, len(header_cols) + 2):
            outside_loop_var = ws.cell(row=1, column=outside_loop_ind).value
            for inside_loop_ind in range(inside_loop_ind_start,
                                         len(header_rows) + 2, 2):
                inside_loop_var = ws.cell(row=inside_loop_ind, column=1).value
                if outside_loop_var == inside_loop_var:
                    ws.cell(row=inside_loop_ind,
                            column=outside_loop_ind).value = "1"
                else:
                    # here query method is not preferred as it is not only slower as it is much smaller dataset but also cannot refer to two different vaiables (colname and val)
                    df_filtered = output_df[
                        ((output_df["Variable1"] == outside_loop_var) &
                         (output_df["Variable2"] == inside_loop_var)) |
                        ((output_df["Variable1"] == inside_loop_var) &
                         (output_df["Variable2"] == outside_loop_var))].iloc[0]
                    r = df_filtered["Correlation_Coefficient"]
                    p = df_filtered["adjusted_pvalues"]
                    r = helper_funcs.correlations_format_val(r, p)
                    ci_low, ci_high = helper_funcs.correlations_format_val(
                        df_filtered["CI_low"]
                    ), helper_funcs.correlations_format_val(
                        df_filtered["CI_high"])
                    ws.cell(row=inside_loop_ind,
                            column=outside_loop_ind).value = r
                    ws.cell(row=inside_loop_ind + 1, column=outside_loop_ind
                            ).value = "[" + ci_low + ", " + ci_high + "]"

    for row in range(2, len(header_rows) + 1, 2):
        ws.merge_cells(start_row=row,
                       start_column=1,
                       end_row=row + 1,
                       end_column=1)
    if global_vars.corr_table_triangle == "Both":
        for col in range(2, len(header_cols) + 2):
            ws.merge_cells(start_row=col * 2 - 2,
                           start_column=col,
                           end_row=col * 2 - 1,
                           end_column=col)

    for row in range(1, len(header_rows) + 2):
        for cell in ws[row]:
            cell.alignment = global_vars.alignment_center

    for cell in ws[1] + ws["A"]:
        cell.font = global_vars.font_header

    for row in range(3, len(header_rows) + 2, 2):
        for cell in ws[row]:
            cell.font = Font(size=9)

    for cell in ws[1]:
        cell.border = Border(top=global_vars.border_APA,
                             bottom=global_vars.border_APA)
    for cell in ws[len(header_rows) + 1]:
        cell.border = Border(bottom=global_vars.border_APA)

    table_notes = [
        "Correlation coefficient used: {}".format(
            list(global_vars.master_dict.keys())[list(
                global_vars.master_dict.values()).index(
                    global_vars.raw_corr_type)])
    ]
    table_notes.append("**p < 0.01")
    table_notes.append("*p < {}".format(global_vars.alpha_threshold))
    helper_funcs.add_table_notes(ws, table_notes)

    helper_funcs.savefile(wb=wb)
Beispiel #8
0
def summ_corr_apa_table_excel(mod_raw_data_df, output_df):
	# unique vars in var1 given by user's order and here ONLY adding vars from col 2 that are NOT in var 1
	variables_list = list(output_df[global_vars.summ_corr_varOne].unique()) + list(set(output_df[global_vars.summ_corr_varTwo].unique()) - set(output_df[global_vars.summ_corr_varOne].unique()))
	if global_vars.corr_table_triangle == "Upper triangle":
		header_cols = variables_list[1:]
		header_rows = variables_list[:-1]
	elif global_vars.corr_table_triangle == "Lower triangle":
		header_cols = variables_list[:-1]
		header_rows = variables_list[1:]
	elif global_vars.corr_table_triangle == "Both":
		header_cols, header_rows = variables_list, variables_list

	wb = Workbook()
	ws = wb.active

	ws.append([""] + header_cols)
	for ind,var in enumerate(header_rows):
		ws.cell(row=ind+2, column=1).value = var

	inside_loop_ind_start = 2
	for outside_loop_ind in range(2, len(header_rows) + 2):

		if global_vars.corr_table_triangle == "Upper triangle" or global_vars.corr_table_triangle == "Both":
			outside_loop_var = ws.cell(row=outside_loop_ind, column=1).value
		elif global_vars.corr_table_triangle == "Lower triangle":
			outside_loop_var = ws.cell(row=1, column=outside_loop_ind).value
		
		for inside_loop_ind in range(inside_loop_ind_start, len(header_cols) + 2):
			
			if global_vars.corr_table_triangle == "Upper triangle" or global_vars.corr_table_triangle == "Both":
				inside_loop_var = ws.cell(row=1, column=inside_loop_ind).value
			elif global_vars.corr_table_triangle == "Lower triangle":
				inside_loop_var = ws.cell(row=inside_loop_ind, column=1).value

			if outside_loop_var == inside_loop_var:
				ws.cell(row=outside_loop_ind, column=inside_loop_ind).value = 1
			else:
				# here query method is not preferred as it is not only slower as it is much smaller dataset but also cannot refer to two different vaiables (colname and val)
				df_filtered = output_df[((output_df[global_vars.summ_corr_varOne]==outside_loop_var) & (output_df[global_vars.summ_corr_varTwo]==inside_loop_var)) | ((output_df[global_vars.summ_corr_varOne]==inside_loop_var) & (output_df[global_vars.summ_corr_varTwo]==outside_loop_var))].iloc[0]
				r = df_filtered[global_vars.summ_corr_coeff]
				p = df_filtered["adjusted_pvalues"]
				r = helper_funcs.correlations_format_val(r, p)

				if global_vars.corr_table_triangle == "Upper triangle" or global_vars.corr_table_triangle == "Both":
					ws.cell(row=outside_loop_ind, column=inside_loop_ind).value = r
				elif global_vars.corr_table_triangle == "Lower triangle":
					ws.cell(row=inside_loop_ind, column=outside_loop_ind).value = r
		
		if global_vars.corr_table_triangle != "Both":
			inside_loop_ind_start += 1


	for row in range(1, len(header_rows) + 2):
		for cell in ws[row]:
			cell.alignment = global_vars.alignment_center

	for cell in ws[1]:
		cell.font = global_vars.font_header

	for row in range(2, len(header_rows) + 2):
		ws.cell(row=row, column=1).font = global_vars.font_header

	for cell in ws[len(header_rows) + 1]:
		cell.border = Border(bottom=global_vars.border_APA)

	for cell in ws[1]:
		cell.border = Border(top=global_vars.border_APA, bottom=global_vars.border_APA)

	table_notes = ["**p < 0.01", "*p < {}".format(global_vars.alpha_threshold)]
	helper_funcs.add_table_notes(ws, table_notes)

	helper_funcs.savefile(wb=wb)
def spss_indttest_apa_table_excel(mod_raw_data_df, output_df):
    output_df.drop(columns=["pvalues"], inplace=True)

    pd.options.mode.chained_assignment = None
    output_df[list(output_df.columns)[7:-1]] = output_df[list(
        output_df.columns)[7:-1]].applymap(lambda x: "{:.2f}".format(x))

    output_df["adjusted_pvalues"] = output_df["adjusted_pvalues"].map(
        helper_funcs.pvalue_formatting)
    pd.options.mode.chained_assignment = "warn"

    wb = Workbook()
    ws = wb.active

    ws.cell(row=1, column=1).value = "Variable"
    ws.merge_cells('A1:A2')
    ws.cell(row=1, column=1).font = global_vars.font_header

    ws.cell(row=1, column=2).value = "All, n=?"
    ws.merge_cells('B1:C1')

    if global_vars.spss_indttest_nOne != -1:
        n1_label = global_vars.spss_indttest_nOne
    else:
        n1_label = "?"
    ws.cell(row=1, column=4).value = "{g}, n={n}".format(
        g=global_vars.spss_indttest_groupOneLabel, n=n1_label)
    ws.merge_cells('D1:E1')

    if global_vars.spss_indttest_nOne != -1:
        n2_label = global_vars.spss_indttest_nTwo
    else:
        n2_label = "?"
    ws.cell(row=1, column=6).value = "{g}, n={n}".format(
        g=global_vars.spss_indttest_groupTwoLabel, n=n2_label)
    ws.merge_cells('F1:G1')

    ws.cell(row=1, column=8).value = "df"
    ws.merge_cells('H1:H2')
    ws.cell(row=1, column=8).font = global_vars.font_header

    ws.cell(row=1, column=9).value = "t"
    ws.merge_cells('I1:I2')
    ws.cell(row=1, column=9).font = global_vars.font_header

    ws.cell(row=1, column=10).value = global_vars.effect_size_choice
    ws.merge_cells('J1:J2')
    ws.cell(row=1, column=10).font = global_vars.font_header

    ws.cell(row=1, column=11).value = "p"
    ws.merge_cells('K1:K2')
    ws.cell(row=1, column=11).font = global_vars.font_header

    for col in range(2, 7, 2):
        ws.cell(row=2, column=col).value = "M"
        ws.cell(row=2, column=col).font = global_vars.font_header
        ws.cell(row=2, column=col + 1).value = "SD"
        ws.cell(row=2, column=col + 1).font = global_vars.font_header

    for row in dataframe_to_rows(output_df, index=False, header=False):
        ws.append(row)

    for row in range(1, len(output_df) + 3):
        for cell in ws[row]:
            cell.alignment = global_vars.alignment_center

    for cell in ws[1]:
        cell.border = Border(top=global_vars.border_APA)
    for cell in ws[2] + ws[len(output_df) + 2]:
        cell.border = Border(bottom=global_vars.border_APA)

    if global_vars.effect_size_choice == "None":
        ws.delete_cols(10)

    table_notes = [
        "Means and Standard Deviations cannot be read from the SPSS table. Please add them yourself or remove those columns if they are not needed."
    ]
    helper_funcs.add_table_notes(ws, table_notes)

    helper_funcs.savefile(wb=wb)
Beispiel #10
0
def raw_indttest_apa_table_excel(mod_raw_data_df, output_df):

    apa_table_df = output_df[[
        "Variable", "All_Mean", "All_SD",
        global_vars.raw_indttest_grouplevel1 + "_Mean",
        global_vars.raw_indttest_grouplevel1 + "_SD",
        global_vars.raw_indttest_grouplevel2 + "_Mean",
        global_vars.raw_indttest_grouplevel2 + "_SD", "Degrees_of_Freedom",
        "t", global_vars.effect_size_choice, "adjusted_pvalues"
    ]]

    # the two operations below are correct so the SettingWithCopyWarning pandas error is supressed temporarily
    pd.options.mode.chained_assignment = None
    apa_table_df[list(apa_table_df.columns)[1:-1]] = apa_table_df[list(
        apa_table_df.columns)[1:-1]].applymap(lambda x: "{:.2f}".format(x))

    apa_table_df["adjusted_pvalues"] = apa_table_df["adjusted_pvalues"].map(
        helper_funcs.pvalue_formatting)
    pd.options.mode.chained_assignment = "warn"

    wb = Workbook()
    ws = wb.active

    ws.cell(row=1, column=1).value = "Variable"
    ws.merge_cells('A1:A2')
    ws.cell(row=1, column=1).font = global_vars.font_header

    ws.cell(row=1, column=2).value = "All, n={}".format(output_df.iloc[0, 1])
    ws.merge_cells('B1:C1')

    ws.cell(row=1, column=4).value = "{g}, n={n}".format(
        g=global_vars.raw_indttest_grouplevel1, n=output_df.iloc[0, 4])
    ws.merge_cells('D1:E1')

    ws.cell(row=1, column=6).value = "{g}, n={n}".format(
        g=global_vars.raw_indttest_grouplevel2, n=output_df.iloc[0, 7])
    ws.merge_cells('F1:G1')

    ws.cell(row=1, column=8).value = "df"
    ws.merge_cells('H1:H2')
    ws.cell(row=1, column=8).font = global_vars.font_header

    ws.cell(row=1, column=9).value = "t"
    ws.merge_cells('I1:I2')
    ws.cell(row=1, column=9).font = global_vars.font_header

    ws.cell(row=1, column=10).value = global_vars.effect_size_choice
    ws.merge_cells('J1:J2')
    ws.cell(row=1, column=10).font = global_vars.font_header

    ws.cell(row=1, column=11).value = "p"
    ws.merge_cells('K1:K2')
    ws.cell(row=1, column=11).font = global_vars.font_header

    for col in range(2, 7, 2):
        ws.cell(row=2, column=col).value = "M"
        ws.cell(row=2, column=col).font = global_vars.font_header
        ws.cell(row=2, column=col + 1).value = "SD"
        ws.cell(row=2, column=col + 1).font = global_vars.font_header

    for row in dataframe_to_rows(apa_table_df, index=False, header=False):
        ws.append(row)

    for row in range(1, len(apa_table_df) + 3):
        for cell in ws[row]:
            cell.alignment = global_vars.alignment_center

    for cell in ws[1]:
        cell.border = Border(top=global_vars.border_APA)
    for cell in ws[2] + ws[len(apa_table_df) + 2]:
        cell.border = Border(bottom=global_vars.border_APA)

    if global_vars.effect_size_choice == "None":
        ws.delete_cols(10)

    helper_funcs.add_table_notes(ws, [])

    helper_funcs.savefile(wb=wb)