In [1]:
import pandas as pd
In [2]:
bird_data = pd.read_csv('./bird_tracking.csv')
In [3]:
bird_data.head()
Out[3]:
In [4]:
bird_data.info()
In [5]:
import matplotlib.pyplot as plt
import numpy as np
In [6]:
idx = bird_data.bird_name == 'Eric'
In [7]:
eric_x, eric_y = bird_data.longitude[idx], bird_data.latitude[idx]
In [8]:
plt.figure(figsize=(7,7))
Out[8]:
In [9]:
%matplotlib inline
plt.plot(eric_x,eric_y,'.')
Out[9]:
In [10]:
all_bird_names = pd.unique(bird_data.bird_name)
In [11]:
all_bird_names
Out[11]:
In [12]:
plt.figure(figsize=(7,7))
for name in all_bird_names:
idx = bird_data.bird_name == name
x, y = bird_data.longitude[idx], bird_data.latitude[idx]
plt.plot(x,y,'.',label=name)
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.legend(loc='lower right')
Out[12]:
looking at the speed of bird Eric¶
In [13]:
idx = bird_data.bird_name == 'Eric'
In [14]:
speed = bird_data.speed_2d[idx]
In [15]:
any(np.isnan(speed)) # or np.isnan(speed).any()
Out[15]:
There must be some value is not a numerical value.We need to pay attention.
In [16]:
np.sum(np.isnan(speed))
Out[16]:
There are 85 values that are not numerical values.
In [17]:
ind = np.isnan(speed)
In [18]:
plt.hist(speed[~ind])
Out[18]:
In [19]:
plt.figure(figsize=(8,4))
plt.hist(speed[~ind], bins=np.linspace(0,30,20),normed=True)
plt.xlabel('2d speed m/s')
plt.ylabel('frequency')
Out[19]:
using pandas to plot histogram of speed¶
In [20]:
bird_data.speed_2d.plot(kind='hist',range=[0,30])
plt.xlabel('2d speed m/s')
plt.ylabel('frequency')
Out[20]:
dealing with dates¶
In [21]:
import datetime
In [22]:
datetime.datetime.today()
Out[22]:
In [23]:
time1 = datetime.datetime.today()
In [24]:
time2 = datetime.datetime.today()
In [25]:
time2 - time1
Out[25]:
In [26]:
date_str = bird_data.date_time[0]
In [27]:
date_str
Out[27]:
In [28]:
date_str = date_str[:-3]
In [29]:
date_str
Out[29]:
In [30]:
datetime.datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
Out[30]:
In [31]:
def date_str2_date(date_str):
return datetime.datetime.strptime(date_str[:-3], "%Y-%m-%d %H:%M:%S")
In [32]:
timestamp = bird_data.date_time.apply(date_str2_date)
In [33]:
timestamp[:10]
Out[33]:
In [34]:
bird_data['timestamp'] = timestamp
In [35]:
bird_data.columns
Out[35]:
In [36]:
time = bird_data.timestamp[idx] # Eric
In [37]:
elasped_time = time - time[0]
In [38]:
elasped_time[:10]
Out[38]:
In [39]:
elasped_time[1000] / datetime.timedelta(days=1)
Out[39]:
In [40]:
elasped_time[1000] / datetime.timedelta(hours=1)
Out[40]:
In [41]:
elasped_days = elasped_time / datetime.timedelta(days=1)
In [42]:
next_day = 1
ins = []
daily_mean_speed = []
for i,t in enumerate(elasped_days):
if t < next_day:
ins.append(next_day)
else:
daily_mean_speed.append(np.mean(bird_data.speed_2d[ins]))
next_day += 1
ins = []
In [43]:
plt.figure(figsize=(8,6))
plt.plot(daily_mean_speed)
plt.xlabel('Day')
plt.ylabel('Speed 2d m/s')
Out[43]:
In [44]:
idx = bird_data.bird_name == 'Sanne'
In [45]:
bird_data.columns
Out[45]:
In [46]:
bird_data.date_time[idx]
Out[46]:
In [48]:
import cartopy.crs as ccrs
import cartopy.feature as cfeature
In [49]:
proj = ccrs.Mercator()
plt.figure(figsize=(10,10))
ax = plt.axes(projection=proj)
ax.set_extent((-25.0,20.0,52.0,10.0))
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS, linestyle=':')
for name in all_bird_names:
idx = bird_data.bird_name == name
x, y = bird_data.longitude[idx], bird_data.latitude[idx]
ax.plot(x,y, '.',transform=ccrs.Geodetic(), label=name)
plt.legend(loc='best')
Out[49]:
In [53]:
grouped_birds = bird_data.groupby("bird_name")
In [54]:
bird_data.date_time = pd.to_datetime(bird_data.date_time)
In [57]:
bird_data["date"] = bird_data.date_time.dt.date
In [65]:
grouped_bydates = bird_data.groupby(['bird_name','date'])
In [66]:
grouped_birdday = bird_data.groupby(['bird_name','date'])
In [67]:
eric_daily_speed = grouped_bydates.speed_2d.mean()["Eric"]
sanne_daily_speed = grouped_bydates.speed_2d.mean()["Sanne"]
nico_daily_speed = grouped_bydates.speed_2d.mean()["Nico"]
eric_daily_speed.plot(label="Eric")
sanne_daily_speed.plot(label="Sanne")
nico_daily_speed.plot(label="Nico")
plt.legend(loc="best")
Out[67]: