{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Research example\n", "This notebook preserves a 2019 exploratory method by Andrew Dyck.\n", "Read README.md before running it. Full scientific validation is still needed.\n", "Outputs below have been cleared and do not represent a new execution.\n" ] }, { "cell_type": "code", "metadata": {}, "source": [ "from pathlib import Path\n", "Path(\"generated\").mkdir(exist_ok=True)\n" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "# FRB Pipeline Simulation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib notebook\n", "import numpy as np\n", "from matplotlib.pyplot import *\n", "import gc" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "def axis_labels(x, y, z):\n", " xlab = xlabel(x)\n", " ylab = ylabel(y)\n", " titles = title(z)\n", " legends = legend\n", " return xlab, ylab, titles, legends\n", "\n", "def centfinder(x, threshold, length):\n", " intensity = x\n", " peaksx = [] #This is the values that the peaks are centred around\n", " peaksy = []\n", " peak_dex = (list(intensity))\n", " inversep_dex=intensity[::-1]\n", " for i in range(len(intensity)-1):\n", " if intensity[i] > threshold and intensity[i-2] peaksy[i]:\n", " break\n", "\n", " for j in range(len((intensity[0:peak_index] - halfmax))):\n", " xmax = (intensity[peak_index:-1][j] - halfmax)\n", " xmaxs = peak_index+j-1\n", " if xmax < 0 or intensity[peak_index:-1][j] > peaksy[i]:\n", " break\n", " \n", " x_range = length[xmins:xmaxs]\n", " y_range = x[xmins:xmaxs]\n", " return x_range, yrange\n", "\n", "\n", "def peakfinder(x, threshold, array_length):\n", " \"\"\"This function finds all maxima above a certain threshold, with the condition that the two points before\n", " and after the maxima must have values below it.\"\"\"\n", " pixels = array_length\n", " intensity = x\n", " peaksx = [] #This is the values that the peaks are centred around\n", " peaksy = []\n", " peak_dex = (list(intensity))\n", " inversep_dex=intensity[::-1]\n", " for i in range(len(intensity)-1):\n", " if intensity[i] > threshold and intensity[i-2] threshold and intensity[i-2] peaksy[i] or inversep_dex[pind:][j] <= np.median(intensity) or pind+j+2 > len(intensity) or pind==0: #here the restrictions break the loop if the counter goes below zero, it the yvalue surpasses \n", " #the yvalue of the peak, or if the yvalue of the index drops below the median of the data set\n", " break\n", " for j in range(len((intensity[0:peak_index]))):#This loop finds the maximum x value that could be considered part of the pulse\n", " if peak_index == len(intensity)-1: #This ensures that a point that is at the end is not considered as a peak\n", " break\n", " ymax = (intensity[peak_index:][j] - halfmax) #Does the same as its counterpart ymax above\n", " xmaxs = peak_index+j #Again finds the corresponding index value\n", " if ymax < 0 or intensity[peak_index:][j] > peaksy[i] or intensity[peak_index:][j] < np.median(intensity) or peak_index+j +2 > len(intensity):\n", " #The above line does the dame as in the previous for loop, and will also break is one of the conditions are met.\n", " break\n", " \n", " \n", " xrange= xrange+(np.ndarray.tolist(np.arange(len(intensity))[xmins:xmaxs])) #This adds a each new set of values to the existing\n", " #set to make a master set of all values not considered to be noise\n", " yrange= yrange+(np.ndarray.tolist(intensity[xmins:xmaxs])) #This finds the y-value to these points\n", " \n", " allx_range.append(xrange)\n", " ally_range.append(yrange)\n", " return allx_range, ally_range" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The function is used below to find the corresponding x and y parameters of the peaks" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x_ranges,yranges = pulsefinder(DM_measure)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The peaks are then set into a list, and using np.pop, values from the dispersion data are excluded is their x value is included in the list of peaks" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "all_noised_datas = []\n", "for k in range(len(DM_measure)):\n", " new_list = np.ndarray.tolist(np.sort(list(set(x_ranges[k]))))\n", " \n", " new_list = new_list+[0]#An extra incedies is added so that when the final useful value is poped below, the loop will\n", " #not throw an error.\n", " noised_data = [] #This is the resulting data points that will be considered noise\n", " for i in np.arange(len(DM_measure[k])):\n", " if i==new_list[0]: #If the counter matches the value of the x_range, the x_range value will be removed, and the \n", " #corresponding data point from the Dispersion data will not be placed in the noise data\n", " new_list.pop(0)\n", " continue\n", " noised_data.append(DM_measure[k][i]) #The values of the DM data that will be considered noise are now added to the noise array\n", " all_noised_datas.append(noised_data)\n", "all_noised_datas = np.asarray(all_noised_datas)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1/2.41" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "figure()\n", "plot(all_noised_datas[300], marker='.')\n", "plot(all_noised_datas[285], marker='.')\n", "\n", "#plot(DM_measure[0])\n", "axis_labels('time(s)(samples have been cut out, so point locations not accurate)','Amplitude','Noise with a DM of 600, at DM=600')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using the new base noise data, the std of each DM data set is found below" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Now find the standard deviation and the mean of the noise for each DM. More accurate DMs will have a more accurate noise reading.\n", "DM_std = np.zeros(len(all_noised_datas))\n", "DM_mean = np.zeros(len(all_noised_datas))\n", "##Find the mean and std of the noise data\n", "for k in range(len(all_noised_datas)):\n", " DM_std[k] = np.std(all_noised_datas[k])\n", " DM_mean[k] = np.mean(all_noised_datas[k])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally to calculate the signal to noise ratio, the peak needs to be calculated. This is done by using those same peaks and averaging them together." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def peakaverage(DM_data,adder):\n", " allx_range = []\n", " ally_range = []\n", " for k in range(len(DM_data)):\n", " intensity = DM_data[k] #This defines the input data that we wish to find the peaks in\n", " threshold = np.mean(DM_data[k])+np.std(DM_data[k])#This creates a limit to which peaks with values\n", " #below this number will not be considered in the peak finder\n", " peaksx = [] #This is the values that the peaks are centred around\n", " peaksy = [] #This is the amplitude value of the peaks\n", " peak_dex = (list(intensity)) #This turns the input from an array to a list.\n", " inversep_dex=intensity[::-1] #This reverses the list of the input data\n", " for i in range(len(intensity)-2): #This is the loop that identifies the peaks. two previous values before and after the considered \n", " #point must have values less than the point for said point to be considered a maxima\n", " if intensity[i] > threshold and intensity[i-2] peaksy[i] or pind+j+2 > len(intensity) or pind==0: #here the restrictions break the loop if the counter goes below zero, it the yvalue surpasses \n", " #the yvalue of the peak, or if the yvalue of the index drops below the median of the data set\n", " break\n", " for j in range(len((intensity[0:peak_index]))):#This loop finds the maximum x value that could be considered part of the pulse\n", " if peak_index == len(intensity)-1: #This ensures that a point that is at the end is not considered as a peak\n", " break\n", " ymax = (intensity[peak_index:][j] - halfmax) #Does the same as its counterpart ymax above\n", " xmaxs = peak_index+j #Again finds the corresponding index value\n", " if ymax < 0 or intensity[peak_index:][j] > peaksy[i] or peak_index+j +2 > len(intensity):\n", " #The above line does the dame as in the previous for loop, and will also break is one of the conditions are met.\n", " break\n", " \n", " \n", " xrange= xrange+(np.ndarray.tolist(np.arange(len(intensity))[xmins:xmaxs])) #This adds a each new set of values to the existing\n", " #set to make a master set of all values not considered to be noise\n", " yrange= yrange+(np.ndarray.tolist(intensity[xmins:xmaxs])) #This finds the y-value to these points\n", " \n", " allx_range.append(xrange)\n", " ally_range.append(yrange)\n", " \n", " peaks_av = np.zeros(len(ally_range))\n", " for k in range(len(ally_range)):\n", " peaks_av[k] = np.mean(ally_range[k]+np.mean(adder[k]))\n", " \n", " return peaks_av" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "DM_meaned = np.zeros((len(DM_measure),n_time))\n", "for i in range(len(DM_measure)):\n", " DM_meaned[i] = DM_measure[i]-np.mean(DM_measure[i])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ypeaks_av = peakaverage(DM_meaned, DM_measure)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "figure()\n", "plot((ypeaks_av[:]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Another method to find appropriate DMs is to use matched filtering" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "DM_measure[200].max()-DM_measure[201].max()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(DM_mean[295])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(ypeaks_av[249:350]-DM_mean[249:350])/DM_std[249:350]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1500000/400" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }