@@ -328,84 +328,82 @@ def get_atom_selection(*, atoms_data: abins.AtomsData, selection: list) -> Tuple
328
328
all_atms_smbls = list (set ([atoms_data [atom_index ]["symbol" ] for atom_index in range (num_atoms )]))
329
329
all_atms_smbls .sort ()
330
330
331
+ # Exit early if nothing was selected: default is to return no numbers, all symbols
331
332
if len (selection ) == 0 : # case: all atoms
332
- atom_symbols = all_atms_smbls
333
- atom_numbers = []
334
- else : # case selected atoms
335
- # Specific atoms are identified with prefix and integer index, e.g 'atom_5'. Other items are element symbols
336
- # A regular expression match is used to make the underscore separator optional and check the index format
337
-
338
- # Acceptable formats for index: atom_1 atom1 1
339
- numbered_atom_test = re .compile (
340
- f"""^ # No arbitrary prefixes
341
- ({ ATOM_PREFIX } )? # optional ATOM_PREFIX (i.e. "atom")
342
- _? # optional underscore
343
- (?P<index>[0-9]+) # capture digits as "index"
344
- $ # No suffix
345
- """ ,
346
- re .VERBOSE ,
347
- )
348
- atom_numbers = [int (match .group ("index" )) for item in selection if (match := numbered_atom_test .match (item ))]
349
-
350
- # Acceptable formats for range: 1-2
351
- atom_range_test = re .compile (
352
- """^ # No prefix
353
- (?P<start>\\ d+) # Capture starting index
354
- (-|\\ .\\ .)
355
- # range indicated with "-" or ".."
356
- (?P<end>\\ d+) # Capture ending index
357
- $ # No suffix
358
- """ ,
359
- re .VERBOSE ,
360
- )
361
- atom_ranges = [
362
- (int (match .group ("start" )), int (match .group ("end" ))) for item in selection if (match := atom_range_test .match (item ))
363
- ]
364
-
365
- # Acceptable formats for symbol: Ca
366
- element_symbol_test = re .compile (
367
- f"""^(?!{ ATOM_PREFIX } ) # Must not start with ATOM_PREFIX (i.e. "atom")
368
- [a-zA-Z]+$ # Must contain only letters
369
- """ ,
370
- re .VERBOSE ,
333
+ return [], all_atms_smbls
334
+
335
+ # Specific atoms are identified with prefix and integer index, e.g 'atom_5'. Other items are element symbols
336
+ # A regular expression match is used to make the underscore separator optional and check the index format
337
+
338
+ # Acceptable formats for index: atom_1 atom1 1
339
+ numbered_atom_test = re .compile (
340
+ f"""^ # No arbitrary prefixes
341
+ ({ ATOM_PREFIX } )? # optional ATOM_PREFIX (i.e. "atom")
342
+ _? # optional underscore
343
+ (?P<index>[0-9]+) # capture digits as "index"
344
+ $ # No suffix
345
+ """ ,
346
+ re .VERBOSE ,
347
+ )
348
+ atom_numbers = [int (match .group ("index" )) for item in selection if (match := numbered_atom_test .match (item ))]
349
+
350
+ # Acceptable formats for range: 1-2
351
+ atom_range_test = re .compile (
352
+ """^ # No prefix
353
+ (?P<start>\\ d+) # Capture starting index
354
+ (-|\\ .\\ .)
355
+ # range indicated with "-" or ".."
356
+ (?P<end>\\ d+) # Capture ending index
357
+ $ # No suffix
358
+ """ ,
359
+ re .VERBOSE ,
360
+ )
361
+ atom_ranges = [(int (match .group ("start" )), int (match .group ("end" ))) for item in selection if (match := atom_range_test .match (item ))]
362
+
363
+ # Acceptable formats for symbol: Ca
364
+ element_symbol_test = re .compile (
365
+ f"""^(?!{ ATOM_PREFIX } ) # Must not start with ATOM_PREFIX (i.e. "atom")
366
+ [a-zA-Z]+$ # Must contain only letters
367
+ """ ,
368
+ re .VERBOSE ,
369
+ )
370
+ atom_symbols = [item for item in selection if element_symbol_test .match (item )]
371
+
372
+ if len (atom_numbers ) != len (set (atom_numbers )):
373
+ raise ValueError (
374
+ "User atom selection (by number) contains repeated atom. This is not permitted as Abins"
375
+ " cannot create multiple workspaces with the same name."
371
376
)
372
- atom_symbols = [item for item in selection if element_symbol_test .match (item )]
373
377
374
- if len (atom_numbers ) != len (set (atom_numbers )):
378
+ for atom_number in atom_numbers :
379
+ if atom_number < 1 or atom_number > num_atoms :
375
380
raise ValueError (
376
- "User atom selection (by number) contains repeated atom. This is not permitted as Abins"
377
- " cannot create multiple workspaces with the same name."
381
+ "Invalid user atom selection (by number) '%s%s': out of range (%s - %s)" % (ATOM_PREFIX , atom_number , 1 , num_atoms )
378
382
)
379
383
380
- for atom_number in atom_numbers :
381
- if atom_number < 1 or atom_number > num_atoms :
382
- raise ValueError (
383
- "Invalid user atom selection (by number) '%s%s': out of range (%s - %s)" % (ATOM_PREFIX , atom_number , 1 , num_atoms )
384
- )
384
+ for atom_symbol in atom_symbols :
385
+ if atom_symbol not in all_atms_smbls :
386
+ raise ValueError ("User defined atom selection (by element) '%s': not present in the system." % atom_symbol )
385
387
386
- for atom_symbol in atom_symbols :
387
- if atom_symbol not in all_atms_smbls :
388
- raise ValueError ("User defined atom selection (by element) '%s': not present in the system." % atom_symbol )
389
-
390
- if len (atom_symbols ) != len (set (atom_symbols )): # only different types
391
- raise ValueError (
392
- "User atom selection (by symbol) contains repeated species. This is not permitted as "
393
- "Abins cannot create multiple workspaces with the same name."
394
- )
388
+ if len (atom_symbols ) != len (set (atom_symbols )): # only different types
389
+ raise ValueError (
390
+ "User atom selection (by symbol) contains repeated species. This is not permitted as "
391
+ "Abins cannot create multiple workspaces with the same name."
392
+ )
395
393
396
- # Final sanity check that everything in "atoms" field was understood
397
- if len (atom_symbols ) + len (atom_numbers ) + len (atom_ranges ) < len (selection ):
398
- elements_report = " Symbols: " + ", " .join (atom_symbols ) if atom_symbols else ""
399
- numbers_report = " Numbers: " + ", " .join (atom_numbers ) if atom_numbers else ""
400
- ranges_report = " Ranges: " + ", " .join (f"{ start } -{ end } " for start , end in atom_ranges ) if atom_ranges else ""
401
- raise ValueError (
402
- "Not all user atom selections ('atoms' option) were understood." + elements_report + numbers_report + ranges_report
403
- )
394
+ # Final sanity check that everything in "atoms" field was understood
395
+ if len (atom_symbols ) + len (atom_numbers ) + len (atom_ranges ) < len (selection ):
396
+ elements_report = " Symbols: " + ", " .join (atom_symbols ) if atom_symbols else ""
397
+ numbers_report = " Numbers: " + ", " .join (atom_numbers ) if atom_numbers else ""
398
+ ranges_report = " Ranges: " + ", " .join (f"{ start } -{ end } " for start , end in atom_ranges ) if atom_ranges else ""
399
+ raise ValueError (
400
+ "Not all user atom selections ('atoms' option) were understood." + elements_report + numbers_report + ranges_report
401
+ )
404
402
405
- for start , end in atom_ranges :
406
- if start > end :
407
- start , end = end , start
408
- atom_numbers = atom_numbers + list (range (start , end + 1 ))
403
+ for start , end in atom_ranges :
404
+ if start > end :
405
+ start , end = end , start
406
+ atom_numbers = atom_numbers + list (range (start , end + 1 ))
409
407
410
408
return sorted (atom_numbers ), atom_symbols
411
409
0 commit comments