Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix plot_singles and optimize memory #4566

Merged
merged 4 commits into from
Nov 15, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions bin/plotting/pycbc_plot_singles_vs_params
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,32 @@ opts = parser.parse_args()

logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)

snr_filter = '(self.snr>%f)' % (opts.min_snr) if opts.min_snr > 0. else None
filts = [f for f in [snr_filter, opts.filter_string] if f is not None]
data_mask = None
if opts.min_snr > 0:
with pycbc.io.HFile(opts.single_trig_file, 'r') as trig_file:
n_triggers_orig = trig_file[f'{opts.detector}/snr'].size
logging.info("Trigger file has %d triggers", n_triggers_orig)
logging.info('Generating trigger mask (on SNR)')
idx, _ = trig_file.select(
lambda snr: snr >= opts.min_snr,
f'{opts.detector}/snr',
return_indices=True
)
data_mask = np.zeros(n_triggers_orig, dtype=bool)
data_mask[idx] = True

filts = [f for f in [opts.filter_string] if f is not None]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the square bracket surrounding opts.filter_string intended here? This line does not make much sense to me (but I am quite tired right now…)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears the same as the existing code and join works with a (non-empty) list.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(could be a tuple as well, no difference to the outcome)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code does work, but I think having moved the SNR mask, the following two lines:

filts = [f for f in [opts.filter_string] if f is not None]
filter_func = ' & '.join(filts) if filts else None

basically reduce to:

filter_func = opts.filter_string

I'll just remove these lines and send opts.filter_string directly to the relevant function call. Then I'll merge unless there are objections.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I missed the point that this previously added the SNR cut to the filter option and now doesn't ..

filter_func = ' & '.join(filts) if filts else None

trigs = pycbc.io.SingleDetTriggers(opts.single_trig_file, opts.bank_file,
opts.veto_file, opts.segment_name, filter_func, opts.detector)
trigs = pycbc.io.SingleDetTriggers(
opts.single_trig_file,
opts.bank_file,
opts.veto_file,
opts.segment_name,
filter_func,
opts.detector,
premask=data_mask
)

x = getattr(trigs, opts.x_var)
y = getattr(trigs, opts.y_var)
Expand All @@ -108,10 +128,14 @@ y = y[mask]

hexbin_style = {
'gridsize': opts.grid_size,
# hexbin shows bins with *less* than mincnt as blank
'mincnt': 0,
'linewidths': 0.03
}

# In earlier versions mpl will try to take the max over bins with 0 triggers
# and fail, unless we tell it to leave these blank by setting mincnt
if matplotlib.__version__ < '3.8.1':
hexbin_style['mincnt'] = 0

if opts.log_x:
hexbin_style['xscale'] = 'log'
if opts.log_y:
Expand All @@ -137,7 +161,7 @@ elif opts.z_var in ranking.sngls_ranking_function_dict:
max_z = z.max() if opts.max_z is None else opts.max_z
if max_z / min_z > 10:
cb_style['ticks'] = LogLocator(subs=range(10))
hb = ax.hexbin(x, y, C=z, reduce_C_function=max, **hexbin_style)
hb = ax.hexbin(x, y, C=z, reduce_C_function=np.max, **hexbin_style)
fig.colorbar(hb, **cb_style)
else:
raise RuntimeError('z_var = %s is not recognized!' % (opts.z_var))
Expand Down
Loading