Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

https://doi.org/10.5281/zenodo.14318846
17 December 2024, 12:45:03 UTC
  • Code
  • Branches (0)
  • Releases (1)
  • Visits
    • Branches
    • Releases
      • 1
      • 1
    • c8b2287
    • /
    • combining-hmm-and-ssf-code
    • /
    • Code to filter raw data
    • /
    • 02 - filter_data_near_pass_new.py
    Raw File Download

    To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
    Select below a type of object currently browsed in order to display its associated SWHID and permalink.

    • content
    • directory
    • snapshot
    • release
    origin badgecontent badge Iframe embedding
    swh:1:cnt:f36bd8cf4b81c12f2daf78466fb76811154e5cdd
    origin badgedirectory badge Iframe embedding
    swh:1:dir:7e02716be0f7d098f9aea3075eb51d7842c5a320
    origin badgesnapshot badge
    swh:1:snp:05a2af42b588522ca08f036c1f785d8457dcf25e
    origin badgerelease badge
    swh:1:rel:aa35d9e39d94cbf3f73362c2c2b5cd04c355e955

    This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
    Select below a type of object currently browsed in order to generate citations for them.

    • content
    • directory
    • snapshot
    • release
    Generate software citation in BibTex format (requires biblatex-software package)
    Generating citation ...
    Generate software citation in BibTex format (requires biblatex-software package)
    Generating citation ...
    Generate software citation in BibTex format (requires biblatex-software package)
    Generating citation ...
    Generate software citation in BibTex format (requires biblatex-software package)
    Generating citation ...
    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)
            
    

    back to top

    Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
    The source code of Software Heritage itself is available on our development forge.
    The source code files archived by Software Heritage are available under their own copyright and licenses.
    Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API