|
9 | 9 | "outputs": [], |
10 | 10 | "source": [ |
11 | 11 | "import warnings\n", |
12 | | - "warnings.filterwarnings(\"ignore\")" |
| 12 | + "\n", |
| 13 | + "warnings.filterwarnings('ignore')" |
13 | 14 | ] |
14 | 15 | }, |
15 | 16 | { |
|
33 | 34 | "source": [ |
34 | 35 | "#%matplotlib inline\n", |
35 | 36 | "import os\n", |
36 | | - "import json\n", |
37 | | - "import pandas as pd\n", |
38 | | - "import glob\n", |
39 | | - "import numpy as np\n", |
40 | 37 | "from pathlib import Path\n", |
| 38 | + "\n", |
41 | 39 | "import matplotlib as mpl\n", |
| 40 | + "import numpy as np\n", |
| 41 | + "import pandas as pd\n", |
| 42 | + "\n", |
42 | 43 | "mpl.use('pgf')\n", |
43 | 44 | "\n", |
44 | 45 | "import matplotlib.pyplot as plt\n", |
|
234 | 235 | ], |
235 | 236 | "source": [ |
236 | 237 | "from collections import Counter\n", |
237 | | - "from matplotlib.colors import LinearSegmentedColormap, Normalize\n", |
| 238 | + "\n", |
238 | 239 | "import seaborn as sns\n", |
239 | | - "from scipy import stats\n", |
| 240 | + "from matplotlib.colors import LinearSegmentedColormap, Normalize\n", |
240 | 241 | "\n", |
241 | 242 | "plt.clf()\n", |
242 | 243 | "\n", |
243 | | - "sns.set_style(\"whitegrid\", {\n", |
| 244 | + "sns.set_style('whitegrid', {\n", |
244 | 245 | " 'ytick.major.size': 5,\n", |
245 | 246 | " 'xtick.major.size': 5,\n", |
246 | 247 | "})\n", |
247 | | - "sns.set_context(\"notebook\", font_scale=1)\n", |
| 248 | + "sns.set_context('notebook', font_scale=1)\n", |
248 | 249 | "\n", |
249 | 250 | "pgf_with_custom_preamble = {\n", |
250 | 251 | "# 'font.sans-serif': ['Helvetica Light'],\n", |
|
253 | 254 | " 'pgf.rcfonts': False, # don't setup fonts from rc parameters\n", |
254 | 255 | " 'pgf.texsystem': 'xelatex',\n", |
255 | 256 | " 'verbose.level': 'debug-annoying',\n", |
256 | | - " \"pgf.preamble\": [\n", |
| 257 | + " 'pgf.preamble': [\n", |
257 | 258 | "# r'\\renewcommand{\\sfdefault}{phv}',\n", |
258 | 259 | "# r'\\usepackage[scaled=.92]{helvet}',\n", |
259 | 260 | " r'\\usepackage{fontspec}',\n", |
|
301 | 302 | "# Plot qc1\n", |
302 | 303 | "df0 = df[df.version.str.contains('1.0.0')]\n", |
303 | 304 | "unique, counts = np.unique(df0[['overall']].values.ravel(), return_counts=True)\n", |
304 | | - "qc1_counts = dict(zip(unique, counts))\n", |
| 305 | + "qc1_counts = dict(zip(unique, counts, strict=False))\n", |
305 | 306 | "ax.scatter([1] * len(unique), unique, c='w', s=counts * factor, zorder=2)\n", |
306 | 307 | "qc1_scatter = ax.scatter([1] * len(unique), unique, c=cm(norm(unique)), alpha=0.7, s=counts * factor, zorder=3)\n", |
307 | 308 | "\n", |
308 | | - "print(dict(zip(unique, counts)))\n", |
| 309 | + "print(dict(zip(unique, counts, strict=False)))\n", |
309 | 310 | "\n", |
310 | 311 | "# Plot qc2\n", |
311 | 312 | "df1 = df[df.version.str.contains('1.0.7')]\n", |
312 | 313 | "unique, counts = np.unique(df1[['overall']].values.ravel(), return_counts=True)\n", |
313 | | - "qc2_counts = dict(zip(unique, counts))\n", |
| 314 | + "qc2_counts = dict(zip(unique, counts, strict=False))\n", |
314 | 315 | "ax.scatter([2] * len(unique), unique, c='w', s=counts * factor, zorder=2)\n", |
315 | 316 | "qc2_scatter = ax.scatter([2] * len(unique), unique, c=cm(norm(unique)), alpha=0.7, s=counts * factor, zorder=3)\n", |
316 | 317 | "\n", |
317 | | - "print(dict(zip(unique, counts)))\n", |
| 318 | + "print(dict(zip(unique, counts, strict=False)))\n", |
318 | 319 | "\n", |
319 | 320 | "ax.set(yticks=[0.0, 1.0, 2.0, 3.0])\n", |
320 | 321 | "ax.set(yticklabels=['Critical', 'Poor', 'Acceptable', 'Excellent'])\n", |
|
329 | 330 | " plt.scatter([],[], s=factor * 30, color='gray', edgecolors='none', alpha=.3)\n", |
330 | 331 | "]\n", |
331 | 332 | "\n", |
332 | | - "labels = [\"1 dataset\", \"4 datasets\", \"10 datasets\", \"30 datasets\"]\n", |
| 333 | + "labels = ['1 dataset', '4 datasets', '10 datasets', '30 datasets']\n", |
333 | 334 | "\n", |
334 | 335 | "leg = plt.legend(sc, labels, ncol=1, frameon=False, fontsize=12,\n", |
335 | 336 | " handlelength=2, loc=1, borderpad = 1.2,\n", |
336 | 337 | " handletextpad=1, scatterpoints = 1,\n", |
337 | 338 | " bbox_to_anchor=(2.0, 1.0))\n", |
338 | 339 | "\n", |
339 | | - "sns.despine(offset=60, trim=True);\n", |
| 340 | + "sns.despine(offset=60, trim=True)\n", |
340 | 341 | "sns.despine(bottom=True)\n", |
341 | 342 | "\n", |
342 | 343 | "\n", |
|
360 | 361 | "DS000108 and DS000148 \\\\textbf{improved the most} after addressing particular \\\n", |
361 | 362 | "issues\"\"\"\n", |
362 | 363 | "\n", |
363 | | - "annotfmt = r\"\\noindent\\parbox{{{0:.1f}cm}}{{\\raggedright \\textit{{{1}}}}}\".format\n", |
| 364 | + "annotfmt = r'\\noindent\\parbox{{{0:.1f}cm}}{{\\raggedright \\textit{{{1}}}}}'.format\n", |
364 | 365 | "# ax.annotate(\n", |
365 | 366 | "# annotfmt(5.0, annot1),\n", |
366 | 367 | "# xy=(2.05, 2), xycoords='data', xytext=(2.5, 1.9),\n", |
|
407 | 408 | }, |
408 | 409 | "outputs": [], |
409 | 410 | "source": [ |
410 | | - "from scipy import stats\n", |
411 | | - "from seaborn.categorical import _CategoricalScatterPlotter\n", |
412 | | - "from seaborn.axisgrid import PairGrid, utils\n", |
413 | 411 | "from matplotlib.colors import LinearSegmentedColormap, Normalize\n", |
414 | | - "from matplotlib import gridspec\n", |
| 412 | + "from seaborn.axisgrid import PairGrid, utils\n", |
415 | 413 | "\n", |
416 | 414 | "# class PairGrid(Grid):\n", |
417 | 415 | "# def __init__(self, data, hue=None, hue_order=None, palette=None,\n", |
|
420 | 418 | "# despine=True, dropna=True, size=None):\n", |
421 | 419 | "\n", |
422 | 420 | "\n", |
423 | | - " \n", |
| 421 | + "\n", |
424 | 422 | "\n", |
425 | 423 | "class MyPairGrid(PairGrid):\n", |
426 | 424 | " def __init__(self, data, data0, hue=None, hue_order=None, palette=None,\n", |
|
438 | 436 | " y_vars = list(vars)\n", |
439 | 437 | " elif (x_vars is not None) or (y_vars is not None):\n", |
440 | 438 | " if (x_vars is None) or (y_vars is None):\n", |
441 | | - " raise ValueError(\"Must specify `x_vars` and `y_vars`\")\n", |
| 439 | + " raise ValueError('Must specify `x_vars` and `y_vars`')\n", |
442 | 440 | " else:\n", |
443 | 441 | " numeric_cols = self._find_numeric_cols(data)\n", |
444 | 442 | " x_vars = numeric_cols\n", |
|
464 | 462 | "\n", |
465 | 463 | " fig, axes = plt.subplots(len(y_vars), len(x_vars),\n", |
466 | 464 | " figsize=figsize,\n", |
467 | | - " sharex=\"col\", sharey=\"row\",\n", |
| 465 | + " sharex='col', sharey='row',\n", |
468 | 466 | " squeeze=False)\n", |
469 | | - " \n", |
| 467 | + "\n", |
470 | 468 | " self.fig = fig\n", |
471 | 469 | " self.axes = axes\n", |
472 | 470 | " self.data = data\n", |
|
481 | 479 | " # Sort out the hue variable\n", |
482 | 480 | " self._hue_var = hue\n", |
483 | 481 | " if hue is None:\n", |
484 | | - " self.hue_names = [\"_nolegend_\"]\n", |
485 | | - " self.hue_vals = pd.Series([\"_nolegend_\"] * len(data),\n", |
| 482 | + " self.hue_names = ['_nolegend_']\n", |
| 483 | + " self.hue_vals = pd.Series(['_nolegend_'] * len(data),\n", |
486 | 484 | " index=data.index)\n", |
487 | 485 | " else:\n", |
488 | 486 | " hue_names = utils.categorical_order(data[hue], hue_order)\n", |
|
502 | 500 | " if despine:\n", |
503 | 501 | " utils.despine(fig=fig)\n", |
504 | 502 | " fig.tight_layout()\n", |
505 | | - " \n", |
506 | | - " \n", |
| 503 | + "\n", |
| 504 | + "\n", |
507 | 505 | " data.index = data.dataset\n", |
508 | 506 | " data0.index = data0.dataset\n", |
509 | 507 | " self.data0 = data0.reindex(data.index)\n", |
510 | 508 | " self.hue_names = None\n", |
511 | 509 | " self.plots = None\n", |
512 | | - " \n", |
| 510 | + "\n", |
513 | 511 | " def map(self, func, **kwargs):\n", |
514 | 512 | " \"\"\"Plot with the same function in every subplot.\n", |
515 | 513 | " Parameters\n", |
|
518 | 516 | " Must take x, y arrays as positional arguments and draw onto the\n", |
519 | 517 | " \"currently active\" matplotlib Axes.\n", |
520 | 518 | " \"\"\"\n", |
521 | | - " kw_color = kwargs.pop(\"color\", None)\n", |
| 519 | + " kw_color = kwargs.pop('color', None)\n", |
522 | 520 | "\n", |
523 | 521 | " self.plots = []\n", |
524 | 522 | " for j, x_var in enumerate(self.x_vars):\n", |
|
531 | 529 | "\n", |
532 | 530 | " self._clean_axis(ax)\n", |
533 | 531 | " self._update_legend_data(ax)\n", |
534 | | - " \n", |
535 | | - " \n", |
| 532 | + "\n", |
| 533 | + "\n", |
536 | 534 | "def stripplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None,\n", |
537 | 535 | " jitter=False, dodge=False, orient=None, color=None, palette=None,\n", |
538 | | - " size=5, edgecolor=\"gray\", linewidth=0, ax=None, **kwargs):\n", |
| 536 | + " size=5, edgecolor='gray', linewidth=0, ax=None, **kwargs):\n", |
539 | 537 | " plotter = _StripPlotter(x, y, hue, data, order, hue_order,\n", |
540 | 538 | " jitter, dodge, orient, color, palette)\n", |
541 | 539 | " if ax is None:\n", |
542 | 540 | " ax = plt.gca()\n", |
543 | 541 | "\n", |
544 | | - " kwargs.setdefault(\"zorder\", 3)\n", |
545 | | - " size = kwargs.get(\"s\", size)\n", |
| 542 | + " kwargs.setdefault('zorder', 3)\n", |
| 543 | + " size = kwargs.get('s', size)\n", |
546 | 544 | " if linewidth is None:\n", |
547 | 545 | " linewidth = size / 10\n", |
548 | | - " if edgecolor == \"gray\":\n", |
| 546 | + " if edgecolor == 'gray':\n", |
549 | 547 | " edgecolor = plotter.gray\n", |
550 | 548 | " kwargs.update(dict(s=size ** 2,\n", |
551 | 549 | " edgecolor=edgecolor,\n", |
|
557 | 555 | "\n", |
558 | 556 | "def mystripplot(x=None, x0=None, y=None, hue=None, data=None, order=None, hue_order=None,\n", |
559 | 557 | " jitter=False, dodge=False, orient=None, color=None, palette=None,\n", |
560 | | - " size=5, edgecolor=\"gray\", linewidth=0, ax=None, **kwargs):\n", |
| 558 | + " size=5, edgecolor='gray', linewidth=0, ax=None, **kwargs):\n", |
561 | 559 | "\n", |
562 | 560 | " if ax is None:\n", |
563 | 561 | " ax = plt.gca()\n", |
|
566 | 564 | " colors = ['red', 'goldenrod', 'green']\n", |
567 | 565 | " cm = LinearSegmentedColormap.from_list('ratings', colors, N=50)\n", |
568 | 566 | " norm = Normalize(vmin=0.5, vmax=3.0)\n", |
569 | | - " \n", |
| 567 | + "\n", |
570 | 568 | " y = range(len(y))[::-1]\n", |
571 | 569 | " ecs = [cm(norm(v)) for v in x0.values.ravel()]\n", |
572 | 570 | " ax.scatter(x0, y, c='w', linewidth=2, edgecolors=ecs, s=120, zorder=2, clip_on=False)\n", |
|
594 | 592 | "import matplotlib.patches as mpatches\n", |
595 | 593 | "from matplotlib.collections import PatchCollection\n", |
596 | 594 | "\n", |
| 595 | + "\n", |
597 | 596 | "def gradient_patch(xy, width, height, cmap='viridis', colors=None, N=20, zorder=10):\n", |
598 | 597 | " x0, y0 = xy\n", |
599 | 598 | " if isinstance(colors, (list, tuple)):\n", |
600 | 599 | " cmap = LinearSegmentedColormap.from_list('ratings', colors, N=50)\n", |
601 | | - " \n", |
| 600 | + "\n", |
602 | 601 | " patches = []\n", |
603 | 602 | " levels = np.linspace(0.0, 1.0, N)\n", |
604 | 603 | " elw = width / N\n", |
605 | 604 | " for i, l in enumerate(levels):\n", |
606 | | - " rect = mpatches.Rectangle((x0 + i * elw, y0), elw, height, ec=\"none\", fc=cm(l), clip_on=False)\n", |
| 605 | + " rect = mpatches.Rectangle((x0 + i * elw, y0), elw, height, ec='none', fc=cm(l), clip_on=False)\n", |
607 | 606 | " patches.append(rect)\n", |
608 | 607 | "\n", |
609 | 608 | " return PatchCollection(patches, match_original=True, zorder=zorder, clip_on=False)\n", |
610 | | - " \n", |
611 | 609 | "\n", |
612 | | - "class AnyObject(object):\n", |
| 610 | + "\n", |
| 611 | + "class AnyObject:\n", |
613 | 612 | " def __init__(self, label):\n", |
614 | 613 | " self.label = label\n", |
615 | | - " \n", |
| 614 | + "\n", |
616 | 615 | " def get_label(self):\n", |
617 | 616 | " return self.label\n", |
618 | 617 | "\n", |
619 | | - "class AnyObjectHandler(object):\n", |
| 618 | + "class AnyObjectHandler:\n", |
620 | 619 | " def legend_artist(self, legend, orig_handle, fontsize, handlebox):\n", |
621 | 620 | " x0, y0 = handlebox.xdescent, handlebox.ydescent\n", |
622 | 621 | " width, height = handlebox.width, handlebox.height\n", |
|
642 | 641 | } |
643 | 642 | ], |
644 | 643 | "source": [ |
645 | | - "sns.set_context(\"notebook\", font_scale=2)\n", |
| 644 | + "sns.set_context('notebook', font_scale=2)\n", |
646 | 645 | "\n", |
647 | 646 | "pgf_with_custom_preamble = {\n", |
648 | 647 | " 'ytick.major.size': 0,\n", |
|
663 | 662 | " height=25, aspect=.10)\n", |
664 | 663 | "\n", |
665 | 664 | "# Draw a dot plot using the stripplot function\n", |
666 | | - "g.map(mystripplot, size=15, orient=\"h\")\n", |
| 665 | + "g.map(mystripplot, size=15, orient='h')\n", |
667 | 666 | "\n", |
668 | 667 | "# Use semantically meaningful titles for the columns\n", |
669 | | - "titles = [\"Overall\", \"Surf. recon.\", \"T1w ROIs\", \n", |
670 | | - " \"T1w to MNI\", \"BOLD ROIs\", \"BOLD to T1w\", \"SyN SDC\"]\n", |
| 668 | + "titles = ['Overall', 'Surf. recon.', 'T1w ROIs',\n", |
| 669 | + " 'T1w to MNI', 'BOLD ROIs', 'BOLD to T1w', 'SyN SDC']\n", |
671 | 670 | "\n", |
672 | 671 | "# Use the same x axis limits on all columns and add better labels\n", |
673 | 672 | "g.set(xlim=(-0.2, 3.2), xlabel='', ylabel='')\n", |
|
676 | 675 | "g.axes.flat[0].set(yticks=range(len(datasets))[::-1])\n", |
677 | 676 | "g.axes.flat[0].set(yticklabels=datasets)\n", |
678 | 677 | "g.axes.flat[0].tick_params(axis='y', which='major', pad=45)\n", |
679 | | - "for ax, title in zip(g.axes.flat, titles):\n", |
| 678 | + "for ax, title in zip(g.axes.flat, titles, strict=False):\n", |
680 | 679 | " # Set a different title for each axes\n", |
681 | 680 | " ax.set(title=title)\n", |
682 | 681 | "# ax.set(xlabel=title)\n", |
683 | 682 | " ax.set(xticks=[])\n", |
684 | | - " \n", |
| 683 | + "\n", |
685 | 684 | " # Make the grid horizontal instead of vertical\n", |
686 | 685 | " ax.xaxis.grid(False)\n", |
687 | 686 | " ax.yaxis.grid(True)\n", |
|
0 commit comments