{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d19a1c0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import geopandas as gpd\n",
    "\n",
    "from fracability import Entities\n",
    "from fracability.operations.Geometry import center_object, tidy_intersections\n",
    "from fracability.operations.Topology import nodes_conn\n",
    "from fracability.Plotters import matplot_stats_summary\n",
    "from fracability.operations.Statistics import NetworkFitter\n",
    "import scipy.stats as ss\n",
    "from scipy.optimize import minimize\n",
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0cd98e0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "n1_path = 'fracability/datasets/Set_1.shp'\n",
    "n2_path = 'fracability/datasets/Set_2.shp'\n",
    "b_path = 'fracability/datasets/Interpretation_boundary_laghettoSalza.shp'\n",
    "\n",
    "\n",
    "fracs_1 = gpd.read_file(n1_path)\n",
    "fracs_2 = gpd.read_file(n2_path)\n",
    "\n",
    "bound_gpd = gpd.read_file(b_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d54d74f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "fractures_1 = Entities.Fractures(fracs_1)\n",
    "fractures_2 = Entities.Fractures(fracs_2)\n",
    "\n",
    "boundaries = Entities.Boundary(bound_gpd)\n",
    "\n",
    "fracture_net = Entities.FractureNetwork()\n",
    "\n",
    "fracture_net.boundaries = boundaries\n",
    "\n",
    "fracture_net.add_fractures(fractures_1,1)\n",
    "fracture_net.add_fractures(fractures_2,2)\n",
    "\n",
    "\n",
    "print(fracture_net.fractures.entity_df)\n",
    "\n",
    "# clean_dup_points(fractures)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a49af347",
   "metadata": {},
   "source": [
    "### Statistics on set 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebc42edf",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "fracture_net.fractures.activate_set(1)\n",
    "\n",
    "center_object(fracture_net)\n",
    "\n",
    "print(fracture_net.entity_df)\n",
    "\n",
    "tidy_intersections(fracture_net)\n",
    "\n",
    "nodes_conn(fracture_net)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72d967e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "fracture_net.matplot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88e21d01",
   "metadata": {},
   "outputs": [],
   "source": [
    "fitter = NetworkFitter(fracture_net)\n",
    "\n",
    "best_fit = fitter.find_best_distribution()\n",
    "matplot_stats_summary(best_fit['distribution'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8871a67",
   "metadata": {},
   "source": [
    "### Statistics on set 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c6a4eb1",
   "metadata": {},
   "outputs": [],
   "source": [
    "fracture_net.fractures.activate_set(2)\n",
    "\n",
    "center_object(fracture_net)\n",
    "\n",
    "tidy_intersections(fracture_net)\n",
    "\n",
    "nodes_conn(fracture_net)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc52adff",
   "metadata": {},
   "outputs": [],
   "source": [
    "fracture_net.matplot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad65c37b",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "fitter = NetworkFitter(fracture_net)\n",
    "\n",
    "best_fit = fitter.find_best_distribution()\n",
    "matplot_stats_summary(best_fit['distribution'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34d35da0",
   "metadata": {},
   "source": [
    "### Statistics on whole fracture net"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98cec8e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "fracture_net = Entities.FractureNetwork()\n",
    "\n",
    "fracture_net.add_boundaries(boundaries)\n",
    "\n",
    "fracture_net.add_fractures([fractures_1, fractures_2])\n",
    "\n",
    "center_object(fracture_net)\n",
    "\n",
    "tidy_intersections(fracture_net)\n",
    "\n",
    "nodes = nodes_conn(fracture_net)\n",
    "fracture_net.add_nodes(nodes)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2e4583a",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "fitter = NetworkFitter(fracture_net)\n",
    "\n",
    "fitter_name = 'gamma'\n",
    "\n",
    "fitter.fit(fitter_name)\n",
    "\n",
    "fitter.find_best_distribution()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0ccf0c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "matplot_stats_summary(fitter)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bbcb2bf",
   "metadata": {},
   "source": [
    "### Plot network"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1655bdcf",
   "metadata": {},
   "outputs": [],
   "source": [
    "fracture_net.vtkplot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54c9b912",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
