diff --git a/.github/ISSUE_TEMPLATE/project_setup.yaml b/.github/ISSUE_TEMPLATE/project_setup.yaml index a74211c..41aa000 100644 --- a/.github/ISSUE_TEMPLATE/project_setup.yaml +++ b/.github/ISSUE_TEMPLATE/project_setup.yaml @@ -40,5 +40,4 @@ body: id: assignment-summary attributes: label: Project Assignment - description: Please provide a summary of the project and assignment and, when available, upload work plan shared with client. - + description: Please provide a summary of the project and assignment and, when available, upload work plan shared with client. diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index df4a3cf..aaa5f4e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ # See https://pre-commit.com/hooks.html for more hooks repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.6.0 + rev: v5.0.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer @@ -14,7 +14,7 @@ repos: args: [--allow-missing-credentials] - id: detect-private-key - repo: https://github.com/igorshubovych/markdownlint-cli - rev: v0.39.0 + rev: v0.44.0 hooks: - id: markdownlint name: Markdownlint @@ -24,13 +24,13 @@ repos: "--disable=MD033", # no-inline-html ] - repo: https://github.com/codespell-project/codespell - rev: v2.2.6 + rev: v2.4.1 hooks: - id: codespell name: codespell description: Checks for common misspellings in text files - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.4.1 + rev: v0.9.10 hooks: - id: ruff types_or: [python, pyi, jupyter] diff --git a/README.md b/README.md index 1221d4e..dcc0618 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,11 @@ The following datasets are used in this assignmet. Further details about each da * [BlackMarble](http://blackmarble.gsfc.nasa.gov/) - Visible Infrared Imaging Radiometer Suite (VIIRS) Nighttime Lights from NASA. -* [MODIS](https://modis.gsfc.nasa.gov/) - NASA's 'Modern Resolution Imaging Spectroradiometer' dataset, used to monitor changes in surface vegetation. +* [MODIS](https://modis.gsfc.nasa.gov/) - NASA's 'Modern Resolution Imaging Spectroradiometer' dataset, used to monitor changes in surface vegetation. * [Veraset Movement Data](https://docs.datapartnership.org/partners/veraset/README.html). Veraset Movement is an unfiltered stream of location and proximity data that Veraset has aggregated from data suppliers. The data can be requested (for purchase) through the [Development Data Partnership](https://datapartnership.org/). - + #### Data Availability Statement diff --git a/docs/1-intro-to-data-lab.md b/docs/1-intro-to-data-lab.md index 2d0b375..37c74d9 100644 --- a/docs/1-intro-to-data-lab.md +++ b/docs/1-intro-to-data-lab.md @@ -1,6 +1,6 @@ # Introduction to the Data Lab -The Data Lab supports World Bank operations -- lending, technical assistance, and economic reporting -- by coordinating ad-hoc teams of data analysts and specialists from across our organization. Through the Lab, teams solve global challenges using best practices in coding, code documentation, and data visualization. +The Data Lab supports World Bank operations -- lending, technical assistance, and economic reporting -- by coordinating ad-hoc teams of data analysts and specialists from across our organization. Through the Lab, teams solve global challenges using best practices in coding, code documentation, and data visualization. Unlike a traditional data analysis, which results in a single-use report or visualization, Data Lab products are designed to be customized, reused, and updated, thereby building the capacity of the World Bank and partner organizations to quickly deliver complex data science solutions to pressing global challenges. @@ -18,29 +18,29 @@ Data Lab-supported projects may include: 2. **Analytics**. Leveraging curated datasets, the team conducts analytics across a range of topics (e.g., understanding population movement in response to a crisis or monitoring trends in nighttime lights). Each analysis will include original code, documentation, links to original data sources (and/or information on how to access them), and a description of their limitations. Reference resources are also cited, where relevant. - + 3. **Additional Resources.** Links and descriptions of additional resources for each project may include: - - Description of common baseline data used to support the analyses -- administrative boundaries, population, infrastructure, etc. + - Description of common baseline data used to support the analyses -- administrative boundaries, population, infrastructure, etc. - Project SharePoint where original data and documents are maintained. - - Additional static images and data visualizations. + - Additional static images and data visualizations. + + - +4. **Project Team**. For each project, the [World Bank Data Lab](https://wbdatalab.org/) recruits colleagues from throughout the World Bank, pooling our collective data talents in support of our lending and technical assistance operations. Project packages include names and contact information for the unique teams that prepared the analytics. -4. **Project Team**. For each project, the [World Bank Data Lab](https://wbdatalab.org/) recruits colleagues from throughout the World Bank, pooling our collective data talents in support of our lending and technical assistance operations. Project packages include names and contact information for the unique teams that prepared the analytics. - ## How Data Lab Projects are Managed 1. **Dynamic, Web-Hosted Documentation**. Unless specified otherwise, all code and documentation used to produce the analytics is hosted in a project GitHub repository to facilitate reuse for future updates and projects, as well as to support collaboration and capacity building activities. - + 2. **Data Catalogue**. Where possible, all datasets used in the production of Data Goods are added as entries to the World Bank’s [Development Data Hub](https://datacatalog.worldbank.org/home), where they are tagged with meta data, license attributes, and access information. - + 3. **Internal Project Management and File Sharing System**. To facilitate project management across teams, the Lab creates a Project SharePoint, which includes project management information (work plan, milestones, check-in slides, log of hours charged, final report), related literature, data files, indicator tables, and links to resources, such as this documentation. The advantage of SharePoint for World Bank usage is that all contents are automatically encrypted and tagged as Official Use Only. The project SharePoint is accessible to project team members and, with permission, can be replicated as a basis for future project updates or for similar projects. diff --git a/docs/2-analytics.md b/docs/2-analytics.md index d4aa072..1b67850 100644 --- a/docs/2-analytics.md +++ b/docs/2-analytics.md @@ -4,9 +4,9 @@ All Data Lab analytics include information on data sources, as well as original 1. **Overview** - Summary of the analytical challenge. + Summary of the analytical challenge. + - 2. **Data Description** Everything a user would need to access and use the data that supports the analysis. For each source, we include: @@ -21,23 +21,23 @@ All Data Lab analytics include information on data sources, as well as original - Point of Contact - + 3. **Methodology** - All analytses include step-by-step directions, code snippets, links to complete code, and notes on any critical dependencies. The user should be able to fully understand how the analytical results were achieved and be able to replicate them by following the methodology. + All analytses include step-by-step directions, code snippets, links to complete code, and notes on any critical dependencies. The user should be able to fully understand how the analytical results were achieved and be able to replicate them by following the methodology. + - 4. **Findings** - This section includes initial results, including statistics, graphs, and maps to illustrate findings. + This section includes initial results, including statistics, graphs, and maps to illustrate findings. + - 5. **Limitations** - It is critical that all analyses are accompanied by a detailed description of limitations of the data and methodology for interpreting or reproducing results. + It is critical that all analyses are accompanied by a detailed description of limitations of the data and methodology for interpreting or reproducing results. + - 6. **References and Works Cited** diff --git a/docs/3-add-resources.md b/docs/3-add-resources.md index 2fe8366..5b62d81 100644 --- a/docs/3-add-resources.md +++ b/docs/3-add-resources.md @@ -2,7 +2,7 @@ **Baseline Data** -Each project includes a common set of foundational data that is shared across analytical work, such as administrative boundaries, population, and location of roads and buildings. Following is a summary of baseline data sources for this project. +Each project includes a common set of foundational data that is shared across analytical work, such as administrative boundaries, population, and location of roads and buildings. Following is a summary of baseline data sources for this project. | Topic | Data Source | Notes | | ---------------------------------------- | ----------- | ----- | @@ -12,10 +12,10 @@ Each project includes a common set of foundational data that is shared across an **Project Data** -Official-Use-Only original and derived datasets used in the project analytics are made available only to the project team. To request access, World Bank staff can write: datalab@worlldbank.org. Open datasets can be made available thorugh the World Bank's [open data catalog](https://datacatalog.worldbank.org/), at the request of the project team. +Official-Use-Only original and derived datasets used in the project analytics are made available only to the project team. To request access, World Bank staff can write: datalab@worlldbank.org. Open datasets can be made available thorugh the World Bank's [open data catalog](https://datacatalog.worldbank.org/), at the request of the project team. * [Project Data SharePoint Link](https://worldbankgroup.sharepoint.com/:f:/r/teams/DevelopmentDataPartnershipCommunity-WBGroup/Shared%20Documents/Projects/Data%20Lab/Myanmar%20Economic%20Monitor?csf=1&web=1&e=pyzHxK) **Other Resources** - Additional links or resources are sometimes provided to supplement analytical work. + Additional links or resources are sometimes provided to supplement analytical work. diff --git a/notebooks/conflict/conflict-and-ntl-updated.ipynb b/notebooks/conflict/conflict-and-ntl-updated.ipynb index 45f7a77..df95882 100644 --- a/notebooks/conflict/conflict-and-ntl-updated.ipynb +++ b/notebooks/conflict/conflict-and-ntl-updated.ipynb @@ -11,9 +11,7 @@ "outputs": [], "source": [ "import pandas as pd\n", - "import numpy as np\n", "import geopandas as gpd\n", - "import matplotlib.pyplot as plt\n", "\n", "from bokeh.io import output_notebook" ] @@ -28,72 +26,104 @@ }, "outputs": [], "source": [ - "import pandas as pd\n", - "import numpy as np\n", "from bokeh.plotting import figure, show\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, ColumnDataSource, HoverTool\n", - "\n", - "def get_multi_tab_line_plot(df1, df2, loop_list, category_column, conflict_measure, ntl_measure):\n", - "\n", + "from bokeh.models import (\n", + " LinearAxis,\n", + " Range1d,\n", + " Tabs,\n", + " TabPanel,\n", + " ColumnDataSource,\n", + " HoverTool,\n", + ")\n", + "\n", + "\n", + "def get_multi_tab_line_plot(\n", + " df1, df2, loop_list, category_column, conflict_measure, ntl_measure\n", + "):\n", " tabs = []\n", "\n", " for ts in loop_list:\n", - " # # Create a new Bokeh figure\n", - " p = figure(title=f\"Comparing Nighttime Light Trends and {conflict_measure.capitalize()}\", x_axis_label='Month', width=800, height=400, x_axis_type='datetime')\n", + " # # Create a new Bokeh figure\n", + " p = figure(\n", + " title=f\"Comparing Nighttime Light Trends and {conflict_measure.capitalize()}\",\n", + " x_axis_label=\"Month\",\n", + " width=800,\n", + " height=400,\n", + " x_axis_type=\"datetime\",\n", + " )\n", "\n", - " p.y_range.start = df2[conflict_measure].min() \n", + " p.y_range.start = df2[conflict_measure].min()\n", " p.y_range.end = df2[conflict_measure].max()\n", "\n", - " p.extra_y_ranges = {\"y2\": Range1d(start=df1[df1[category_column]==ts][ntl_measure].min(), end=df1[df1[category_column]==ts][ntl_measure].max())}\n", + " p.extra_y_ranges = {\n", + " \"y2\": Range1d(\n", + " start=df1[df1[category_column] == ts][ntl_measure].min(),\n", + " end=df1[df1[category_column] == ts][ntl_measure].max(),\n", + " )\n", + " }\n", " # Add the second line plot\n", " ntl_source = ColumnDataSource(df1[df1[category_column] == ts])\n", - " ntl_line = p.vbar('date', top=ntl_measure,source=ntl_source, legend_label=ntl_measure, width=2000*2000*750, color=\"blue\", alpha=0.7, y_range_name='y2')\n", - "\n", + " ntl_line = p.vbar(\n", + " \"date\",\n", + " top=ntl_measure,\n", + " source=ntl_source,\n", + " legend_label=ntl_measure,\n", + " width=2000 * 2000 * 750,\n", + " color=\"blue\",\n", + " alpha=0.7,\n", + " y_range_name=\"y2\",\n", + " )\n", "\n", " # Add the first line plot\n", " conflict_source = ColumnDataSource(df2[df2[category_column] == ts])\n", - " conflict_line = p.line('date', conflict_measure,source=conflict_source, legend_label=f\"{conflict_measure.capitalize()}\", line_width=2, color=\"red\", alpha=0.7)\n", + " conflict_line = p.line(\n", + " \"date\",\n", + " conflict_measure,\n", + " source=conflict_source,\n", + " legend_label=f\"{conflict_measure.capitalize()}\",\n", + " line_width=2,\n", + " color=\"red\",\n", + " alpha=0.7,\n", + " )\n", "\n", - " \n", " # Customize legend\n", " p.legend.location = \"top_left\"\n", " p.legend.click_policy = \"hide\" # Optional: Allow clicking to hide/show lines\n", "\n", - " p.add_layout(LinearAxis(y_range_name=\"y2\", axis_label='Luminosity'), 'right')\n", + " p.add_layout(LinearAxis(y_range_name=\"y2\", axis_label=\"Luminosity\"), \"right\")\n", "\n", " # Add hover tool for conflict data\n", " hover_conflict = HoverTool(\n", " renderers=[conflict_line], # Reference the renderer directly\n", " tooltips=[\n", - " ('Date', '@date{%F}'),\n", - " (f'{conflict_measure.capitalize()}', f'@{conflict_measure}'),\n", + " (\"Date\", \"@date{%F}\"),\n", + " (f\"{conflict_measure.capitalize()}\", f\"@{conflict_measure}\"),\n", " ],\n", " formatters={\n", - " '@date': 'datetime', # Format the date\n", + " \"@date\": \"datetime\", # Format the date\n", " },\n", - " mode='vline'\n", + " mode=\"vline\",\n", " )\n", - " \n", + "\n", " # Add hover tool for NTL data\n", " hover_ntl = HoverTool(\n", " renderers=[ntl_line], # Reference the renderer directly\n", " tooltips=[\n", - " ('Date', '@date{%F}'),\n", - " (ntl_measure, f'@{ntl_measure}'),\n", + " (\"Date\", \"@date{%F}\"),\n", + " (ntl_measure, f\"@{ntl_measure}\"),\n", " ],\n", " formatters={\n", - " '@date': 'datetime', # Format the date\n", + " \"@date\": \"datetime\", # Format the date\n", " },\n", - " mode='vline'\n", + " mode=\"vline\",\n", " )\n", - " \n", + "\n", " p.add_tools(hover_conflict, hover_ntl)\n", "\n", " tab = TabPanel(child=p, title=ts)\n", " tabs.append(tab)\n", "\n", - " return tabs\n", - "\n" + " return tabs" ] }, { @@ -106,51 +136,57 @@ }, "outputs": [], "source": [ - "import pandas as pd\n", - "import numpy as np\n", - "from bokeh.plotting import figure, show\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, ColumnDataSource, HoverTool\n", - "\n", "def get_multi_tab_bar_plot(df1, loop_list, category_column, ntl_measure, title):\n", - "\n", " tabs = []\n", "\n", " for ts in loop_list:\n", - " # # Create a new Bokeh figure\n", - " p = figure(title=f\"{title}\", x_axis_label='', width=800, height=400, x_axis_type='datetime')\n", + " # # Create a new Bokeh figure\n", + " p = figure(\n", + " title=f\"{title}\",\n", + " x_axis_label=\"\",\n", + " width=800,\n", + " height=400,\n", + " x_axis_type=\"datetime\",\n", + " )\n", "\n", " ntl_source = ColumnDataSource(df1[df1[category_column] == ts])\n", " p.y_range.start = df1[ntl_measure].min() - 5\n", " p.y_range.end = df1[ntl_measure].max() + 5\n", " # Add the second line plot\n", - " \n", - " ntl_line = p.vbar(x='date', top=ntl_measure,source=ntl_source, \n", - " legend_label=ntl_measure,width=2000*2000*750, color=\"#4E79A7\", alpha=0.7)\n", + "\n", + " ntl_line = p.vbar(\n", + " x=\"date\",\n", + " top=ntl_measure,\n", + " source=ntl_source,\n", + " legend_label=ntl_measure,\n", + " width=2000 * 2000 * 750,\n", + " color=\"#4E79A7\",\n", + " alpha=0.7,\n", + " )\n", "\n", " # Customize legend\n", " p.legend.location = \"top_left\"\n", " p.legend.click_policy = \"hide\" # Optional: Allow clicking to hide/show lines\n", - " \n", + "\n", " # Add hover tool for NTL data\n", " hover_ntl = HoverTool(\n", " renderers=[ntl_line], # Reference the renderer directly\n", " tooltips=[\n", - " ('Date', '@date{%F}'),\n", - " (ntl_measure, f'@{ntl_measure}'),\n", + " (\"Date\", \"@date{%F}\"),\n", + " (ntl_measure, f\"@{ntl_measure}\"),\n", " ],\n", " formatters={\n", - " '@date': 'datetime', # Format the date\n", + " \"@date\": \"datetime\", # Format the date\n", " },\n", - " mode='vline'\n", + " mode=\"vline\",\n", " )\n", - " \n", + "\n", " p.add_tools(hover_ntl, hover_ntl)\n", "\n", " tab = TabPanel(child=p, title=ts)\n", " tabs.append(tab)\n", "\n", - " return tabs\n", - "\n" + " return tabs" ] }, { @@ -193,7 +229,7 @@ }, "outputs": [], "source": [ - "mmr_adm3 = gpd.read_file('../../data/boundaries/mmr_polbnda_adm3_250k_mimu.shp')" + "mmr_adm3 = gpd.read_file(\"../../data/boundaries/mmr_polbnda_adm3_250k_mimu.shp\")" ] }, { @@ -206,9 +242,13 @@ }, "outputs": [], "source": [ - "conflict_monthly = pd.read_stata('../../data/conflict/ACLED_01_Jan_2021_21_Oct_2024_township_monthly_spatial.dta')\n", - "conflict_monthly.rename(columns={'month':'date'}, inplace=True)\n", - "conflict_6_monthly = pd.read_stata('../../data/conflict/ACLED_Apr_2022_Sep_2024_township_spatial_period_change.dta')" + "conflict_monthly = pd.read_stata(\n", + " \"../../data/conflict/ACLED_01_Jan_2021_21_Oct_2024_township_monthly_spatial.dta\"\n", + ")\n", + "conflict_monthly.rename(columns={\"month\": \"date\"}, inplace=True)\n", + "conflict_6_monthly = pd.read_stata(\n", + " \"../../data/conflict/ACLED_Apr_2022_Sep_2024_township_spatial_period_change.dta\"\n", + ")" ] }, { @@ -221,9 +261,11 @@ }, "outputs": [], "source": [ - "ntl_adm3_monthly = pd.read_csv('../../data/ntl/ntl_mmr_adm3_monthly.csv')\n", - "ntl_adm3_monthly = mmr_adm3[['ST', 'DT', 'TS', 'geometry']].merge(ntl_adm3_monthly, on = ['ST', 'DT', 'TS'])\n", - "ntl_adm3_monthly['date'] = pd.to_datetime(ntl_adm3_monthly['date'])" + "ntl_adm3_monthly = pd.read_csv(\"../../data/ntl/ntl_mmr_adm3_monthly.csv\")\n", + "ntl_adm3_monthly = mmr_adm3[[\"ST\", \"DT\", \"TS\", \"geometry\"]].merge(\n", + " ntl_adm3_monthly, on=[\"ST\", \"DT\", \"TS\"]\n", + ")\n", + "ntl_adm3_monthly[\"date\"] = pd.to_datetime(ntl_adm3_monthly[\"date\"])" ] }, { @@ -236,9 +278,11 @@ }, "outputs": [], "source": [ - "ntl_adm3_annual = pd.read_csv('../../data/ntl/ntl_mmr_adm3_annual.csv')\n", - "ntl_adm3_annual = mmr_adm3[['ST', 'DT', 'TS', 'geometry']].merge(ntl_adm3_annual, on = ['ST', 'DT', 'TS'])\n", - "ntl_adm3_annual['date'] = pd.to_datetime(ntl_adm3_annual['date'], format='%Y')" + "ntl_adm3_annual = pd.read_csv(\"../../data/ntl/ntl_mmr_adm3_annual.csv\")\n", + "ntl_adm3_annual = mmr_adm3[[\"ST\", \"DT\", \"TS\", \"geometry\"]].merge(\n", + " ntl_adm3_annual, on=[\"ST\", \"DT\", \"TS\"]\n", + ")\n", + "ntl_adm3_annual[\"date\"] = pd.to_datetime(ntl_adm3_annual[\"date\"], format=\"%Y\")" ] }, { @@ -252,12 +296,12 @@ "outputs": [], "source": [ "def get_conflict_groups(code):\n", - " if code<=2:\n", - " return 'SEC Control'\n", - " if code>2 and code<=6:\n", - " return 'Contested Areas'\n", - " if code>6:\n", - " return 'EAO Control'" + " if code <= 2:\n", + " return \"SEC Control\"\n", + " if code > 2 and code <= 6:\n", + " return \"Contested Areas\"\n", + " if code > 6:\n", + " return \"EAO Control\"" ] }, { @@ -270,13 +314,23 @@ }, "outputs": [], "source": [ - "control_categories = pd.read_csv('../../data/conflict/control_category_map.csv')\n", - "control_categories.rename(columns={'Township': 'TS'}, inplace=True)\n", - "control_categories['category'] = control_categories['code_no'].apply(lambda x: get_conflict_groups(x))\n", - "\n", - "eao_controlled = list(control_categories[control_categories['category']=='EAO Control']['TS'].unique())\n", - "junta_controlled = list(control_categories[control_categories['category']=='SEC Control']['TS'].unique())\n", - "contested = list(control_categories[control_categories['category']=='Contested Areas']['TS'].unique())" + "control_categories = pd.read_csv(\"../../data/conflict/control_category_map.csv\")\n", + "control_categories.rename(columns={\"Township\": \"TS\"}, inplace=True)\n", + "control_categories[\"category\"] = control_categories[\"code_no\"].apply(\n", + " lambda x: get_conflict_groups(x)\n", + ")\n", + "\n", + "eao_controlled = list(\n", + " control_categories[control_categories[\"category\"] == \"EAO Control\"][\"TS\"].unique()\n", + ")\n", + "junta_controlled = list(\n", + " control_categories[control_categories[\"category\"] == \"SEC Control\"][\"TS\"].unique()\n", + ")\n", + "contested = list(\n", + " control_categories[control_categories[\"category\"] == \"Contested Areas\"][\n", + " \"TS\"\n", + " ].unique()\n", + ")" ] }, { @@ -289,8 +343,8 @@ }, "outputs": [], "source": [ - "ntl_adm3_monthly = ntl_adm3_monthly.merge(control_categories, on = 'TS')\n", - "ntl_adm3_annual = ntl_adm3_annual.merge(control_categories, on = 'TS')" + "ntl_adm3_monthly = ntl_adm3_monthly.merge(control_categories, on=\"TS\")\n", + "ntl_adm3_annual = ntl_adm3_annual.merge(control_categories, on=\"TS\")" ] }, { @@ -303,8 +357,22 @@ }, "outputs": [], "source": [ - "ntl_adm3_annual_grouped = ntl_adm3_monthly.groupby([pd.Grouper(key='date', freq='YS'), 'category'])[['ntl_nogf_5km_sum', 'ntl_sum']].mean().reset_index().sort_values(by=['date'])\n", - "ntl_adm3_monthly_grouped = ntl_adm3_monthly.groupby([pd.Grouper(key='date', freq='MS'), 'category'])[['ntl_nogf_5km_sum', 'ntl_sum']].mean().reset_index().sort_values(by=['date'])" + "ntl_adm3_annual_grouped = (\n", + " ntl_adm3_monthly.groupby([pd.Grouper(key=\"date\", freq=\"YS\"), \"category\"])[\n", + " [\"ntl_nogf_5km_sum\", \"ntl_sum\"]\n", + " ]\n", + " .mean()\n", + " .reset_index()\n", + " .sort_values(by=[\"date\"])\n", + ")\n", + "ntl_adm3_monthly_grouped = (\n", + " ntl_adm3_monthly.groupby([pd.Grouper(key=\"date\", freq=\"MS\"), \"category\"])[\n", + " [\"ntl_nogf_5km_sum\", \"ntl_sum\"]\n", + " ]\n", + " .mean()\n", + " .reset_index()\n", + " .sort_values(by=[\"date\"])\n", + ")" ] }, { @@ -317,16 +385,18 @@ }, "outputs": [], "source": [ - "control_code_dict = {1: \"Stable junta control\",\n", - "2: \"Junta dependent on local proxy militias for control\",\n", - "3:\"Junta forces under regular attack from resistance forces; administration functions remain weak\",\n", - "4:\"Resistance controls growing territory but still cannot consolidate fuller control\",\n", - "5:\"Limited junta movement, dependent on ceasefires\",\n", - "6:\"Junta control receding; resistance defending increasing territories & asserting local administration\",\n", - "7:\"Strong resistance control & local administration—90%+ of township\",\n", - "8:\"Full resistance control & local administration—whole township\"}\n", - "\n", - "control_category_list = ['SEC Control', 'EAO Control', 'Contested Areas']" + "control_code_dict = {\n", + " 1: \"Stable junta control\",\n", + " 2: \"Junta dependent on local proxy militias for control\",\n", + " 3: \"Junta forces under regular attack from resistance forces; administration functions remain weak\",\n", + " 4: \"Resistance controls growing territory but still cannot consolidate fuller control\",\n", + " 5: \"Limited junta movement, dependent on ceasefires\",\n", + " 6: \"Junta control receding; resistance defending increasing territories & asserting local administration\",\n", + " 7: \"Strong resistance control & local administration—90%+ of township\",\n", + " 8: \"Full resistance control & local administration—whole township\",\n", + "}\n", + "\n", + "control_category_list = [\"SEC Control\", \"EAO Control\", \"Contested Areas\"]" ] }, { @@ -339,8 +409,8 @@ }, "outputs": [], "source": [ - "conflict_monthly = conflict_monthly.merge(control_categories, on = 'TS')\n", - "#conflict_monthly['code_no'] = conflict_monthly['code_no'].astype(str)" + "conflict_monthly = conflict_monthly.merge(control_categories, on=\"TS\")\n", + "# conflict_monthly['code_no'] = conflict_monthly['code_no'].astype(str)" ] }, { @@ -406,16 +476,19 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", "\n", "df1 = ntl_adm3_annual_grouped\n", - "#df1=df1[df1['date']>'2021-01-1']\n", - "ntl_measure = 'ntl_nogf_5km_sum'\n", + "# df1=df1[df1['date']>'2021-01-1']\n", + "ntl_measure = \"ntl_nogf_5km_sum\"\n", "\n", "\n", - "tabs = get_multi_tab_bar_plot(df1,loop_list=control_category_list, \n", - " category_column='category', \n", - " ntl_measure=ntl_measure, title='Nighttime Lights Trend')\n", + "tabs = get_multi_tab_bar_plot(\n", + " df1,\n", + " loop_list=control_category_list,\n", + " category_column=\"category\",\n", + " ntl_measure=ntl_measure,\n", + " title=\"Nighttime Lights Trend\",\n", + ")\n", "\n", "# Create the Tabs layout\n", "tabs_layout = Tabs(tabs=tabs)\n", @@ -423,8 +496,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] }, { @@ -444,12 +517,25 @@ }, "outputs": [], "source": [ - "ntl_annual_baseline = ntl_adm3_annual_grouped[ntl_adm3_annual_grouped['date']=='2019-01-01'].groupby(['category'])['ntl_nogf_5km_sum'].first()\n", - "\n", - "ntl_adm3_annual_grouped = ntl_adm3_annual_grouped.set_index('category')\n", - "ntl_adm3_annual_grouped['baseline'] = ntl_adm3_annual_grouped.index.map(ntl_annual_baseline)\n", - "\n", - "ntl_adm3_annual_grouped['pchange_from_2019'] = 100*(ntl_adm3_annual_grouped['ntl_nogf_5km_sum'] - ntl_adm3_annual_grouped['baseline'])/ntl_adm3_annual_grouped['baseline']" + "ntl_annual_baseline = (\n", + " ntl_adm3_annual_grouped[ntl_adm3_annual_grouped[\"date\"] == \"2019-01-01\"]\n", + " .groupby([\"category\"])[\"ntl_nogf_5km_sum\"]\n", + " .first()\n", + ")\n", + "\n", + "ntl_adm3_annual_grouped = ntl_adm3_annual_grouped.set_index(\"category\")\n", + "ntl_adm3_annual_grouped[\"baseline\"] = ntl_adm3_annual_grouped.index.map(\n", + " ntl_annual_baseline\n", + ")\n", + "\n", + "ntl_adm3_annual_grouped[\"pchange_from_2019\"] = (\n", + " 100\n", + " * (\n", + " ntl_adm3_annual_grouped[\"ntl_nogf_5km_sum\"]\n", + " - ntl_adm3_annual_grouped[\"baseline\"]\n", + " )\n", + " / ntl_adm3_annual_grouped[\"baseline\"]\n", + ")" ] }, { @@ -515,16 +601,20 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", + "from bokeh.models import Tabs\n", "\n", "df1 = ntl_adm3_annual_grouped.reset_index()\n", - "#df1=df1[df1['date']>'2021-01-1']\n", - "ntl_measure = 'pchange_from_2019'\n", + "# df1=df1[df1['date']>'2021-01-1']\n", + "ntl_measure = \"pchange_from_2019\"\n", "\n", "\n", - "tabs = get_multi_tab_bar_plot(df1,loop_list=control_category_list, \n", - " category_column='category', \n", - " ntl_measure=ntl_measure, title='Percentage change in NTL from 2019')\n", + "tabs = get_multi_tab_bar_plot(\n", + " df1,\n", + " loop_list=control_category_list,\n", + " category_column=\"category\",\n", + " ntl_measure=ntl_measure,\n", + " title=\"Percentage change in NTL from 2019\",\n", + ")\n", "\n", "# Create the Tabs layout\n", "tabs_layout = Tabs(tabs=tabs)\n", @@ -532,8 +622,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] }, { @@ -546,8 +636,10 @@ }, "outputs": [], "source": [ - "ntl_adm3_annual_grouped = ntl_adm3_annual_grouped.sort_values(by=['category', 'date'])\n", - "ntl_adm3_annual_grouped['pchange_from_py'] = ntl_adm3_annual_grouped.groupby(['category'])['ntl_nogf_5km_sum'].pct_change()*100" + "ntl_adm3_annual_grouped = ntl_adm3_annual_grouped.sort_values(by=[\"category\", \"date\"])\n", + "ntl_adm3_annual_grouped[\"pchange_from_py\"] = (\n", + " ntl_adm3_annual_grouped.groupby([\"category\"])[\"ntl_nogf_5km_sum\"].pct_change() * 100\n", + ")" ] }, { @@ -613,16 +705,20 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", + "from bokeh.models import Tabs\n", "\n", "df1 = ntl_adm3_annual_grouped.reset_index()\n", - "#df1=df1[df1['date']>'2021-01-1']\n", - "ntl_measure = 'pchange_from_py'\n", + "# df1=df1[df1['date']>'2021-01-1']\n", + "ntl_measure = \"pchange_from_py\"\n", "\n", "\n", - "tabs = get_multi_tab_bar_plot(df1,loop_list=control_category_list, \n", - " category_column='category', \n", - " ntl_measure=ntl_measure, title='Percentage change in NTL from previous year')\n", + "tabs = get_multi_tab_bar_plot(\n", + " df1,\n", + " loop_list=control_category_list,\n", + " category_column=\"category\",\n", + " ntl_measure=ntl_measure,\n", + " title=\"Percentage change in NTL from previous year\",\n", + ")\n", "\n", "# Create the Tabs layout\n", "tabs_layout = Tabs(tabs=tabs)\n", @@ -630,8 +726,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] }, { @@ -644,15 +740,31 @@ }, "outputs": [], "source": [ - "ntl_adm3_monthly_grouped['year'] = ntl_adm3_monthly_grouped['date'].dt.year\n", - "ntl_adm3_monthly_grouped['month'] = ntl_adm3_monthly_grouped['date'].dt.month\n", - "\n", - "ntl_monthly_baseline = ntl_adm3_monthly_grouped[(ntl_adm3_monthly_grouped['date']>='2017-01-01')&(ntl_adm3_monthly_grouped['date']<='2019-12-01')].groupby(['category', 'month'])['ntl_nogf_5km_sum'].mean()\n", - "\n", - "ntl_adm3_monthly_grouped = ntl_adm3_monthly_grouped.set_index(['category', 'month'])\n", - "ntl_adm3_monthly_grouped['baseline'] = ntl_adm3_monthly_grouped.index.map(ntl_monthly_baseline)\n", - "\n", - "ntl_adm3_monthly_grouped['pchange_from_2017_2019'] = 100*(ntl_adm3_monthly_grouped['ntl_nogf_5km_sum'] - ntl_adm3_monthly_grouped['baseline'])/ntl_adm3_monthly_grouped['baseline']" + "ntl_adm3_monthly_grouped[\"year\"] = ntl_adm3_monthly_grouped[\"date\"].dt.year\n", + "ntl_adm3_monthly_grouped[\"month\"] = ntl_adm3_monthly_grouped[\"date\"].dt.month\n", + "\n", + "ntl_monthly_baseline = (\n", + " ntl_adm3_monthly_grouped[\n", + " (ntl_adm3_monthly_grouped[\"date\"] >= \"2017-01-01\")\n", + " & (ntl_adm3_monthly_grouped[\"date\"] <= \"2019-12-01\")\n", + " ]\n", + " .groupby([\"category\", \"month\"])[\"ntl_nogf_5km_sum\"]\n", + " .mean()\n", + ")\n", + "\n", + "ntl_adm3_monthly_grouped = ntl_adm3_monthly_grouped.set_index([\"category\", \"month\"])\n", + "ntl_adm3_monthly_grouped[\"baseline\"] = ntl_adm3_monthly_grouped.index.map(\n", + " ntl_monthly_baseline\n", + ")\n", + "\n", + "ntl_adm3_monthly_grouped[\"pchange_from_2017_2019\"] = (\n", + " 100\n", + " * (\n", + " ntl_adm3_monthly_grouped[\"ntl_nogf_5km_sum\"]\n", + " - ntl_adm3_monthly_grouped[\"baseline\"]\n", + " )\n", + " / ntl_adm3_monthly_grouped[\"baseline\"]\n", + ")" ] }, { @@ -718,16 +830,20 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", + "from bokeh.models import Tabs\n", "\n", "df1 = ntl_adm3_monthly_grouped.reset_index()\n", - "#df1=df1[df1['date']>'2021-01-1']\n", - "ntl_measure = 'pchange_from_2017_2019'\n", + "# df1=df1[df1['date']>'2021-01-1']\n", + "ntl_measure = \"pchange_from_2017_2019\"\n", "\n", "\n", - "tabs = get_multi_tab_bar_plot(df1[df1['date']>='2017-01-01'],loop_list=control_category_list, \n", - " category_column='category', \n", - " ntl_measure=ntl_measure, title='Percentage change in NTL from corresponding months in 2017-2019')\n", + "tabs = get_multi_tab_bar_plot(\n", + " df1[df1[\"date\"] >= \"2017-01-01\"],\n", + " loop_list=control_category_list,\n", + " category_column=\"category\",\n", + " ntl_measure=ntl_measure,\n", + " title=\"Percentage change in NTL from corresponding months in 2017-2019\",\n", + ")\n", "\n", "# Create the Tabs layout\n", "tabs_layout = Tabs(tabs=tabs)\n", @@ -735,8 +851,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] }, { @@ -749,10 +865,20 @@ }, "outputs": [], "source": [ - "ntl_adm3_monthly_grouped = ntl_adm3_monthly_grouped.sort_values(by=['category', 'month', 'year'])\n", - "ntl_adm3_monthly_grouped['py_same_month'] = (ntl_adm3_monthly_grouped\n", - " .groupby(['category', 'month'])['ntl_nogf_5km_sum'].shift(1))\n", - "ntl_adm3_monthly_grouped['pchange_from_py_same_month'] = 100*(ntl_adm3_monthly_grouped['ntl_nogf_5km_sum'] - ntl_adm3_monthly_grouped['py_same_month'])/ntl_adm3_monthly_grouped['py_same_month']\n" + "ntl_adm3_monthly_grouped = ntl_adm3_monthly_grouped.sort_values(\n", + " by=[\"category\", \"month\", \"year\"]\n", + ")\n", + "ntl_adm3_monthly_grouped[\"py_same_month\"] = ntl_adm3_monthly_grouped.groupby(\n", + " [\"category\", \"month\"]\n", + ")[\"ntl_nogf_5km_sum\"].shift(1)\n", + "ntl_adm3_monthly_grouped[\"pchange_from_py_same_month\"] = (\n", + " 100\n", + " * (\n", + " ntl_adm3_monthly_grouped[\"ntl_nogf_5km_sum\"]\n", + " - ntl_adm3_monthly_grouped[\"py_same_month\"]\n", + " )\n", + " / ntl_adm3_monthly_grouped[\"py_same_month\"]\n", + ")" ] }, { @@ -818,16 +944,20 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", + "from bokeh.models import Tabs\n", "\n", "df1 = ntl_adm3_monthly_grouped.reset_index()\n", - "#df1=df1[df1['date']>'2021-01-1']\n", - "ntl_measure = 'pchange_from_py_same_month'\n", + "# df1=df1[df1['date']>'2021-01-1']\n", + "ntl_measure = \"pchange_from_py_same_month\"\n", "\n", "\n", - "tabs = get_multi_tab_bar_plot(df1,loop_list=control_category_list, \n", - " category_column='category', \n", - " ntl_measure=ntl_measure, title='Percentage change in NTL from corresponding months in 2017-2019')\n", + "tabs = get_multi_tab_bar_plot(\n", + " df1,\n", + " loop_list=control_category_list,\n", + " category_column=\"category\",\n", + " ntl_measure=ntl_measure,\n", + " title=\"Percentage change in NTL from corresponding months in 2017-2019\",\n", + ")\n", "\n", "# Create the Tabs layout\n", "tabs_layout = Tabs(tabs=tabs)\n", @@ -835,8 +965,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] }, { @@ -849,11 +979,36 @@ }, "outputs": [], "source": [ - "very_severe_conflict = list(conflict_monthly[(conflict_monthly['conflict_category']=='Very severe conflict')&(conflict_monthly['date']>='2024-03-01')]['TS'].unique())\n", - "severe_conflict = list(conflict_monthly[(conflict_monthly['conflict_category']=='Severe conflict')&(conflict_monthly['date']>='2024-03-01')]['TS'].unique())\n", - "no_conflict = list(conflict_monthly[(conflict_monthly['conflict_category']=='No conflict')&(conflict_monthly['date']>='2024-03-01')]['TS'].unique())\n", - "mild_conflict = list(conflict_monthly[(conflict_monthly['conflict_category']=='Mild conflict')&(conflict_monthly['date']>='2024-03-01')]['TS'].unique())\n", - "moderate_conflict = list(conflict_monthly[(conflict_monthly['conflict_category']=='Moderate conflict')&(conflict_monthly['date']>='2024-03-01')]['TS'].unique())" + "very_severe_conflict = list(\n", + " conflict_monthly[\n", + " (conflict_monthly[\"conflict_category\"] == \"Very severe conflict\")\n", + " & (conflict_monthly[\"date\"] >= \"2024-03-01\")\n", + " ][\"TS\"].unique()\n", + ")\n", + "severe_conflict = list(\n", + " conflict_monthly[\n", + " (conflict_monthly[\"conflict_category\"] == \"Severe conflict\")\n", + " & (conflict_monthly[\"date\"] >= \"2024-03-01\")\n", + " ][\"TS\"].unique()\n", + ")\n", + "no_conflict = list(\n", + " conflict_monthly[\n", + " (conflict_monthly[\"conflict_category\"] == \"No conflict\")\n", + " & (conflict_monthly[\"date\"] >= \"2024-03-01\")\n", + " ][\"TS\"].unique()\n", + ")\n", + "mild_conflict = list(\n", + " conflict_monthly[\n", + " (conflict_monthly[\"conflict_category\"] == \"Mild conflict\")\n", + " & (conflict_monthly[\"date\"] >= \"2024-03-01\")\n", + " ][\"TS\"].unique()\n", + ")\n", + "moderate_conflict = list(\n", + " conflict_monthly[\n", + " (conflict_monthly[\"conflict_category\"] == \"Moderate conflict\")\n", + " & (conflict_monthly[\"date\"] >= \"2024-03-01\")\n", + " ][\"TS\"].unique()\n", + ")" ] }, { @@ -868,15 +1023,15 @@ "source": [ "def get_current_conflict_category(x):\n", " if x in very_severe_conflict:\n", - " return 'very_severe_conflict'\n", + " return \"very_severe_conflict\"\n", " elif x in severe_conflict:\n", - " return 'severe_conflict'\n", + " return \"severe_conflict\"\n", " elif x in no_conflict:\n", - " return 'no_conflict'\n", + " return \"no_conflict\"\n", " elif x in mild_conflict:\n", - " return 'mild_conflict'\n", - " elif x in 'moderate_conflict':\n", - " return 'moderate_conflict'" + " return \"mild_conflict\"\n", + " elif x in \"moderate_conflict\":\n", + " return \"moderate_conflict\"" ] }, { @@ -889,7 +1044,9 @@ }, "outputs": [], "source": [ - "ntl_adm3_monthly['conflict_category'] = ntl_adm3_monthly['TS'].apply(lambda x : get_current_conflict_category(x))" + "ntl_adm3_monthly[\"conflict_category\"] = ntl_adm3_monthly[\"TS\"].apply(\n", + " lambda x: get_current_conflict_category(x)\n", + ")" ] }, { @@ -902,8 +1059,22 @@ }, "outputs": [], "source": [ - "ntl_adm3_annual_grouped_conflict = ntl_adm3_monthly.groupby([pd.Grouper(key='date', freq='YS'), 'conflict_category'])[['ntl_nogf_5km_sum', 'ntl_sum']].mean().reset_index().sort_values(by=['date'])\n", - "ntl_adm3_monthly_grouped_conflict = ntl_adm3_monthly.groupby([pd.Grouper(key='date', freq='MS'), 'conflict_category'])[['ntl_nogf_5km_sum', 'ntl_sum']].mean().reset_index().sort_values(by=['date'])" + "ntl_adm3_annual_grouped_conflict = (\n", + " ntl_adm3_monthly.groupby([pd.Grouper(key=\"date\", freq=\"YS\"), \"conflict_category\"])[\n", + " [\"ntl_nogf_5km_sum\", \"ntl_sum\"]\n", + " ]\n", + " .mean()\n", + " .reset_index()\n", + " .sort_values(by=[\"date\"])\n", + ")\n", + "ntl_adm3_monthly_grouped_conflict = (\n", + " ntl_adm3_monthly.groupby([pd.Grouper(key=\"date\", freq=\"MS\"), \"conflict_category\"])[\n", + " [\"ntl_nogf_5km_sum\", \"ntl_sum\"]\n", + " ]\n", + " .mean()\n", + " .reset_index()\n", + " .sort_values(by=[\"date\"])\n", + ")" ] }, { @@ -916,12 +1087,29 @@ }, "outputs": [], "source": [ - "ntl_annual_baseline = ntl_adm3_annual_grouped_conflict[ntl_adm3_annual_grouped_conflict['date']=='2019-01-01'].groupby(['conflict_category'])['ntl_nogf_5km_sum'].first()\n", - "\n", - "ntl_adm3_annual_grouped_conflict = ntl_adm3_annual_grouped_conflict.set_index('conflict_category')\n", - "ntl_adm3_annual_grouped_conflict['baseline'] = ntl_adm3_annual_grouped_conflict.index.map(ntl_annual_baseline)\n", - "\n", - "ntl_adm3_annual_grouped_conflict['pchange_from_2019'] = 100*(ntl_adm3_annual_grouped_conflict['ntl_nogf_5km_sum'] - ntl_adm3_annual_grouped_conflict['baseline'])/ntl_adm3_annual_grouped_conflict['baseline']" + "ntl_annual_baseline = (\n", + " ntl_adm3_annual_grouped_conflict[\n", + " ntl_adm3_annual_grouped_conflict[\"date\"] == \"2019-01-01\"\n", + " ]\n", + " .groupby([\"conflict_category\"])[\"ntl_nogf_5km_sum\"]\n", + " .first()\n", + ")\n", + "\n", + "ntl_adm3_annual_grouped_conflict = ntl_adm3_annual_grouped_conflict.set_index(\n", + " \"conflict_category\"\n", + ")\n", + "ntl_adm3_annual_grouped_conflict[\"baseline\"] = (\n", + " ntl_adm3_annual_grouped_conflict.index.map(ntl_annual_baseline)\n", + ")\n", + "\n", + "ntl_adm3_annual_grouped_conflict[\"pchange_from_2019\"] = (\n", + " 100\n", + " * (\n", + " ntl_adm3_annual_grouped_conflict[\"ntl_nogf_5km_sum\"]\n", + " - ntl_adm3_annual_grouped_conflict[\"baseline\"]\n", + " )\n", + " / ntl_adm3_annual_grouped_conflict[\"baseline\"]\n", + ")" ] }, { @@ -934,7 +1122,13 @@ }, "outputs": [], "source": [ - "conflict_category_list = ['no_conflict', 'mild_conflict', 'moderate_conflict', 'severe_conflict', 'very_severe_conflict']" + "conflict_category_list = [\n", + " \"no_conflict\",\n", + " \"mild_conflict\",\n", + " \"moderate_conflict\",\n", + " \"severe_conflict\",\n", + " \"very_severe_conflict\",\n", + "]" ] }, { @@ -1000,16 +1194,20 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", + "from bokeh.models import Tabs\n", "\n", "df1 = ntl_adm3_annual_grouped_conflict.reset_index()\n", - "#df1=df1[df1['date']>'2021-01-1']\n", - "ntl_measure = 'pchange_from_2019'\n", + "# df1=df1[df1['date']>'2021-01-1']\n", + "ntl_measure = \"pchange_from_2019\"\n", "\n", "\n", - "tabs = get_multi_tab_bar_plot(df1[df1['date']>='2019-01-01'],loop_list=conflict_category_list, \n", - " category_column='conflict_category', \n", - " ntl_measure=ntl_measure, title='Percentage change in NTL from 2019')\n", + "tabs = get_multi_tab_bar_plot(\n", + " df1[df1[\"date\"] >= \"2019-01-01\"],\n", + " loop_list=conflict_category_list,\n", + " category_column=\"conflict_category\",\n", + " ntl_measure=ntl_measure,\n", + " title=\"Percentage change in NTL from 2019\",\n", + ")\n", "\n", "# Create the Tabs layout\n", "tabs_layout = Tabs(tabs=tabs)\n", @@ -1017,8 +1215,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] }, { @@ -1031,8 +1229,15 @@ }, "outputs": [], "source": [ - "ntl_adm3_annual_grouped_conflict = ntl_adm3_annual_grouped_conflict.sort_values(by=['conflict_category', 'date'])\n", - "ntl_adm3_annual_grouped_conflict['pchange_from_py'] = ntl_adm3_annual_grouped_conflict.groupby(['conflict_category'])['ntl_nogf_5km_sum'].pct_change()*100" + "ntl_adm3_annual_grouped_conflict = ntl_adm3_annual_grouped_conflict.sort_values(\n", + " by=[\"conflict_category\", \"date\"]\n", + ")\n", + "ntl_adm3_annual_grouped_conflict[\"pchange_from_py\"] = (\n", + " ntl_adm3_annual_grouped_conflict.groupby([\"conflict_category\"])[\n", + " \"ntl_nogf_5km_sum\"\n", + " ].pct_change()\n", + " * 100\n", + ")" ] }, { @@ -1098,16 +1303,20 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", + "from bokeh.models import Tabs\n", "\n", "df1 = ntl_adm3_annual_grouped_conflict.reset_index()\n", - "#df1=df1[df1['date']>'2021-01-1']\n", - "ntl_measure = 'pchange_from_py'\n", + "# df1=df1[df1['date']>'2021-01-1']\n", + "ntl_measure = \"pchange_from_py\"\n", "\n", "\n", - "tabs = get_multi_tab_bar_plot(df1,loop_list=conflict_category_list, \n", - " category_column='conflict_category', \n", - " ntl_measure=ntl_measure, title='Percentage change in NTL from previous year')\n", + "tabs = get_multi_tab_bar_plot(\n", + " df1,\n", + " loop_list=conflict_category_list,\n", + " category_column=\"conflict_category\",\n", + " ntl_measure=ntl_measure,\n", + " title=\"Percentage change in NTL from previous year\",\n", + ")\n", "\n", "# Create the Tabs layout\n", "tabs_layout = Tabs(tabs=tabs)\n", @@ -1115,8 +1324,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] }, { @@ -1129,16 +1338,37 @@ }, "outputs": [], "source": [ - "ntl_adm3_monthly_grouped_conflict['year'] = ntl_adm3_monthly_grouped_conflict['date'].dt.year\n", - "ntl_adm3_monthly_grouped_conflict['month'] = ntl_adm3_monthly_grouped_conflict['date'].dt.month\n", - "\n", - "ntl_monthly_baseline = (ntl_adm3_monthly_grouped_conflict[(ntl_adm3_monthly_grouped_conflict['date']>='2017-01-01')&(ntl_adm3_monthly_grouped_conflict['date']<='2019-12-01')]\n", - " .groupby(['conflict_category', 'month'])['ntl_nogf_5km_sum'].mean())\n", - "\n", - "ntl_adm3_monthly_grouped_conflict = ntl_adm3_monthly_grouped_conflict.set_index(['conflict_category', 'month'])\n", - "ntl_adm3_monthly_grouped_conflict['baseline'] = ntl_adm3_monthly_grouped_conflict.index.map(ntl_monthly_baseline)\n", - "\n", - "ntl_adm3_monthly_grouped_conflict['pchange_from_2017_2019'] = 100*(ntl_adm3_monthly_grouped_conflict['ntl_nogf_5km_sum'] - ntl_adm3_monthly_grouped_conflict['baseline'])/ntl_adm3_monthly_grouped_conflict['baseline']" + "ntl_adm3_monthly_grouped_conflict[\"year\"] = ntl_adm3_monthly_grouped_conflict[\n", + " \"date\"\n", + "].dt.year\n", + "ntl_adm3_monthly_grouped_conflict[\"month\"] = ntl_adm3_monthly_grouped_conflict[\n", + " \"date\"\n", + "].dt.month\n", + "\n", + "ntl_monthly_baseline = (\n", + " ntl_adm3_monthly_grouped_conflict[\n", + " (ntl_adm3_monthly_grouped_conflict[\"date\"] >= \"2017-01-01\")\n", + " & (ntl_adm3_monthly_grouped_conflict[\"date\"] <= \"2019-12-01\")\n", + " ]\n", + " .groupby([\"conflict_category\", \"month\"])[\"ntl_nogf_5km_sum\"]\n", + " .mean()\n", + ")\n", + "\n", + "ntl_adm3_monthly_grouped_conflict = ntl_adm3_monthly_grouped_conflict.set_index(\n", + " [\"conflict_category\", \"month\"]\n", + ")\n", + "ntl_adm3_monthly_grouped_conflict[\"baseline\"] = (\n", + " ntl_adm3_monthly_grouped_conflict.index.map(ntl_monthly_baseline)\n", + ")\n", + "\n", + "ntl_adm3_monthly_grouped_conflict[\"pchange_from_2017_2019\"] = (\n", + " 100\n", + " * (\n", + " ntl_adm3_monthly_grouped_conflict[\"ntl_nogf_5km_sum\"]\n", + " - ntl_adm3_monthly_grouped_conflict[\"baseline\"]\n", + " )\n", + " / ntl_adm3_monthly_grouped_conflict[\"baseline\"]\n", + ")" ] }, { @@ -1204,16 +1434,20 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", + "from bokeh.models import Tabs\n", "\n", "df1 = ntl_adm3_monthly_grouped_conflict.reset_index()\n", - "#df1=df1[df1['date']>'2021-01-1']\n", - "ntl_measure = 'pchange_from_2017_2019'\n", + "# df1=df1[df1['date']>'2021-01-1']\n", + "ntl_measure = \"pchange_from_2017_2019\"\n", "\n", "\n", - "tabs = get_multi_tab_bar_plot(df1[df1['date']>'2017-01-01'],loop_list=conflict_category_list, \n", - " category_column='conflict_category', \n", - " ntl_measure=ntl_measure, title='Percentage change in NTL from corresponding months in 2017-2019')\n", + "tabs = get_multi_tab_bar_plot(\n", + " df1[df1[\"date\"] > \"2017-01-01\"],\n", + " loop_list=conflict_category_list,\n", + " category_column=\"conflict_category\",\n", + " ntl_measure=ntl_measure,\n", + " title=\"Percentage change in NTL from corresponding months in 2017-2019\",\n", + ")\n", "\n", "# Create the Tabs layout\n", "tabs_layout = Tabs(tabs=tabs)\n", @@ -1221,8 +1455,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] }, { @@ -1235,10 +1469,22 @@ }, "outputs": [], "source": [ - "ntl_adm3_monthly_grouped_conflict = ntl_adm3_monthly_grouped_conflict.sort_values(by=['conflict_category', 'month', 'year'])\n", - "ntl_adm3_monthly_grouped_conflict['py_same_month'] = (ntl_adm3_monthly_grouped_conflict\n", - " .groupby(['conflict_category', 'month'])['ntl_nogf_5km_sum'].shift(1))\n", - "ntl_adm3_monthly_grouped_conflict['pchange_from_py_same_month'] = 100*(ntl_adm3_monthly_grouped_conflict['ntl_nogf_5km_sum'] - ntl_adm3_monthly_grouped_conflict['py_same_month'])/ntl_adm3_monthly_grouped_conflict['py_same_month']\n" + "ntl_adm3_monthly_grouped_conflict = ntl_adm3_monthly_grouped_conflict.sort_values(\n", + " by=[\"conflict_category\", \"month\", \"year\"]\n", + ")\n", + "ntl_adm3_monthly_grouped_conflict[\"py_same_month\"] = (\n", + " ntl_adm3_monthly_grouped_conflict.groupby([\"conflict_category\", \"month\"])[\n", + " \"ntl_nogf_5km_sum\"\n", + " ].shift(1)\n", + ")\n", + "ntl_adm3_monthly_grouped_conflict[\"pchange_from_py_same_month\"] = (\n", + " 100\n", + " * (\n", + " ntl_adm3_monthly_grouped_conflict[\"ntl_nogf_5km_sum\"]\n", + " - ntl_adm3_monthly_grouped_conflict[\"py_same_month\"]\n", + " )\n", + " / ntl_adm3_monthly_grouped_conflict[\"py_same_month\"]\n", + ")" ] }, { @@ -1304,16 +1550,20 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", + "from bokeh.models import Tabs\n", "\n", "df1 = ntl_adm3_monthly_grouped_conflict.reset_index()\n", - "#df1=df1[df1['date']>'2021-01-1']\n", - "ntl_measure = 'pchange_from_py_same_month'\n", + "# df1=df1[df1['date']>'2021-01-1']\n", + "ntl_measure = \"pchange_from_py_same_month\"\n", "\n", "\n", - "tabs = get_multi_tab_bar_plot(df1,loop_list=conflict_category_list, \n", - " category_column='conflict_category', \n", - " ntl_measure=ntl_measure, title='Percentage change in NTL from corresponding months in previous year')\n", + "tabs = get_multi_tab_bar_plot(\n", + " df1,\n", + " loop_list=conflict_category_list,\n", + " category_column=\"conflict_category\",\n", + " ntl_measure=ntl_measure,\n", + " title=\"Percentage change in NTL from corresponding months in previous year\",\n", + ")\n", "\n", "# Create the Tabs layout\n", "tabs_layout = Tabs(tabs=tabs)\n", @@ -1321,8 +1571,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] } ], diff --git a/notebooks/conflict/conflict-and-ntl.ipynb b/notebooks/conflict/conflict-and-ntl.ipynb index fc2238f..9a403ed 100644 --- a/notebooks/conflict/conflict-and-ntl.ipynb +++ b/notebooks/conflict/conflict-and-ntl.ipynb @@ -11,9 +11,7 @@ "outputs": [], "source": [ "import pandas as pd\n", - "import numpy as np\n", - "import geopandas as gpd\n", - "import matplotlib.pyplot as plt" + "import geopandas as gpd" ] }, { @@ -26,72 +24,104 @@ }, "outputs": [], "source": [ - "import pandas as pd\n", - "import numpy as np\n", "from bokeh.plotting import figure, show\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, ColumnDataSource, HoverTool\n", - "\n", - "def get_multi_tab_line_plot(df1, df2, loop_list, category_column, conflict_measure, ntl_measure):\n", - "\n", + "from bokeh.models import (\n", + " LinearAxis,\n", + " Range1d,\n", + " Tabs,\n", + " TabPanel,\n", + " ColumnDataSource,\n", + " HoverTool,\n", + ")\n", + "\n", + "\n", + "def get_multi_tab_line_plot(\n", + " df1, df2, loop_list, category_column, conflict_measure, ntl_measure\n", + "):\n", " tabs = []\n", "\n", " for ts in loop_list:\n", - " # # Create a new Bokeh figure\n", - " p = figure(title=f\"Comparing Nighttime Light Trends and {conflict_measure.capitalize()}\", x_axis_label='Month', width=800, height=400, x_axis_type='datetime')\n", + " # # Create a new Bokeh figure\n", + " p = figure(\n", + " title=f\"Comparing Nighttime Light Trends and {conflict_measure.capitalize()}\",\n", + " x_axis_label=\"Month\",\n", + " width=800,\n", + " height=400,\n", + " x_axis_type=\"datetime\",\n", + " )\n", "\n", - " p.y_range.start = df2[df2[category_column]==ts][conflict_measure].min() \n", - " p.y_range.end = df2[df2[category_column]==ts][conflict_measure].max()\n", + " p.y_range.start = df2[df2[category_column] == ts][conflict_measure].min()\n", + " p.y_range.end = df2[df2[category_column] == ts][conflict_measure].max()\n", "\n", - " p.extra_y_ranges = {\"y2\": Range1d(start=df1[df1[category_column]==ts][ntl_measure].min(), end=df1[df1[category_column]==ts][ntl_measure].max())}\n", + " p.extra_y_ranges = {\n", + " \"y2\": Range1d(\n", + " start=df1[df1[category_column] == ts][ntl_measure].min(),\n", + " end=df1[df1[category_column] == ts][ntl_measure].max(),\n", + " )\n", + " }\n", " # Add the second line plot\n", " ntl_source = ColumnDataSource(df1[df1[category_column] == ts])\n", - " ntl_line = p.vbar('date', top=ntl_measure,source=ntl_source, legend_label=ntl_measure, width=2000*2000*750, color=\"blue\", alpha=0.7, y_range_name='y2')\n", - "\n", + " ntl_line = p.vbar(\n", + " \"date\",\n", + " top=ntl_measure,\n", + " source=ntl_source,\n", + " legend_label=ntl_measure,\n", + " width=2000 * 2000 * 750,\n", + " color=\"blue\",\n", + " alpha=0.7,\n", + " y_range_name=\"y2\",\n", + " )\n", "\n", " # Add the first line plot\n", " conflict_source = ColumnDataSource(df2[df2[category_column] == ts])\n", - " conflict_line = p.line('date', conflict_measure,source=conflict_source, legend_label=f\"{conflict_measure.capitalize()}\", line_width=2, color=\"red\", alpha=0.7)\n", + " conflict_line = p.line(\n", + " \"date\",\n", + " conflict_measure,\n", + " source=conflict_source,\n", + " legend_label=f\"{conflict_measure.capitalize()}\",\n", + " line_width=2,\n", + " color=\"red\",\n", + " alpha=0.7,\n", + " )\n", "\n", - " \n", " # Customize legend\n", " p.legend.location = \"top_left\"\n", " p.legend.click_policy = \"hide\" # Optional: Allow clicking to hide/show lines\n", "\n", - " p.add_layout(LinearAxis(y_range_name=\"y2\", axis_label='Luminosity'), 'right')\n", + " p.add_layout(LinearAxis(y_range_name=\"y2\", axis_label=\"Luminosity\"), \"right\")\n", "\n", " # Add hover tool for conflict data\n", " hover_conflict = HoverTool(\n", " renderers=[conflict_line], # Reference the renderer directly\n", " tooltips=[\n", - " ('Date', '@date{%F}'),\n", - " (f'{conflict_measure.capitalize()}', f'@{conflict_measure}'),\n", + " (\"Date\", \"@date{%F}\"),\n", + " (f\"{conflict_measure.capitalize()}\", f\"@{conflict_measure}\"),\n", " ],\n", " formatters={\n", - " '@date': 'datetime', # Format the date\n", + " \"@date\": \"datetime\", # Format the date\n", " },\n", - " mode='vline'\n", + " mode=\"vline\",\n", " )\n", - " \n", + "\n", " # Add hover tool for NTL data\n", " hover_ntl = HoverTool(\n", " renderers=[ntl_line], # Reference the renderer directly\n", " tooltips=[\n", - " ('Date', '@date{%F}'),\n", - " (ntl_measure, f'@{ntl_measure}'),\n", + " (\"Date\", \"@date{%F}\"),\n", + " (ntl_measure, f\"@{ntl_measure}\"),\n", " ],\n", " formatters={\n", - " '@date': 'datetime', # Format the date\n", + " \"@date\": \"datetime\", # Format the date\n", " },\n", - " mode='vline'\n", + " mode=\"vline\",\n", " )\n", - " \n", + "\n", " p.add_tools(hover_conflict, hover_ntl)\n", "\n", " tab = TabPanel(child=p, title=ts)\n", " tabs.append(tab)\n", "\n", - " return tabs\n", - "\n" + " return tabs" ] }, { @@ -104,50 +134,57 @@ }, "outputs": [], "source": [ - "import pandas as pd\n", - "import numpy as np\n", - "from bokeh.plotting import figure, show\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, ColumnDataSource, HoverTool\n", - "\n", "def get_multi_tab_bar_plot(df1, loop_list, category_column, ntl_measure):\n", - "\n", " tabs = []\n", "\n", " for ts in loop_list:\n", - " # # Create a new Bokeh figure\n", - " p = figure(title=f\"Nighttime Light Trends\", x_axis_label='Month', width=800, height=400, x_axis_type='datetime')\n", + " # # Create a new Bokeh figure\n", + " p = figure(\n", + " title=\"Nighttime Light Trends\",\n", + " x_axis_label=\"Month\",\n", + " width=800,\n", + " height=400,\n", + " x_axis_type=\"datetime\",\n", + " )\n", "\n", " ntl_source = ColumnDataSource(df1[df1[category_column] == ts])\n", - " p.y_range.start = df1[df1[category_column]==ts][ntl_measure].min() \n", - " p.y_range.end = df1[df1[category_column]==ts][ntl_measure].max()\n", + " p.y_range.start = df1[df1[category_column] == ts][ntl_measure].min()\n", + " p.y_range.end = df1[df1[category_column] == ts][ntl_measure].max()\n", " # Add the second line plot\n", - " \n", - " ntl_line = p.vbar(x='date', top=ntl_measure,source=ntl_source, legend_label=ntl_measure,width=2000*2000*750, color=\"blue\", alpha=0.7)\n", + "\n", + " ntl_line = p.vbar(\n", + " x=\"date\",\n", + " top=ntl_measure,\n", + " source=ntl_source,\n", + " legend_label=ntl_measure,\n", + " width=2000 * 2000 * 750,\n", + " color=\"blue\",\n", + " alpha=0.7,\n", + " )\n", "\n", " # Customize legend\n", " p.legend.location = \"top_left\"\n", " p.legend.click_policy = \"hide\" # Optional: Allow clicking to hide/show lines\n", - " \n", + "\n", " # Add hover tool for NTL data\n", " hover_ntl = HoverTool(\n", " renderers=[ntl_line], # Reference the renderer directly\n", " tooltips=[\n", - " ('Date', '@date{%F}'),\n", - " (ntl_measure, f'@{ntl_measure}'),\n", + " (\"Date\", \"@date{%F}\"),\n", + " (ntl_measure, f\"@{ntl_measure}\"),\n", " ],\n", " formatters={\n", - " '@date': 'datetime', # Format the date\n", + " \"@date\": \"datetime\", # Format the date\n", " },\n", - " mode='vline'\n", + " mode=\"vline\",\n", " )\n", - " \n", + "\n", " p.add_tools(hover_ntl, hover_ntl)\n", "\n", " tab = TabPanel(child=p, title=ts)\n", " tabs.append(tab)\n", "\n", - " return tabs\n", - "\n" + " return tabs" ] }, { @@ -190,10 +227,14 @@ }, "outputs": [], "source": [ - "ntl_adm3_monthly = pd.read_csv('../../data/ntl/ntl_mmr_adm3_monthly.csv')\n", - "conflict_monthly = pd.read_stata('../../data/conflict/ACLED_01_Jan_2021_21_Oct_2024_township_monthly_spatial.dta')\n", - "conflict_6_monthly = pd.read_stata('../../data/conflict/ACLED_Apr_2022_Sep_2024_township_spatial_period_change.dta')\n", - "mmr_adm3 = gpd.read_file('../../data/boundaries/mmr_polbnda_adm3_250k_mimu.shp')" + "ntl_adm3_monthly = pd.read_csv(\"../../data/ntl/ntl_mmr_adm3_monthly.csv\")\n", + "conflict_monthly = pd.read_stata(\n", + " \"../../data/conflict/ACLED_01_Jan_2021_21_Oct_2024_township_monthly_spatial.dta\"\n", + ")\n", + "conflict_6_monthly = pd.read_stata(\n", + " \"../../data/conflict/ACLED_Apr_2022_Sep_2024_township_spatial_period_change.dta\"\n", + ")\n", + "mmr_adm3 = gpd.read_file(\"../../data/boundaries/mmr_polbnda_adm3_250k_mimu.shp\")" ] }, { @@ -206,9 +247,11 @@ }, "outputs": [], "source": [ - "ntl_adm3_monthly = mmr_adm3[['ST', 'DT', 'TS', 'geometry']].merge(ntl_adm3_monthly, on = ['ST', 'DT', 'TS'])\n", - "ntl_adm3_monthly['date'] = pd.to_datetime(ntl_adm3_monthly['date'])\n", - "conflict_monthly.rename(columns={'month':'date'}, inplace=True)" + "ntl_adm3_monthly = mmr_adm3[[\"ST\", \"DT\", \"TS\", \"geometry\"]].merge(\n", + " ntl_adm3_monthly, on=[\"ST\", \"DT\", \"TS\"]\n", + ")\n", + "ntl_adm3_monthly[\"date\"] = pd.to_datetime(ntl_adm3_monthly[\"date\"])\n", + "conflict_monthly.rename(columns={\"month\": \"date\"}, inplace=True)" ] }, { @@ -221,10 +264,14 @@ }, "outputs": [], "source": [ - "control_categories = pd.read_csv('../../data/conflict/control_category_map.csv')\n", - "control_categories.rename(columns={'Township': 'TS'}, inplace=True)\n", - "eao_controlled = list(control_categories[control_categories['code_no']==8.0]['TS'].unique())\n", - "junta_controlled = list(control_categories[control_categories['code_no']==1.0]['TS'].unique())" + "control_categories = pd.read_csv(\"../../data/conflict/control_category_map.csv\")\n", + "control_categories.rename(columns={\"Township\": \"TS\"}, inplace=True)\n", + "eao_controlled = list(\n", + " control_categories[control_categories[\"code_no\"] == 8.0][\"TS\"].unique()\n", + ")\n", + "junta_controlled = list(\n", + " control_categories[control_categories[\"code_no\"] == 1.0][\"TS\"].unique()\n", + ")" ] }, { @@ -237,8 +284,8 @@ }, "outputs": [], "source": [ - "ntl_adm3_monthly = ntl_adm3_monthly.merge(control_categories, on = 'TS')\n", - "ntl_adm3_monthly['code_no'] = ntl_adm3_monthly['code_no'].astype(str)" + "ntl_adm3_monthly = ntl_adm3_monthly.merge(control_categories, on=\"TS\")\n", + "ntl_adm3_monthly[\"code_no\"] = ntl_adm3_monthly[\"code_no\"].astype(str)" ] }, { @@ -251,8 +298,8 @@ }, "outputs": [], "source": [ - "conflict_monthly = conflict_monthly.merge(control_categories, on = 'TS')\n", - "conflict_monthly['code_no'] = conflict_monthly['code_no'].astype(str)" + "conflict_monthly = conflict_monthly.merge(control_categories, on=\"TS\")\n", + "conflict_monthly[\"code_no\"] = conflict_monthly[\"code_no\"].astype(str)" ] }, { @@ -265,14 +312,16 @@ }, "outputs": [], "source": [ - "control_code_dict = {'1.0': \"Stable junta control\",\n", - "'2.0': \"Junta dependent on local proxy militias for control\",\n", - "'3.0':\"Junta forces under regular attack from resistance forces; administration functions remain weak\",\n", - "'4.0':\"Resistance controls growing territory but still cannot consolidate fuller control\",\n", - "'5.0':\"Limited junta movement, dependent on ceasefires\",\n", - "'6.0':\"Junta control receding; resistance defending increasing territories & asserting local administration\",\n", - "'7.0':\"Strong resistance control & local administration—90%+ of township\",\n", - "'8.0':\"Full resistance control & local administration—whole township\"}" + "control_code_dict = {\n", + " \"1.0\": \"Stable junta control\",\n", + " \"2.0\": \"Junta dependent on local proxy militias for control\",\n", + " \"3.0\": \"Junta forces under regular attack from resistance forces; administration functions remain weak\",\n", + " \"4.0\": \"Resistance controls growing territory but still cannot consolidate fuller control\",\n", + " \"5.0\": \"Limited junta movement, dependent on ceasefires\",\n", + " \"6.0\": \"Junta control receding; resistance defending increasing territories & asserting local administration\",\n", + " \"7.0\": \"Strong resistance control & local administration—90%+ of township\",\n", + " \"8.0\": \"Full resistance control & local administration—whole township\",\n", + "}" ] }, { @@ -352,14 +401,23 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", "\n", - "df1 = ntl_adm3_monthly[['TS','code_no', 'date', 'ntl_sum', 'ntl_nogf_5km_sum']].groupby(['date','code_no'])[['ntl_sum', 'ntl_nogf_5km_sum']].mean().reset_index()\n", - "df1=df1[df1['date']>'2021-01-1']\n", - "ntl_measure = 'ntl_nogf_5km_sum'\n", + "df1 = (\n", + " ntl_adm3_monthly[[\"TS\", \"code_no\", \"date\", \"ntl_sum\", \"ntl_nogf_5km_sum\"]]\n", + " .groupby([\"date\", \"code_no\"])[[\"ntl_sum\", \"ntl_nogf_5km_sum\"]]\n", + " .mean()\n", + " .reset_index()\n", + ")\n", + "df1 = df1[df1[\"date\"] > \"2021-01-1\"]\n", + "ntl_measure = \"ntl_nogf_5km_sum\"\n", "\n", "\n", - "tabs = get_multi_tab_bar_plot(df1,loop_list=list(control_code_dict.keys()), category_column='code_no', ntl_measure=ntl_measure)\n", + "tabs = get_multi_tab_bar_plot(\n", + " df1,\n", + " loop_list=list(control_code_dict.keys()),\n", + " category_column=\"code_no\",\n", + " ntl_measure=ntl_measure,\n", + ")\n", "\n", "# Create the Tabs layout\n", "tabs_layout = Tabs(tabs=tabs)\n", @@ -367,8 +425,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] }, { @@ -448,14 +506,21 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", + "from bokeh.models import Tabs\n", "\n", - "df1 = ntl_adm3_monthly[['TS','code_no', 'date', 'ntl_sum', 'ntl_nogf_5km_sum']].groupby(['date','TS'])[['ntl_sum', 'ntl_nogf_5km_sum']].mean().reset_index()\n", - "df1=df1[df1['date']>'2021-01-1']\n", - "ntl_measure = 'ntl_nogf_5km_sum'\n", + "df1 = (\n", + " ntl_adm3_monthly[[\"TS\", \"code_no\", \"date\", \"ntl_sum\", \"ntl_nogf_5km_sum\"]]\n", + " .groupby([\"date\", \"TS\"])[[\"ntl_sum\", \"ntl_nogf_5km_sum\"]]\n", + " .mean()\n", + " .reset_index()\n", + ")\n", + "df1 = df1[df1[\"date\"] > \"2021-01-1\"]\n", + "ntl_measure = \"ntl_nogf_5km_sum\"\n", "\n", "\n", - "tabs = get_multi_tab_bar_plot(df1,loop_list=eao_controlled, category_column='TS', ntl_measure=ntl_measure)\n", + "tabs = get_multi_tab_bar_plot(\n", + " df1, loop_list=eao_controlled, category_column=\"TS\", ntl_measure=ntl_measure\n", + ")\n", "\n", "# Create the Tabs layout\n", "tabs_layout = Tabs(tabs=tabs)\n", @@ -463,8 +528,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] }, { @@ -537,14 +602,21 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", + "from bokeh.models import Tabs\n", "\n", - "df1 = ntl_adm3_monthly[['TS','code_no', 'date', 'ntl_sum', 'ntl_nogf_5km_sum']].groupby(['date','TS'])[['ntl_sum', 'ntl_nogf_5km_sum']].mean().reset_index()\n", - "df1=df1[df1['date']>'2021-01-1']\n", - "ntl_measure = 'ntl_nogf_5km_sum'\n", + "df1 = (\n", + " ntl_adm3_monthly[[\"TS\", \"code_no\", \"date\", \"ntl_sum\", \"ntl_nogf_5km_sum\"]]\n", + " .groupby([\"date\", \"TS\"])[[\"ntl_sum\", \"ntl_nogf_5km_sum\"]]\n", + " .mean()\n", + " .reset_index()\n", + ")\n", + "df1 = df1[df1[\"date\"] > \"2021-01-1\"]\n", + "ntl_measure = \"ntl_nogf_5km_sum\"\n", "\n", "\n", - "tabs = get_multi_tab_bar_plot(df1,loop_list=junta_controlled, category_column='TS', ntl_measure=ntl_measure)\n", + "tabs = get_multi_tab_bar_plot(\n", + " df1, loop_list=junta_controlled, category_column=\"TS\", ntl_measure=ntl_measure\n", + ")\n", "\n", "# Create the Tabs layout\n", "tabs_layout = Tabs(tabs=tabs)\n", @@ -552,8 +624,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] }, { @@ -573,9 +645,24 @@ }, "outputs": [], "source": [ - "very_severe_conflict = list(conflict_monthly[(conflict_monthly['conflict_category']=='Very severe conflict')&(conflict_monthly['date']>='2024-03-01')]['TS'].unique())\n", - "severe_conflict = list(conflict_monthly[(conflict_monthly['conflict_category']=='Severe conflict')&(conflict_monthly['date']>='2024-03-01')]['TS'].unique())\n", - "no_conflict = list(conflict_monthly[(conflict_monthly['conflict_category']=='No conflict')&(conflict_monthly['date']>='2024-03-01')]['TS'].unique())" + "very_severe_conflict = list(\n", + " conflict_monthly[\n", + " (conflict_monthly[\"conflict_category\"] == \"Very severe conflict\")\n", + " & (conflict_monthly[\"date\"] >= \"2024-03-01\")\n", + " ][\"TS\"].unique()\n", + ")\n", + "severe_conflict = list(\n", + " conflict_monthly[\n", + " (conflict_monthly[\"conflict_category\"] == \"Severe conflict\")\n", + " & (conflict_monthly[\"date\"] >= \"2024-03-01\")\n", + " ][\"TS\"].unique()\n", + ")\n", + "no_conflict = list(\n", + " conflict_monthly[\n", + " (conflict_monthly[\"conflict_category\"] == \"No conflict\")\n", + " & (conflict_monthly[\"date\"] >= \"2024-03-01\")\n", + " ][\"TS\"].unique()\n", + ")" ] }, { @@ -648,17 +735,35 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", - "\n", - "df1 = ntl_adm3_monthly[['TS','code_no', 'date', 'ntl_sum', 'ntl_nogf_5km_sum']].groupby(['date','code_no'])[['ntl_sum', 'ntl_nogf_5km_sum']].mean().reset_index()\n", - "df1=df1[df1['date']>'2021-01-1']\n", - "ntl_measure = 'ntl_nogf_5km_sum'\n", - "\n", - "df2 = conflict_monthly.groupby(['date', 'code_no'], observed=False)[['conflict_index', 'fatalities', 'events']].mean().reset_index()\n", - "df2.dropna(subset='conflict_index', inplace=True)\n", - "conflict_measure = 'conflict_index'\n", - "\n", - "tabs = get_multi_tab_line_plot(df1,df2,loop_list=list(control_code_dict.keys()),category_column='code_no', conflict_measure=conflict_measure, ntl_measure=ntl_measure)\n", + "from bokeh.models import Tabs\n", + "\n", + "df1 = (\n", + " ntl_adm3_monthly[[\"TS\", \"code_no\", \"date\", \"ntl_sum\", \"ntl_nogf_5km_sum\"]]\n", + " .groupby([\"date\", \"code_no\"])[[\"ntl_sum\", \"ntl_nogf_5km_sum\"]]\n", + " .mean()\n", + " .reset_index()\n", + ")\n", + "df1 = df1[df1[\"date\"] > \"2021-01-1\"]\n", + "ntl_measure = \"ntl_nogf_5km_sum\"\n", + "\n", + "df2 = (\n", + " conflict_monthly.groupby([\"date\", \"code_no\"], observed=False)[\n", + " [\"conflict_index\", \"fatalities\", \"events\"]\n", + " ]\n", + " .mean()\n", + " .reset_index()\n", + ")\n", + "df2.dropna(subset=\"conflict_index\", inplace=True)\n", + "conflict_measure = \"conflict_index\"\n", + "\n", + "tabs = get_multi_tab_line_plot(\n", + " df1,\n", + " df2,\n", + " loop_list=list(control_code_dict.keys()),\n", + " category_column=\"code_no\",\n", + " conflict_measure=conflict_measure,\n", + " ntl_measure=ntl_measure,\n", + ")\n", "\n", "# Create the Tabs layout\n", "tabs_layout = Tabs(tabs=tabs)\n", @@ -666,8 +771,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] }, { @@ -685,7 +790,6 @@ "# output_notebook()\n", "\n", "\n", - "\n", "# # If you are running this in a Jupyter notebook, uncomment the next line\n", "# # output_notebook()\n", "\n", @@ -702,7 +806,7 @@ "# # Create a new Bokeh figure\n", "# p = figure(title=f\"Nighttime Light Trends and {conflict_measure.capitalize()} at a National Level\", x_axis_label='Month', width=800, height=400, x_axis_type='datetime')\n", "\n", - "# p.y_range.start = 0 \n", + "# p.y_range.start = 0\n", "# p.y_range.end = df2[conflict_measure].max()\n", "\n", "# # Add the first line plot\n", @@ -805,17 +909,41 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", - "\n", - "df1 = ntl_adm3_monthly.groupby(['date', 'TS'])[['ntl_sum', 'ntl_gf_5km_sum',\n", - " 'ntl_nogf_5km_sum', 'ntl_gf_10km_sum', 'ntl_nogf_10km_sum']].mean().reset_index()\n", - "df1=df1[df1['date']>'2021-01-1']\n", - "df2 = conflict_monthly.groupby(['date', 'TS', 'conflict_category'], observed=False)[['conflict_index', 'fatalities', 'events']].mean().reset_index()\n", - "df2.dropna(subset='conflict_index', inplace=True)\n", - "conflict_measure = 'conflict_index'\n", - "ntl_measure = 'ntl_nogf_5km_sum'\n", - "\n", - "tabs = get_multi_tab_line_plot(df1,df2,loop_list=very_severe_conflict,category_column='TS', conflict_measure=conflict_measure, ntl_measure=ntl_measure)\n", + "from bokeh.models import Tabs\n", + "\n", + "df1 = (\n", + " ntl_adm3_monthly.groupby([\"date\", \"TS\"])[\n", + " [\n", + " \"ntl_sum\",\n", + " \"ntl_gf_5km_sum\",\n", + " \"ntl_nogf_5km_sum\",\n", + " \"ntl_gf_10km_sum\",\n", + " \"ntl_nogf_10km_sum\",\n", + " ]\n", + " ]\n", + " .mean()\n", + " .reset_index()\n", + ")\n", + "df1 = df1[df1[\"date\"] > \"2021-01-1\"]\n", + "df2 = (\n", + " conflict_monthly.groupby([\"date\", \"TS\", \"conflict_category\"], observed=False)[\n", + " [\"conflict_index\", \"fatalities\", \"events\"]\n", + " ]\n", + " .mean()\n", + " .reset_index()\n", + ")\n", + "df2.dropna(subset=\"conflict_index\", inplace=True)\n", + "conflict_measure = \"conflict_index\"\n", + "ntl_measure = \"ntl_nogf_5km_sum\"\n", + "\n", + "tabs = get_multi_tab_line_plot(\n", + " df1,\n", + " df2,\n", + " loop_list=very_severe_conflict,\n", + " category_column=\"TS\",\n", + " conflict_measure=conflict_measure,\n", + " ntl_measure=ntl_measure,\n", + ")\n", "\n", "# Create the Tabs layout\n", "tabs_layout = Tabs(tabs=tabs)\n", @@ -823,8 +951,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] }, { @@ -901,52 +1029,77 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", - "from bokeh.plotting import figure, show\n", - "import numpy as np\n", + "from bokeh.models import Tabs\n", + "from bokeh.plotting import show\n", "from scipy import stats # For linear regression\n", "\n", "# Data preparation\n", - "df1 = ntl_adm3_monthly.groupby(['date', 'TS'])[['ntl_sum', 'ntl_gf_5km_sum',\n", - " 'ntl_nogf_5km_sum', 'ntl_gf_10km_sum', 'ntl_nogf_10km_sum']].mean().reset_index()\n", - "df1 = df1[df1['date'] > '2021-01-01']\n", - "\n", - "df2 = conflict_monthly.groupby(['date', 'TS', 'conflict_category'], observed=False)[['conflict_index', 'fatalities', 'events']].mean().reset_index()\n", - "df2.dropna(subset='conflict_index', inplace=True)\n", - "\n", - "conflict_measure = 'conflict_index'\n", - "ntl_measure = 'ntl_nogf_5km_sum'\n", + "df1 = (\n", + " ntl_adm3_monthly.groupby([\"date\", \"TS\"])[\n", + " [\n", + " \"ntl_sum\",\n", + " \"ntl_gf_5km_sum\",\n", + " \"ntl_nogf_5km_sum\",\n", + " \"ntl_gf_10km_sum\",\n", + " \"ntl_nogf_10km_sum\",\n", + " ]\n", + " ]\n", + " .mean()\n", + " .reset_index()\n", + ")\n", + "df1 = df1[df1[\"date\"] > \"2021-01-01\"]\n", + "\n", + "df2 = (\n", + " conflict_monthly.groupby([\"date\", \"TS\", \"conflict_category\"], observed=False)[\n", + " [\"conflict_index\", \"fatalities\", \"events\"]\n", + " ]\n", + " .mean()\n", + " .reset_index()\n", + ")\n", + "df2.dropna(subset=\"conflict_index\", inplace=True)\n", + "\n", + "conflict_measure = \"conflict_index\"\n", + "ntl_measure = \"ntl_nogf_5km_sum\"\n", "\n", "tabs = []\n", "\n", "# Loop through severe conflicts and create a scatter plot with a best fit line for each\n", "for ts in very_severe_conflict:\n", " # Merge df1 and df2 on 'date' and 'TS' to align conflict and NTL data for the scatter plot\n", - " merged_df = pd.merge(df1[df1['TS'] == ts], df2[df2['TS'] == ts], on=['date', 'TS'])\n", + " merged_df = pd.merge(df1[df1[\"TS\"] == ts], df2[df2[\"TS\"] == ts], on=[\"date\", \"TS\"])\n", "\n", " # Create a new Bokeh figure\n", - " p = figure(title=f\"Conflict vs Nighttime Light (TS={ts})\", \n", - " x_axis_label=f\"{conflict_measure.capitalize()}\", \n", - " y_axis_label=f\"{ntl_measure}\", \n", - " width=800, height=400)\n", + " p = figure(\n", + " title=f\"Conflict vs Nighttime Light (TS={ts})\",\n", + " x_axis_label=f\"{conflict_measure.capitalize()}\",\n", + " y_axis_label=f\"{ntl_measure}\",\n", + " width=800,\n", + " height=400,\n", + " )\n", "\n", " # Scatter plot with conflict on x-axis and NTL on y-axis using `scatter()`\n", " scatter_source = ColumnDataSource(merged_df)\n", - " scatter_plot = p.scatter(conflict_measure, ntl_measure, source=scatter_source, \n", - " size=8, color=\"blue\", alpha=0.7)\n", + " scatter_plot = p.scatter(\n", + " conflict_measure,\n", + " ntl_measure,\n", + " source=scatter_source,\n", + " size=8,\n", + " color=\"blue\",\n", + " alpha=0.7,\n", + " )\n", "\n", " # Add hover tool to show both conflict and NTL values\n", " hover_scatter = HoverTool(\n", " renderers=[scatter_plot],\n", " tooltips=[\n", - " ('Date', '@date{%F}'),\n", - " (f'{conflict_measure.capitalize()}', f'@{conflict_measure}'),\n", - " (ntl_measure, f'@{ntl_measure}')\n", + " (\"Date\", \"@date{%F}\"),\n", + " (f\"{conflict_measure.capitalize()}\", f\"@{conflict_measure}\"),\n", + " (ntl_measure, f\"@{ntl_measure}\"),\n", " ],\n", - " formatters={'@date': 'datetime'}, # Format the date\n", - " mode='mouse'\n", + " formatters={\"@date\": \"datetime\"}, # Format the date\n", + " mode=\"mouse\",\n", " )\n", - " \n", + "\n", " p.add_tools(hover_scatter)\n", "\n", " # Calculate the best fit line using scipy's linear regression\n", @@ -968,7 +1121,7 @@ "tabs_layout = Tabs(tabs=tabs)\n", "\n", "# Show the tabs\n", - "show(tabs_layout)\n" + "show(tabs_layout)" ] }, { @@ -1034,52 +1187,77 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", + "from bokeh.models import Tabs, TabPanel, HoverTool, ColumnDataSource\n", "from bokeh.plotting import figure, show\n", - "import numpy as np\n", "from scipy import stats # For linear regression\n", "\n", "# Data preparation\n", - "df1 = ntl_adm3_monthly.groupby(['date', 'TS'])[['ntl_sum', 'ntl_gf_5km_sum',\n", - " 'ntl_nogf_5km_sum', 'ntl_gf_10km_sum', 'ntl_nogf_10km_sum']].mean().reset_index()\n", - "df1 = df1[df1['date'] > '2021-01-01']\n", - "\n", - "df2 = conflict_monthly.groupby(['date', 'TS', 'conflict_category'], observed=False)[['conflict_index', 'fatalities', 'events']].mean().reset_index()\n", - "df2.dropna(subset='conflict_index', inplace=True)\n", - "\n", - "conflict_measure = 'conflict_index'\n", - "ntl_measure = 'ntl_nogf_5km_sum'\n", + "df1 = (\n", + " ntl_adm3_monthly.groupby([\"date\", \"TS\"])[\n", + " [\n", + " \"ntl_sum\",\n", + " \"ntl_gf_5km_sum\",\n", + " \"ntl_nogf_5km_sum\",\n", + " \"ntl_gf_10km_sum\",\n", + " \"ntl_nogf_10km_sum\",\n", + " ]\n", + " ]\n", + " .mean()\n", + " .reset_index()\n", + ")\n", + "df1 = df1[df1[\"date\"] > \"2021-01-01\"]\n", + "\n", + "df2 = (\n", + " conflict_monthly.groupby([\"date\", \"TS\", \"conflict_category\"], observed=False)[\n", + " [\"conflict_index\", \"fatalities\", \"events\"]\n", + " ]\n", + " .mean()\n", + " .reset_index()\n", + ")\n", + "df2.dropna(subset=\"conflict_index\", inplace=True)\n", + "\n", + "conflict_measure = \"conflict_index\"\n", + "ntl_measure = \"ntl_nogf_5km_sum\"\n", "\n", "tabs = []\n", "\n", "# Loop through severe conflicts and create a scatter plot with a best fit line for each\n", "for ts in severe_conflict:\n", " # Merge df1 and df2 on 'date' and 'TS' to align conflict and NTL data for the scatter plot\n", - " merged_df = pd.merge(df1[df1['TS'] == ts], df2[df2['TS'] == ts], on=['date', 'TS'])\n", + " merged_df = pd.merge(df1[df1[\"TS\"] == ts], df2[df2[\"TS\"] == ts], on=[\"date\", \"TS\"])\n", "\n", " # Create a new Bokeh figure\n", - " p = figure(title=f\"{conflict_measure.capitalize()} vs Nighttime Light (TS={ts})\", \n", - " x_axis_label=f\"{conflict_measure.capitalize()}\", \n", - " y_axis_label=f\"{ntl_measure}\", \n", - " width=800, height=400)\n", + " p = figure(\n", + " title=f\"{conflict_measure.capitalize()} vs Nighttime Light (TS={ts})\",\n", + " x_axis_label=f\"{conflict_measure.capitalize()}\",\n", + " y_axis_label=f\"{ntl_measure}\",\n", + " width=800,\n", + " height=400,\n", + " )\n", "\n", " # Scatter plot with conflict on x-axis and NTL on y-axis using `scatter()`\n", " scatter_source = ColumnDataSource(merged_df)\n", - " scatter_plot = p.scatter(conflict_measure, ntl_measure, source=scatter_source, \n", - " size=8, color=\"blue\", alpha=0.7)\n", + " scatter_plot = p.scatter(\n", + " conflict_measure,\n", + " ntl_measure,\n", + " source=scatter_source,\n", + " size=8,\n", + " color=\"blue\",\n", + " alpha=0.7,\n", + " )\n", "\n", " # Add hover tool to show both conflict and NTL values\n", " hover_scatter = HoverTool(\n", " renderers=[scatter_plot],\n", " tooltips=[\n", - " ('Date', '@date{%F}'),\n", - " (f'{conflict_measure.capitalize()}', f'@{conflict_measure}'),\n", - " (ntl_measure, f'@{ntl_measure}')\n", + " (\"Date\", \"@date{%F}\"),\n", + " (f\"{conflict_measure.capitalize()}\", f\"@{conflict_measure}\"),\n", + " (ntl_measure, f\"@{ntl_measure}\"),\n", " ],\n", - " formatters={'@date': 'datetime'}, # Format the date\n", - " mode='mouse'\n", + " formatters={\"@date\": \"datetime\"}, # Format the date\n", + " mode=\"mouse\",\n", " )\n", - " \n", + "\n", " p.add_tools(hover_scatter)\n", "\n", " # Calculate the best fit line using scipy's linear regression\n", @@ -1101,7 +1279,7 @@ "tabs_layout = Tabs(tabs=tabs)\n", "\n", "# Show the tabs\n", - "show(tabs_layout)\n" + "show(tabs_layout)" ] }, { @@ -1121,10 +1299,25 @@ }, "outputs": [], "source": [ - "ntl_adm3_6_monthly = ntl_adm3_monthly.groupby(['TS', 'date'])[['ntl_sum', 'ntl_gf_5km_sum',\n", - " 'ntl_nogf_5km_sum', 'ntl_gf_10km_sum', 'ntl_nogf_10km_sum']].mean().reset_index()\n", - "\n", - "conflict_adm3_6_monthly = conflict_monthly.groupby(['TS', 'date'])[['conflict_index', 'events', 'fatalities']].mean().reset_index()" + "ntl_adm3_6_monthly = (\n", + " ntl_adm3_monthly.groupby([\"TS\", \"date\"])[\n", + " [\n", + " \"ntl_sum\",\n", + " \"ntl_gf_5km_sum\",\n", + " \"ntl_nogf_5km_sum\",\n", + " \"ntl_gf_10km_sum\",\n", + " \"ntl_nogf_10km_sum\",\n", + " ]\n", + " ]\n", + " .mean()\n", + " .reset_index()\n", + ")\n", + "\n", + "conflict_adm3_6_monthly = (\n", + " conflict_monthly.groupby([\"TS\", \"date\"])[[\"conflict_index\", \"events\", \"fatalities\"]]\n", + " .mean()\n", + " .reset_index()\n", + ")" ] }, { @@ -1149,7 +1342,7 @@ " if month >= 10:\n", " return f\"{date.year}-Oct-Mar\"\n", " else:\n", - " return f\"{date.year-1}-Oct-Mar\" # For Jan-Mar, it's part of the previous year's Oct-Mar\n" + " return f\"{date.year - 1}-Oct-Mar\" # For Jan-Mar, it's part of the previous year's Oct-Mar" ] }, { @@ -1162,12 +1355,33 @@ }, "outputs": [], "source": [ - "ntl_adm3_6_monthly['6-month-group'] = ntl_adm3_6_monthly['date'].apply(lambda x: assign_6month_group(x))\n", - "ntl_adm3_6_monthly = ntl_adm3_6_monthly.groupby(['TS', '6-month-group'])[['ntl_sum', 'ntl_gf_5km_sum',\n", - " 'ntl_nogf_5km_sum', 'ntl_gf_10km_sum', 'ntl_nogf_10km_sum']].mean().reset_index()\n", - "\n", - "conflict_adm3_6_monthly['6-month-group'] = conflict_adm3_6_monthly['date'].apply(lambda x: assign_6month_group(x))\n", - "conflict_adm3_6_monthly = conflict_adm3_6_monthly.groupby(['TS', '6-month-group'])[['conflict_index', 'events', 'fatalities']].mean().reset_index()" + "ntl_adm3_6_monthly[\"6-month-group\"] = ntl_adm3_6_monthly[\"date\"].apply(\n", + " lambda x: assign_6month_group(x)\n", + ")\n", + "ntl_adm3_6_monthly = (\n", + " ntl_adm3_6_monthly.groupby([\"TS\", \"6-month-group\"])[\n", + " [\n", + " \"ntl_sum\",\n", + " \"ntl_gf_5km_sum\",\n", + " \"ntl_nogf_5km_sum\",\n", + " \"ntl_gf_10km_sum\",\n", + " \"ntl_nogf_10km_sum\",\n", + " ]\n", + " ]\n", + " .mean()\n", + " .reset_index()\n", + ")\n", + "\n", + "conflict_adm3_6_monthly[\"6-month-group\"] = conflict_adm3_6_monthly[\"date\"].apply(\n", + " lambda x: assign_6month_group(x)\n", + ")\n", + "conflict_adm3_6_monthly = (\n", + " conflict_adm3_6_monthly.groupby([\"TS\", \"6-month-group\"])[\n", + " [\"conflict_index\", \"events\", \"fatalities\"]\n", + " ]\n", + " .mean()\n", + " .reset_index()\n", + ")" ] }, { @@ -1180,12 +1394,23 @@ }, "outputs": [], "source": [ - "df1 = ntl_adm3_monthly.groupby(['TS', 'date'])[[ 'ntl_sum', 'ntl_gf_5km_sum',\n", - " 'ntl_nogf_5km_sum', 'ntl_gf_10km_sum', 'ntl_nogf_10km_sum']].mean().reset_index()\n", - "df1 = df1.sort_values(by=['TS', 'date'])\n", - "df1['ntl_nogf_5km_diff_pm'] = df1.groupby('TS')['ntl_nogf_5km_sum'].diff()\n", - "df1['ntl_diff_pm'] = df1.groupby('TS')['ntl_sum'].diff()\n", - "df1['ntl_gf_5km_pm'] = df1.groupby('TS')['ntl_gf_5km_sum'].diff()" + "df1 = (\n", + " ntl_adm3_monthly.groupby([\"TS\", \"date\"])[\n", + " [\n", + " \"ntl_sum\",\n", + " \"ntl_gf_5km_sum\",\n", + " \"ntl_nogf_5km_sum\",\n", + " \"ntl_gf_10km_sum\",\n", + " \"ntl_nogf_10km_sum\",\n", + " ]\n", + " ]\n", + " .mean()\n", + " .reset_index()\n", + ")\n", + "df1 = df1.sort_values(by=[\"TS\", \"date\"])\n", + "df1[\"ntl_nogf_5km_diff_pm\"] = df1.groupby(\"TS\")[\"ntl_nogf_5km_sum\"].diff()\n", + "df1[\"ntl_diff_pm\"] = df1.groupby(\"TS\")[\"ntl_sum\"].diff()\n", + "df1[\"ntl_gf_5km_pm\"] = df1.groupby(\"TS\")[\"ntl_gf_5km_sum\"].diff()" ] }, { @@ -1198,11 +1423,15 @@ }, "outputs": [], "source": [ - "df2 = conflict_monthly.groupby(['TS', 'date'])[['events', 'fatalities', 'conflict_index']].mean().reset_index()\n", - "df2 = df2.sort_values(by=['TS', 'date'])\n", - "df2['conflict_index_diff_pm'] = df2.groupby('TS')['conflict_index'].diff()\n", - "df2['events_diff_pm'] = df2.groupby('TS')['events'].diff()\n", - "df2['fatalities_pm'] = df2.groupby('TS')['fatalities'].diff()" + "df2 = (\n", + " conflict_monthly.groupby([\"TS\", \"date\"])[[\"events\", \"fatalities\", \"conflict_index\"]]\n", + " .mean()\n", + " .reset_index()\n", + ")\n", + "df2 = df2.sort_values(by=[\"TS\", \"date\"])\n", + "df2[\"conflict_index_diff_pm\"] = df2.groupby(\"TS\")[\"conflict_index\"].diff()\n", + "df2[\"events_diff_pm\"] = df2.groupby(\"TS\")[\"events\"].diff()\n", + "df2[\"fatalities_pm\"] = df2.groupby(\"TS\")[\"fatalities\"].diff()" ] }, { @@ -1215,9 +1444,9 @@ }, "outputs": [], "source": [ - "df = ntl_adm3_6_monthly.merge(conflict_adm3_6_monthly, on = ['TS', '6-month-group'])\n", - "df['ntl_6_month_diff'] = df.groupby('TS')['ntl_nogf_5km_sum'].diff()\n", - "df['conflict_6_month_diff'] = df.groupby('TS')['conflict_index'].diff()" + "df = ntl_adm3_6_monthly.merge(conflict_adm3_6_monthly, on=[\"TS\", \"6-month-group\"])\n", + "df[\"ntl_6_month_diff\"] = df.groupby(\"TS\")[\"ntl_nogf_5km_sum\"].diff()\n", + "df[\"conflict_6_month_diff\"] = df.groupby(\"TS\")[\"conflict_index\"].diff()" ] }, { @@ -1283,14 +1512,16 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", + "from bokeh.models import Tabs, TabPanel, HoverTool, ColumnDataSource\n", "\n", "\n", - "df1=df1[df1['date']>'2021-01-1']\n", - "conflict_measure = 'conflict_index_diff_pm'\n", - "ntl_measure = 'ntl_nogf_5km_diff_pm'\n", + "df1 = df1[df1[\"date\"] > \"2021-01-1\"]\n", + "conflict_measure = \"conflict_index_diff_pm\"\n", + "ntl_measure = \"ntl_nogf_5km_diff_pm\"\n", "\n", - "tabs = get_multi_tab_line_plot(df1,df2,very_severe_conflict, 'TS', conflict_measure, ntl_measure)\n", + "tabs = get_multi_tab_line_plot(\n", + " df1, df2, very_severe_conflict, \"TS\", conflict_measure, ntl_measure\n", + ")\n", "\n", "# Create the Tabs layout\n", "tabs_layout = Tabs(tabs=tabs)\n", @@ -1298,8 +1529,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] }, { @@ -1365,14 +1596,16 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", + "from bokeh.models import Tabs, TabPanel, HoverTool, ColumnDataSource\n", "\n", "\n", - "df1=df1[df1['date']>'2021-01-1']\n", - "conflict_measure = 'conflict_index_diff_pm'\n", - "ntl_measure = 'ntl_nogf_5km_diff_pm'\n", + "df1 = df1[df1[\"date\"] > \"2021-01-1\"]\n", + "conflict_measure = \"conflict_index_diff_pm\"\n", + "ntl_measure = \"ntl_nogf_5km_diff_pm\"\n", "\n", - "tabs = get_multi_tab_line_plot(df1,df2,no_conflict[0:10], 'TS', conflict_measure, ntl_measure)\n", + "tabs = get_multi_tab_line_plot(\n", + " df1, df2, no_conflict[0:10], \"TS\", conflict_measure, ntl_measure\n", + ")\n", "\n", "# Create the Tabs layout\n", "tabs_layout = Tabs(tabs=tabs)\n", @@ -1380,8 +1613,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] }, { @@ -1447,63 +1680,90 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", + "from bokeh.models import Tabs, TabPanel, HoverTool, ColumnDataSource\n", "\n", "\n", - "conflict_measure = 'conflict_6_month_diff'\n", - "ntl_measure = 'ntl_6_month_diff'\n", + "conflict_measure = \"conflict_6_month_diff\"\n", + "ntl_measure = \"ntl_6_month_diff\"\n", "\n", "tabs = []\n", "\n", "for ts in very_severe_conflict:\n", - "# # Create a new Bokeh figure\n", - " p = figure(title=f\"Comparing Nighttime Light Trends and {conflict_measure.capitalize()}\", x_axis_label='6-Month-Period',\n", - " width=800, height=400,x_range=sorted(df['6-month-group'].unique()))\n", + " # # Create a new Bokeh figure\n", + " p = figure(\n", + " title=f\"Comparing Nighttime Light Trends and {conflict_measure.capitalize()}\",\n", + " x_axis_label=\"6-Month-Period\",\n", + " width=800,\n", + " height=400,\n", + " x_range=sorted(df[\"6-month-group\"].unique()),\n", + " )\n", "\n", - " p.y_range.start = df[df['TS']==ts][conflict_measure].min()\n", - " p.y_range.end = df[df['TS']==ts][conflict_measure].max()\n", + " p.y_range.start = df[df[\"TS\"] == ts][conflict_measure].min()\n", + " p.y_range.end = df[df[\"TS\"] == ts][conflict_measure].max()\n", "\n", " # Add the first line plot\n", - " conflict_source = ColumnDataSource(df[df['TS'] == ts])\n", - " conflict_line = p.line('6-month-group', conflict_measure,source=conflict_source, legend_label=f\"{conflict_measure.capitalize()}\", line_width=2, color=\"red\", alpha=0.7)\n", + " conflict_source = ColumnDataSource(df[df[\"TS\"] == ts])\n", + " conflict_line = p.line(\n", + " \"6-month-group\",\n", + " conflict_measure,\n", + " source=conflict_source,\n", + " legend_label=f\"{conflict_measure.capitalize()}\",\n", + " line_width=2,\n", + " color=\"red\",\n", + " alpha=0.7,\n", + " )\n", "\n", - " p.extra_y_ranges = {\"y2\": Range1d(start=df[df['TS']==ts][ntl_measure].min(), end=df[df['TS']==ts][ntl_measure].max())}\n", + " p.extra_y_ranges = {\n", + " \"y2\": Range1d(\n", + " start=df[df[\"TS\"] == ts][ntl_measure].min(),\n", + " end=df[df[\"TS\"] == ts][ntl_measure].max(),\n", + " )\n", + " }\n", " # Add the second line plot\n", - " ntl_source = ColumnDataSource(df[df['TS'] == ts])\n", - " ntl_line = p.line('6-month-group', ntl_measure,source=ntl_source, legend_label=ntl_measure, line_width=2, color=\"blue\", alpha=0.7, y_range_name='y2')\n", + " ntl_source = ColumnDataSource(df[df[\"TS\"] == ts])\n", + " ntl_line = p.line(\n", + " \"6-month-group\",\n", + " ntl_measure,\n", + " source=ntl_source,\n", + " legend_label=ntl_measure,\n", + " line_width=2,\n", + " color=\"blue\",\n", + " alpha=0.7,\n", + " y_range_name=\"y2\",\n", + " )\n", "\n", " # Customize legend\n", " p.legend.location = \"top_left\"\n", " p.legend.click_policy = \"hide\" # Optional: Allow clicking to hide/show lines\n", "\n", - " p.add_layout(LinearAxis(y_range_name=\"y2\", axis_label='Luminosity'), 'right')\n", + " p.add_layout(LinearAxis(y_range_name=\"y2\", axis_label=\"Luminosity\"), \"right\")\n", "\n", - " # Add hover tool for conflict data\n", + " # Add hover tool for conflict data\n", " hover_conflict = HoverTool(\n", " renderers=[conflict_line], # Reference the renderer directly\n", " tooltips=[\n", - " ('Date', '@6-month-group{%F}'),\n", - " (f'{conflict_measure.capitalize()}', f'@{conflict_measure}'),\n", + " (\"Date\", \"@6-month-group{%F}\"),\n", + " (f\"{conflict_measure.capitalize()}\", f\"@{conflict_measure}\"),\n", " ],\n", " formatters={\n", - " '@6-month-group': 'datetime', # Format the date\n", + " \"@6-month-group\": \"datetime\", # Format the date\n", " },\n", - " mode='vline'\n", + " mode=\"vline\",\n", " )\n", - " \n", + "\n", " # Add hover tool for NTL data\n", " hover_ntl = HoverTool(\n", " renderers=[ntl_line], # Reference the renderer directly\n", " tooltips=[\n", - " ('Date', '@6-month-group{%F}'),\n", - " (ntl_measure, f'@{ntl_measure}'),\n", + " (\"Date\", \"@6-month-group{%F}\"),\n", + " (ntl_measure, f\"@{ntl_measure}\"),\n", " ],\n", " formatters={\n", - " '@6-month-group': 'datetime', # Format the date\n", + " \"@6-month-group\": \"datetime\", # Format the date\n", " },\n", - " mode='vline'\n", + " mode=\"vline\",\n", " )\n", - " \n", + "\n", " p.add_tools(hover_conflict, hover_ntl)\n", "\n", " tab = TabPanel(child=p, title=ts)\n", @@ -1515,8 +1775,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] }, { @@ -1582,63 +1842,97 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", + "from bokeh.models import (\n", + " LinearAxis,\n", + " Range1d,\n", + " Tabs,\n", + " TabPanel,\n", + " HoverTool,\n", + " ColumnDataSource,\n", + ")\n", "\n", "\n", - "conflict_measure = 'conflict_6_month_diff'\n", - "ntl_measure = 'ntl_6_month_diff'\n", + "conflict_measure = \"conflict_6_month_diff\"\n", + "ntl_measure = \"ntl_6_month_diff\"\n", "\n", "tabs = []\n", "\n", "for ts in no_conflict[0:10]:\n", - "# # Create a new Bokeh figure\n", - " p = figure(title=f\"Comparing Nighttime Light Trends and {conflict_measure.capitalize()}\", x_axis_label='6-Month-Period',\n", - " width=800, height=400,x_range=sorted(df['6-month-group'].unique()))\n", + " # # Create a new Bokeh figure\n", + " p = figure(\n", + " title=f\"Comparing Nighttime Light Trends and {conflict_measure.capitalize()}\",\n", + " x_axis_label=\"6-Month-Period\",\n", + " width=800,\n", + " height=400,\n", + " x_range=sorted(df[\"6-month-group\"].unique()),\n", + " )\n", "\n", - " p.y_range.start = df[df['TS']==ts][conflict_measure].min()\n", - " p.y_range.end = df[df['TS']==ts][conflict_measure].max()\n", + " p.y_range.start = df[df[\"TS\"] == ts][conflict_measure].min()\n", + " p.y_range.end = df[df[\"TS\"] == ts][conflict_measure].max()\n", "\n", " # Add the first line plot\n", - " conflict_source = ColumnDataSource(df[df['TS'] == ts])\n", - " conflict_line = p.line('6-month-group', conflict_measure,source=conflict_source, legend_label=f\"{conflict_measure.capitalize()}\", line_width=2, color=\"red\", alpha=0.7)\n", + " conflict_source = ColumnDataSource(df[df[\"TS\"] == ts])\n", + " conflict_line = p.line(\n", + " \"6-month-group\",\n", + " conflict_measure,\n", + " source=conflict_source,\n", + " legend_label=f\"{conflict_measure.capitalize()}\",\n", + " line_width=2,\n", + " color=\"red\",\n", + " alpha=0.7,\n", + " )\n", "\n", - " p.extra_y_ranges = {\"y2\": Range1d(start=df[df['TS']==ts][ntl_measure].min(), end=df[df['TS']==ts][ntl_measure].max())}\n", + " p.extra_y_ranges = {\n", + " \"y2\": Range1d(\n", + " start=df[df[\"TS\"] == ts][ntl_measure].min(),\n", + " end=df[df[\"TS\"] == ts][ntl_measure].max(),\n", + " )\n", + " }\n", " # Add the second line plot\n", - " ntl_source = ColumnDataSource(df[df['TS'] == ts])\n", - " ntl_line = p.line('6-month-group', ntl_measure,source=ntl_source, legend_label=ntl_measure, line_width=2, color=\"blue\", alpha=0.7, y_range_name='y2')\n", + " ntl_source = ColumnDataSource(df[df[\"TS\"] == ts])\n", + " ntl_line = p.line(\n", + " \"6-month-group\",\n", + " ntl_measure,\n", + " source=ntl_source,\n", + " legend_label=ntl_measure,\n", + " line_width=2,\n", + " color=\"blue\",\n", + " alpha=0.7,\n", + " y_range_name=\"y2\",\n", + " )\n", "\n", " # Customize legend\n", " p.legend.location = \"top_left\"\n", " p.legend.click_policy = \"hide\" # Optional: Allow clicking to hide/show lines\n", "\n", - " p.add_layout(LinearAxis(y_range_name=\"y2\", axis_label='Luminosity'), 'right')\n", + " p.add_layout(LinearAxis(y_range_name=\"y2\", axis_label=\"Luminosity\"), \"right\")\n", "\n", - " # Add hover tool for conflict data\n", + " # Add hover tool for conflict data\n", " hover_conflict = HoverTool(\n", " renderers=[conflict_line], # Reference the renderer directly\n", " tooltips=[\n", - " ('Date', '@6-month-group{%F}'),\n", - " (f'{conflict_measure.capitalize()}', f'@{conflict_measure}'),\n", + " (\"Date\", \"@6-month-group{%F}\"),\n", + " (f\"{conflict_measure.capitalize()}\", f\"@{conflict_measure}\"),\n", " ],\n", " formatters={\n", - " '@6-month-group': 'datetime', # Format the date\n", + " \"@6-month-group\": \"datetime\", # Format the date\n", " },\n", - " mode='vline'\n", + " mode=\"vline\",\n", " )\n", - " \n", + "\n", " # Add hover tool for NTL data\n", " hover_ntl = HoverTool(\n", " renderers=[ntl_line], # Reference the renderer directly\n", " tooltips=[\n", - " ('Date', '@6-month-group{%F}'),\n", - " (ntl_measure, f'@{ntl_measure}'),\n", + " (\"Date\", \"@6-month-group{%F}\"),\n", + " (ntl_measure, f\"@{ntl_measure}\"),\n", " ],\n", " formatters={\n", - " '@6-month-group': 'datetime', # Format the date\n", + " \"@6-month-group\": \"datetime\", # Format the date\n", " },\n", - " mode='vline'\n", + " mode=\"vline\",\n", " )\n", - " \n", + "\n", " p.add_tools(hover_conflict, hover_ntl)\n", "\n", " tab = TabPanel(child=p, title=ts)\n", @@ -1650,8 +1944,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] }, { @@ -1671,10 +1965,34 @@ }, "outputs": [], "source": [ - "conflict_baseline_ts = conflict_monthly[conflict_monthly['date']<'2021-04-01'].groupby(['TS'])[['conflict_index', 'events', 'fatalities']].mean().reset_index()\n", - "conflict_baseline_ts.rename(columns = {'conflict_index':'conflict_index_baseline', 'events':'events_baseline', 'fatalities':'fatalities_baseline'}, inplace=True)\n", - "ntl_baseline_ts = ntl_adm3_monthly[(ntl_adm3_monthly['date']<'2021-04-01')].groupby(['TS'])[['ntl_sum', 'ntl_nogf_5km_sum', 'ntl_gf_5km_sum']].mean().reset_index()\n", - "ntl_baseline_ts.rename(columns = {'ntl_sum':'ntl_sum_baseline', 'ntl_nogf_5km_sum':'ntl_nogf_5km_sum_baseline', 'ntl_gf_5km_sum':'ntl_gf_5km_sum_baseline'}, inplace=True)" + "conflict_baseline_ts = (\n", + " conflict_monthly[conflict_monthly[\"date\"] < \"2021-04-01\"]\n", + " .groupby([\"TS\"])[[\"conflict_index\", \"events\", \"fatalities\"]]\n", + " .mean()\n", + " .reset_index()\n", + ")\n", + "conflict_baseline_ts.rename(\n", + " columns={\n", + " \"conflict_index\": \"conflict_index_baseline\",\n", + " \"events\": \"events_baseline\",\n", + " \"fatalities\": \"fatalities_baseline\",\n", + " },\n", + " inplace=True,\n", + ")\n", + "ntl_baseline_ts = (\n", + " ntl_adm3_monthly[(ntl_adm3_monthly[\"date\"] < \"2021-04-01\")]\n", + " .groupby([\"TS\"])[[\"ntl_sum\", \"ntl_nogf_5km_sum\", \"ntl_gf_5km_sum\"]]\n", + " .mean()\n", + " .reset_index()\n", + ")\n", + "ntl_baseline_ts.rename(\n", + " columns={\n", + " \"ntl_sum\": \"ntl_sum_baseline\",\n", + " \"ntl_nogf_5km_sum\": \"ntl_nogf_5km_sum_baseline\",\n", + " \"ntl_gf_5km_sum\": \"ntl_gf_5km_sum_baseline\",\n", + " },\n", + " inplace=True,\n", + ")" ] }, { @@ -1687,13 +2005,23 @@ }, "outputs": [], "source": [ - "conflict_baseline_diff = conflict_monthly[['TS', 'date', 'conflict_index', 'events', 'fatalities']]\n", - "conflict_baseline_diff = conflict_baseline_diff.merge(conflict_baseline_ts, on = ['TS'])\n", - "conflict_baseline_diff['conflict_diff'] = conflict_baseline_diff['conflict_index'] - conflict_baseline_diff['conflict_index_baseline']\n", - "\n", - "ntl_baseline_diff = ntl_adm3_monthly[['TS', 'date','ntl_sum', 'ntl_nogf_5km_sum', 'ntl_gf_5km_sum' ]]\n", - "ntl_baseline_diff = ntl_baseline_diff.merge(ntl_baseline_ts, on = ['TS'])\n", - "ntl_baseline_diff['ntl_nogf_5km_diff'] = ntl_baseline_diff['ntl_nogf_5km_sum'] - ntl_baseline_diff['ntl_nogf_5km_sum_baseline']" + "conflict_baseline_diff = conflict_monthly[\n", + " [\"TS\", \"date\", \"conflict_index\", \"events\", \"fatalities\"]\n", + "]\n", + "conflict_baseline_diff = conflict_baseline_diff.merge(conflict_baseline_ts, on=[\"TS\"])\n", + "conflict_baseline_diff[\"conflict_diff\"] = (\n", + " conflict_baseline_diff[\"conflict_index\"]\n", + " - conflict_baseline_diff[\"conflict_index_baseline\"]\n", + ")\n", + "\n", + "ntl_baseline_diff = ntl_adm3_monthly[\n", + " [\"TS\", \"date\", \"ntl_sum\", \"ntl_nogf_5km_sum\", \"ntl_gf_5km_sum\"]\n", + "]\n", + "ntl_baseline_diff = ntl_baseline_diff.merge(ntl_baseline_ts, on=[\"TS\"])\n", + "ntl_baseline_diff[\"ntl_nogf_5km_diff\"] = (\n", + " ntl_baseline_diff[\"ntl_nogf_5km_sum\"]\n", + " - ntl_baseline_diff[\"ntl_nogf_5km_sum_baseline\"]\n", + ")" ] }, { @@ -1759,16 +2087,25 @@ "source": [ "# Import necessary libraries\n", "output_notebook()\n", - "from bokeh.models import LinearAxis, Range1d, Tabs, Panel, TabPanel, HoverTool, ColumnDataSource\n", + "from bokeh.models import (\n", + " LinearAxis,\n", + " Range1d,\n", + " Tabs,\n", + " TabPanel,\n", + " HoverTool,\n", + " ColumnDataSource,\n", + ")\n", "\n", "df1 = ntl_baseline_diff\n", - "df1=df1[df1['date']>'2021-01-1']\n", + "df1 = df1[df1[\"date\"] > \"2021-01-1\"]\n", "df2 = conflict_baseline_diff\n", - "conflict_measure = 'conflict_diff'\n", - "ntl_measure = 'ntl_nogf_5km_diff'\n", + "conflict_measure = \"conflict_diff\"\n", + "ntl_measure = \"ntl_nogf_5km_diff\"\n", "\n", "\n", - "tabs = get_multi_tab_line_plot(df1,df2,very_severe_conflict,'TS',conflict_measure, ntl_measure)\n", + "tabs = get_multi_tab_line_plot(\n", + " df1, df2, very_severe_conflict, \"TS\", conflict_measure, ntl_measure\n", + ")\n", "\n", "# Create the Tabs layout\n", "tabs_layout = Tabs(tabs=tabs)\n", @@ -1776,8 +2113,8 @@ "# Show the tabs\n", "show(tabs_layout)\n", "\n", - " # # Show the plot\n", - " # show(p)\n" + "# # Show the plot\n", + "# show(p)" ] }, { diff --git a/notebooks/nighttime-lights/analysis-2024-q4/nighttime_lights.html b/notebooks/nighttime-lights/analysis-2024-q4/nighttime_lights.html index 2af4af2..377818f 100644 --- a/notebooks/nighttime-lights/analysis-2024-q4/nighttime_lights.html +++ b/notebooks/nighttime-lights/analysis-2024-q4/nighttime_lights.html @@ -19,7 +19,7 @@ ul.task-list{list-style: none;} ul.task-list li input[type="checkbox"] { width: 0.8em; - margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */ + margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */ vertical-align: middle; } /* CSS for syntax highlighting */ @@ -90,7 +90,7 @@
- +