示例#1
0
 def test_min_only_and_greater_than_60(self):
     h = None
     mins = 75
     self.assertEqual(duration_to_string(h, mins), '01:15')
示例#2
0
def trainings_to_df(user_id,
                    columns,
                    startdate=None,
                    enddate=None,
                    sport='Kaikki',
                    restdays=True,
                    duration_format='str',
                    date_format='%Y-%m-%d'):
    """
    Fetch training data from database and return cleaned pandas dataframe of it
    """
    if not Harjoitus.objects.filter(user=user_id):
        return None

    startdate = int(
        coalesce(startdate,
                 first_training_date(user_id).strftime('%Y%m%d')))
    enddate = int(coalesce(enddate, datetime.now().date().strftime('%Y%m%d')))
    join_type = 'left' if restdays is True else 'inner'

    days = Aika.objects.filter(vvvvkkpp__gte=startdate,
                               vvvvkkpp__lte=enddate).values_list(
                                   'pvm', 'vvvvkkpp', 'viikonpaiva_lyh', 'vko')
    days_df = pd.DataFrame(days,
                           columns=['Pvm', 'vvvvkkpp', 'Viikonpäivä', 'Vko'])
    trainings = Harjoitus.objects.filter(user=user_id).values_list(
        'id', 'id', 'aika_id', 'kesto', 'kesto_h', 'kesto_min',
        'laji__laji_nimi', 'matka', 'vauhti_km_h', 'keskisyke', 'tuntuma',
        'kommentti')
    trainings_df = pd.DataFrame(trainings,
                                columns=[
                                    'edit', 'delete', 'vvvvkkpp', 'Kesto', 'h',
                                    'min', 'Laji', 'Matka (km)',
                                    'Vauhti (km/h)', 'Keskisyke', 'Tuntuma',
                                    'Kommentti'
                                ])
    trainings_df['details'] = np.nan
    trainings_df = days_df.merge(trainings_df,
                                 how=join_type,
                                 left_on='vvvvkkpp',
                                 right_on='vvvvkkpp')

    # cleaning data
    trainings_df['Laji'].fillna('Lepo', inplace=True)
    trainings_df['Pvm'] = pd.to_datetime(
        trainings_df['Pvm']).dt.strftime(date_format)
    trainings_df[['Matka (km)', 'Vauhti (km/h)'
                  ]] = trainings_df[['Matka (km)',
                                     'Vauhti (km/h)']].astype(float).round(1)
    trainings_df[['delete', 'edit', 'Keskisyke', 'Tuntuma']] = trainings_df[[
        'delete', 'edit', 'Keskisyke', 'Tuntuma'
    ]].fillna(-1).astype(int).astype(str).replace('-1', np.nan)
    trainings_df['Päivä'] = trainings_df[['Pvm', 'Viikonpäivä'
                                          ]].apply(lambda x: ' '.join(x),
                                                   axis=1)
    if duration_format == 'str':
        trainings_df['Kesto'] = trainings_df.apply(
            lambda row: duration_to_string(row['h'], row['min']), axis=1)

    # calculate duration per zone
    zones = Teho.objects.filter(harjoitus_id__user=user_id).values_list(
        'harjoitus_id', 'tehoalue_id__tehoalue', 'kesto', 'kesto_h',
        'kesto_min')
    if zones:
        zones_df = pd.DataFrame(zones,
                                columns=['id', 'teho', 'kesto', 'h',
                                         'min']).fillna(np.nan)
        zones_df['kesto'] = zones_df['kesto'].astype(float)
        zones_df = zones_df.groupby(['id', 'teho']).sum().reset_index()
        if duration_format == 'str':
            zones_df['kesto'] = zones_df.apply(
                lambda row: duration_to_string(row['h'], row['min']), axis=1)
        zones_df = zones_df.pivot(index='id', columns='teho', values='kesto')
        zones_df.index = zones_df.index.map(str)
        trainings_df = trainings_df.merge(zones_df,
                                          how='left',
                                          left_on='edit',
                                          right_index=True)

    # filter by sport
    if sport != 'Kaikki':
        user_sports = sports_to_dict(user_id)
        if sport in user_sports.keys():
            trainings_df = trainings_df[trainings_df['Laji'].isin(
                user_sports[sport])]
        else:
            trainings_df = trainings_df[trainings_df['Laji'] == sport]

    trainings_df.sort_values(by='vvvvkkpp', ascending=False, inplace=True)
    return trainings_df[columns]
示例#3
0
 def test_decimals(self):
     h = 1.5
     mins = 1.2
     self.assertEqual(duration_to_string(h, mins), '01:01')
示例#4
0
 def test_min_greater_than_60(self):
     h = 1
     mins = 100
     self.assertEqual(duration_to_string(h, mins), '02:40')
示例#5
0
 def test_small_integers(self):
     h = 1
     mins = 1
     self.assertEqual(duration_to_string(h, mins), '01:01')
示例#6
0
 def test_min_only(self):
     h = None
     mins = 25
     self.assertEqual(duration_to_string(h, mins), '00:25')
示例#7
0
 def test_h_only(self):
     h = 15
     mins = None
     self.assertEqual(duration_to_string(h, mins), '15:00')
示例#8
0
 def test_nan(self):
     h = np.nan
     mins = np.nan
     self.assertEqual(duration_to_string(h, mins), None)
示例#9
0
 def test_none(self):
     h = None
     mins = None
     self.assertEqual(duration_to_string(h, mins), None)