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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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
"""
Data Tool - Data Analysis Module
Handles CSV/data file analysis and reporting
"""

import os
from pathlib import Path
from typing import Dict, List, Optional, Any
from datetime import datetime


class DataTool:
    """
    Data analysis tool for reading and analyzing data files.
    Supports CSV, JSON, and other common data formats.
    """
    
    def __init__(self):
        """Initialize Data Tool"""
        pass
    
    def read_csv(self, file_path: str, encoding: str = 'utf-8') -> Dict[str, any]:
        """
        Read a CSV file and return its contents.
        
        Args:
            file_path: Path to CSV file
            encoding: File encoding
            
        Returns:
            Dictionary with data and metadata
        """
        try:
            import pandas as pd
            
            path = Path(file_path)
            
            if not path.exists():
                return {"success": False, "error": f"File not found: {file_path}"}
            
            df = pd.read_csv(path, encoding=encoding)
            
            return {
                "success": True,
                "file_path": str(path.absolute()),
                "rows": len(df),
                "columns": len(df.columns),
                "column_names": df.columns.tolist(),
                "dtypes": df.dtypes.astype(str).to_dict(),
                "preview": df.head(10).to_dict('records'),
                "dataframe": df  # For further processing
            }
            
        except ImportError:
            return {"success": False, "error": "Pandas not installed. Run: pip install pandas"}
        except Exception as e:
            return {"success": False, "error": f"Error reading CSV: {str(e)}"}
    
    def analyze_data(self, file_path: str) -> Dict[str, any]:
        """
        Perform comprehensive data analysis.
        
        Args:
            file_path: Path to data file
            
        Returns:
            Analysis results
        """
        try:
            import pandas as pd
            import numpy as np
            
            # Read the file
            read_result = self.read_csv(file_path)
            
            if not read_result.get("success"):
                return read_result
            
            df = read_result["dataframe"]
            
            # Basic statistics
            analysis = {
                "success": True,
                "file_path": file_path,
                "basic_info": {
                    "rows": len(df),
                    "columns": len(df.columns),
                    "column_names": df.columns.tolist(),
                    "memory_usage": f"{df.memory_usage(deep=True).sum() / 1024:.2f} KB"
                },
                "missing_values": df.isnull().sum().to_dict(),
                "data_types": df.dtypes.astype(str).to_dict(),
                "numeric_summary": {},
                "categorical_summary": {},
                "insights": []
            }
            
            # Numeric columns analysis
            numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()
            
            if numeric_cols:
                numeric_stats = df[numeric_cols].describe().to_dict()
                analysis["numeric_summary"] = numeric_stats
                
                # Generate insights
                for col in numeric_cols:
                    mean_val = df[col].mean()
                    std_val = df[col].std()
                    
                    if std_val > mean_val:
                        analysis["insights"].append(
                            f"Column '{col}' has high variability (std > mean)"
                        )
                    
                    # Check for outliers
                    q1 = df[col].quantile(0.25)
                    q3 = df[col].quantile(0.75)
                    iqr = q3 - q1
                    outliers = ((df[col] < q1 - 1.5 * iqr) | (df[col] > q3 + 1.5 * iqr)).sum()
                    
                    if outliers > 0:
                        analysis["insights"].append(
                            f"Column '{col}' has {outliers} potential outliers"
                        )
            
            # Categorical columns analysis
            categorical_cols = df.select_dtypes(include=['object']).columns.tolist()
            
            if categorical_cols:
                for col in categorical_cols:
                    value_counts = df[col].value_counts().head(10).to_dict()
                    unique_count = df[col].nunique()
                    
                    analysis["categorical_summary"][col] = {
                        "unique_values": unique_count,
                        "top_values": value_counts
                    }
                    
                    # Insight for categorical
                    if unique_count == len(df):
                        analysis["insights"].append(
                            f"Column '{col}' appears to be a unique identifier"
                        )
            
            # Check for missing values
            missing_total = df.isnull().sum().sum()
            if missing_total > 0:
                missing_pct = (missing_total / (len(df) * len(df.columns))) * 100
                analysis["insights"].append(
                    f"Dataset has {missing_total} missing values ({missing_pct:.2f}% of total)"
                )
            
            # Check for duplicates
            duplicates = df.duplicated().sum()
            if duplicates > 0:
                analysis["insights"].append(
                    f"Dataset has {duplicates} duplicate rows"
                )
            
            return analysis
            
        except ImportError:
            return {"success": False, "error": "Pandas/NumPy not installed"}
        except Exception as e:
            return {"success": False, "error": f"Analysis error: {str(e)}"}
    
    def generate_summary(self, file_path: str) -> Dict[str, any]:
        """
        Generate a text summary of the data analysis.
        
        Args:
            file_path: Path to data file
            
        Returns:
            Summary dictionary with formatted text
        """
        analysis = self.analyze_data(file_path)
        
        if not analysis.get("success"):
            return analysis
        
        summary_lines = [
            "=" * 60,
            "DATA ANALYSIS SUMMARY",
            "=" * 60,
            f"File: {analysis['file_path']}",
            f"Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
            "",
            "BASIC INFORMATION",
            "-" * 40,
            f"Total Rows: {analysis['basic_info']['rows']}",
            f"Total Columns: {analysis['basic_info']['columns']}",
            f"Memory Usage: {analysis['basic_info']['memory_usage']}",
            f"Columns: {', '.join(analysis['basic_info']['column_names'])}",
            ""
        ]
        
        # Numeric summary
        if analysis["numeric_summary"]:
            summary_lines.extend([
                "NUMERIC COLUMNS SUMMARY",
                "-" * 40
            ])
            
            for col, stats in analysis["numeric_summary"].items():
                summary_lines.append(f"\n{col}:")
                summary_lines.append(f"  Mean: {stats.get('mean', 'N/A'):.2f}")
                summary_lines.append(f"  Std: {stats.get('std', 'N/A'):.2f}")
                summary_lines.append(f"  Min: {stats.get('min', 'N/A'):.2f}")
                summary_lines.append(f"  Max: {stats.get('max', 'N/A'):.2f}")
        
        # Categorical summary
        if analysis["categorical_summary"]:
            summary_lines.extend([
                "",
                "CATEGORICAL COLUMNS SUMMARY",
                "-" * 40
            ])
            
            for col, info in analysis["categorical_summary"].items():
                summary_lines.append(f"\n{col}:")
                summary_lines.append(f"  Unique Values: {info['unique_values']}")
                top_val = list(info['top_values'].items())[:3]
                for val, count in top_val:
                    summary_lines.append(f"  - {val}: {count}")
        
        # Missing values
        missing = analysis["missing_values"]
        cols_with_missing = {k: v for k, v in missing.items() if v > 0}
        
        if cols_with_missing:
            summary_lines.extend([
                "",
                "MISSING VALUES",
                "-" * 40
            ])
            for col, count in cols_with_missing.items():
                summary_lines.append(f"  {col}: {count}")
        
        # Insights
        if analysis["insights"]:
            summary_lines.extend([
                "",
                "KEY INSIGHTS",
                "-" * 40
            ])
            for insight in analysis["insights"]:
                summary_lines.append(f"• {insight}")
        
        summary_lines.extend(["", "=" * 60])
        
        summary_text = "\n".join(summary_lines)
        
        return {
            "success": True,
            "summary_text": summary_text,
            "analysis": analysis
        }
    
    def save_analysis(
        self,
        file_path: str,
        output_path: str = None,
        format: str = "txt"
    ) -> Dict[str, any]:
        """
        Analyze data and save results to file.
        
        Args:
            file_path: Path to data file
            output_path: Path for output file
            format: Output format ('txt' or 'json')
            
        Returns:
            Result dictionary
        """
        summary = self.generate_summary(file_path)
        
        if not summary.get("success"):
            return summary
        
        if output_path is None:
            from config import OUTPUT_DIR
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            output_path = OUTPUT_DIR / f"analysis_{timestamp}.{format}"
        
        try:
            output = Path(output_path)
            output.parent.mkdir(parents=True, exist_ok=True)
            
            if format == "json":
                import json
                # Remove non-serializable dataframe
                analysis = summary["analysis"].copy()
                analysis.pop("dataframe", None)
                
                with open(output, 'w', encoding='utf-8') as f:
                    json.dump(analysis, f, indent=2, default=str)
            else:
                with open(output, 'w', encoding='utf-8') as f:
                    f.write(summary["summary_text"])
            
            return {
                "success": True,
                "message": f"Analysis saved to {output_path}",
                "file_path": str(output.absolute())
            }
            
        except Exception as e:
            return {"success": False, "error": f"Error saving analysis: {str(e)}"}
    
    def filter_data(
        self,
        file_path: str,
        conditions: Dict[str, any],
        output_path: str = None
    ) -> Dict[str, any]:
        """
        Filter data based on conditions.
        
        Args:
            file_path: Path to data file
            conditions: Dict of column: value or column: {operator: value}
            output_path: Optional path to save filtered data
            
        Returns:
            Filtered data results
        """
        try:
            import pandas as pd
            
            read_result = self.read_csv(file_path)
            
            if not read_result.get("success"):
                return read_result
            
            df = read_result["dataframe"]
            mask = pd.Series([True] * len(df))
            
            for col, condition in conditions.items():
                if col not in df.columns:
                    continue
                
                if isinstance(condition, dict):
                    for op, value in condition.items():
                        if op == "gt":
                            mask &= df[col] > value
                        elif op == "lt":
                            mask &= df[col] < value
                        elif op == "gte":
                            mask &= df[col] >= value
                        elif op == "lte":
                            mask &= df[col] <= value
                        elif op == "contains":
                            mask &= df[col].astype(str).str.contains(value, case=False)
                else:
                    mask &= df[col] == condition
            
            filtered_df = df[mask]
            
            result = {
                "success": True,
                "original_rows": len(df),
                "filtered_rows": len(filtered_df),
                "preview": filtered_df.head(20).to_dict('records')
            }
            
            # Save if output path provided
            if output_path:
                filtered_df.to_csv(output_path, index=False)
                result["saved_to"] = output_path
            
            return result
            
        except Exception as e:
            return {"success": False, "error": f"Filter error: {str(e)}"}
    
    def get_correlation(self, file_path: str) -> Dict[str, any]:
        """
        Calculate correlation matrix for numeric columns.
        
        Args:
            file_path: Path to data file
            
        Returns:
            Correlation matrix
        """
        try:
            import pandas as pd
            import numpy as np
            
            read_result = self.read_csv(file_path)
            
            if not read_result.get("success"):
                return read_result
            
            df = read_result["dataframe"]
            numeric_df = df.select_dtypes(include=[np.number])
            
            if numeric_df.empty:
                return {"success": False, "error": "No numeric columns found"}
            
            corr_matrix = numeric_df.corr()
            
            # Find high correlations
            high_corr = []
            for i in range(len(corr_matrix.columns)):
                for j in range(i + 1, len(corr_matrix.columns)):
                    corr_val = corr_matrix.iloc[i, j]
                    if abs(corr_val) > 0.7:
                        high_corr.append({
                            "column1": corr_matrix.columns[i],
                            "column2": corr_matrix.columns[j],
                            "correlation": round(corr_val, 3)
                        })
            
            return {
                "success": True,
                "correlation_matrix": corr_matrix.to_dict(),
                "high_correlations": high_corr
            }
            
        except Exception as e:
            return {"success": False, "error": f"Correlation error: {str(e)}"}
    
    def create_sample_csv(self, output_path: str) -> Dict[str, any]:
        """
        Create a sample CSV file for testing.
        
        Args:
            output_path: Path to save sample file
            
        Returns:
            Result dictionary
        """
        try:
            import random
            
            # Generate sample data
            headers = ["ID", "Name", "Age", "City", "Salary", "Department"]
            
            names = ["Alice", "Bob", "Charlie", "Diana", "Eve", "Frank", "Grace", "Henry"]
            cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
            departments = ["Engineering", "Marketing", "Sales", "HR", "Finance"]
            
            rows = []
            for i in range(1, 51):
                row = [
                    i,
                    random.choice(names),
                    random.randint(22, 60),
                    random.choice(cities),
                    random.randint(30000, 120000),
                    random.choice(departments)
                ]
                rows.append(row)
            
            # Write to file
            path = Path(output_path)
            path.parent.mkdir(parents=True, exist_ok=True)
            
            with open(path, 'w', encoding='utf-8') as f:
                f.write(",".join(headers) + "\n")
                for row in rows:
                    f.write(",".join(map(str, row)) + "\n")
            
            return {
                "success": True,
                "message": f"Sample CSV created: {output_path}",
                "file_path": str(path.absolute()),
                "rows": len(rows),
                "columns": len(headers)
            }
            
        except Exception as e:
            return {"success": False, "error": f"Error creating sample: {str(e)}"}