File size: 9,303 Bytes
48909ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
"""
Data processing module for the Business Intelligence Dashboard.

This module handles data loading, cleaning, filtering, and profiling
using the Strategy Pattern for different data operations.
"""

from abc import ABC, abstractmethod
from typing import Dict, List, Optional, Tuple, Any
import pandas as pd
import numpy as np
from utils import detect_column_types, validate_dataframe, get_missing_value_summary
from constants import MIN_NUMERICAL_COLUMNS_FOR_CORRELATION


class DataLoadStrategy(ABC):
    """Abstract base class for data loading strategies."""
    
    @abstractmethod
    def load(self, file_path: str) -> pd.DataFrame:
        """
        Load data from file.
        
        Args:
            file_path: Path to the data file
            
        Returns:
            Loaded DataFrame
        """
        pass


class CSVLoadStrategy(DataLoadStrategy):
    """Strategy for loading CSV files."""
    
    def load(self, file_path: str) -> pd.DataFrame:
        """Load CSV file."""
        return pd.read_csv(file_path)


class ExcelLoadStrategy(DataLoadStrategy):
    """Strategy for loading Excel files."""
    
    def load(self, file_path: str) -> pd.DataFrame:
        """Load Excel file."""
        return pd.read_excel(file_path)


class DataLoader:
    """Context class for data loading using Strategy Pattern."""
    
    def __init__(self):
        """Initialize with default strategies."""
        self._strategies = {
            '.csv': CSVLoadStrategy(),
            '.xlsx': ExcelLoadStrategy(),
            '.xls': ExcelLoadStrategy()
        }
    
    def load_data(self, file_path: str) -> Tuple[pd.DataFrame, Optional[str]]:
        """
        Load data file using appropriate strategy.
        
        Args:
            file_path: Path to the data file
            
        Returns:
            Tuple of (DataFrame, error_message)
        """
        try:
            import os
            _, ext = os.path.splitext(file_path.lower())
            
            if ext not in self._strategies:
                return None, f"Unsupported file format: {ext}"
            
            strategy = self._strategies[ext]
            df = strategy.load(file_path)
            
            # Validate loaded data
            is_valid, error = validate_dataframe(df)
            if not is_valid:
                return None, error
            
            return df, None
            
        except Exception as e:
            return None, f"Error loading file: {str(e)}"


class FilterStrategy(ABC):
    """Abstract base class for filtering strategies."""
    
    @abstractmethod
    def apply_filter(
        self, 
        df: pd.DataFrame, 
        column: str, 
        filter_value: Any
    ) -> pd.DataFrame:
        """
        Apply filter to DataFrame.
        
        Args:
            df: Input DataFrame
            column: Column to filter on
            filter_value: Filter value/range
            
        Returns:
            Filtered DataFrame
        """
        pass


class NumericalFilterStrategy(FilterStrategy):
    """Strategy for filtering numerical columns."""
    
    def apply_filter(
        self, 
        df: pd.DataFrame, 
        column: str, 
        filter_value: Tuple[float, float]
    ) -> pd.DataFrame:
        """Apply range filter to numerical column."""
        min_val, max_val = filter_value
        return df[(df[column] >= min_val) & (df[column] <= max_val)]


class CategoricalFilterStrategy(FilterStrategy):
    """Strategy for filtering categorical columns."""
    
    def apply_filter(
        self, 
        df: pd.DataFrame, 
        column: str, 
        filter_value: List[str]
    ) -> pd.DataFrame:
        """Apply multi-select filter to categorical column."""
        if not filter_value:
            return df
        return df[df[column].isin(filter_value)]


class DateFilterStrategy(FilterStrategy):
    """Strategy for filtering date columns."""
    
    def apply_filter(
        self, 
        df: pd.DataFrame, 
        column: str, 
        filter_value: Tuple[str, str]
    ) -> pd.DataFrame:
        """Apply date range filter."""
        start_date, end_date = filter_value
        if start_date and end_date:
            df[column] = pd.to_datetime(df[column], errors='coerce')
            return df[(df[column] >= start_date) & (df[column] <= end_date)]
        return df


class DataFilter:
    """Context class for data filtering using Strategy Pattern."""
    
    def __init__(self):
        """Initialize with filter strategies."""
        self._strategies = {
            'numerical': NumericalFilterStrategy(),
            'categorical': CategoricalFilterStrategy(),
            'date': DateFilterStrategy()
        }
    
    def apply_filters(
        self, 
        df: pd.DataFrame, 
        filters: Dict[str, Any]
    ) -> pd.DataFrame:
        """
        Apply multiple filters to DataFrame.
        
        Args:
            df: Input DataFrame
            filters: Dictionary of {column: filter_value}
            
        Returns:
            Filtered DataFrame
        """
        filtered_df = df.copy()
        numerical, categorical, date_columns = detect_column_types(df)
        
        for column, filter_value in filters.items():
            if filter_value is None:
                continue
            
            if column in numerical:
                strategy = self._strategies['numerical']
            elif column in categorical:
                strategy = self._strategies['categorical']
            elif column in date_columns:
                strategy = self._strategies['date']
            else:
                continue
            
            try:
                filtered_df = strategy.apply_filter(filtered_df, column, filter_value)
            except Exception as e:
                print(f"Error applying filter to {column}: {e}")
                continue
        
        return filtered_df


class DataProfiler:
    """Class for generating data profiling and statistics."""
    
    @staticmethod
    def get_basic_info(df: pd.DataFrame) -> Dict[str, Any]:
        """
        Get basic dataset information.
        
        Args:
            df: Input DataFrame
            
        Returns:
            Dictionary with basic info
        """
        return {
            'shape': df.shape,
            'columns': list(df.columns),
            'dtypes': df.dtypes.to_dict(),
            'memory_usage': df.memory_usage(deep=True).sum()
        }
    
    @staticmethod
    def get_numerical_stats(df: pd.DataFrame) -> pd.DataFrame:
        """
        Get statistics for numerical columns.
        
        Args:
            df: Input DataFrame
            
        Returns:
            DataFrame with numerical statistics, with column names as a column
        """
        numerical, _, _ = detect_column_types(df)
        if not numerical:
            return pd.DataFrame()
        
        stats = df[numerical].describe()
        stats.loc['median'] = df[numerical].median()
        stats.loc['std'] = df[numerical].std()
        
        # Transpose so column names become rows (index)
        stats_transposed = stats.T
        
        # Reset index to make column names a regular column for display
        stats_transposed = stats_transposed.reset_index()
        stats_transposed.rename(columns={'index': 'Column'}, inplace=True)
        
        # Reorder columns for better readability (Column first, then statistics)
        column_order = ['Column', 'count', 'mean', 'std', 'min', '25%', '50%', '75%', 'max', 'median']
        # Only include columns that exist
        available_columns = [col for col in column_order if col in stats_transposed.columns]
        stats_transposed = stats_transposed[available_columns]
        
        return stats_transposed
    
    @staticmethod
    def get_categorical_stats(df: pd.DataFrame) -> pd.DataFrame:
        """
        Get statistics for categorical columns.
        
        Args:
            df: Input DataFrame
            
        Returns:
            DataFrame with categorical statistics
        """
        _, categorical, _ = detect_column_types(df)
        if not categorical:
            return pd.DataFrame()
        
        stats = []
        for col in categorical:
            unique_count = df[col].nunique()
            mode_value = df[col].mode().iloc[0] if not df[col].mode().empty else None
            mode_count = df[col].value_counts().iloc[0] if not df[col].empty else 0
            
            stats.append({
                'Column': col,
                'Unique_Values': unique_count,
                'Mode': mode_value,
                'Mode_Count': mode_count,
                'Total_Count': len(df)
            })
        
        return pd.DataFrame(stats)
    
    @staticmethod
    def get_correlation_matrix(df: pd.DataFrame) -> pd.DataFrame:
        """
        Get correlation matrix for numerical columns.
        
        Args:
            df: Input DataFrame
            
        Returns:
            Correlation matrix DataFrame
        """
        numerical, _, _ = detect_column_types(df)
        if len(numerical) < MIN_NUMERICAL_COLUMNS_FOR_CORRELATION:
            return pd.DataFrame()
        
        return df[numerical].corr()