示例#1
0
def generate_world_formatted_forecast(forecast_date,
                                      target_metric='death',
                                      target_aggr='inc'):
    world_forecast = pd.DataFrame()
    forecast_date = pd.to_datetime(forecast_date).date()
    last_epiweek_enddate = get_epiweek_enddate(forecast_date +
                                               epiweeks.timedelta(-7))
    country_list = mu.get_data(scope='global', type='deaths').Country.unique()
    top_country_list = [
        'US', 'India', 'Brazil', 'Russia', 'France', 'United Kingdom',
        'Turkey', 'Italy', 'Spain', 'Germany', 'Colombia', 'Argentina',
        'Mexico', 'Poland', 'Iran', 'Iraq', 'Ukraine', 'South Africa', 'Peru',
        'Netherlands', 'Belgium', 'Chile', 'Romania', 'Canada', 'Ecuador',
        'Czechia', 'Pakistan', 'Hungary', 'Philippines', 'Switzerland'
    ]

    for country in top_country_list:
        try:
            print(country)
            country_forecast = generate_formatted_forecast('World', country, forecast_date)\
                .query('target!="9 wk ahead inc death"')
            latest_cum_country = mu.get_data_by_country(
                country).loc[last_epiweek_enddate][0]
            country_forecast = add_cum_forecast(country_forecast,
                                                latest_cum_country)
            world_forecast = pd.concat([world_forecast, country_forecast])
        except (ValueError, IndexError):
            pass

    world_forecast.to_csv(
        'data_processed/World-{}-AIpert-pwllnod.csv'.format(forecast_date),
        index=False)
示例#2
0
def generate_US_formatted_forecast(forecast_date,
                                   target_metric='death',
                                   target_aggr='inc'):
    forecast_date = pd.to_datetime(forecast_date).date()
    last_epiweek_enddate = get_epiweek_enddate(forecast_date +
                                               epiweeks.timedelta(-7))
    US_forecast = pd.DataFrame()
    US_state_list = mu.get_data(scope='US', type='deaths').State.unique()

    for state in US_state_list:
        try:
            print(state)
            state_forecast = generate_formatted_forecast('US', state, forecast_date)\
                .query('target!="9 wk ahead inc death"')
            latest_cum_state = mu.get_data_by_state(
                state).loc[last_epiweek_enddate][0]
            state_forecast = add_cum_forecast(state_forecast, latest_cum_state)
            US_forecast = pd.concat([US_forecast, state_forecast])
        except (ValueError, IndexError):
            pass
    # Aggregate all states for US forecast
    US_forecast_new = US_forecast.groupby(
        ['forecast_date', 'target', 'target_end_date', 'quantile',
         'type']).sum().reset_index()
    US_forecast_new['location'] = "US"
    US_forecast_new = pd.concat([US_forecast_new, US_forecast])
    US_forecast_new.to_csv(
        'data_processed/{}-AIpert-pwllnod.csv'.format(forecast_date),
        index=False)
def main():
    X_train_rus, y_train_rus, X_test, y_test = get_data()

    lr_clf = LogisticRegression(random_state=0)
    lr_clf.fit(X_train_rus, y_train_rus)
    y_score = lr_clf.predict_proba(X_test)[:, 1]
    y_hat = lr_clf.predict(X_test)

    lr_scores = pd.DataFrame({'y_true': y_test, 'y_hat': y_hat, 'y_score': y_score})
    lr_scores.to_csv('../../Data/Modeling/Output/scores/logistic_regression_scores.csv')
示例#4
0
def main():
    X_train_rus, y_train_rus, X_test, y_test = get_data()

    clf_xgb = xgb.XGBClassifier()
    '''
    parameters = {'eta': [0.3],
                  'max_depth': [8],
                  'min_child_weight': [5],
                  'gamma': [0.2],
                  'colsample_bytree': [1],
                  'objective': ['binary:logistic']}
    '''
    parameters = {
        'eta': [0.1, 0.15, 0.2, 0.25, 0.3, 0.35],
        'max_depth': [3, 4, 5, 6, 7, 8, 10, 12],
        'min_child_weight': [1, 3, 5, 7],
        'gamma': [0, 0.1, 0.2, 0.3, 0.4],
        'colsample_bytree': [0.5, 0.7, 1],
        'objective': ['binary:logistic']
    }

    grid = GridSearchCV(clf_xgb,
                        parameters,
                        n_jobs=4,
                        scoring='average_precision',
                        cv=2,
                        verbose=5)
    best_params = grid.fit(X_train_rus, y_train_rus).best_params_

    D_train = xgb.DMatrix(X_train_rus, label=y_train_rus)
    D_test = xgb.DMatrix(X_test, label=y_test)
    clf_xgb = xgb.train(best_params, D_train, 20)
    y_score = clf_xgb.predict(D_test)
    #y_hat = np.asarray([np.argmax(line) for line in y_score])
    y_hat = y_score.copy()
    y_hat[y_hat < 0.5] = 0
    y_hat[y_hat >= 0.5] = 1

    xgb_scores = pd.DataFrame({
        'y_true': y_test,
        'y_hat': y_hat,
        'y_score': y_score
    })
    xgb_scores.to_csv('../../Data/Modeling/Output/scores/xgb_scores.csv')
示例#5
0
from pathlib import Path
from fastai.vision import ImageImageList, ImageList
from model_utils import get_data, create_gen_learner

# Loading Paths for Model Load
path = Path('')  # Path to data folder to load your model
path_lr = path / ''  # Path to model weights

# Loading Paths to Inference
path_t = Path('')  # Path to undamaged files
dmgpath = Path('')  # Path to damage templates
inf_path = Path('')  # Directory for files to be inferenced

# Creating gen and Loading saved Weights
src = ImageImageList.from_folder(path_lr).split_by_rand_pct(0.1, seed=42)
data_gen = get_data(1, 500, src, path_lr)
learn_gen = create_gen_learner(data_gen).load('')  # LOAD MODEL HERE
test_list = ImageList.from_folder(inf_path)

# Starting Streamlit App
st.markdown('# **ML for Photo Repair**')
st.markdown('### Choose an Image and Damage Template:')
st.markdown('Click **Generate** to Create a damaged photo')
names = []
for filename in (os.listdir(path_t)):
    if '.png' in filename:
        names.append(filename)
    elif '.jpg' in filename:
        names.append(filename)
dmgnames = []
for filename in (os.listdir(dmgpath)):
示例#6
0
        st.write('Daily metrics', daily)
        st.markdown(
            mu.get_table_download_link(cumulative,
                                       filename='cumulative_' + local + '_' +
                                       str(dt.date.today()) + '.csv'),
            unsafe_allow_html=True)
        st.write('Cumulative metrics', cumulative)
    mu.append_row_2_logs([dt.datetime.today(), scope, local, model_beta],
                         'logs/fitted_models.csv')


scope = st.sidebar.selectbox('Country or US State', ['Country', 'State'],
                             index=0)
if scope == 'Country':
    #data_load_state = st.text('Loading data...')
    death_data = mu.get_data(scope='global', type='deaths')
    #data_load_state.text('Loading data... done!')
    local = st.sidebar.selectbox('Which country do you like to see prognosis',
                                 death_data.Country.unique(),
                                 index=156)
    lockdown_date = st.sidebar.date_input(
        'When did full lockdown happen? Very IMPORTANT to get accurate prediction',
        mu.get_lockdown_date_by_country(local))
    forecast_fun = mu.get_metrics_by_country
    debug_fun = mu.get_log_daily_predicted_death_by_country
else:
    #data_load_state = st.text('Loading data...')
    death_data = mu.get_data(scope='US', type='deaths')
    #data_load_state.text('Loading data... done!')
    local = st.sidebar.selectbox('Which US state do you like to see prognosis',
                                 death_data.State.unique(),
示例#7
0
import fastai
from fastai.vision import ImageList, ImageImageList
from pathlib import Path
from model_utils import get_data
from model_utils import create_gen_learner

path = Path('../data/')
path_hr = path / 'preprocessed'
path_lr = path / 'processed'
path_test = path / 'test_imgs'

# Gather and select Data / Output size
bs, size = 1, 500

src = ImageImageList.from_folder(path_lr).split_by_rand_pct(0.1, seed=42)
data_gen = get_data(bs, size, src, path_hr)

# Load model to inference from
learn_gen = create_gen_learner(data_gen).load('') # Input model to load

# Open file to be inferenced
test_list = ImageList.from_folder(path_test)
test_list.open(test_list.items[0])

# Inference and display output
test_list[0].show(figsize=(7, 7),y=learn_gen.predict(test_list[0])[0])

# Save File if desired
#y = learn_gen.predict(test_list[8])[0]
#y.save(path_test/'inf1.png')