ld2daps/Notebooks/YAML Serialization.ipynb
2018-07-13 17:01:09 +02:00

266 lines
5.4 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Serialize Attribute Profiles Classification\n",
"\n",
"- [X] Read a YAML recipe\n",
"- [X] Brew recipe\n",
"- [] Compute hashes\n",
"- [] Write hashes\n",
"- [] Time metrics\n",
"- [] Result metrics\n",
"- [] Write metrics\n",
"- [] Write/move results\n",
"- [] Watch folder\n",
"- [] Main loop\n",
"- [] Logs\n",
"- [] Catch errors\n",
"- [] Custom CVG\n",
"\n",
"\n",
"## Init"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import yaml\n",
"import numpy as np\n",
"import importlib\n",
"import sys\n",
"\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"\n",
"sys.path.append('..')\n",
"import Descriptors\n",
"from CrossValidationGenerator import APsCVG\n",
"\n",
"sys.path.append('../triskele/python')\n",
"import triskele"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Serial Classifier"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2\n",
"with open('../test.yml') as f:\n",
" expe = yaml.safe_load(f)['expe']\n",
"display(expe)\n",
"\n",
"# Ground truth\n",
"gt = triskele.read(expe['ground_truth'])\n",
"\n",
"# Descriptors\n",
"script = expe['descriptors_script']\n",
"desc = importlib.import_module(script['name'], package=Descriptors)\n",
"importlib.reload(Descriptors)\n",
"att = desc.run(**script['parameters'])\n",
"\n",
"# CrossVal and ML\n",
"cv = expe['cross_validation']\n",
"cl = expe['classifier']\n",
"\n",
"prediction = np.zeros_like(gt)\n",
"\n",
"for xt, xv, yt, yv, ti in APsCVG(gt, att, **cv['parameters']):\n",
" rfc = RandomForestClassifier(**cl['parameters'])\n",
" rfc.fit(xt, yt)\n",
" \n",
" ypred = rfc.predict(xv)\n",
" \n",
" prediction[ti] = ypred"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"plt.figure(figsize=(16, 9))\n",
"plt.imshow(prediction)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import from string module, class and instantiate"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import importlib\n",
"module = importlib.import_module(module_name)\n",
"class_ = getattr(module, class_name)\n",
"instance = class_()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def run(rasters, treshold=1e4, areas=None, sd=None, moi=None):\n",
" treshold = float(treshold)\n",
" areas = None if areas is None else np.array(areas).astype(np.float).astype(np.int)\n",
" sd = None if sd is None else np.array(sd).astype(np.float)\n",
" moi = None if moi is None else np.array(moi).astype(np.float)\n",
" return treshold, areas, sd, moi\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"run(**expe['descriptors_param'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"desc."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sha1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import hashlib"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"hashlib.md5()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sorted(expe.items())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"expe"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"expe"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.array(expe['descriptors_param']['areas']).astype(np.float).astype(np.int)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.array(None).astype(np.float).astype(np.int)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"desc = importlib.import_module(expe['descriptors_script']['path'])\n",
"desc.run(**expe['descriptors_param'])"
]
},
{
"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"
}
},
"nbformat": 4,
"nbformat_minor": 2
}