Skip to content

Maps

mlpoppyns.generator.maps.axes_scaling

Adjusting bin edges for a specific scaling of the axes.

Authors:

Alberto Garcia Garcia (garciagarcia@ice.csic.es)
Michele Ronchi (ronchi@ice.csic.es)
Vanessa Graber (graber@ice.csic.es)

check_range_above_zero(r_range)

Check that a range larger than zero is provided.

Parameters:

Name Type Description Default
r_range Tuple[float, float]

Range of values for the r coordinate to be checked.

required
Source code in mlpoppyns/generator/maps/axes_scaling.py
16
17
18
19
20
21
22
23
24
25
def check_range_above_zero(r_range: typing.Tuple[float, float]) -> None:
    """
    Check that a range larger than zero is provided.

    Args:
        r_range (Tuple[float, float]): Range of values for the r coordinate to be checked.
    """

    if (r_range[0] <= 0) or (r_range[1] <= 0):
        raise ValueError("Value range has to be above zero.")

log_scale_vs_linear_scale(x_range, y_range, x_log_scale, y_log_scale, n_x_bins, n_y_bins)

Determine the edge positions of the bins in x and y direction according to the choice of scale, i.e., log scale vs linear scale, for a given number of bins in both directions.

Parameters:

Name Type Description Default
x_range Tuple[float, float]

Horizontal range of values for the points.

required
y_range Tuple[float, float]

Vertical range of values for the points.

required
x_log_scale bool

If True set the x-axis scale to log scale.

required
y_log_scale bool

If True set the y-axis scale to log scale.

required
n_x_bins int

Number of horizontal bins for the density map.

required
n_y_bins int

Number of vertical bins for the density map.

required

Returns:

Type Description
Tuple[float, float]

Edges of the bins in x and y direction according to the chosen scale.

Source code in mlpoppyns/generator/maps/axes_scaling.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def log_scale_vs_linear_scale(
    x_range: typing.Tuple[float, float],
    y_range: typing.Tuple[float, float],
    x_log_scale: bool,
    y_log_scale: bool,
    n_x_bins: int,
    n_y_bins: int,
) -> typing.Tuple[np.ndarray, np.ndarray]:
    """
    Determine the edge positions of the bins in x and y direction according to the
    choice of scale, i.e., log scale vs linear scale, for a given number of bins
    in both directions.

    Args:
        x_range (Tuple[float, float]): Horizontal range of values for the points.
        y_range (Tuple[float, float]): Vertical range of values for the points.
        x_log_scale (bool): If True set the x-axis scale to log scale.
        y_log_scale (bool): If True set the y-axis scale to log scale.
        n_x_bins (int): Number of horizontal bins for the density map.
        n_y_bins (int): Number of vertical bins for the density map.

    Returns:
        (Tuple[float, float]): Edges of the bins in x and y direction according to the chosen scale.
    """

    if x_log_scale and y_log_scale:
        check_range_above_zero(x_range)
        check_range_above_zero(y_range)
        x_edges = np.logspace(
            np.log10(x_range[0]), np.log10(x_range[1]), n_x_bins + 1
        )
        y_edges = np.logspace(
            np.log10(y_range[0]), np.log10(y_range[1]), n_y_bins + 1
        )

    elif x_log_scale and (y_log_scale is False):
        check_range_above_zero(x_range)
        x_edges = np.logspace(
            np.log10(x_range[0]), np.log10(x_range[1]), n_x_bins + 1
        )
        y_edges = np.linspace(y_range[0], y_range[1], n_y_bins + 1)

    elif (x_log_scale is False) and y_log_scale:
        check_range_above_zero(y_range)
        x_edges = np.linspace(x_range[0], x_range[1], n_x_bins + 1)
        y_edges = np.logspace(
            np.log10(y_range[0]), np.log10(y_range[1]), n_y_bins + 1
        )

    else:
        x_edges = np.linspace(x_range[0], x_range[1], n_x_bins + 1)
        y_edges = np.linspace(y_range[0], y_range[1], n_y_bins + 1)

    return x_edges, y_edges

mlpoppyns.generator.maps.maps2d_generator

Dataset generator functions for 2D maps.

Authors:

Alberto Garcia Garcia (garciagarcia@ice.csic.es)
Michele Ronchi (ronchi@ice.csic.es)
Vanessa Graber (graber@ice.csic.es)
Celsa Pardo Araujo (pardo@ice.csic.es)

generate_avg_fluxes_map(x, x_range, y, y_range, w, filename, x_log_scale=False, y_log_scale=False, n_x_bins=32, n_y_bins=32, colormap='Greys')

Average fluxes map generator.

Creates a map of the average weight w of a distribution of points given their X/Y coordinates in a 2D space where w is the log10 of the radio flux. The resulting map shows the average value of the weights in each bin.

Parameters:

Name Type Description Default
x ndarray

Horizontal coordinate values for the points.

required
x_range Tuple[float, float]

Horizontal range of values for the points.

required
y ndarray

Vertical coordinate values for the points.

required
y_range Tuple[float, float]

Vertical range of values for the points.

required
w ndarray

Weight values for the points, i.e., the log10 of the radio flux value.

required
filename str

File path to generate the heat map image.

required
x_log_scale bool

If True set the x-axis scale to log scale. Default False.

False
y_log_scale bool

If True set the y-axis scale to log scale. Default False.

False
n_x_bins int

Number of horizontal bins for the weight map. Default 32 bins.

32
n_y_bins int

Number of vertical bins for the weight map. Default 32 bins.

32
colormap str

Colormap to use for the image. Default Greys.

'Greys'
Source code in mlpoppyns/generator/maps/maps2d_generator.py
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
def generate_avg_fluxes_map(
    x: np.ndarray,
    x_range: typing.Tuple[float, float],
    y: np.ndarray,
    y_range: typing.Tuple[float, float],
    w: np.ndarray,
    filename: str,
    x_log_scale: bool = False,
    y_log_scale: bool = False,
    n_x_bins: int = 32,
    n_y_bins: int = 32,
    colormap: str = "Greys",
) -> None:
    """
    Average fluxes map generator.

    Creates a map of the average weight w of a distribution of points given their
    X/Y coordinates in a 2D space where w is the log10 of the radio flux.
    The resulting map shows the average value of the weights in each bin.

    Args:
        x (np.ndarray): Horizontal coordinate values for the points.
        x_range (Tuple[float, float]): Horizontal range of values for the points.
        y (np.ndarray): Vertical coordinate values for the points.
        y_range (Tuple[float, float]): Vertical range of values for the points.
        w (np.ndarray): Weight values for the points, i.e., the log10 of the radio flux value.
        filename (str): File path to generate the heat map image.
        x_log_scale (bool): If True set the x-axis scale to log scale. Default False.
        y_log_scale (bool): If True set the y-axis scale to log scale. Default False.
        n_x_bins (int): Number of horizontal bins for the weight map. Default 32 bins.
        n_y_bins (int): Number of vertical bins for the weight map. Default 32 bins.
        colormap (str): Colormap to use for the image. Default Greys.
    """

    x_edges, y_edges = axs.log_scale_vs_linear_scale(
        x_range,
        y_range,
        x_log_scale,
        y_log_scale,
        n_x_bins,
        n_y_bins,
    )

    # If the quantity desired as the weight can become negative, e.g.,
    # one of the velocity components, take the absolute value and use that
    # as the weight to avoid the possibility of summing to zero.
    total_per_bin, _, _ = np.histogram2d(x, y, bins=[x_edges, y_edges])
    total_weight, x_edges, y_edges = np.histogram2d(
        x, y, bins=[x_edges, y_edges], weights=w
    )

    # Dividing the total summed weight per bin by the number of objects to
    # obtain the average value per pixel; to avoid dividing by 0, we change
    # the values in total_weight from 0 to 0.0001; doing so does not affect
    # the final result as the original array elements are zero anyway;
    # to avoid potential sharp edges, we apply a Gaussian filter.
    total_per_bin[total_per_bin == 0] = 0.0001
    avg_weight = total_weight / total_per_bin

    # If there are no pulsars in a given area, we will set the value of log10(fluxes) to -6.
    # This prevents the low flux values of actual pulsars from being mixed with areas that have no pulsars.
    avg_weight[total_per_bin == 0.0001] = -6
    avg_weight = scipy.ndimage.gaussian_filter(avg_weight, sigma=1)

    DPI = 512
    fig = plt.figure(dpi=DPI, frameon=False)
    fig.set_size_inches(n_x_bins / DPI, n_y_bins / DPI)
    ax = plt.Axes(fig, [0.0, 0.0, 1.0, 1.0])
    ax.set_axis_off()
    fig.add_axes(ax)

    # Generating a pseudocolor plot of the smeared out average distribution;
    # we transpose the array as pcolormesh is indexed starting from the lower
    # left , i.e., the column (row) index corresponds to the x (y) coordinate.
    ax.pcolormesh(x_edges, y_edges, avg_weight.T, cmap=colormap)
    ax.set_xlim(x_range[0], x_range[1])
    ax.set_ylim(y_range[0], y_range[1])

    if x_log_scale:
        ax.set_xscale("log")

    if y_log_scale:
        ax.set_yscale("log")

    fig.savefig(filename, dpi=DPI)

    plt.close(fig)

generate_avg_fluxes_matrix(x, x_range, y, y_range, w, filename, x_log_scale=False, y_log_scale=False, n_x_bins=32, n_y_bins=32)

Average log10 fluxes matrix generator.

Creates a matrix of the average weight w of a distribution of points given their X/Y coordinates in a 2D space, where w is the log10 of the radio flux. The resulting matrix shows the average value of the weights in each bin.

Parameters:

Name Type Description Default
x ndarray

Horizontal coordinate values for the points.

required
x_range Tuple[float, float]

Horizontal range of values for the points.

required
y ndarray

Vertical coordinate values for the points.

required
y_range Tuple[float, float]

Vertical range of values for the points.

required
w ndarray

Weight values for the points, i.e., the log10 of the radio flux value.

required
filename str

File path to generate the density matrix.

required
x_log_scale bool

If True set the x-axis scale to log scale. Default False.

False
y_log_scale bool

If True set the y-axis scale to log scale. Default False.

False
n_x_bins int

Number of horizontal bins for the density matrix. Default 32 bins.

32
n_y_bins int

Number of vertical bins for the density matrix. Default 32 bins.

32
Source code in mlpoppyns/generator/maps/maps2d_generator.py
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
def generate_avg_fluxes_matrix(
    x: np.ndarray,
    x_range: typing.Tuple[float, float],
    y: np.ndarray,
    y_range: typing.Tuple[float, float],
    w: np.ndarray,
    filename: str,
    x_log_scale: bool = False,
    y_log_scale: bool = False,
    n_x_bins: int = 32,
    n_y_bins: int = 32,
) -> None:
    """
    Average log10 fluxes matrix generator.

    Creates a matrix of the average weight w of a distribution of points given their
    X/Y coordinates in a 2D space, where w is the log10 of the radio flux.
    The resulting matrix shows the average value of the weights in each bin.

    Args:
        x (np.ndarray): Horizontal coordinate values for the points.
        x_range (Tuple[float, float]): Horizontal range of values for the points.
        y (np.ndarray): Vertical coordinate values for the points.
        y_range (Tuple[float, float]): Vertical range of values for the points.
        w (np.ndarray): Weight values for the points, i.e., the log10 of the radio flux value.
        filename (str): File path to generate the density matrix.
        x_log_scale (bool): If True set the x-axis scale to log scale. Default False.
        y_log_scale (bool): If True set the y-axis scale to log scale. Default False.
        n_x_bins (int): Number of horizontal bins for the density matrix. Default 32 bins.
        n_y_bins (int): Number of vertical bins for the density matrix. Default 32 bins.
    """

    x_edges, y_edges = axs.log_scale_vs_linear_scale(
        x_range,
        y_range,
        x_log_scale,
        y_log_scale,
        n_x_bins,
        n_y_bins,
    )

    # If the quantity desired as the weight can become negative, e.g.,
    # one of the velocity components, take the absolute value and use that
    # as the weight to avoid the possibility of summing to zero.
    total_per_bin, _, _ = np.histogram2d(x, y, bins=[x_edges, y_edges])
    total_weight, x_edges, y_edges = np.histogram2d(
        x, y, bins=[x_edges, y_edges], weights=w
    )

    # Dividing the total summed weight per bin by the number of objects to
    # obtain the average value per bin; to avoid dividing by 0, we change
    # the values in total_weight from 0 to 0.0001; doing so does not affect
    # the final result as the original array elements are zero anyway;
    # to avoid potential sharp edges, we apply a Gaussian filter.
    total_per_bin[total_per_bin == 0] = 0.0001
    avg_weight = total_weight / total_per_bin

    # If there are no pulsars in a given area, we will set the value of log10(fluxes) to -7.
    # This prevents the low flux values of actual pulsars from being mixed with areas that have no pulsars.
    avg_weight[total_per_bin == 0.0001] = -7
    avg_weight = scipy.ndimage.gaussian_filter(avg_weight, sigma=1)

    np.save(filename, avg_weight)

generate_avg_weight_map(x, x_range, y, y_range, w, filename, x_log_scale=False, y_log_scale=False, n_x_bins=128, n_y_bins=128, colormap='Greys')

Average weighted map generator.

Creates a map of the average weight w of a distribution of points given their X/Y coordinates in a 2D space. The resulting map shows the average value of the weights in each bin.

Parameters:

Name Type Description Default
x ndarray

Horizontal coordinate values for the points.

required
x_range Tuple[float, float]

Horizontal range of values for the points.

required
y ndarray

Vertical coordinate values for the points.

required
y_range Tuple[float, float]

Vertical range of values for the points.

required
w ndarray

Weight values for the points.

required
filename str

File path to generate the heat map image.

required
x_log_scale bool

If True set the x-axis scale to log scale.

False
y_log_scale bool

If True set the y-axis scale to log scale.

False
n_x_bins int

Number of horizontal bins for the weight map.

128
n_y_bins int

Number of vertical bins for the weight map.

128
colormap str

Colormap to use for the image.

'Greys'
Source code in mlpoppyns/generator/maps/maps2d_generator.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
def generate_avg_weight_map(
    x: np.ndarray,
    x_range: typing.Tuple[float, float],
    y: np.ndarray,
    y_range: typing.Tuple[float, float],
    w: np.ndarray,
    filename: str,
    x_log_scale: bool = False,
    y_log_scale: bool = False,
    n_x_bins: int = 128,
    n_y_bins: int = 128,
    colormap: str = "Greys",
) -> None:
    """
    Average weighted map generator.

    Creates a map of the average weight w of a distribution of points given their
    X/Y coordinates in a 2D space.
    The resulting map shows the average value of the weights in each bin.

    Args:
        x (np.ndarray): Horizontal coordinate values for the points.
        x_range (Tuple[float, float]): Horizontal range of values for the points.
        y (np.ndarray): Vertical coordinate values for the points.
        y_range (Tuple[float, float]): Vertical range of values for the points.
        w (np.ndarray): Weight values for the points.
        filename (str): File path to generate the heat map image.
        x_log_scale (bool): If True set the x-axis scale to log scale.
        y_log_scale (bool): If True set the y-axis scale to log scale.
        n_x_bins (int): Number of horizontal bins for the weight map.
        n_y_bins (int): Number of vertical bins for the weight map.
        colormap (str): Colormap to use for the image.
    """

    x_edges, y_edges = axs.log_scale_vs_linear_scale(
        x_range,
        y_range,
        x_log_scale,
        y_log_scale,
        n_x_bins,
        n_y_bins,
    )

    # If the quantity desired as the weight can become negative, e.g.,
    # one of the velocity components, take the absolute value and use that
    # as the weight to avoid the possibility of summing to zero.
    total_per_bin, _, _ = np.histogram2d(x, y, bins=[x_edges, y_edges])
    total_weight, x_edges, y_edges = np.histogram2d(
        x, y, bins=[x_edges, y_edges], weights=w
    )

    # Dividing the total summed weight per bin by the number of objects to
    # obtain the average value per pixel; to avoid dividing by 0, we change
    # the values in total_weight from 0 to 0.0001; doing so does not affect
    # the final result as the original array elements are zero anyway;
    # to avoid potential sharp edges, we apply a Gaussian filter.
    total_per_bin[total_per_bin == 0] = 0.0001
    avg_weight = total_weight / total_per_bin
    avg_weight = scipy.ndimage.gaussian_filter(avg_weight, sigma=1)

    DPI = 512
    fig = plt.figure(dpi=DPI, frameon=False)
    fig.set_size_inches(n_x_bins / DPI, n_y_bins / DPI)
    ax = plt.Axes(fig, [0.0, 0.0, 1.0, 1.0])
    ax.set_axis_off()
    fig.add_axes(ax)

    # Generating a pseudocolor plot of the smeared out average distribution;
    # we transpose the array as pcolormesh is indexed starting from the lower
    # left , i.e., the column (row) index corresponds to the x (y) coordinate.
    ax.pcolormesh(x_edges, y_edges, avg_weight.T, cmap=colormap)
    ax.set_xlim(x_range[0], x_range[1])
    ax.set_ylim(y_range[0], y_range[1])

    if x_log_scale:
        ax.set_xscale("log")

    if y_log_scale:
        ax.set_yscale("log")

    fig.savefig(filename, dpi=DPI)

    plt.close(fig)

generate_avg_weight_matrix(x, x_range, y, y_range, w, filename, x_log_scale=False, y_log_scale=False, n_x_bins=32, n_y_bins=32)

Average weighted matrix generator.

Creates a matrix of the average weight w of a distribution of points given their X/Y coordinates in a 2D space. The resulting matrix shows the average value of the weights in each bin.

Parameters:

Name Type Description Default
x ndarray

Horizontal coordinate values for the points.

required
x_range Tuple[float, float]

Horizontal range of values for the points.

required
y ndarray

Vertical coordinate values for the points.

required
y_range Tuple[float, float]

Vertical range of values for the points.

required
w ndarray

Weight values for the points.

required
filename str

File path to generate the density matrix.

required
x_log_scale bool

If True set the x-axis scale to log scale.

False
y_log_scale bool

If True set the y-axis scale to log scale.

False
n_x_bins int

Number of horizontal bins for the density matrix.

32
n_y_bins int

Number of vertical bins for the density matrix.

32
Source code in mlpoppyns/generator/maps/maps2d_generator.py
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
def generate_avg_weight_matrix(
    x: np.ndarray,
    x_range: typing.Tuple[float, float],
    y: np.ndarray,
    y_range: typing.Tuple[float, float],
    w: np.ndarray,
    filename: str,
    x_log_scale: bool = False,
    y_log_scale: bool = False,
    n_x_bins: int = 32,
    n_y_bins: int = 32,
) -> None:
    """
    Average weighted matrix generator.

    Creates a matrix of the average weight w of a distribution of points given their
    X/Y coordinates in a 2D space.
    The resulting matrix shows the average value of the weights in each bin.

    Args:
        x (np.ndarray): Horizontal coordinate values for the points.
        x_range (Tuple[float, float]): Horizontal range of values for the points.
        y (np.ndarray): Vertical coordinate values for the points.
        y_range (Tuple[float, float]): Vertical range of values for the points.
        w (np.ndarray): Weight values for the points.
        filename (str): File path to generate the density matrix.
        x_log_scale (bool): If True set the x-axis scale to log scale.
        y_log_scale (bool): If True set the y-axis scale to log scale.
        n_x_bins (int): Number of horizontal bins for the density matrix.
        n_y_bins (int): Number of vertical bins for the density matrix.
    """

    x_edges, y_edges = axs.log_scale_vs_linear_scale(
        x_range,
        y_range,
        x_log_scale,
        y_log_scale,
        n_x_bins,
        n_y_bins,
    )

    # If the quantity desired as the weight can become negative, e.g.,
    # one of the velocity components, take the absolute value and use that
    # as the weight to avoid the possibility of summing to zero.
    total_per_bin, _, _ = np.histogram2d(x, y, bins=[x_edges, y_edges])
    total_weight, x_edges, y_edges = np.histogram2d(
        x, y, bins=[x_edges, y_edges], weights=w
    )

    # Dividing the total summed weight per bin by the number of objects to
    # obtain the average value per bin; to avoid dividing by 0, we change
    # the values in total_weight from 0 to 0.0001; doing so does not affect
    # the final result as the original array elements are zero anyway;
    # to avoid potential sharp edges, we apply a Gaussian filter.
    total_per_bin[total_per_bin == 0] = 0.0001
    avg_weight = total_weight / total_per_bin
    avg_weight = scipy.ndimage.gaussian_filter(avg_weight, sigma=1)

    np.save(filename, avg_weight)

generate_density_map(x, x_range, y, y_range, filename, x_log_scale=False, y_log_scale=False, n_x_bins=128, n_y_bins=128, colormap='Greys')

Density map generator.

Creates a density or heat map of a distribution of points given their X/Y coordinates in a 2D space. The resulting image is generated in the specified file path.

Parameters:

Name Type Description Default
x ndarray

Horizontal coordinate values for the points.

required
x_range Tuple[float, float]

Horizontal range of values for the points.

required
y ndarray

Vertical coordinate values for the points.

required
y_range Tuple[float, float]

Vertical range of values for the points.

required
filename str

File path to generate the density map image.

required
x_log_scale bool

If True set the x-axis scale to log scale.

False
y_log_scale bool

If True set the y-axis scale to log scale.

False
n_x_bins int

Number of horizontal bins for the density map.

128
n_y_bins int

Number of vertical bins for the density map.

128
colormap str

Colormap to use for the image.

'Greys'
Source code in mlpoppyns/generator/maps/maps2d_generator.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def generate_density_map(
    x: np.ndarray,
    x_range: typing.Tuple[float, float],
    y: np.ndarray,
    y_range: typing.Tuple[float, float],
    filename: str,
    x_log_scale: bool = False,
    y_log_scale: bool = False,
    n_x_bins: int = 128,
    n_y_bins: int = 128,
    colormap: str = "Greys",
) -> None:
    """
    Density map generator.

    Creates a density or heat map of a distribution of points given their
    X/Y coordinates in a 2D space. The resulting image is generated in
    the specified file path.

    Args:
        x (np.ndarray): Horizontal coordinate values for the points.
        x_range (Tuple[float, float]): Horizontal range of values for the points.
        y (np.ndarray): Vertical coordinate values for the points.
        y_range (Tuple[float, float]): Vertical range of values for the points.
        filename (str): File path to generate the density map image.
        x_log_scale (bool): If True set the x-axis scale to log scale.
        y_log_scale (bool): If True set the y-axis scale to log scale.
        n_x_bins (int): Number of horizontal bins for the density map.
        n_y_bins (int): Number of vertical bins for the density map.
        colormap (str): Colormap to use for the image.
    """

    x_edges, y_edges = axs.log_scale_vs_linear_scale(
        x_range,
        y_range,
        x_log_scale,
        y_log_scale,
        n_x_bins,
        n_y_bins,
    )

    # Generating a 2D histogram that counts the number of objects contained
    # in each respective pixel; following the discrete count, we apply a
    # Gaussian filter to smear out the hard edges of the distribution to
    # improve the stability of the machine learning framework;
    # x (y) values are histogrammed along first (second) dimension.
    density, _, _ = np.histogram2d(x, y, bins=[x_edges, y_edges])
    density = scipy.ndimage.gaussian_filter(density, sigma=1)

    DPI = 512
    fig = plt.figure(dpi=DPI, frameon=False)
    fig.set_size_inches(n_x_bins / DPI, n_y_bins / DPI)
    ax = plt.Axes(fig, [0.0, 0.0, 1.0, 1.0])
    ax.set_axis_off()
    fig.add_axes(ax)

    # Generating a pseudocolor plot of the smeared out density distribution;
    # we transpose the array as pcolormesh is indexed starting from the lower
    # left , i.e., the column (row) index corresponds to the x (y) coordinate.
    ax.pcolormesh(x_edges, y_edges, density.T, cmap=colormap)
    ax.set_xlim(x_range[0], x_range[1])
    ax.set_ylim(y_range[0], y_range[1])

    if x_log_scale:
        ax.set_xscale("log")

    if y_log_scale:
        ax.set_yscale("log")

    fig.savefig(filename, dpi=DPI)
    plt.close(fig)

generate_density_matrix(x, x_range, y, y_range, filename, x_log_scale=False, y_log_scale=False, n_x_bins=128, n_y_bins=128)

Density matrix generator.

Creates a density matrix of a distribution of points given their X/Y coordinates in a 2D space. The resulting matrix is saved as .npy file.

Parameters:

Name Type Description Default
x ndarray

Horizontal coordinate values for the points.

required
x_range Tuple[float, float]

Horizontal range of values for the points.

required
y ndarray

Vertical coordinate values for the points.

required
y_range Tuple[float, float]

Vertical range of values for the points.

required
filename str

File path to generate the density matrix.

required
x_log_scale bool

If True set the x-axis scale to log scale.

False
y_log_scale bool

If True set the y-axis scale to log scale.

False
n_x_bins int

Number of horizontal bins for the density matrix.

128
n_y_bins int

Number of vertical bins for the density matrix.

128
Source code in mlpoppyns/generator/maps/maps2d_generator.py
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
def generate_density_matrix(
    x: np.ndarray,
    x_range: typing.Tuple[float, float],
    y: np.ndarray,
    y_range: typing.Tuple[float, float],
    filename: str,
    x_log_scale: bool = False,
    y_log_scale: bool = False,
    n_x_bins: int = 128,
    n_y_bins: int = 128,
) -> None:
    """
    Density matrix generator.

    Creates a density matrix of a distribution of points given their
    X/Y coordinates in a 2D space. The resulting matrix is saved as .npy file.

    Args:
        x (np.ndarray): Horizontal coordinate values for the points.
        x_range (Tuple[float, float]): Horizontal range of values for the points.
        y (np.ndarray): Vertical coordinate values for the points.
        y_range (Tuple[float, float]): Vertical range of values for the points.
        filename (str): File path to generate the density matrix.
        x_log_scale (bool): If True set the x-axis scale to log scale.
        y_log_scale (bool): If True set the y-axis scale to log scale.
        n_x_bins (int): Number of horizontal bins for the density matrix.
        n_y_bins (int): Number of vertical bins for the density matrix.
    """

    x_edges, y_edges = axs.log_scale_vs_linear_scale(
        x_range,
        y_range,
        x_log_scale,
        y_log_scale,
        n_x_bins,
        n_y_bins,
    )

    # Generating a 2D histogram that counts the number of objects contained
    # in each respective bin; x (y) values are histogrammed along first
    # (second) dimension; to avoid potential sharp edges, we apply a Gaussian filter.
    density, _, _ = np.histogram2d(x, y, bins=[x_edges, y_edges])
    density = scipy.ndimage.gaussian_filter(density, sigma=1)

    np.save(filename, density)

mlpoppyns.generator.maps.position_maps

Position maps generation routines.

Authors:

Alberto Garcia Garcia (garciagarcia@ice.csic.es)
Michele Ronchi (ronchi@ice.csic.es)
Vanessa Graber (graber@ice.csic.es)

generate_position_map(dataset_path, map_name, sample_number, map_type, x_positions, y_positions, x_resolution, y_resolution, position_maps_dictionary, x_limits=(-20.0, 20.0), y_limits=(-20.0, 20.0))

This method generates a discrete position map with great flexibility, the dimensions of the map can be chosen, the type (image or array) can also be decided, and the limits and resolution for it can be specified. As a result, a map with the specified filename and an extension determined by the chosen type is created as output.

The dictionary of position maps for the dataset is also updated with the generated example.

Parameters:

Name Type Description Default
dataset_path str

Path to the folder where the map will be created.

required
map_name str

Specific name for this map.

required
sample_number int

Number to suffix this map in the dataset.

required
map_type str

Type of map to generate (array or image).

required
x_positions array

Positions in the first axis (horizontal).

required
y_positions array

Positions in the second axis (vertical).

required
x_resolution int

Resolution in the horizontal axis.

required
y_resolution int

Resolution in the vertical axis.

required
position_maps_dictionary dict

Partial dictionary of position maps.

required
x_limits Tuple[float, float]

Limits of the horizontal axis.

(-20.0, 20.0)
y_limits Tuple[float, float]

Limits of the vertical axis.

(-20.0, 20.0)
Source code in mlpoppyns/generator/maps/position_maps.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def generate_position_map(
    dataset_path: str,
    map_name: str,
    sample_number: int,
    map_type: str,
    x_positions: np.array,
    y_positions: np.array,
    x_resolution: int,
    y_resolution: int,
    position_maps_dictionary: dict,
    x_limits: typing.Tuple[float, float] = (-20.0, 20.0),
    y_limits: typing.Tuple[float, float] = (-20.0, 20.0),
) -> None:
    """
    This method generates a discrete position map with great flexibility, the
    dimensions of the map can be chosen, the type (image or array) can also be
    decided, and the limits and resolution for it can be specified. As a result,
    a map with the specified filename and an extension determined by the chosen
    type is created as output.

    The dictionary of position maps for the dataset is also updated with the
    generated example.

    Args:
        dataset_path (str): Path to the folder where the map will be created.
        map_name (str): Specific name for this map.
        sample_number (int): Number to suffix this map in the dataset.
        map_type (str): Type of map to generate (array or image).
        x_positions (np.array): Positions in the first axis (horizontal).
        y_positions (np.array): Positions in the second axis (vertical).
        x_resolution (int): Resolution in the horizontal axis.
        y_resolution (int): Resolution in the vertical axis.
        position_maps_dictionary (dict): Partial dictionary of position maps.
        x_limits (Tuple[float, float]): Limits of the horizontal axis.
        y_limits (Tuple[float, float]): Limits of the vertical axis.
    """

    # Compose the final filename with the dataset path, the name for the map,
    # the current sample suffix and the appropriate extension.
    position_map_filename = "{}/{}_{}.{}".format(
        dataset_path, map_name, sample_number, extensions[map_type]
    )

    # Automagically select and call the appropriate generator depending on the
    # specified type for the map.
    position_map_generators[map_type](
        x_positions,
        x_limits,
        y_positions,
        y_limits,
        position_map_filename,
        n_x_bins=x_resolution,
        n_y_bins=y_resolution,
    )

    # Save density map file names into the partial dataset dictionary.
    position_maps_dictionary.setdefault("input:" + map_name, []).append(
        position_map_filename
    )

    log.info("{} generated...".format(position_map_filename))

mlpoppyns.generator.maps.ppdot_maps

P-Pdot maps generation routines.

Authors:

Alberto Garcia Garcia (garciagarcia@ice.csic.es)
Michele Ronchi (ronchi@ice.csic.es)
Vanessa Graber (graber@ice.csic.es)

generate_ppdot_map(dataset_path, map_name, sample_number, map_type, periods, period_derivatives, p_resolution, pdot_resolution, ppdot_maps_dictionary, p_limits=(0.001, 100.0), pdot_limits=(1e-21, 1e-09))

This method generates a discrete P-Pdot diagram map with great flexibility, the dimensions of the map can be chosen, the type (image or array) can also be decided, and the limits and resolution for it can be specified. As a result, a map with the specified filename and an extension determined by the chosen type is created as output.

The dictionary of P-Pdot maps for the dataset is also updated with the generated example.

Parameters:

Name Type Description Default
dataset_path str

Path to the folder where the map will be created.

required
map_name str

Specific name for this map.

required
sample_number int

Number to suffix this map in the dataset.

required
map_type str

Type of map to generate (array or image).

required
periods array

Spin periods of the neutron stars (horizontal axis).

required
period_derivatives array

Spin period derivatives of the neutron stars (vertical axis).

required
p_resolution int

Resolution in the horizontal axis.

required
pdot_resolution int

Resolution in the vertical axis.

required
ppdot_maps_dictionary dict

Partial dictionary of P-Pdot maps.

required
p_limits Tuple[float, float]

Limits of the horizontal axis.

(0.001, 100.0)
pdot_limits Tuple[float, float]

Limits of the vertical axis.

(1e-21, 1e-09)
Source code in mlpoppyns/generator/maps/ppdot_maps.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def generate_ppdot_map(
    dataset_path: str,
    map_name: str,
    sample_number: int,
    map_type: str,
    periods: np.array,
    period_derivatives: np.array,
    p_resolution: int,
    pdot_resolution: int,
    ppdot_maps_dictionary: dict,
    p_limits: typing.Tuple[float, float] = (0.001, 100.0),
    pdot_limits: typing.Tuple[float, float] = (1.0e-21, 1.0e-9),
) -> None:
    """
    This method generates a discrete P-Pdot diagram map with great flexibility, the
    dimensions of the map can be chosen, the type (image or array) can also be
    decided, and the limits and resolution for it can be specified. As a result,
    a map with the specified filename and an extension determined by the chosen
    type is created as output.

    The dictionary of P-Pdot maps for the dataset is also updated with the
    generated example.

    Args:
        dataset_path (str): Path to the folder where the map will be created.
        map_name (str): Specific name for this map.
        sample_number (int): Number to suffix this map in the dataset.
        map_type (str): Type of map to generate (array or image).
        periods (np.array): Spin periods of the neutron stars (horizontal axis).
        period_derivatives (np.array): Spin period derivatives of the neutron stars (vertical axis).
        p_resolution (int): Resolution in the horizontal axis.
        pdot_resolution (int): Resolution in the vertical axis.
        ppdot_maps_dictionary (dict): Partial dictionary of P-Pdot maps.
        p_limits (Tuple[float, float]): Limits of the horizontal axis.
        pdot_limits (Tuple[float, float]): Limits of the vertical axis.
    """

    # Compose the final filename with the dataset path, the name for the map,
    # the current sample suffix and the appropriate extension.
    ppdot_map_filename = "{}/{}_{}.{}".format(
        dataset_path, map_name, sample_number, extensions[map_type]
    )

    # Automagically select and call the appropriate generator depending on the
    # specified type for the map.
    ppdot_map_generators[map_type](
        periods,
        p_limits,
        period_derivatives,
        pdot_limits,
        ppdot_map_filename,
        x_log_scale=True,
        y_log_scale=True,
        n_x_bins=p_resolution,
        n_y_bins=pdot_resolution,
    )

    # Save density map file names into the partial dataset dictionary.
    ppdot_maps_dictionary.setdefault("input:" + map_name, []).append(
        ppdot_map_filename
    )

    log.info("{} generated...".format(ppdot_map_filename))

mlpoppyns.generator.maps.velocity_maps

Velocity maps generation routines.

Authors:

Alberto Garcia Garcia (garciagarcia@ice.csic.es)
Michele Ronchi (ronchi@ice.csic.es)
Vanessa Graber (graber@ice.csic.es)

generate_velocity_map(dataset_path, map_name, sample_number, map_type, x_positions, y_positions, velocities, x_resolution, y_resolution, velocity_maps_dictionary, x_limits=(-20.0, 20.0), y_limits=(-20.0, 20.0))

This method generates a discrete velocity map with great flexibility, the dimensions of the map can be chosen, the type (image or array) can also be decided, and the limits and resolution for it can be specified. As a result, a map with the specified filename and an extension determined by the chosen type is created as output.

The dictionary of velocity maps for the dataset is also updated with the generated example.

Parameters:

Name Type Description Default
dataset_path str

Path to the folder where the map will be created.

required
map_name str

Specific name for this map.

required
sample_number int

Number to suffix this map in the dataset.

required
map_type str

Type of map to generate (array or image).

required
x_positions array

Positions in the first axis (horizontal).

required
y_positions array

Positions in the second axis (vertical).

required
velocities array

Array of velocities to put in the map.

required
x_resolution int

Resolution in the horizontal axis.

required
y_resolution int

Resolution in the vertical axis.

required
velocity_maps_dictionary dict

Dictionary of velocity maps.

required
x_limits Tuple[float, float]

Limits of the horizontal axis.

(-20.0, 20.0)
y_limits Tuple[float, float]

Limits of the vertical axis.

(-20.0, 20.0)
Source code in mlpoppyns/generator/maps/velocity_maps.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def generate_velocity_map(
    dataset_path: str,
    map_name: str,
    sample_number: int,
    map_type: str,
    x_positions: np.array,
    y_positions: np.array,
    velocities: np.array,
    x_resolution: int,
    y_resolution: int,
    velocity_maps_dictionary: dict,
    x_limits: typing.Tuple[float, float] = (-20.0, 20.0),
    y_limits: typing.Tuple[float, float] = (-20.0, 20.0),
) -> None:
    """
    This method generates a discrete velocity map with great flexibility, the
    dimensions of the map can be chosen, the type (image or array) can also be
    decided, and the limits and resolution for it can be specified. As a result,
    a map with the specified filename and an extension determined by the chosen
    type is created as output.

    The dictionary of velocity maps for the dataset is also updated with the
    generated example.

    Args:
        dataset_path (str): Path to the folder where the map will be created.
        map_name (str): Specific name for this map.
        sample_number (int): Number to suffix this map in the dataset.
        map_type (str): Type of map to generate (array or image).
        x_positions (np.array): Positions in the first axis (horizontal).
        y_positions (np.array): Positions in the second axis (vertical).
        velocities (np.array): Array of velocities to put in the map.
        x_resolution (int): Resolution in the horizontal axis.
        y_resolution (int): Resolution in the vertical axis.
        velocity_maps_dictionary (dict): Dictionary of velocity maps.
        x_limits (Tuple[float, float]): Limits of the horizontal axis.
        y_limits (Tuple[float, float]): Limits of the vertical axis.
    """

    # Compose the final filename with the dataset path, the name for the map,
    # the current sample suffix and the appropriate extension.
    velocity_map_filename = "{}/{}_{}.{}".format(
        dataset_path, map_name, sample_number, extensions[map_type]
    )

    # Automagically select and call the appropriate generator depending on the
    # specified type for the map.
    velocity_map_generators[map_type](
        x_positions,
        x_limits,
        y_positions,
        y_limits,
        velocities,
        velocity_map_filename,
        n_x_bins=x_resolution,
        n_y_bins=y_resolution,
    )

    # Save velocity map file names into the partial dataset dictionary.
    velocity_maps_dictionary.setdefault("input:" + map_name, []).append(
        velocity_map_filename
    )

    log.info("{} generated...".format(velocity_map_filename))