Skip to content

Commit 41c5c57

Browse files
authored
Merge pull request #56 from AUAS-Pulsar/dedisp_implementation
Dedisp implementation
2 parents 554a294 + 37dec0c commit 41c5c57

File tree

6 files changed

+176
-38
lines changed

6 files changed

+176
-38
lines changed

dedisperse/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"""
22
import file for dedisperse.py
33
"""
4-
from .dedisperse import dedisperse
4+
from .dedisperse import dedisperse, find_dm

dedisperse/dedisperse.py

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
'''
2-
Dedisperses data
2+
Dedisperses data
33
'''
44
# pylint: disable-msg=C0103
55
import numpy as np
66

7-
def dedisperse(samples, dm):
7+
def dedisperse(samples, highest_x=None, max_delay=0, disperion_measure=None):
88
'''
99
This method performs dedispersion on the filterbank data
10+
The maximum_delay specifies between the currently considered pulsar signal and the next pulsar
11+
signal should be
12+
The highest_x specifies the amount of intensities that are used for estimating the minimum
13+
pulsar intensity
1014
'''
11-
samples = np.asarray(samples)
12-
# Distribute the DM over the amount of samples
13-
delays_per_sample = np.round(np.linspace(dm, 0, samples.shape[1])).astype(int)
15+
16+
# Check if parameters contain a Dispersion Measure, if not, estimate one
17+
if disperion_measure is None:
18+
# Estimates the minimum for an intensity to be considered a pulsar
19+
pulsar_intensity = find_estimation_intensity(samples, highest_x)
20+
disperion_measure = find_dm(samples, pulsar_intensity, max_delay)
21+
22+
# Distribute the Dispersion Measure over the amount of samples
23+
delays_per_sample = np.round(np.linspace(disperion_measure, 0, samples.shape[1])).astype(int)
1424

1525
# Loop over the frequencies
16-
for i, _ in enumerate(delays_per_sample):
26+
for i in range(delays_per_sample.size):
1727

1828
# Temporary array that is used to later delay the frequency
1929
temporary_samples = []
@@ -25,3 +35,80 @@ def dedisperse(samples, dm):
2535
samples[:, i] = np.roll(temporary_samples, delays_per_sample[i])
2636

2737
return samples
38+
39+
def find_dm(samples, pulsar_intensity, max_delay):
40+
'''
41+
This method attempts to find a dispersion measure
42+
'''
43+
44+
# Loop through the samples to find a pulsar intensity to start calculating from
45+
for s, sample in enumerate(samples[:, 0]):
46+
47+
# If the sample meets the minimum intensity, attempt to find a line continuing from
48+
# this intensity
49+
if sample > pulsar_intensity:
50+
start_sample_index = s
51+
52+
# Attempt to find a line, line_coordinates contains first and last index of the pulsar
53+
line_coordinates = find_line(samples, start_sample_index, max_delay, pulsar_intensity)
54+
55+
# If a line is found, calculate and return the dispersion measure
56+
if line_coordinates is not None:
57+
disperion_measure = line_coordinates[1] - line_coordinates[0]
58+
return disperion_measure
59+
60+
return None
61+
62+
63+
def find_line(samples, start_sample_index, max_delay, pulsar_intensity):
64+
'''
65+
This method tries to find a line starting from the sample index given in the parameters
66+
it stops if there is no intensity within the max_delay higher than the average_intensity
67+
'''
68+
69+
previous_index = start_sample_index
70+
71+
failed_to_find_line = True
72+
73+
# Loop through the frequencies
74+
for f in range(samples[1].size):
75+
76+
# Loop through previous intensity until the max delay is reached
77+
for i, intensity in enumerate(samples[:, f][previous_index:previous_index + max_delay]):
78+
79+
# Skip the first frequency, since we start measuring from the first intensity
80+
if f == 0:
81+
failed_to_find_line = False
82+
break
83+
84+
# If the intensity is higher than the pulsar_intensity, continue finding a signal
85+
if intensity > pulsar_intensity:
86+
previous_index = previous_index + i
87+
failed_to_find_line = False
88+
break
89+
90+
# If there is no line found, return None
91+
if failed_to_find_line:
92+
return None
93+
94+
# If all frequencies are looped, a continuous signal is found,
95+
# so we return the first and last index of the line
96+
return start_sample_index, previous_index
97+
98+
def find_estimation_intensity(samples, highest_x):
99+
'''
100+
This method finds the average intensity for the highest x intensities
101+
The average_intensity is considered a requirement for intensities to be considered a pulsar
102+
'''
103+
104+
# Sum of all intensities
105+
sum_intensities = 0
106+
107+
# Looks for the top x highest intensities in the samples and adds them up together
108+
for sample in samples:
109+
sum_intensities += np.sum(sorted(sample, reverse=True)[:highest_x])
110+
111+
# Calculates the average_intensity
112+
average_intensity = (sum_intensities) / (samples.shape[0] * highest_x)
113+
114+
return average_intensity

examples/dedisperse_samples.py

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,15 @@
1616

1717
from timeseries.timeseries import Timeseries
1818

19-
def print_possible_dm(data):
20-
'''
21-
This method is for testing purposes and should probably be removed later on
22-
'''
23-
first_sample = 400
24-
25-
for i, data_point in enumerate(data[first_sample]):
26-
if(data_point > 10):
27-
print("first sample ", i, " - ", data_point)
28-
break
29-
30-
highest_difference = 0
31-
32-
for s, samples in enumerate(data):
33-
for i, data_point in enumerate(samples):
34-
if(i > highest_difference):
35-
36-
if(data_point > 10):
37-
print(s, i, " - ", data_point)
38-
highest_difference = i
39-
break
40-
41-
print(highest_difference)
42-
43-
4419
# Read filterbank data
4520
special_pspm = fb.Filterbank(filename = "../data/my_special_pspm.fil")
4621

4722
special_pspm.read_filterbank()
4823

4924
frequencies, samples = special_pspm.select_data()
5025

51-
# Use this if you have your own file with a clear pulsar signal, this method assumes all signals other than the pulsar are lower than 10
52-
#print_possible_dm(dispersed_samples)
53-
5426
# Dispersion Measure
55-
DM = 230
27+
DM = 235
5628

5729
plt.subplot(2,1,1)
5830
data, extent = waterfall_plot(samples, frequencies)

examples/dedisperse_stream.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'''
2+
Example that dedisperses filterbank data and plots it together with a timeseries plot
3+
'''
4+
# pylint: disable-all
5+
import os,sys,inspect
6+
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
7+
parentdir = os.path.dirname(currentdir)
8+
sys.path.insert(0,parentdir)
9+
10+
import numpy as np
11+
import matplotlib.pyplot as plt
12+
13+
import filterbank.filterbank as fb
14+
import dedisperse as dedisperse
15+
from plot.static_waterfall import waterfall_plot
16+
17+
from clipping import clipping
18+
19+
# Read filterbank data,
20+
21+
# Standard file
22+
special_pspm = fb.Filterbank(filename = "./my_special_pspm.fil")
23+
highest_x=10
24+
max_delay=10
25+
26+
special_pspm.read_filterbank()
27+
28+
frequencies, samples = special_pspm.select_data()
29+
30+
# Plot the original data
31+
plt.subplot(2,1,1)
32+
data, extent = waterfall_plot(samples, frequencies)
33+
34+
img = plt.imshow(data.T,
35+
aspect='auto',
36+
origin='lower',
37+
rasterized=True,
38+
interpolation='nearest',
39+
extent=extent,
40+
cmap='cubehelix')
41+
42+
plt.colorbar()
43+
44+
time_series = []
45+
46+
for sample_set in samples:
47+
summed_sample = np.sum(sample_set)
48+
time_series.append(summed_sample)
49+
50+
plt.subplot(2,1,2)
51+
plt.plot(time_series)
52+
plt.show()
53+
54+
# Dedisperse the samples
55+
samples = dedisperse.dedisperse(samples, highest_x, max_delay)
56+
57+
# Plot the dedispersed data
58+
plt.subplot(2,1,1)
59+
data, extent = waterfall_plot(samples, frequencies)
60+
61+
img = plt.imshow(data.T,
62+
aspect='auto',
63+
origin='lower',
64+
rasterized=True,
65+
interpolation='nearest',
66+
extent=extent,
67+
cmap='cubehelix')
68+
69+
time_series = []
70+
71+
for sample_set in samples:
72+
summed_sample = np.sum(sample_set)
73+
time_series.append(summed_sample)
74+
75+
plt.subplot(2,1,2)
76+
plt.plot(time_series)
77+
plt.show()

pipeline/pipeline.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,13 @@ def measure_methods(self, stopwatch, fil_data, freqs, DM, scale):
112112
Run and time all methods/modules
113113
"""
114114
# clipping
115+
115116
time_clipping = timer()
116117
_, _ = clipping.clipping(freqs, fil_data)
117118
stopwatch['time_clipping'] = timer() - time_clipping
118119
# dedisperse
119120
time_dedisp = timer()
120-
fil_data = dedisperse.dedisperse(fil_data, DM)
121+
fil_data = dedisperse.dedisperse(fil_data, disperion_measure=DM)
121122
stopwatch['time_dedisp'] = timer() - time_dedisp
122123
# timeseries
123124
time_t_series = timer()

tests/test_dedisperse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import unittest
6+
import numpy as np
67

78
from .context import dedisperse # pylint: disable-msg=E0611
89

@@ -25,5 +26,5 @@ def test_dedisperse(self):
2526
expect moved frequencies per sample
2627
"""
2728
disp_measure = 6
28-
results = dedisperse.dedisperse(self.samples, disp_measure)
29+
results = dedisperse.dedisperse(np.array(self.samples), disperion_measure=disp_measure)
2930
self.assertListEqual(list(results[len(self.samples)-1]), [10]*7)

0 commit comments

Comments
 (0)