|
421 | 421 | " self.end = self.data[-1, 0]\n",
|
422 | 422 | " self.range = self.end - self.start\n",
|
423 | 423 | " self.step = self.range / self.count\n",
|
424 |
| - " # create separate arrays as some algorithms require an array as an argument\n", |
425 |
| - " # and will throw an exception a view of an array is passed as an argument\n", |
| 424 | + " # create separate arrays as some algorithms require an array\n", |
| 425 | + " # as an argument and will throw an exception \n", |
| 426 | + " # if a view of an array is passed as an argument\n", |
426 | 427 | " self.times = self.data[:, 0].copy()\n",
|
427 | 428 | " self.values = self.data[:, 1].copy()\n",
|
428 | 429 | " self.plot_data = [self.times, self.values]\n",
|
429 |
| - " self.inverse_plot_data = [1.0 / self.times[20:], self.values[20:]]" |
| 430 | + " self.inverse_plot_data = [1 / self.times[20:], self.values[20:]]" |
430 | 431 | ]
|
431 | 432 | },
|
432 | 433 | {
|
|
447 | 448 | "outputs": [],
|
448 | 449 | "source": [
|
449 | 450 | "class SunspotDataAnalyser(object):\n",
|
| 451 | + " def __init__(self, frequency_strategy):\n", |
| 452 | + " self.secs_per_year = (\n", |
| 453 | + " datetime(2014, 1, 1) - datetime(2013, 1, 1)\n", |
| 454 | + " ).total_seconds()\n", |
| 455 | + " self.load_data()\n", |
| 456 | + " self.frequency_strategy = frequency_strategy\n", |
| 457 | + "\n", |
450 | 458 | " def format_date(self, date):\n",
|
451 | 459 | " date_format=\"%Y-%m-%d\"\n",
|
452 | 460 | " return datetime.strptime(date, date_format)\n",
|
453 | 461 | "\n",
|
| 462 | + " def date_to_years(self, date_string):\n", |
| 463 | + " return (self.format_date(date_string) - self.start_date\n", |
| 464 | + " ).total_seconds() / self.secs_per_year\n", |
| 465 | + "\n", |
454 | 466 | " def load_data(self):\n",
|
455 | 467 | " start_date_str = '1700-12-31'\n",
|
456 | 468 | " self.start_date = self.format_date(start_date_str)\n",
|
457 |
| - " secs_per_year = (datetime(2014, 1, 1) - datetime(2013, 1, 1)).total_seconds()\n", |
| 469 | + "\n", |
458 | 470 | "\n",
|
459 | 471 | " with open(\"SIDC-SUNSPOTS_A.csv\") as header:\n",
|
460 | 472 | " data = csv.reader(header)\n",
|
461 | 473 | "\n",
|
462 | 474 | " next(data) # Skip header row\n",
|
463 | 475 | " self.series = Series([[\n",
|
464 |
| - " (self.format_date(row[0]) - self.start_date).total_seconds() / secs_per_year,\n", |
465 |
| - " float(row[1])] for row in data])\n", |
466 |
| - "\n", |
467 |
| - " def __init__(self, frequency_strategy):\n", |
468 |
| - " self.load_data()\n", |
469 |
| - " self.frequency_strategy = frequency_strategy\n", |
| 476 | + " self.date_to_years(row[0]), float(row[1])]\n", |
| 477 | + " for row in data])\n", |
470 | 478 | "\n",
|
471 | 479 | " def frequency_data(self):\n",
|
472 | 480 | " return self.frequency_strategy.transform(self.series)"
|
|
492 | 500 | "class FourierNearestFrequencyStrategy:\n",
|
493 | 501 | " def transform(self, series):\n",
|
494 | 502 | " transformed = fft(series.values)[0:series.count//2]\n",
|
495 |
| - " frequencies = fftfreq(series.count, series.step)[0:series.count//2]\n", |
496 |
| - " return Series(list(zip(frequencies, abs(transformed)/series.count)))" |
| 503 | + " frequencies = fftfreq(series.count, series.step\n", |
| 504 | + " )[0:series.count//2]\n", |
| 505 | + " return Series(list(\n", |
| 506 | + " zip(frequencies, abs(transformed)/series.count))\n", |
| 507 | + " )" |
497 | 508 | ]
|
498 | 509 | },
|
499 | 510 | {
|
|
555 | 566 | " result = lombscargle(series.times,\n",
|
556 | 567 | " series.values,\n",
|
557 | 568 | " 2.0 * math.pi * frequencies)\n",
|
558 |
| - " return Series(list(zip(frequencies, sqrt(result / series.count))))" |
| 569 | + " return Series(list(\n", |
| 570 | + " zip(frequencies, sqrt(result / series.count)))\n", |
| 571 | + " )" |
559 | 572 | ]
|
560 | 573 | },
|
561 | 574 | {
|
|
833 | 846 | " plt.show()\n",
|
834 | 847 | "\n",
|
835 | 848 | " def _pre_process(self):\n",
|
836 |
| - " \"\"\"Default preprocessing for an image. This can be normalisation or converstion from raw data into a 2d image\"\"\"\n", |
| 849 | + " \"\"\"\n", |
| 850 | + " Default preprocessing for an image.\n", |
| 851 | + " This can be normalisation or conversion from raw data into a\n", |
| 852 | + " 2d image\n", |
| 853 | + " \"\"\"\n", |
837 | 854 | " # image normaliser not shown here, but used as an example\n",
|
838 | 855 | " normaliser = ImageNormalize(stretch=source_stretch(0.01), clip=False)\n",
|
839 | 856 | " return normaliser.normalise(self.data)\n",
|
840 | 857 | "\n",
|
841 | 858 | " def _get_colour_image(self, normalised_data):\n",
|
842 |
| - " \"\"\"Converts image data into coloured image based on colourmap for the instrument\"\"\"\n", |
| 859 | + " \"\"\"\n", |
| 860 | + " Converts image data into coloured image\n", |
| 861 | + " based on colourmap for the instrument\n", |
| 862 | + " \"\"\"\n", |
843 | 863 | " normalised_data.plot_settings['cmap'] = plt.get_cmap('inferno')\n",
|
844 |
| - " normalised_data.plot_settings['norm'] = colors.Normalize(0, normalised_data.max())\n", |
| 864 | + " normalised_data.plot_settings['norm'] = colors.Normalize(\n", |
| 865 | + " 0, normalised_data.max())\n", |
845 | 866 | " return normalised_data"
|
846 | 867 | ]
|
847 | 868 | },
|
|
879 | 900 | "\n",
|
880 | 901 | " def _pre_process(self):\n",
|
881 | 902 | " # image normaliser not shown here, but used as an example\n",
|
882 |
| - " normaliser = ImageNormalize(stretch=source_stretch(0.01), clip=False)\n", |
| 903 | + " normaliser = ImageNormalize(stretch=source_stretch(0.01),\n", |
| 904 | + " clip=False)\n", |
883 | 905 | " return normaliser.normalise(self.data)\n",
|
884 | 906 | "\n",
|
885 | 907 | " def _get_colour_image(self, normalised_data):\n",
|
886 | 908 | " normalised_data.plot_settings['cmap'] = plt.get_cmap('Greys_r')\n",
|
887 |
| - " normalised_data.plot_settings['norm'] = colors.LogNorm(100, normalised_data.max())\n", |
| 909 | + " normalised_data.plot_settings['norm'] = colors.LogNorm(\n", |
| 910 | + " 100, normalised_data.max())\n", |
888 | 911 | " return normalised_data\n",
|
889 | 912 | "\n",
|
890 | 913 | "\n",
|
|
897 | 920 | "\n",
|
898 | 921 | " def _pre_process(self):\n",
|
899 | 922 | " # image normaliser not shown here, but used as an example\n",
|
900 |
| - " normaliser = ImageNormalize(stretch=source_stretch(0.25), clip=False)\n", |
| 923 | + " normaliser = ImageNormalize(stretch=source_stretch(0.25),\n", |
| 924 | + " clip=False)\n", |
901 | 925 | " return normaliser.normalise(self.data)\n",
|
902 | 926 | "\n",
|
903 | 927 | " def _get_colour_image(self, normalised_data):\n",
|
904 |
| - " normalised_data.plot_settings['cmap'] = 'stereocor{det!s}'.format(det=self.detector[-1])\n", |
905 |
| - " normalised_data.plot_settings['norm'] = colors.Normalize(0, normalised_data.max())\n", |
| 928 | + " normalised_data.plot_settings['cmap'] = (\n", |
| 929 | + " f'stereocor{self.detector[-1]!s}')\n", |
| 930 | + " normalised_data.plot_settings['norm'] = colors.Normalize(\n", |
| 931 | + " 0, normalised_data.max())\n", |
906 | 932 | " return normalised_data\n",
|
907 | 933 | "\n",
|
908 | 934 | "\n",
|
909 | 935 | "# Imaging from microscopes\n",
|
910 | 936 | "\n",
|
911 | 937 | "\n",
|
912 | 938 | "class MRC2014Image(GenericImage):\n",
|
913 |
| - " \"\"\"MRC/CCR4 2014 format Transmission Electron Microscopy (LM) Image reader\"\"\"\n", |
| 939 | + " \"\"\"MRC/CCR4 2014 format Transmission Electron Microscopy (LM)\n", |
| 940 | + " Image reader\"\"\"\n", |
914 | 941 | " def __init__(self, data, header):\n",
|
915 | 942 | " super().__init__(data, header)\n",
|
916 | 943 | " self.instrument = self._determine_instrument(header)\n",
|
|
927 | 954 | "\n",
|
928 | 955 | " def _get_colour_image(self, normalised_data):\n",
|
929 | 956 | " normalised_data.plot_settings['cmap'] = plt.get_cmap('Greys_r')\n",
|
930 |
| - " normalised_data.plot_settings['norm'] = colors.LogNorm(100, normalised_data.max())\n", |
| 957 | + " normalised_data.plot_settings['norm'] = colors.LogNorm(\n", |
| 958 | + " 100, normalised_data.max())\n", |
931 | 959 | " return normalised_data\n",
|
932 | 960 | "\n",
|
933 | 961 | "\n",
|
|
949 | 977 | "\n",
|
950 | 978 | " def _get_colour_image(self, normalised_data):\n",
|
951 | 979 | " normalised_data.plot_settings['cmap'] = plt.get_cmap('Greys_r')\n",
|
952 |
| - " normalised_data.plot_settings['norm'] = colors.Normalize(0, normalised_data.max())\n", |
| 980 | + " normalised_data.plot_settings['norm'] = colors.Normalize(\n", |
| 981 | + " 0, normalised_data.max())\n", |
953 | 982 | " return normalised_data\n"
|
954 | 983 | ]
|
955 | 984 | },
|
|
975 | 1004 | "outputs": [],
|
976 | 1005 | "source": [
|
977 | 1006 | "class ImageFactory:\n",
|
978 |
| - " \"\"\"Base class for an image factory that defines the factory interface\"\"\"\n", |
| 1007 | + " \"\"\"Base class that defines the factory interface\"\"\"\n", |
979 | 1008 | " \n",
|
980 | 1009 | " def read_image(self, path):\n",
|
981 | 1010 | " \"\"\"Reads image from path, using the appropriate image class\"\"\"\n",
|
982 |
| - " raise NotImplementedError(\"Child classes must implement this method\")\n", |
| 1011 | + " raise NotImplementedError(\n", |
| 1012 | + " \"Child classes must implement this method\")\n", |
983 | 1013 | "\n",
|
984 | 1014 | "\n",
|
985 | 1015 | "class AstronomyFactory(ImageFactory):\n",
|
|
991 | 1021 | " return HIImage(data, header)\n",
|
992 | 1022 | " elif str(header.get('instrume', '')).startswith('AIA'):\n",
|
993 | 1023 | " return AIAImage(data, header)\n",
|
994 |
| - " # ...this would continue for all 10 other image sources\n", |
995 |
| - " # after we've gone through all possible matches to known data types\n", |
| 1024 | + " # ...this would continue for all 10 other image sources after\n", |
| 1025 | + " # we've gone through all possible matches to known data types\n", |
996 | 1026 | " else:\n",
|
997 | 1027 | " return GenericImage(data, header)\n",
|
998 | 1028 | " \n",
|
|
1008 | 1038 | " # ...this would continue for all 10 other image sources\n",
|
1009 | 1039 | " # after we've gone through all possible matches to known data types\n",
|
1010 | 1040 | " else:\n",
|
1011 |
| - " raise ValueError(f\"File was not a recognised microscopy image: {path}\")" |
| 1041 | + " raise ValueError(\n", |
| 1042 | + " f\"File was not a recognised microscopy image: {path}\")" |
1012 | 1043 | ]
|
1013 | 1044 | },
|
1014 | 1045 | {
|
|
0 commit comments