Skip to content

Commit e8bd939

Browse files
committed
Close #705 Negative axis 0 index in distribution plot error
1 parent e07a97c commit e8bd939

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

providentia/plot_aux.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,8 @@ def kde_fft(xin, gridsize=1024, extents=None, weights=None, adjust=1., bw='scott
337337
else:
338338
weights = np.squeeze(np.asarray(weights))
339339
if weights.size != x.size:
340-
raise ValueError('Input weights must be an array of the same size as xin!')
340+
error = 'Input weights must be an array of the same size as xin. '
341+
return error
341342

342343
# Optimize gridsize ------------------------------------------------------
343344
# Make grid and discretize the data and round it to the next power of 2
@@ -363,7 +364,11 @@ def kde_fft(xin, gridsize=1024, extents=None, weights=None, adjust=1., bw='scott
363364
# Next, make a histogram of x
364365
# Exploit a sparse coo_matrix avoiding np.histogram due to excessive
365366
# memory usage with many points
366-
grid = coo_matrix((weights, xyi), shape=(nx, 1)).toarray()
367+
try:
368+
grid = coo_matrix((weights, xyi), shape=(nx, 1)).toarray()
369+
except ValueError:
370+
error = 'Too many zeros. '
371+
return error
367372

368373
# Kernel Preliminary Calculations ---------------------------------------
369374
std_x = np.std(xyi[0])
@@ -380,7 +385,10 @@ def kde_fft(xin, gridsize=1024, extents=None, weights=None, adjust=1., bw='scott
380385

381386
# If bandwidth is 0, skip plot for current data label
382387
if kern_nx == 0:
383-
return None
388+
error = 'The kernel bandwidth is 0. '
389+
error += 'To change the bandwith, we recommend increasing the number of '
390+
error += 'pdf_min_samples in the plot characteristics settings files. '
391+
return error
384392

385393
# Then evaluate the gaussian function on the kernel grid
386394
kernel = np.reshape(gaussian(kern_nx, bw_factor * std_x), (kern_nx, 1))

providentia/plotting.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,11 +1148,9 @@ def make_distribution(self, relevant_axis, networkspeci, data_labels, plot_chara
11481148
#PDF_fit = FFTKDE(kernel='gaussian', bw='scott').fit(kde_data_obs)
11491149
#PDF_obs_sampled = PDF_fit.evaluate(x_grid)
11501150

1151-
if PDF_obs_sampled is None:
1152-
msg = 'The kernel bandwidth is 0 for {}. '.format(data_label)
1153-
msg += 'The distribution plot will be created and not include data for this label. '
1154-
msg += 'To change the bandwith, we recommend increasing the number of '
1155-
msg += 'pdf_min_samples in the plot characteristics settings files.'
1151+
if isinstance(PDF_obs_sampled, str):
1152+
msg = PDF_obs_sampled
1153+
msg += f'The distribution plot will be created and not include data for this label ({data_label}). '
11561154
show_message(self.read_instance, msg)
11571155
continue
11581156

@@ -1173,11 +1171,9 @@ def make_distribution(self, relevant_axis, networkspeci, data_labels, plot_chara
11731171
continue
11741172
# calculate PDF
11751173
PDF_model_sampled = kde_fft(kde_data_model, xgrid=x_grid)
1176-
if PDF_model_sampled is None:
1177-
msg = 'The kernel bandwidth is 0 for {}. '.format(data_label)
1178-
msg += 'The distribution plot will be created and not include data for this label. '
1179-
msg += 'To change the bandwith, we recommend increasing the number of '
1180-
msg += 'pdf_min_samples in the plot characteristics settings files.'
1174+
if isinstance(PDF_model_sampled, str):
1175+
msg = PDF_model_sampled
1176+
msg += f'The distribution plot will be created and not include data for this label ({data_label}). '
11811177
show_message(self.read_instance, msg)
11821178
continue
11831179

@@ -1243,11 +1239,9 @@ def make_distribution(self, relevant_axis, networkspeci, data_labels, plot_chara
12431239
continue
12441240
else:
12451241
PDF_sampled = kde_fft(kde_data, xgrid=x_grid)
1246-
if PDF_sampled is None:
1247-
msg = 'The kernel bandwidth is 0 for {}. '.format(data_label)
1248-
msg += 'The distribution plot will be created and not include data for this label. '
1249-
msg += 'To change the bandwith, we recommend increasing the number of '
1250-
msg += 'pdf_min_samples in the plot characteristics settings files.'
1242+
if isinstance(PDF_sampled, str):
1243+
msg = PDF_sampled
1244+
msg += f'The distribution plot will be created and not include data for this label ({data_label}). '
12511245
show_message(self.read_instance, msg)
12521246
continue
12531247

0 commit comments

Comments
 (0)