0

I have this code and I face a problem:

# Prepare the DataFrame to store results
columns_accuracy = [
    'method', 'accuracy_mohr_F1', 'accuracy_non_mohr_F1', 'accuracy_mohr_F1_total',
    'accuracy_mohr_F2', 'accuracy_non_mohr_F2', 'accuracy_mohr_F2_total',
    'accuracy_ten', 'accuracy_non_ten', 'accuracy_ten_total',
    'accuracy_total_F1', 'accuracy_total_F2'
]
rows = []  # Collect rows for the DataFrame here

for sheet_name, method, Umethod, press in product(sheet_names, methods_dict, UCSmethods, ppmethods):
    stability_u = UCS[sheet_name][method][Umethod][
        UCS[sheet_name][method][Umethod]['Depth (m)'] >= Pp[sheet_name]['Depth'][0]
    ].copy()

    # Assign Pore Pressure dynamically
    stability_u['Pore_Pressure'] = Pp[sheet_name][press]
    
    stability_p[press] = stability_u

    for mpress, svmethod in product(pmudmethods, Svmethods):
        stability_u['Mud_Pressure'] = Pmud[sheet_name][mpress]
        stability_u['Vertical_Stress'] = Sv[sheet_name][svmethod]

        for dec in DEC:
            stability_u['Drilling_Events'] = DE[sheet_name][dec]

        poisson_ratio = stability_u['Poission Ratio']
        young_modulus = stability_u['St_Young_Modulus'] * 1e9  # Pa
        vertical_stress = stability_u['Vertical_Stress'] * 1e6  # Pa

        x_strain = (vertical_stress * poisson_ratio) / (young_modulus * (1 - poisson_ratio))
        y_strain = x_strain * (1 - (poisson_ratio ** 2) / (1 - poisson_ratio))

        stability_u['x_tectonic_strain'] = x_strain
        stability_u['y_tectonic_strain'] = y_strain

        stability_u['SHmax'] = (
            (poisson_ratio / (1 - poisson_ratio)) * (stability_u['Vertical_Stress'] - stability_u['Pore_Pressure'])
            + stability_u['Pore_Pressure']
            + (stability_u['St_Young_Modulus'] * 1e3 / (1 - poisson_ratio ** 2)) *
              (y_strain + poisson_ratio * x_strain)
        )
        stability_u['Shmin'] = (
            (poisson_ratio / (1 - poisson_ratio)) * (stability_u['Vertical_Stress'] - stability_u['Pore_Pressure'])
            + stability_u['Pore_Pressure']
            + (stability_u['St_Young_Modulus'] * 1e3 / (1 - poisson_ratio ** 2)) *
              (x_strain + poisson_ratio * y_strain)
        )

        numeratora = (
            stability_u['Vertical_Stress'] + 2 * poisson_ratio * 
            (stability_u['SHmax'] - stability_u['Shmin']) -
            stability_u['Pore_Pressure'] - stability_u['UCS (MPa)']
        )
        denominator = np.tan(np.pi / 4 + stability_u['FANG'] / 2) ** 2
        stability_u['Mohr_colomb_F1'] = stability_u['Pore_Pressure'] + numeratora / denominator

        numeratorb = (
            3 * stability_u['SHmax'] - stability_u['Shmin'] -
            2 * stability_u['Pore_Pressure'] - stability_u['UCS (MPa)']
        )
        stability_u['Mohr_colomb_F2'] = stability_u['Pore_Pressure'] + numeratorb / (1 + denominator)

        stability_u['Mohr_colomb_Ten'] = (
            3 * stability_u['Shmin'] - stability_u['SHmax'] +
            stability_u['UTS (MPa)'] - stability_u['Pore_Pressure']
        )

        stability_u['assesst_F1'] = (stability_u['Mohr_colomb_F1'] >= stability_u['Mud_Pressure']).astype(int)
        stability_u['assesst_F2'] = (stability_u['Mohr_colomb_F2'] >= stability_u['Mud_Pressure']).astype(int)
        stability_u['assesst_ten'] = (stability_u['Mohr_colomb_Ten'] <= stability_u['Mud_Pressure']).astype(int) * 2

        stability_u['accuracy_F1_OK'] = ((stability_u['assesst_F1'] == 1) & (stability_u['Drilling_Events'] == 1)).astype(int)
        stability_u['accuracy_F1_NOK'] = ((stability_u['assesst_F1'] == 0) & (stability_u['Drilling_Events'] == 0)).astype(int)

        stability_u['accuracy_F2_OK'] = ((stability_u['assesst_F2'] == 1) & (stability_u['Drilling_Events'] == 1)).astype(int)
        stability_u['accuracy_F2_NOK'] = ((stability_u['assesst_F2'] == 0) & (stability_u['Drilling_Events'] == 0)).astype(int)

        stability_u['accuracy_Ten_OK'] = ((stability_u['assesst_ten'] == 2) & (stability_u['Drilling_Events'] == 2)).astype(int)
        stability_u['accuracy_Ten_NOK'] = ((stability_u['assesst_ten'] == 0) & (stability_u['Drilling_Events'] == 0)).astype(int)

        accuracy_mohr_F1 = stability_u['accuracy_F1_OK'].sum() / len(stability_u) * 100
        accuracy_non_mohr_F1 = stability_u['accuracy_F1_NOK'].sum() / len(stability_u) * 100
        accuracy_mohr_F1_total = (accuracy_mohr_F1 + accuracy_non_mohr_F1) / 2

        accuracy_mohr_F2 = stability_u['accuracy_F2_OK'].sum() / len(stability_u) * 100
        accuracy_non_mohr_F2 = stability_u['accuracy_F2_NOK'].sum() / len(stability_u) * 100
        accuracy_mohr_F2_total = (accuracy_mohr_F2 + accuracy_non_mohr_F2) / 2

        accuracy_ten = stability_u['accuracy_Ten_OK'].sum() / len(stability_u) * 100
        accuracy_non_ten = stability_u['accuracy_Ten_NOK'].sum() / len(stability_u) * 100
        accuracy_ten_total = (accuracy_ten + accuracy_non_ten) / 2

        accuracy_total_F1 = (accuracy_mohr_F1 + accuracy_mohr_F2) / 2
        accuracy_total_F2 = (accuracy_ten + accuracy_non_ten) / 2

        method_name = f"Method:_{sheet_name}_{method}_{Umethod}_{press}"
        new_row = {
            'method': method_name, 'accuracy_mohr_F1': accuracy_mohr_F1,
            'accuracy_non_mohr_F1': accuracy_non_mohr_F1, 'accuracy_mohr_F1_total': accuracy_mohr_F1_total,
            'accuracy_mohr_F2': accuracy_mohr_F2, 'accuracy_non_mohr_F2': accuracy_non_mohr_F2,
            'accuracy_mohr_F2_total': accuracy_mohr_F2_total, 'accuracy_ten': accuracy_ten,
            'accuracy_non_ten': accuracy_non_ten, 'accuracy_ten_total': accuracy_ten_total,
            'accuracy_total_F1': accuracy_total_F1, 'accuracy_total_F2': accuracy_total_F2
        }
        rows.append(new_row)

# Create the DataFrame and save it
accuracy_strain_formula = pd.DataFrame(rows)
accuracy_strain_formula.to_csv('accuracy_strain_formula.csv', index=False)

It is clear that stability_u changes in each iteration. This DataFrame must be saved as my data such as: stability_strain_formula[sheet_name][method][Umethod][press][pmudmethods][Svmethods] = stability_u (of each iteration). How can I do it? Is there any solution without changing the for loop format?

2
  • 1
    Please edit your question to include a minimal reproducible example so that readers can run your code to answer your question. Your current code is hardly minimal, and contains all sorts of undefined variables. The question also lacks a proper explanation of what the code is supposed to do, an attempt to pinpoint the apparent error(s), and mention of the exact desired result (based on a minimal sample).
    – ouroboros1
    Commented Oct 15 at 12:42
  • What's stopping you from simply doing that in your existing loop? You will probably need to check that the key exists at each level in the dictionary. See stackoverflow.com/questions/16333296/…
    – Barmar
    Commented Oct 15 at 15:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.