File size: 6,131 Bytes
77ba698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'prob': {'file': 'prob.json'}}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import json\n",
    "import numpy as np\n",
    "import plotly.graph_objects as go\n",
    "RED_FULL=\"rgba(255, 0, 0, 1)\"\n",
    "\n",
    "# Define the function 1 - (1 - x^8)^14\n",
    "def func1(x):\n",
    "    return 1 - np.power(1 - np.power(x, 8), 14)\n",
    "\n",
    "# Define the function 1 - (1 - x^20)^450\n",
    "def func2(x):\n",
    "    return 1 - np.power(1 - np.power(x, 20), 450)\n",
    "\n",
    "# Generate x values from 0 to 1\n",
    "x = np.linspace(0, 1, 1000)\n",
    "\n",
    "# Calculate y values for each function\n",
    "y1 = func1(x)\n",
    "y2 = func2(x)\n",
    "\n",
    "# Create traces\n",
    "trace1 = go.Scatter(x=x, y=y1, mode='lines', name='FineWeb: 1-(1-s^8)^14')\n",
    "trace2 = go.Scatter(x=x, y=y2, mode='lines', name='RefinedWeb: 1-(1-s^20)^450')\n",
    "vertical_line = go.Scatter(x=[0.75, 0.75], y=[0, 1], mode='lines', line=dict(color='red', dash='dash'), name='Threshold')\n",
    "\n",
    "# Define layout\n",
    "layout = {\n",
    "    'title': {\n",
    "        'text': 'MinHash parameters',\n",
    "    },\n",
    "    'xaxis': {\n",
    "        'title': {\n",
    "            'text': 'Document similarity (s)',\n",
    "        },\n",
    "    },\n",
    "    'yaxis': {\n",
    "        'title': {\n",
    "            'text': 'Matched as dups probability',\n",
    "        },\n",
    "    },\n",
    "}\n",
    "\n",
    "\n",
    "def normalize_run_name(run_name):\n",
    "    return run_name.replace(\"/\", \"_\")\n",
    "\n",
    "\n",
    "def save_for_plot(dir_name, df, views, xlabel=\"Dataset\", ylabel=\"Matched as dups probability\", plot_name=\"plot name\", custom_layout={}, ranges={}, x_column=None, default_metric=None):\n",
    "    import os\n",
    "    files = {}\n",
    "    os.makedirs(f\"data/plots/{dir_name}\", exist_ok=True)\n",
    "    for view in views:\n",
    "        data = {}\n",
    "        for run_name in df[\"runname\"].unique():\n",
    "            run_name_only=df[df[\"runname\"]==run_name]\n",
    "            data[run_name] = {\n",
    "                \"x\": run_name_only[x_column].tolist() if x_column else [run_name],\n",
    "                \"y\": run_name_only[view].tolist(),\n",
    "                \"label\": run_name,\n",
    "            }\n",
    "        file_name = f\"{normalize_run_name(view)}.json\"\n",
    "        files[view] = {\"file\": f\"{file_name}\"}\n",
    "        with open(f\"data/plots/{dir_name}/{file_name}\", \"w\") as f:\n",
    "            json.dump({\n",
    "                \"data\": data,\n",
    "                \"layout\": {\n",
    "                    \"title\": {\n",
    "                        \"text\": plot_name,\n",
    "                    },\n",
    "                    \"xaxis\": {\n",
    "                        \"title\": {\n",
    "                            \"text\": xlabel,\n",
    "                        },\n",
    "                    },\n",
    "                    \"yaxis\": {\n",
    "                        # \"range\": ranges.get(view, None),\n",
    "                        \"title\": {\n",
    "                            \"text\": ylabel,\n",
    "                        },\n",
    "                    },\n",
    "                    \"shapes\": [\n",
    "                        {\n",
    "                            \"type\": \"line\",\n",
    "                            \"x0\": 0.75,\n",
    "                            \"y0\": 0.0,\n",
    "                            \"x1\": 0.75,\n",
    "                            \"y1\": 1.2,\n",
    "                            \"xref\": \"x\",\n",
    "                            \"yref\": \"y\",\n",
    "                            \"line\": {\n",
    "                                \"color\": RED_FULL,\n",
    "                                \"width\": 1,\n",
    "                                \"dash\": \"dashdot\"\n",
    "                            },\n",
    "                            \"showarrow\": False\n",
    "                        }\n",
    "        ],\n",
    "                    **custom_layout,\n",
    "                },\n",
    "            }, f)\n",
    "    with open(f\"data/plots/{dir_name}/index.json\", \"w\") as f:\n",
    "        json.dump({\n",
    "            \"files\": files,\n",
    "            \"settings\": {\n",
    "                \"defaultMetric\": default_metric,\n",
    "                \"slider\": None,\n",
    "                \"autoSetXRange\": False,\n",
    "            }\n",
    "        }, f)\n",
    "    return files\n",
    "\n",
    "import pandas as pd\n",
    "df = pd.DataFrame({\n",
    "    \"runname\": [\"FineWeb: 1-(1-s^8)^14\"]*len(x) + [\"RefinedWeb: 1-(1-s^20)^450\"]*len(x),\n",
    "    \"similarity\": x.tolist()+x.tolist(),\n",
    "    \"prob\": y1.tolist()+y2.tolist(),\n",
    "    \"view\": [\"normal\"]*2*len(x)\n",
    "})\n",
    "\n",
    "custom_layout = {\n",
    "    \"legend\": {\n",
    "        \"orientation\": \"v\",\n",
    "        \"xanchor\": \"left\",\n",
    "        \"yanchor\": \"top\",\n",
    "        \"x\": 0,\n",
    "        \"y\": 1,\n",
    "    },\n",
    "}\n",
    "\n",
    "save_for_plot(\"minhash_params\", df, [\"prob\"], xlabel=\"Document similarity (s)\", plot_name=\"MinHash parameters\", custom_layout=custom_layout, ranges={}, x_column=\"similarity\", default_metric=\"prob\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "datatrove",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}