Autor Tema: Código de Phyton - Casos Covid - No funciona al ejecutar - Adjunto códigos  (Leído 1007 veces)

LordFer

  • Sin experiencia
  • *
  • APR2.COM
  • Mensajes: 3
    • Ver Perfil
Buenos días, hace un par de años escribí una duda con un código de java. Y me gustaría saber si también pueden ayudar con el funcionamiento de un código Python.

El código trata de sacar dos archivos .csv de casos covid, y un archivo jpg de tres gráficas de como avanzan los casos covid en tres países.

El problema es que al ejecutar el archivo test.py, el problema es que sale "no tests were found". He instalado matplotlib, numpy, etc. Pero sigue sin funcionar


Adjunto los códigos: (El sim_parameters.py y helper.py nos los dio el profesor por lo que creo que están perfectos)

(test.py):
Código: [Seleccionar]
import unittest
import assignment3

SAMPLE_RATIO = 1e6

# START_DATE = '2020-04-01'
START_DATE = '2021-04-01'
END_DATE = '2022-04-30'

class A3Test(unittest.TestCase):
    @staticmethod
    def runTest():
        assignment3.run(countries_csv_name='C:/Users/Alejandro BTH/Documents/Assign3code/a3-countries.csv',
                        countries=['Afghanistan', 'Sweden', 'Japan'], sample_ratio=SAMPLE_RATIO, start_date=START_DATE,
                        end_date=END_DATE)

    unittest.main()



(sim_parameters.py):
Código: [Seleccionar]
TRASITION_PROBS = {
    'less_5':
        {
            'H': {'H': 0.9, 'I': 0.1},
            'I': {'I': 0, 'S': 0.5, 'M': 0.5},
            'S': {'S': 0, 'D': 0.01, 'M': 0.99},
            'D': {'D': 1},
            'M': {'M': 0, 'H': 1}
        },
    '5_to_14':
        {
            'H': {'H': 0.9, 'I': 0.1},
            'I': {'I': 0, 'S': 0.5, 'M': 0.5},
            'S': {'S': 0, 'D': 0.01, 'M': 0.99},
            'D': {'D': 1},
            'M': {'M': 0, 'H': 1}
        },
    '15_to_24':
        {
            'H': {'H': 0.7, 'I': 0.3},
            'I': {'I': 0, 'S': 0.5, 'M': 0.5},
            'S': {'S': 0, 'D': 0.05, 'M': 0.95},
            'D': {'D': 1},
            'M': {'M': 0, 'H': 1}
        },
    '25_to_64':
        {
            'H': {'H': 0.7, 'I': 0.3},
            'I': {'I': 0, 'S': 0.5, 'M': 0.5},
            'S': {'S': 0, 'D': 0.2, 'M': 0.8},
            'D': {'D': 1},
            'M': {'M': 0, 'H': 1}
        },
    'over_65':
        {
            'H': {'H': 0.7, 'I': 0.3},
            'I': {'I': 0, 'S': 0.5, 'M': 0.5},
            'S': {'S': 0, 'D': 0.4, 'M': 0.6},
            'D': {'D': 1},
            'M': {'M': 0, 'H': 1}
        }
}

HOLDING_TIMES = {
    'less_5':

        {'H': 0, 'I': 4, 'S': 14, 'D': 0, 'M': 120}

    ,
    '5_to_14':

        {'H': 0, 'I': 4, 'S': 14, 'D': 0, 'M': 120}
    ,
    '15_to_24':

        {'H': 0, 'I': 4, 'S': 14, 'D': 0, 'M': 120}
    ,
    '25_to_64':

        {'H': 0, 'I': 4, 'S': 14, 'D': 0, 'M': 120}
    ,
    'over_65':

        {'H': 0, 'I': 4, 'S': 14, 'D': 0, 'M': 120}

}

(helper.py)
Código: [Seleccionar]
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as md
from pathlib import Path
import os

OUTPUT_NAME = 'a3-covid-simulation.png'

def get_filepath(filename):
    source_path = Path(__file__).resolve()
    source_dir = source_path.parent
    filepath = os.path.join(source_dir, filename)
    return filepath

def save_plot(fig, filename):
    filepath = get_filepath(filename)
    fig.savefig(filepath, dpi=300)

def read_dataset(filename):
    filepath = get_filepath(filename)
    df = pd.read_csv(filepath, sep=',', header=0)
    return df

def create_plot(summary_csv, countries):
    states_timeseries_df = read_dataset(summary_csv)
    print(f'Plotting is being prepared for the following dataset ...')
    print(states_timeseries_df)
    states_timeseries_df = states_timeseries_df[['country', 'date', 'H', 'I', 'S', 'M', 'D']]  # reorder columns
    states_timeseries_df['date'] = pd.to_datetime(states_timeseries_df['date'])
    states_timeseries_df.set_index('date')

    # Multiple countries in subfigures
    countries_num = len(countries)
    fig, ax = plt.subplots(countries_num, figsize=(16, 9 * countries_num))

    for i in range(countries_num):
        states_timeseries_df[states_timeseries_df['country'] == countries[i]].plot(
            kind='bar',
            x='date',
            stacked=True,
            width=1,
            color=['green', 'darkorange', 'indianred', 'lightseagreen', 'slategray'],
            ax=ax[i]
        )
        ax[i].legend(['Healthy', 'Infected (without symptoms)', 'Infected (with symptoms)', 'Immune', 'Deceased'])
        ax[i].set_xticklabels(ax[i].get_xticks(), rotation=30)
        plot_name = countries[i]
        ax[i].set_title(f"Covid Infection Status in {plot_name}")
        ax[i].set_xlabel("Date")
        ax[i].set_ylabel("Population in Millions")
        ax[i].xaxis.set_major_locator(md.MonthLocator())
        selected_dates = states_timeseries_df['date'].dt.to_period('M').unique()
        ax[i].set_xticklabels(selected_dates.strftime('%b %Y'), rotation=30, horizontalalignment="center")

    save_plot(fig, OUTPUT_NAME)
    print(f'Plotting Done!')


(assigment3.py)
Código: [Seleccionar]
import csv
import random
from datetime import datetime, timedelta
import pandas as pd
import helper
from sim_parameters import TRASITION_PROBS, HOLDING_TIMES

# Define 'timeline_data' to be used later
timeline_data = []

def get_population(countries_data, country):
    return countries_data[countries_data['country'] == country]['population'].values[0]

def get_age_group_population(countries_data, country, age_group):
    return countries_data[countries_data['country'] == country][age_group].values[0]

def get_state_population(people_data, country, state, date, start_datetime):
    state_counts = {'H': 0, 'I': 0, 'S': 0, 'D': 0, 'M': 0}
    for person in people_data:
        if person['country'] == country and person['timeline'][(date - start_datetime).days]['state'] == state:
            state_counts[state] += 1
    return state_counts[state]

def run(countries_csv_name: str, countries: list[str], sample_ratio: float, start_date: str, end_date: str):
    global timeline_data  # Make 'timeline_data' global so it can be accessed and modified

    countries_data = pd.read_csv(countries_csv_name)
    countries_data = countries_data[countries_data['country'].isin(countries)]
    countries_data['sample_population'] = (countries_data['population'] * sample_ratio).astype(float)

    age_groups = ['less_5', '5_to_14', '15_to_24', '25_to_64', 'over_65']

    people_data = []
    person_id = 0

    for _, row in countries_data.iterrows():
        for age_group in age_groups:
            num_people = int(row[age_group] * row['sample_population'] / 100)
            for _ in range(num_people):
                people_data.append({
                    'person_id': person_id,
                    'age_group': age_group,
                    'country': row['country'],
                    'timeline': []
                })
                person_id += 1

    start_datetime = datetime.strptime(start_date, '%Y-%m-%d')
    end_datetime = datetime.strptime(end_date, '%Y-%m-%d')
    num_days = (end_datetime - start_datetime).days + 1

    # Create and populate the 'timeline_data'
    for person in people_data:
        person_timeline = []  # A list to store each person's timeline
        current_state = 'H'  # Initialize the current state to 'H' (Healthy)
        for _ in range(num_days):
            # Sample the next state using TRASITION_PROBS
            next_state = random.choices(['H', 'I', 'S', 'D', 'M'], TRASITION_PROBS[person['age_group']][current_state])[
                0]
            staying_days = random.choices([0, 1], [1 - HOLDING_TIMES[person['age_group']][current_state],
                                                   HOLDING_TIMES[person['age_group']][current_state]])[0]
            person_timeline.append({
                'date': (start_datetime + timedelta(days=_)).strftime('%Y-%m-%d'),
                'state': next_state,
                'staying_days': staying_days
            })
            current_state = next_state
        timeline_data.append(person_timeline)

    with open('a3-covid-simulated-timeseries.csv', 'w', newline='') as csvfile:
        fieldnames = ['person_id', 'age_group_name', 'country', 'date', 'state', 'staying_days']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()
        person_id = 0
        for person in people_data:
            for day_data in person['timeline']:
                writer.writerow({
                    'person_id': person_id,
                    'age_group_name': person['age_group'],
                    'country': person['country'],
                    'date': day_data['date'],
                    'state': day_data['state'],
                    'staying_days': day_data.get('staying_days', 0)
                })
                person_id += 1

    summary_data = []  # Initialize summary_data as an empty list

    for date in pd.date_range(start=start_date, end=end_date, freq='D'):
        date_str = date.strftime('%Y-%m-%d')
        for country in countries:
            country_data = countries_data[countries_data['country'] == country]
            state_counts = {'H': 0, 'I': 0, 'S': 0, 'D': 0, 'M': 0, 'date': date_str, 'country': country}
            for person_timeline in timeline_data:
                if person_timeline[0]['country'] == country:
                    day_data = person_timeline[(date - start_datetime).days]
                    state_counts[day_data['state']] += 1
            summary_data.append(state_counts)

    summary_df = pd.DataFrame(summary_data)
    summary_df.to_csv('a3-covid-summary-timeseries.csv', index=False)
    helper.create_plot('a3-covid-summary-timeseries.csv', countries)



« Última modificación: 19 de Octubre 2023, 11:37 por LordFer »

 

Sobre la educación, sólo puedo decir que es el tema más importante en el que nosotros, como pueblo, debemos involucrarnos.

Abraham Lincoln (1808-1865) Presidente estadounidense.

aprenderaprogramar.com: Desde 2006 comprometidos con la didáctica y divulgación de la programación

Preguntas y respuestas

¿Cómo establecer o cambiar la imagen asociada (avatar) de usuario?
  1. Inicia sesión con tu nombre de usuario y contraseña.
  2. Pulsa en perfil --> perfil del foro
  3. Elige la imagen personalizada que quieras usar. Puedes escogerla de una galería de imágenes o subirla desde tu ordenador.
  4. En la parte final de la página pulsa el botón "cambiar perfil".