File size: 15,235 Bytes
c3ab38e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
import streamlit as st
import pandas as pd
from typing import Dict, List, Optional, Any
from pydantic import BaseModel, Field
import base64
import io
import matplotlib.pyplot as plt
import seaborn as sns
from abc import ABC, abstractmethod  # For abstract base classes
from sklearn.model_selection import train_test_split # Machine learning modules
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.tsa.stattools import adfuller
from langchain.prompts import PromptTemplate
from groq import Groq
import os
import numpy as np
from scipy.stats import ttest_ind, f_oneway

# Initialize Groq Client
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))


# ---------------------- Base Classes and Schemas ---------------------------
class ResearchInput(BaseModel):
    """Base schema for research tool inputs"""
    data_key: str = Field(..., description="Session state key containing DataFrame")
    columns: Optional[List[str]] = Field(None, description="List of columns to analyze")

class TemporalAnalysisInput(ResearchInput):
    """Schema for temporal analysis"""
    time_col: str = Field(..., description="Name of timestamp column")
    value_col: str = Field(..., description="Name of value column to analyze")

class HypothesisInput(ResearchInput):
    """Schema for hypothesis testing"""
    group_col: str = Field(..., description="Categorical column defining groups")
    value_col: str = Field(..., description="Numerical column to compare")

class ModelTrainingInput(ResearchInput):
    """Schema for model training"""
    target_col: str = Field(..., description="Name of target column")

class DataAnalyzer(ABC):
    """Abstract base class for data analysis modules"""
    @abstractmethod
    def invoke(self, **kwargs) -> Dict[str, Any]:
        pass

# ---------------------- Concrete Analyzer Implementations ---------------------------
class AdvancedEDA(DataAnalyzer):
    """Comprehensive Exploratory Data Analysis"""
    def invoke(self, data_key: str, **kwargs) -> Dict[str, Any]:
        try:
            data = st.session_state[data_key]
            analysis = {
                "dimensionality": {
                    "rows": len(data),
                    "columns": list(data.columns),
                    "memory_usage": f"{data.memory_usage().sum() / 1e6:.2f} MB"
                },
                "statistical_profile": data.describe(percentiles=[.25, .5, .75]).to_dict(),
                "temporal_analysis": {
                    "date_ranges": {
                        col: {
                            "min": data[col].min(),
                            "max": data[col].max()
                        } for col in data.select_dtypes(include='datetime').columns
                    }
                },
                "data_quality": {
                    "missing_values": data.isnull().sum().to_dict(),
                    "duplicates": data.duplicated().sum(),
                    "cardinality": {
                        col: data[col].nunique() for col in data.columns
                    }
                }
            }
            return analysis
        except Exception as e:
            return {"error": f"EDA Failed: {str(e)}"}

class DistributionVisualizer(DataAnalyzer):
    """Distribution visualizations"""
    def invoke(self, data_key: str, columns: List[str], **kwargs) -> str:
      try:
          data = st.session_state[data_key]
          plt.figure(figsize=(12, 6))
          for i, col in enumerate(columns, 1):
              plt.subplot(1, len(columns), i)
              sns.histplot(data[col], kde=True, stat="density")
              plt.title(f'Distribution of {col}', fontsize=10)
              plt.xticks(fontsize=8)
              plt.yticks(fontsize=8)
          plt.tight_layout()
        
          buf = io.BytesIO()
          plt.savefig(buf, format='png', dpi=300, bbox_inches='tight')
          plt.close()
          return base64.b64encode(buf.getvalue()).decode()
      except Exception as e:
          return f"Visualization Error: {str(e)}"

class TemporalAnalyzer(DataAnalyzer):
    """Time series analysis"""
    def invoke(self, data_key: str, time_col: str, value_col: str, **kwargs) -> Dict[str, Any]:
        try:
            data = st.session_state[data_key]
            ts_data = data.set_index(pd.to_datetime(data[time_col]))[value_col]
            decomposition = seasonal_decompose(ts_data, period=365)
            
            plt.figure(figsize=(12, 8))
            decomposition.plot()
            plt.tight_layout()
            
            buf = io.BytesIO()
            plt.savefig(buf, format='png')
            plt.close()
            plot_data = base64.b64encode(buf.getvalue()).decode()
            
            return {
                "trend_statistics": {
                    "stationarity": adfuller(ts_data)[1],
                    "seasonality_strength": max(decomposition.seasonal)
                },
                "visualization": plot_data
            }
        except Exception as e:
            return {"error": f"Temporal Analysis Failed: {str(e)}"}

class HypothesisTester(DataAnalyzer):
    """Statistical hypothesis testing"""
    def invoke(self, data_key: str, group_col: str, value_col: str, **kwargs) -> Dict[str, Any]:
      try:
        data = st.session_state[data_key]
        groups = data[group_col].unique()
      
        if len(groups) < 2:
            return {"error": "Insufficient groups for comparison"}
        
        if len(groups) == 2:
            group_data = [data[data[group_col] == g][value_col] for g in groups]
            stat, p = ttest_ind(*group_data)
            test_type = "Independent t-test"
        else:
          group_data = [data[data[group_col] == g][value_col] for g in groups]
          stat, p = f_oneway(*group_data)
          test_type = "ANOVA"
        
        return {
            "test_type": test_type,
            "test_statistic": stat,
            "p_value": p,
            "effect_size": {
              "cohens_d": abs(group_data[0].mean() - group_data[1].mean())/np.sqrt(
                    (group_data[0].var() + group_data[1].var())/2
              ) if len(groups) == 2 else None
            },
            "interpretation": self.interpret_p_value(p)
        }
      except Exception as e:
        return {"error": f"Hypothesis Testing Failed: {str(e)}"}

    def interpret_p_value(self, p: float) -> str:
      if p < 0.001: return "Very strong evidence against H0"
      elif p < 0.01: return "Strong evidence against H0"
      elif p < 0.05: return "Evidence against H0"
      elif p < 0.1: return "Weak evidence against H0"
      else: return "No significant evidence against H0"

class LogisticRegressionTrainer(DataAnalyzer):
    """Logistic Regression Model Trainer"""
    def invoke(self, data_key: str, target_col: str, columns: List[str], **kwargs) -> Dict[str, Any]:
      try:
        data = st.session_state[data_key]
        X = data[columns]
        y = data[target_col]
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
        model = LogisticRegression(max_iter=1000)
        model.fit(X_train, y_train)
        y_pred = model.predict(X_test)
        accuracy = accuracy_score(y_test, y_pred)
        return {
          "model_type": "Logistic Regression",
           "accuracy": accuracy,
           "model_params": model.get_params()
         }
      except Exception as e:
         return {"error": f"Logistic Regression Model Error: {str(e)}"}

# ---------------------- Groq Research Agent ---------------------------

class GroqResearcher:
    """Advanced AI Research Engine using Groq"""
    def __init__(self, model_name="mixtral-8x7b-32768"):
        self.model_name = model_name
        self.system_template = """You are a senior data scientist at a research institution. 
        Analyze this dataset with rigorous statistical methods and provide academic-quality insights:
        {dataset_info}
        
        User Question: {query}
        
        Required Format:
        - Executive Summary (1 paragraph)
        - Methodology (bullet points)
        - Key Findings (numbered list)
        - Limitations
        - Recommended Next Steps"""

    def research(self, query: str, data: pd.DataFrame) -> str:
        """Conduct academic-level analysis using Groq"""
        try:
            dataset_info = f"""
            Dataset Dimensions: {data.shape}
            Variables: {', '.join(data.columns)}
            Temporal Coverage: {data.select_dtypes(include='datetime').columns.tolist()}
            Missing Values: {data.isnull().sum().to_dict()}
            """
            
            prompt = PromptTemplate.from_template(self.system_template).format(
                dataset_info=dataset_info,
                query=query
            )
            
            completion = client.chat.completions.create(
                messages=[
                    {"role": "system", "content": "You are a research AI assistant"},
                    {"role": "user", "content": prompt}
                ],
                model=self.model_name,
                temperature=0.2,
                max_tokens=4096,
                stream=False
            )
            
            return completion.choices[0].message.content
        
        except Exception as e:
            return f"Research Error: {str(e)}"
# ---------------------- Main Streamlit Application ---------------------------
def main():
    st.set_page_config(page_title="AI Data Analysis Lab", layout="wide")
    st.title("🧪 Advanced AI Data Analysis Laboratory")

    # Session State
    if 'data' not in st.session_state:
      st.session_state.data = None
    if 'researcher' not in st.session_state:
        st.session_state.researcher = GroqResearcher()

    # Data Upload
    with st.sidebar:
        st.header("🔬 Data Management")
        uploaded_file = st.file_uploader("Upload research dataset", type=["csv", "parquet"])
        if uploaded_file:
            with st.spinner("Initializing dataset..."):
                try:
                    st.session_state.data = pd.read_csv(uploaded_file)
                    st.success(f"Loaded {len(st.session_state.data):,} research observations")
                except Exception as e:
                    st.error(f"Error loading dataset: {e}")


    if st.session_state.data is not None:
        col1, col2 = st.columns([1, 3])
        with col1:
            st.subheader("Dataset Metadata")
            st.json({
                "Variables": list(st.session_state.data.columns),
                "Time Range": {
                    col: {
                        "min": st.session_state.data[col].min(),
                        "max": st.session_state.data[col].max()
                    } for col in st.session_state.data.select_dtypes(include='datetime').columns
                },
                "Size": f"{st.session_state.data.memory_usage().sum() / 1e6:.2f} MB"
            })

        with col2:
           analysis_tab, research_tab = st.tabs(["Automated Analysis", "Custom Research"])
           with analysis_tab:
               analysis_type = st.selectbox("Select Analysis Mode", [
                  "Exploratory Data Analysis",
                  "Temporal Pattern Analysis",
                  "Comparative Statistics",
                  "Distribution Analysis",
                   "Train Logistic Regression Model"
                ])
            
               if analysis_type == "Exploratory Data Analysis":
                   analyzer = AdvancedEDA()
                   eda_result = analyzer.invoke(data_key="data")
                   st.subheader("Data Quality Report")
                   st.json(eda_result)
            
               elif analysis_type == "Temporal Pattern Analysis":
                    time_col = st.selectbox("Temporal Variable",
                        st.session_state.data.select_dtypes(include='datetime').columns)
                    value_col = st.selectbox("Analysis Variable",
                        st.session_state.data.select_dtypes(include=np.number).columns)
            
                    if time_col and value_col:
                        analyzer = TemporalAnalyzer()
                        result = analyzer.invoke(data_key="data", time_col=time_col, value_col=value_col)
                        if "visualization" in result:
                            st.image(f"data:image/png;base64,{result['visualization']}")
                        st.json(result)
            
               elif analysis_type == "Comparative Statistics":
                    group_col = st.selectbox("Grouping Variable",
                        st.session_state.data.select_dtypes(include='category').columns)
                    value_col = st.selectbox("Metric Variable",
                        st.session_state.data.select_dtypes(include=np.number).columns)
            
                    if group_col and value_col:
                        analyzer = HypothesisTester()
                        result = analyzer.invoke(data_key="data", group_col=group_col, value_col=value_col)
                        st.subheader("Statistical Test Results")
                        st.json(result)
                
               elif analysis_type == "Distribution Analysis":
                    num_cols = st.session_state.data.select_dtypes(include=np.number).columns.tolist()
                    selected_cols = st.multiselect("Select Variables", num_cols)
                    if selected_cols:
                        analyzer = DistributionVisualizer()
                        img_data = analyzer.invoke(data_key="data", columns=selected_cols)
                        st.image(f"data:image/png;base64,{img_data}")
               
               elif analysis_type == "Train Logistic Regression Model":
                 num_cols = st.session_state.data.select_dtypes(include=np.number).columns.tolist()
                 target_col = st.selectbox("Select Target Variable",
                                           st.session_state.data.columns.tolist())
                 selected_cols = st.multiselect("Select Feature Variables", num_cols)
                 if selected_cols and target_col:
                   analyzer = LogisticRegressionTrainer()
                   result = analyzer.invoke(data_key="data", target_col=target_col, columns=selected_cols)
                   st.subheader("Logistic Regression Model Results")
                   st.json(result)

           with research_tab:
               research_query = st.text_area("Enter Research Question:", height=150,
                   placeholder="E.g., 'What factors are most predictive of X outcome?'")
               
               if st.button("Execute Research"):
                    with st.spinner("Conducting rigorous analysis..."):
                      result = st.session_state.researcher.research(
                        research_query, st.session_state.data
                      )
                      st.markdown("## Research Findings")
                      st.markdown(result)

if __name__ == "__main__":
    main()