https://doi.org/10.5281/zenodo.14318846
02 - filter_data_near_pass_new.py
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 27 11:18:23 2020
Code to filter if a fish is near the ladder entrance based on a coordinate and
proximity to it.
First, the code checks all fish positions and marks if within 10m of the
coordinate.
Next, the code retains only entrance points (1s) to create a dataframe of all
times fish entered the shapefiles. These dataframes are saved per fish. These points are filtered on a predefined time gap
(currently 7200s == 2 hours) to remove points within that time frame of
each other. The time between retained entry events is also saved (in seconds).
The resulting dataframes are saved individually per fish.
Next the code extracts 60mins of tracks preceeding an entry, e.g. the approach.
It takes the entry time from an approach, calculates the time 60mins before
and takes all detections from the full dataframe in that time period, plus an
extra 5s at the end. These files are saved individually per approach.
Note: some of the variable names reflect previous ways and durations used on the data.
@author: Rachel
"""
#%% set up
#------ getting set up ---------------#
import os
import pandas as pd
import numpy as np
import shapefile
import datetime
#code below is to save the location fo where you are
proj_directory = os.getcwd()
#need this command to import the Functions
os.chdir(proj_directory+'\\python code')
from functions import (filter_time_gap)
#then return to full directory
os.chdir(proj_directory)
#%% file/variable defining
#change these if you want different folders
#folder containing input data
input_folder = proj_directory+'\\data\data filtered NEW per fish\\'
assert os.path.exists(input_folder)
#extract file names so can read in as loop
raw_file_names = os.listdir(input_folder)
#output to save sheets of entrance events NOT filtered by time gap
output_loc_single_points = ('\\data\data near ladder NEW per fish single points\\')
#output for 60min tracks
output_tracks_folder = proj_directory+'\\data\data near ladder NEW 60mins\\'
assert os.path.exists(output_tracks_folder)
#fish pass entrance coordinates
Entrance = pd.DataFrame(columns=['UTM-x', 'UTM-y'], data={'UTM-x': [591683.2], 'UTM-y': [5296867.5]})
#code to check paths exist
#assert os.path.exists (proj_directory)
#other variables
#threshold for time gap we filter entry events for
time_gap = 7200 #define a threshold for entry events, in seconds
#threshold for track length
threshold = '60min'
#%%LOOP to filter for all files
for i in raw_file_names:
#read in csv
start_time_total = datetime.datetime.now()
data = pd.read_csv(input_folder+i, index_col='Time', parse_dates=['Time'])
#save fish id as variable to append when saving files.
fish_ID = data['id'][0]
print('STARTED fish ',str(fish_ID),' at time:', start_time_total)
#here check data exists e.g. more than 1 line of data
if (len(data)>0):
#calculating distance from fish pass coordinates
data['dist_x'] = (data['x'] - Entrance['UTM-x'][0])
data['dist_y'] = (data['y'] - Entrance['UTM-y'][0])
data['dist from fishway'] = np.sqrt(data['dist_x'] ** 2 + data['dist_y'] ** 2)
data.drop({'dist_x', 'dist_y'}, axis=1, inplace=True)
#if "near entrance", mark as 1, otherwise 0
data['near_entrance'] = np.where(data['dist from fishway'] <= 10,1,0)
#filter to keep only points near entrance
data_near_entrance = data[(data['near_entrance']==1)]
#SAVE HERE as a back up for time gap filtering
# data_near_entrance.to_csv(proj_directory+output_loc_single_points+str(fish_ID)+
# '_points_near_ladder_unfiltered_on_time.csv')
unfilt_export_time = datetime.datetime.now()
print('UNFILTERED NEAR LADDER EXPORTED, fish ',str(fish_ID), 'at time:', unfilt_export_time)
data_near_entrance= filter_time_gap(data_near_entrance,time_gap)
#extract rest of track for 30min tracks
#if test diff track lengths, use loop from here
#if section, so that if no approaches, doesnt kill
if (len(data_near_entrance)>0):
#save fish ID as a variable
fish_ID = data_near_entrance['id'][0]
fish_ID_str = str(fish_ID)
#create spare time column to do stuff
data_near_entrance['Time2'] = data_near_entrance.index
#and get index of the full data frame
data['Time2'] = data.index
#create blank dataframe to make
final_df = pd.DataFrame()
for i in range(len(data_near_entrance['id'])):
entry_time = data_near_entrance['Time2'][i]
entry_time = pd.to_datetime(entry_time)#.tz_localize('UTC')
start_time = entry_time - pd.Timedelta(threshold)
start_time = pd.to_datetime(start_time)
#increase entry time purely to include that point in visualisation
entry_time = entry_time + pd.Timedelta('5s')
#extract points
last_30_mins = data.loc[start_time:entry_time]
last_30_mins['approach'] = i+1
last_30_mins = last_30_mins.drop(['Time2'], axis=1)
#include filter if x % of points within an area 15m of ladder
last_30_mins['if within 15m of ladder'] = np.where(last_30_mins['dist from fishway']<15,1,0)
within_dist = last_30_mins[last_30_mins['if within 15m of ladder']==1].count()['id']
outwith_dist = last_30_mins[last_30_mins['if within 15m of ladder']==0].count()['id']
percentage_in = within_dist/(within_dist+outwith_dist)
if percentage_in>=0.9:
#possibly want to have this sort of data saved though? so may want to do in R instead later?
#or new column to mark this for easy filtering?
last_30_mins['most_track_near_ladder'] = 'Yes'
else:
last_30_mins['most_track_near_ladder'] = 'No'
#append to larger df so can export as 1 per fish
final_df = pd.concat([final_df,last_30_mins])
file_name = fish_ID_str+'_near_ladder_last_'+threshold+'_all_approachs.csv'
#currrently saving files per individual approach
#can later combine them into big file
final_df.to_csv(output_tracks_folder+file_name)
end_time= datetime.datetime.now()
total_time = end_time-start_time_total
print('EXPORTED: fish ',str(fish_ID),'at time:',end_time,'. Time taken:', total_time)