103 lines
2.8 kB
1import sys
2import importlib
3from types import ModuleType
4from typing import Any
5
6
7def check_pkg(
8 pkg_name: str
9) -> tuple[bool, ModuleType | None, str | None]:
10 try:
11 module = importlib.import_module(pkg_name)
12 version = getattr(module, "__version__", "unknown")
13 return True, module, version
14 except ImportError:
15 return False, None, None
16
17
18def show_missing_instructions(
19 missing_pkgs: list[str]
20) -> None:
21 pkgs = " ".join(missing_pkgs)
22
23 print("\nInstall using pip:")
24 print(f"pip install {pkgs}\nor")
25 print("pip install -r requirements.txt\n")
26
27 print("Install using Poetry:")
28 print(f"poetry add {pkgs}\nor")
29 print("poetry install")
30
31
32def generate_matrix_data(
33 numpy: ModuleType
34) -> dict[str, Any]:
35 numpy.random.seed(42)
36 data_size = 1000
37 return {
38 "normal_data": numpy.random.normal(50, 15, data_size),
39 "random_data": numpy.random.randint(0, 100, data_size),
40 }
41
42
43def analyze_data(
44 pandas: ModuleType,
45 numpy: ModuleType,
46 plt: ModuleType
47) -> None:
48 try:
49 print("\nAnalyzing Matrix data...")
50 matrix_data = generate_matrix_data(numpy)
51 df = pandas.DataFrame(matrix_data)
52 print(f"Processing {len(df)} data points...")
53 print("\nGenerating Visualization...")
54 _, axs = plt.subplots(2, 1, figsize=(10, 8))
55 axs[0].plot(df["normal_data"][:100])
56 axs[0].set_title("title")
57 axs[1].hist(df["random_data"], bins=20)
58 axs[1].set_title("titleee")
59 output_file = "matrix_analysis.png"
60 plt.savefig(output_file)
61 print("\nAnalysis complete!")
62 print(f"Results saved to: {output_file}")
63
64 except Exception as error:
65 print(f"Error during analysis: {error}")
66 sys.exit(1)
67
68
69def main() -> None:
70 REQUIRED_PKGS: dict[str, str] = {
71 "pandas": "Data manipulation ready",
72 "numpy": "Numerical computation ready",
73 "matplotlib": "Visualization ready",
74 }
75
76 print("\nLOADING STATUS: Loading programs...")
77 print("\nChecking dependencies:")
78
79 missing_pkgs = []
80 loaded_modules = {}
81
82 for pkg, msg in REQUIRED_PKGS.items():
83 installed, module, version = check_pkg(pkg)
84 if installed and module is not None:
85 print(f"[OK] {pkg} ({version}) - {msg}")
86 loaded_modules[pkg] = module
87 else:
88 print(f"[MISSING] {pkg} - Not installed")
89 missing_pkgs.append(pkg)
90
91 if missing_pkgs:
92 show_missing_instructions(missing_pkgs)
93 sys.exit(1)
94
95 pandas = loaded_modules["pandas"]
96 numpy = loaded_modules["numpy"]
97 plt = importlib.import_module("matplotlib.pyplot")
98
99 analyze_data(pandas, numpy, plt)
100
101
102if __name__ == "__main__":
103 main()