File size: 12,270 Bytes
91614ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Data processing utilities for the Finance Expert model
"""
import json
import os
from pathlib import Path
import jsonlines
from typing import Dict, List, Any, Optional, Tuple
import hashlib
import datetime
import logging
import numpy as np
import pandas as pd
from datasets import Dataset
from tqdm import tqdm
import re
from dateutil.parser import parse as date_parse
from decimal import Decimal, ROUND_HALF_UP

class FinanceDataProcessor:
    def __init__(self, output_dir: str = "processed_data"):
        self.output_dir = Path(output_dir)
        self.output_dir.mkdir(exist_ok=True)
        self.logger = self._setup_logger()
        
    def _setup_logger(self) -> logging.Logger:
        """Setup logging specific to finance data processing"""
        logger = logging.getLogger(__name__)
        logger.setLevel(logging.INFO)
        handler = logging.StreamHandler()
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        handler.setFormatter(formatter)
        logger.addHandler(handler)
        return logger

    def process_financial_data(self, data: Dict[str, Any]) -> Dict[str, Any]:
        """Process and normalize financial data"""
        try:
            # Handle different data types
            processed = self._normalize_data(data)
            
            # Extract financial metrics
            metrics = self._extract_financial_metrics(processed)
            
            # Validate financial data
            validation = self._validate_financial_data(processed)
            
            # Generate financial ratios
            ratios = self._calculate_financial_ratios(processed)
            
            return {
                "processed_data": processed,
                "metrics": metrics,
                "validation": validation,
                "ratios": ratios
            }
        except Exception as e:
            self.logger.warning(f"Error processing financial data: {str(e)}")
            return {"error": str(e)}

    def _normalize_data(self, data: Dict[str, Any]) -> Dict[str, Any]:
        """Normalize financial data types and formats"""
        normalized = {}
        
        for key, value in data.items():
            if isinstance(value, str):
                # Handle currency and number formatting
                if any(c in value for c in ["$", "€", "£", "¥"]):
                    normalized[key] = self._normalize_currency(value)
                elif value.isdigit():
                    normalized[key] = int(value)
                elif self._is_float(value):
                    normalized[key] = float(value)
                else:
                    normalized[key] = value.strip()
            elif isinstance(value, (int, float)):
                normalized[key] = value
            elif isinstance(value, dict):
                normalized[key] = self._normalize_data(value)
            elif isinstance(value, list):
                normalized[key] = [self._normalize_data(item) if isinstance(item, dict) else item for item in value]
            else:
                normalized[key] = value
        
        return normalized

    def _normalize_currency(self, value: str) -> float:
        """Convert currency strings to standardized format"""
        try:
            # Remove currency symbols and commas
            value = re.sub(r'[\$€£¥,]', '', value)
            # Handle negative numbers
            value = value.replace('(', '').replace(')', '')
            # Convert to float with proper decimal places
            return float(value)
        except:
            return 0.0

    def _is_float(self, value: str) -> bool:
        """Check if string can be converted to float"""
        try:
            float(value)
            return True
        except ValueError:
            return False

    def _extract_financial_metrics(self, data: Dict[str, Any]) -> Dict[str, Any]:
        """Extract key financial metrics"""
        metrics = {
            "revenue": self._get_metric(data, "revenue", "income", "sales"),
            "expenses": self._get_metric(data, "expenses", "costs"),
            "profit": self._get_metric(data, "profit", "net_income"),
            "assets": self._get_metric(data, "assets", "total_assets"),
            "liabilities": self._get_metric(data, "liabilities", "total_liabilities"),
            "equity": self._get_metric(data, "equity", "shareholders_equity")
        }
        return metrics

    def _get_metric(self, data: Dict[str, Any], *keys: str) -> float:
        """Get metric value from various possible keys"""
        for key in keys:
            if key in data:
                return self._normalize_currency(str(data[key]))
        return 0.0

    def _validate_financial_data(self, data: Dict[str, Any]) -> Dict[str, bool]:
        """Validate financial data consistency"""
        validation = {
            "balance_sheet_consistency": self._check_balance_sheet(data),
            "income_statement_consistency": self._check_income_statement(data),
            "cash_flow_consistency": self._check_cash_flow(data)
        }
        return validation

    def _check_balance_sheet(self, data: Dict[str, Any]) -> bool:
        """Check balance sheet consistency"""
        assets = self._get_metric(data, "assets", "total_assets")
        liabilities = self._get_metric(data, "liabilities", "total_liabilities")
        equity = self._get_metric(data, "equity", "shareholders_equity")
        
        return abs(assets - (liabilities + equity)) < 1e-6

    def _check_income_statement(self, data: Dict[str, Any]) -> bool:
        """Check income statement consistency"""
        revenue = self._get_metric(data, "revenue", "income", "sales")
        expenses = self._get_metric(data, "expenses", "costs")
        profit = self._get_metric(data, "profit", "net_income")
        
        return abs(profit - (revenue - expenses)) < 1e-6

    def _check_cash_flow(self, data: Dict[str, Any]) -> bool:
        """Check cash flow statement consistency"""
        operating = self._get_metric(data, "operating_cash_flow")
        investing = self._get_metric(data, "investing_cash_flow")
        financing = self._get_metric(data, "financing_cash_flow")
        net_change = self._get_metric(data, "net_change_in_cash")
        
        return abs(net_change - (operating + investing + financing)) < 1e-6

    def _calculate_financial_ratios(self, data: Dict[str, Any]) -> Dict[str, float]:
        """Calculate key financial ratios"""
        try:
            metrics = self._extract_financial_metrics(data)
            
            ratios = {
                "current_ratio": metrics["assets"] / metrics["liabilities"] if metrics["liabilities"] != 0 else float('inf'),
                "debt_to_equity": metrics["liabilities"] / metrics["equity"] if metrics["equity"] != 0 else float('inf'),
                "profit_margin": metrics["profit"] / metrics["revenue"] if metrics["revenue"] != 0 else 0.0,
                "return_on_equity": metrics["profit"] / metrics["equity"] if metrics["equity"] != 0 else 0.0,
                "return_on_assets": metrics["profit"] / metrics["assets"] if metrics["assets"] != 0 else 0.0
            }
            
            return ratios
        except ZeroDivisionError:
            return {"error": "Division by zero in ratio calculation"}

    def process_dataset(self, dataset: Dataset, dataset_name: str) -> List[Dict[str, Any]]:
        """Process a complete financial dataset"""
        processed = []
        error_count = 0
        
        self.logger.info(f"Processing {dataset_name} dataset with {len(dataset)} samples")
        
        for idx, example in enumerate(tqdm(dataset, desc=f"Processing {dataset_name}")):
            try:
                processed_example = self._process_example(example, dataset_name)
                processed.append(processed_example)
            except Exception as e:
                error_count += 1
                self.logger.error(f"Error processing example {idx} in {dataset_name}: {str(e)}")
        
        self.logger.info(f"Processed {len(processed)} examples")
        self.logger.info(f"Encountered {error_count} errors")
        
        return processed

    def _process_example(self, example: Dict[str, Any], dataset_name: str) -> Dict[str, Any]:
        """Process a single example based on dataset type"""
        if dataset_name == "FinQA":
            return self._process_finqa(example)
        elif dataset_name == "TAT-QA":
            return self._process_tat_qa(example)
        elif dataset_name == "DocVQA":
            return self._process_docvqa(example)
        elif dataset_name == "FinancialPhraseBank":
            return self._process_phrasebank(example)
        elif dataset_name == "SECFilings":
            return self._process_sec_filings(example)
        elif dataset_name == "FRED":
            return self._process_fred(example)
        else:
            raise ValueError(f"Unknown dataset: {dataset_name}")

    def _process_finqa(self, example: Dict[str, Any]) -> Dict[str, Any]:
        """Process FinQA example"""
        return {
            "question": example["question"].strip(),
            "table": example["table"],
            "answer": example["answer"],
            "program": example["program"],
            "data_analysis": self.process_financial_data(example["table"])
        }

    def _process_tat_qa(self, example: Dict[str, Any]) -> Dict[str, Any]:
        """Process TAT-QA example"""
        return {
            "passage": example["passage"].strip(),
            "question": example["question"].strip(),
            "answer": example["answer"],
            "scale": example["scale"],
            "type": example["type"],
            "data_analysis": self.process_financial_data({"passage": example["passage"]})
        }

    def _process_docvqa(self, example: Dict[str, Any]) -> Dict[str, Any]:
        """Process DocVQA example"""
        return {
            "question": example["question"].strip(),
            "image": example["image"],
            "answer": example["answer"],
            "type": example["type"],
            "data_analysis": self.process_financial_data({"answer": example["answer"]})
        }

    def _process_phrasebank(self, example: Dict[str, Any]) -> Dict[str, Any]:
        """Process FinancialPhraseBank example"""
        return {
            "sentence": example["sentence"].strip(),
            "label": example["label"],
            "sentiment_analysis": self._analyze_sentiment(example["sentence"])  # Reuse sentiment analysis
        }

    def _process_sec_filings(self, example: Dict[str, Any]) -> Dict[str, Any]:
        """Process SEC filings example"""
        return {
            "company": example["company"].strip(),
            "filing_type": example["filing_type"],
            "content": example["content"],
            "date": example["date"],
            "financial_analysis": self.process_financial_data({"content": example["content"]})
        }

    def _process_fred(self, example: Dict[str, Any]) -> Dict[str, Any]:
        """Process FRED example"""
        return {
            "series_id": example["series_id"],
            "date": example["date"],
            "value": example["value"],
            "economic_analysis": self._analyze_economic_data(example)
        }

    def save_to_jsonl(self, data: List[Dict[str, Any]], filename: str) -> Path:
        """Save processed data to JSONL file"""
        filepath = self.output_dir / filename
        with jsonlines.open(filepath, mode='w') as writer:
            writer.write_all(data)
        self.logger.info(f"Saved data to {filepath}")
        return filepath

    def print_sample(self, data: List[Dict[str, Any]], count: int = 3):
        """Print sample of processed data"""
        self.logger.info("\nSample data:")
        for i, example in enumerate(data[:count]):
            self.logger.info(f"\nSample {i+1}:")
            self.logger.info(json.dumps(example, indent=2))

    def print_memory_usage(self):
        """Print current memory usage"""
        process = psutil.Process()
        memory_info = process.memory_info()
        self.logger.info(f"Current memory usage: {memory_info.rss / 1024 / 1024:.2f} MB")