user512512af
user512512af

Reputation: 37

Float column width are not same like other column using tabulate

I have code where the Selected Probability value is float type, and im using Tabulate to print it into table. The problem is the width of Selected Probability column. Here is the simplified code:

from tabulate import tabulate

def calculate_fitness(total_watt):
    # Placeholder 
    return 42

def format_number(total_kwh):
    # Placeholder 
    return 42

def store_chromosome_attributes(chromosomes, table_data_p):
    chromosome_attributes = []

    headers = ["K", "Individual", "Fitness Value", "Selected Probability"]

    print(tabulate([], tablefmt="pipe", showindex=False, headers=headers))

    total_fitness_values = []
    cumulative_probability = 0

    for k_row in chromosomes:
        combined_row = [k_row[0]]
        individual_values = []

        for k_value, p_row in zip(k_row[1:11], table_data_p):
            if k_value == 1:
                combined_row.append(k_value)
                individual_values.append(str(k_value))
            else:
                combined_row.append(k_value)
                individual_values.append('0')

        selected_watt_values = [p_row[2] for k_value, p_row in zip(k_row[1:11], table_data_p) if k_value == 1]
        selected_kwh_values = [p_row[3] * 1352 for k_value, p_row in zip(k_row[1:11], table_data_p) if k_value == 1]

        total_watt = sum(selected_watt_values)
        total_kwh = sum(selected_kwh_values)
        fitness_value = calculate_fitness(total_watt)
        total_fitness_values.append(fitness_value)

        chromosome_attributes.append({
            'K': combined_row[0],
            'P_values': combined_row[1:11],
            'Total_Watt': total_watt,
            'Total_kWh': total_kwh,
            'Fitness_Value': fitness_value,
        })

    for attr in chromosome_attributes:
        attr['Selected_Probability'] = attr['Fitness_Value'] / sum(total_fitness_values) if sum(total_fitness_values) != 0 else 0.0
        cumulative_probability += attr['Selected_Probability']

    column_widths = [0, 11, 15, 30]  # Updated column widths
    rows_data = [
        [
            f"{attr['K']:<{column_widths[0]}}",
            f"{''.join(map(str, attr['P_values'])):>{column_widths[1]}}",
            f"{attr['Fitness_Value']:>{column_widths[2]}}",
            f"{attr['Selected_Probability']:.4f}",
        ] for attr in chromosome_attributes
    ]
    print(tabulate(rows_data, tablefmt="pipe", showindex=False))

    total_row_data = ["-" * 22, "Total", f"{sum(total_fitness_values):>{column_widths[2]}}", "0.0000"]
    print(tabulate([total_row_data], tablefmt="pipe", showindex=False))

    return chromosome_attributes

# Example usage:
chromosomes = [[1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
           [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]]
table_data_p = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
            [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]]

store_chromosome_attributes(chromosomes, table_data_p)

When I run it, the width of the Selected Probability column is not dynamic or not the same as other Column width (Invidiual, Fitness Value):

| K   | Individual   | Fitness Value   | Selected Probability   |
|-----|--------------|-----------------|------------------------|
|--:|------------:|----------------:|----:|
| 1 |          01 |              42 | 0.5 |
| 0 |          10 |              42 | 0.5 |
|:-----------------------|:------|----------------:|--:|
| ---------------------- | Total |              84 | 0 |

The output should be:

| K   | Individual   | Fitness Value   | Selected Probability   |
|-----|--------------|-----------------|------------------------|
|----:|-------------:|----------------:|-----------------------:|
|  1  |           01 |              42 |                    0.5 |
|  0  |           10 |              42 |                    0.5 |
|:-------------------|:------|--------:|-----------------------:|
| ------------------ | Total |      84 |                      0 |

Upvotes: 0

Views: 61

Answers (0)

Related Questions