Load data file
(file_path)
| 76 | return data_files |
| 77 | |
| 78 | def load_data_file(file_path): |
| 79 | """Load data file""" |
| 80 | try: |
| 81 | if file_path.endswith('.csv'): |
| 82 | df = pd.read_csv(file_path) |
| 83 | elif file_path.endswith('.feather'): |
| 84 | df = pd.read_feather(file_path) |
| 85 | else: |
| 86 | return None, "Unsupported file format" |
| 87 | |
| 88 | # Check required columns |
| 89 | required_cols = ['open', 'high', 'low', 'close'] |
| 90 | if not all(col in df.columns for col in required_cols): |
| 91 | return None, f"Missing required columns: {required_cols}" |
| 92 | |
| 93 | # Process timestamp column |
| 94 | if 'timestamps' in df.columns: |
| 95 | df['timestamps'] = pd.to_datetime(df['timestamps']) |
| 96 | elif 'timestamp' in df.columns: |
| 97 | df['timestamps'] = pd.to_datetime(df['timestamp']) |
| 98 | elif 'date' in df.columns: |
| 99 | # If column name is 'date', rename it to 'timestamps' |
| 100 | df['timestamps'] = pd.to_datetime(df['date']) |
| 101 | else: |
| 102 | # If no timestamp column exists, create one |
| 103 | df['timestamps'] = pd.date_range(start='2024-01-01', periods=len(df), freq='1H') |
| 104 | |
| 105 | # Ensure numeric columns are numeric type |
| 106 | for col in ['open', 'high', 'low', 'close']: |
| 107 | df[col] = pd.to_numeric(df[col], errors='coerce') |
| 108 | |
| 109 | # Process volume column (optional) |
| 110 | if 'volume' in df.columns: |
| 111 | df['volume'] = pd.to_numeric(df['volume'], errors='coerce') |
| 112 | |
| 113 | # Process amount column (optional, but not used for prediction) |
| 114 | if 'amount' in df.columns: |
| 115 | df['amount'] = pd.to_numeric(df['amount'], errors='coerce') |
| 116 | |
| 117 | # Remove rows containing NaN values |
| 118 | df = df.dropna() |
| 119 | |
| 120 | return df, None |
| 121 | |
| 122 | except Exception as e: |
| 123 | return None, f"Failed to load file: {str(e)}" |
| 124 | |
| 125 | def save_prediction_results(file_path, prediction_type, prediction_results, actual_data, input_data, prediction_params): |
| 126 | """Save prediction results to file""" |