This github repository keeps the code and data for calving detection.
- Python 3.11.12
- Libraries:
os,pandas,numpy,scikit-learn,random,matplotlib,joblib,seaborn,datetime,dateutil,pytz,torch
This project focuses on detecting calving behavior in cattle using sensor data. It is divided into two main stages:
First change all the file paths marked with # Change Required #
The paths in the code point to folders in this Github Repo just to indicate which data is being used. When you actually run it you may want to download the data and change the corresponding path to your local path.
- Load and parse CSV files (Processed_data) from the Processed_data folder (one file per cow).
-
Compute features:
-
KNN_Sum: sum of KNN2–KNN5 -
ts: derived behavioral index usingActivityCount_Difference,Distance_moved, andKNN_Sum -
sin_hour:$np.sin(2 * pi * hours / 24)$ -
cos_hour:$np.cos(2 * pi * hours / 24)$
-
-
Drop null entries and set
Rounded_Timeas the index. - Remove outliers using IQR filtering on each feature.
-
Normalize the data using
MinMaxScaler(individually per cow). - Save the fitted scaler for future use.
- Train the autoencoder model.
- Process test data.
- Compute features needed
- Drop null entries
- Normalize the test data using saved
MinMaxScaler(individually per cow).
- Apply the trained AE model to detect anomalies.
- Individual Min-Max scaler files for each cow
- Trained autoencoder model
- Anomalies
First change all the file paths marked with # Change Required #
- Load detected anomaly CSVs for each cow.
- Add sliding features including (MA, STD, Diff1, and Diff3) for each feature.
- Moving average (MA) and standard deviation (STD) over 6 and 12 hours
- Difference from previous hours (1 and 3 hours)
- Applied on features: KNN2–KNN5, Distance_moved, ActivityCount_Difference
- Label anomalies with boolean value to mark if it's a calving event.
- Train Random Forest model
- Trained Random Forest model