89198/.ipynb_checkpoints/Final-checkpoint.ipynb
{
"cells": [
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from PIL import Image\n",
"from scipy.spatial import Delaunay\n",
"from scipy.interpolate import griddata\n",
"from scipy.optimize import leastsq\n",
"import plotly.graph_objects as go\n",
"import plotly.io as pio\n",
"import plotly.figure_factory as ff\n",
"from plotly.subplots import make_subplots"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"def create_folder(type_=None, train_data=None, valid_data=None, classes=[], folder='data'):\n",
" os.mkdir(F'{folder}/{type_}')\n",
" os.mkdir(F'{folder}/{type_}/train')\n",
"\n",
" for label in classes:\n",
" os.mkdir(F'{folder}/{type_}/train/{label}')\n",
" \n",
" for index, elem in train_data.iterrows():\n",
" file_ = elem['filename']\n",
" label = elem[type_]\n",
" if label in classes:\n",
" source = F'{datafolder}/data/{file_}'\n",
" target = F'{datafolder}/{type_}/data/{label}/{file_}'\n",
" copyfile(source, target)\n",
"\n",
" samples = valid_data[labels][type_].value_counts()\n",
" class_ = {}\n",
" for elem in list(zip(samples,samples.index)):\n",
" if elem[1] in classes:\n",
" valid = round(elem[0]*0.5)\n",
" test = elem[0] - valid\n",
" class_[elem[1]] = {'valid':valid, 'test':test}\n",
"\n",
" for index, elem in valid_data.iterrows():\n",
" file_ = elem['filename']\n",
" name = elem[type_]\n",
" if name in classes:\n",
" samples = class_[name]\n",
" if samples['valid'] > 0:\n",
" class_[name]['valid'] -= 1\n",
" source = F'{folder}/data/{file_}'\n",
" target = F'{folder}/{type_}/valid/{name}/{file_}'\n",
" copyfile(source, target)\n",
" else:\n",
" samples['test'] -= 1\n",
" source = F'{folder}/data/{file_}'\n",
" target = F'{folder}/{type_}/data/{name}/{file_}'\n",
" copyfile(source, target)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"datafolder = '.'"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
"\n",
"\n",
" | \n",
"U:0 | \n",
"U:1 | \n",
"U:2 | \n",
"Points:0 | \n",
"Points:1 | \n",
"Points:2 | \n",
"
\n",
"\n",
"\n",
"\n",
"0 | \n",
"36.7028 | \n",
"-0.000665 | \n",
"0.008076 | \n",
"-0.001793 | \n",
"0.078000 | \n",
"0.09 | \n",
"
\n",
"\n",
"1 | \n",
"36.7027 | \n",
"-0.001454 | \n",
"0.004049 | \n",
"-0.004000 | \n",
"0.078000 | \n",
"0.09 | \n",
"
\n",
"\n",
"2 | \n",
"36.7032 | \n",
"-0.001584 | \n",
"0.008007 | \n",
"-0.001793 | \n",
"0.080212 | \n",
"0.09 | \n",
"
\n",
"\n",
"3 | \n",
"36.7000 | \n",
"0.000000 | \n",
"0.000000 | \n",
"-0.004000 | \n",
"0.080215 | \n",
"0.09 | \n",
"
\n",
"\n",
"4 | \n",
"36.7054 | \n",
"-0.001622 | \n",
"0.013399 | \n",
"0.000415 | \n",
"0.078000 | \n",
"0.09 | \n",
"
\n",
"\n",
"
\n",
"
"
],
"text/plain": [
" U:0 U:1 U:2 Points:0 Points:1 Points:2\n",
"0 36.7028 -0.000665 0.008076 -0.001793 0.078000 0.09\n",
"1 36.7027 -0.001454 0.004049 -0.004000 0.078000 0.09\n",
"2 36.7032 -0.001584 0.008007 -0.001793 0.080212 0.09\n",
"3 36.7000 0.000000 0.000000 -0.004000 0.080215 0.09\n",
"4 36.7054 -0.001622 0.013399 0.000415 0.078000 0.09"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dataset = pd.read_csv('data.csv'); \n",
"dataset.head()"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"labels = ['U:0', 'U:1', 'U:2']"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(35267, 6)"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dataset.shape"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"def load_data(size=64):\n",
" # Load image, resize, make monochrome (black and white) copy and convert to Numpy array\n",
" image_rgb = Image.open(\"data.csv\").resize([size, size])\n",
" image_bw = image_rgb.convert('L')\n",
" image_rgb = np.asarray(image_rgb)\n",
" image_bw = np.asarray(image_bw)\n",
"\n",
" # Get size and color info\n",
" height, width, channels = image_rgb.shape\n",
" colors = np.array([f\"rgb{rgb[0], rgb[1], rgb[2]}\" for rgb in image_rgb.reshape(-1, 3)])\n",
" values = image_bw.reshape(-1).astype(str)\n",
"\n",
" # Make grid to paint colors (or pixel values) on\n",
" x, y = np.meshgrid(np.arange(width), np.arange(height))\n",
" flat_x, flat_y = x.reshape(-1), y.reshape(-1)\n",
"\n",
" red = np.array([rgb[0] for rgb in image_rgb.reshape(-1, 3)]).astype(str)\n",
" green = np.array([rgb[1] for rgb in image_rgb.reshape(-1, 3)]).astype(str)\n",
" blue = np.array([rgb[2] for rgb in image_rgb.reshape(-1, 3)]).astype(str)\n",
" \n",
" return {\"rgb\": image_rgb,\n",
" \"mono\": image_bw,\n",
" \"x\": flat_x,\n",
" \"y\": flat_y,\n",
" \"colors\": colors,\n",
" \"values\": values,\n",
" \"red\": red,\n",
" \"green\": green,\n",
" \"blue\": blue}"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"def get_layout():\n",
" return go.Layout(template=\"plotly_white\",\n",
" xaxis=dict(constrain=\"domain\",\n",
" visible=False),\n",
" yaxis=dict(scaleanchor='x',\n",
" visible=False,\n",
" autorange=\"reversed\"),\n",
" hoverlabel=dict(font_size=18),\n",
" height=1024,\n",
" margin=dict(r=0, l=0, b=0, t=0, pad=0))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor":...