{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "181a61c2-c502-4118-b9cb-ec107f8b5054",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "# Results\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dfa49af6-3716-42bc-9897-339243fdaa7a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [],
   "source": [
    "# REQUIRED CODE CELL\n",
    "import os \n",
    "import re\n",
    "from PIL import Image\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.ndimage import gaussian_gradient_magnitude\n",
    "from wordcloud import WordCloud, ImageColorGenerator, STOPWORDS\n",
    "\n",
    "np.seterr(divide = 'ignore') \n",
    "DATA_ROOT = \"../data/repro-mri-scoping/repro_mri_scoping\"\n",
    "\n",
    "# Purify and merge text\n",
    "def read_file(file_path):\n",
    "    with open(file_path, 'r', encoding='utf-8') as file:\n",
    "        content = file.read()\n",
    "    return content\n",
    "\n",
    "def write_file(file_path, content):\n",
    "    with open(file_path, 'w') as file:\n",
    "        file.write(content)\n",
    "\n",
    "def remove_enumeration_lines(text):\n",
    "    # Define the pattern for lines starting with enumeration and ending with a question mark\n",
    "    pattern = r'^\\s*\\d+\\.\\s.*\\?$'\n",
    "    \n",
    "    # Use re.MULTILINE to apply the pattern to each line in the input text\n",
    "    result = re.sub(pattern, '', text, flags=re.MULTILINE)\n",
    "    \n",
    "    return result\n",
    "\n",
    "def remove_by_pattern(input_text, patterns_to_remove):\n",
    "    for pattern in patterns_to_remove:\n",
    "        input_text = re.sub(pattern, '', input_text)\n",
    "\n",
    "    return input_text.strip()\n",
    "\n",
    "def get_output_dir(file_name):\n",
    "    op_dir = \"../output\"\n",
    "    if not os.path.exists(op_dir):\n",
    "        os.mkdir(op_dir)\n",
    "    return os.path.join(op_dir,file_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26559457-fb99-40fa-93d1-586c28423eb4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [],
   "source": [
    "directory_path = os.path.join(DATA_ROOT,\"repro_insights_parsed_nov23\")\n",
    "input_files = [os.path.join(directory_path, f) for f in os.listdir(directory_path) if f.endswith('.txt')]\n",
    "\n",
    "patterns_to_remove = [\n",
    "    re.compile(r'Questions about the specific reproducible research habit'),\n",
    "    re.compile(r'Title:'),\n",
    "    re.compile(r'TLDR:'),\n",
    "    re.compile(r'Abstract:'),\n",
    "    re.compile(r'Reproducibility Insights:'),\n",
    "    re.compile(r'General questions'),\n",
    "    re.compile(r'Questions about the specific reproducible research habit'),\n",
    "    re.compile(r'This MRM Reproducible Research Insights interview'),\n",
    "    re.compile(r'This work was singled out because it demonstrated exemplary reproducible research practices')\n",
    "]\n",
    "\n",
    "all_text = ''\n",
    "for cur_file in input_files:\n",
    "    cur_content = read_file(cur_file)\n",
    "    cur_content = remove_enumeration_lines(cur_content)\n",
    "    cur_content = remove_by_pattern(cur_content, patterns_to_remove)\n",
    "    all_text = all_text + \"\\n\" + cur_content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "991985c7-a45d-4f4a-a70d-c0380cf6a08f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [],
   "source": [
    "#| caption: A word cloud generated from the 31 reproducible research insights published by Magnetic Resonance in Medicine Highlights.\n",
    "#| label: fig4-cell\n",
    "text = all_text\n",
    "\n",
    "stopwords = set(STOPWORDS)\n",
    "stopwords.add(\"will\")\n",
    "stopwords.add(\"you\")\n",
    "stopwords.add(\"others\")\n",
    "stopwords.add(\"people\")\n",
    "stopwords.add(\"using\")\n",
    "stopwords.add(\"By\")\n",
    "stopwords.add(\"Mathieu Boudreau\")\n",
    "stopwords.add(\"Agah Karakuzu\")\n",
    "stopwords.add(\"Pinar S. Ozbay\")\n",
    "\n",
    "brain_color = np.array(Image.open(os.path.join(DATA_ROOT, \"brain_image.png\")))\n",
    "\n",
    "brain_color = brain_color[::3, ::3]\n",
    "\n",
    "brain_mask = brain_color.copy()\n",
    "brain_mask[brain_mask.sum(axis=2) == 0] = 255\n",
    "\n",
    "# some finesse: we enforce boundaries between colors so they get less washed out.\n",
    "# For that we do some edge detection in the image\n",
    "edges = np.mean([gaussian_gradient_magnitude(brain_color[:, :, i] / 255., 2) for i in range(3)], axis=0)\n",
    "brain_mask[edges > .08] = 255\n",
    "\n",
    "# create wordcloud. A bit sluggish, you can subsample more strongly for quicker rendering\n",
    "# relative_scaling=0 means the frequencies in the data are reflected less\n",
    "# acurately but it makes a better picture\n",
    "wc = WordCloud(max_words=2000, mask=brain_mask, max_font_size=50, min_font_size=5, random_state=42, relative_scaling=0.2, stopwords=stopwords)\n",
    "\n",
    "# generate word cloud\n",
    "wc.generate(text)\n",
    "plt.imshow(wc)\n",
    "\n",
    "# create coloring from image\n",
    "image_colors = ImageColorGenerator(brain_color)\n",
    "wc.recolor(color_func=image_colors)\n",
    "plt.figure(figsize=(10, 10))\n",
    "plt.imshow(wc, interpolation=\"bilinear\")\n",
    "wc.to_file(get_output_dir(\"brain_wordcloud.png\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fde22556-7114-4a98-8e76-3d385044c24c",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "```{admonition} Reference documentation\n",
    "Please see [this example](https://github.com/amueller/word_cloud/blob/main/examples/parrot.py) as the reference implementation.\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a39b787e-dec2-4473-95ff-56bd686652fd",
   "metadata": {},
   "source": [
    "```{bibliography}\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
