Skip to content

Commit e701f8a

Browse files
authored
Merge pull request #207 from UCL/design_patterns
Fix code chunks in design patterns
2 parents 628d2e3 + 981ec2e commit e701f8a

File tree

1 file changed

+60
-29
lines changed

1 file changed

+60
-29
lines changed

ch05construction/09patterns.ipynb

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,13 @@
421421
" self.end = self.data[-1, 0]\n",
422422
" self.range = self.end - self.start\n",
423423
" 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",
426427
" self.times = self.data[:, 0].copy()\n",
427428
" self.values = self.data[:, 1].copy()\n",
428429
" 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:]]"
430431
]
431432
},
432433
{
@@ -447,26 +448,33 @@
447448
"outputs": [],
448449
"source": [
449450
"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",
450458
" def format_date(self, date):\n",
451459
" date_format=\"%Y-%m-%d\"\n",
452460
" return datetime.strptime(date, date_format)\n",
453461
"\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",
454466
" def load_data(self):\n",
455467
" start_date_str = '1700-12-31'\n",
456468
" 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",
458470
"\n",
459471
" with open(\"SIDC-SUNSPOTS_A.csv\") as header:\n",
460472
" data = csv.reader(header)\n",
461473
"\n",
462474
" next(data) # Skip header row\n",
463475
" 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",
470478
"\n",
471479
" def frequency_data(self):\n",
472480
" return self.frequency_strategy.transform(self.series)"
@@ -492,8 +500,11 @@
492500
"class FourierNearestFrequencyStrategy:\n",
493501
" def transform(self, series):\n",
494502
" 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+
" )"
497508
]
498509
},
499510
{
@@ -555,7 +566,9 @@
555566
" result = lombscargle(series.times,\n",
556567
" series.values,\n",
557568
" 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+
" )"
559572
]
560573
},
561574
{
@@ -833,15 +846,23 @@
833846
" plt.show()\n",
834847
"\n",
835848
" 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",
837854
" # image normaliser not shown here, but used as an example\n",
838855
" normaliser = ImageNormalize(stretch=source_stretch(0.01), clip=False)\n",
839856
" return normaliser.normalise(self.data)\n",
840857
"\n",
841858
" 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",
843863
" 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",
845866
" return normalised_data"
846867
]
847868
},
@@ -879,12 +900,14 @@
879900
"\n",
880901
" def _pre_process(self):\n",
881902
" # 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",
883905
" return normaliser.normalise(self.data)\n",
884906
"\n",
885907
" def _get_colour_image(self, normalised_data):\n",
886908
" 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",
888911
" return normalised_data\n",
889912
"\n",
890913
"\n",
@@ -897,20 +920,24 @@
897920
"\n",
898921
" def _pre_process(self):\n",
899922
" # 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",
901925
" return normaliser.normalise(self.data)\n",
902926
"\n",
903927
" 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",
906932
" return normalised_data\n",
907933
"\n",
908934
"\n",
909935
"# Imaging from microscopes\n",
910936
"\n",
911937
"\n",
912938
"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",
914941
" def __init__(self, data, header):\n",
915942
" super().__init__(data, header)\n",
916943
" self.instrument = self._determine_instrument(header)\n",
@@ -927,7 +954,8 @@
927954
"\n",
928955
" def _get_colour_image(self, normalised_data):\n",
929956
" 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",
931959
" return normalised_data\n",
932960
"\n",
933961
"\n",
@@ -949,7 +977,8 @@
949977
"\n",
950978
" def _get_colour_image(self, normalised_data):\n",
951979
" 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",
953982
" return normalised_data\n"
954983
]
955984
},
@@ -975,11 +1004,12 @@
9751004
"outputs": [],
9761005
"source": [
9771006
"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",
9791008
" \n",
9801009
" def read_image(self, path):\n",
9811010
" \"\"\"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",
9831013
"\n",
9841014
"\n",
9851015
"class AstronomyFactory(ImageFactory):\n",
@@ -991,8 +1021,8 @@
9911021
" return HIImage(data, header)\n",
9921022
" elif str(header.get('instrume', '')).startswith('AIA'):\n",
9931023
" 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",
9961026
" else:\n",
9971027
" return GenericImage(data, header)\n",
9981028
" \n",
@@ -1008,7 +1038,8 @@
10081038
" # ...this would continue for all 10 other image sources\n",
10091039
" # after we've gone through all possible matches to known data types\n",
10101040
" 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}\")"
10121043
]
10131044
},
10141045
{

0 commit comments

Comments
 (0)