Skip to content

Added write method to _Config class.#85

Merged
claudiodsf merged 8 commits intoSeismicSource:v2-rebase-mainfrom
krisvanneste:v2-write_config
Mar 3, 2026
Merged

Added write method to _Config class.#85
claudiodsf merged 8 commits intoSeismicSource:v2-rebase-mainfrom
krisvanneste:v2-write_config

Conversation

@krisvanneste
Copy link
Collaborator

Claudio,

I think it would be handy to save the configuration made in an interactive session, so I added a write method to the _Config class. Can you check it?

@krisvanneste
Copy link
Collaborator Author

Claudio,
I'm making a new notebook using one of the examples in sourcespec_testruns.
I'm using test_CDSA as an example.
First, I updated sourcespec.conf to sourcespec2.
I then read the configuration file interactively, along with the trace data, quakeml data and inventory. However, when I start the inversion, this results in the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[13], line 2
      1 config.options.outdir = None
----> 2 result = ssp_run(st, inventory, event, picks)

File E:\Home\_kris\Python\cloned_repos\sourcespec2\sourcespec2\source_spec.py:110, in ssp_run(st, inventory, ssp_event, picks, allow_exit)
    108 print('Augmenting event')
    109 augment_event(ssp_event)
--> 110 print(ssp_event.hypocenter.vp)
    111 augment_traces(st, inventory, ssp_event, picks)
    113 # Deconvolve, filter, cut traces:

AttributeError: 'SSPHypocenter' object has no attribute 'vp'

Investigating this issue, I discover that this is because vp_source and similar parameters are strings, which results in a silent error in the _hypo_vel function.
It turns out I should have ran a validation of the config object after reading the file.
However, when I do this, I get a new error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[4], line 2
      1 cfg_file = os.path.join(run_folder, 'source_spec.conf')
----> 2 config.read(cfg_file)
      3 config.running_from_command_line = False

File E:\Home\_kris\Python\cloned_repos\sourcespec2\sourcespec2\setup\config.py:638, in _Config.read(self, config_file)
    636 config_obj = read_config_file(config_file)
    637 self.update(config_obj.dict())
--> 638 self.validate()

File E:\Home\_kris\Python\cloned_repos\sourcespec2\sourcespec2\setup\config.py:365, in _Config.validate(self)
    363     for entry in [e for e in test if not test[e]]:
    364         msg += f'\nInvalid value for "{entry}": "{config_obj[entry]}"'
--> 365     raise ValueError(msg)
    366 if not test:
    367     raise ValueError('No configuration value present!')

ValueError: 
Invalid value for "mis_oriented_channels": "Z, 1, 2"
Invalid value for "epi_dist_ranges": "0.0, 300.0"
Invalid value for "spectral_sn_freq_range": "0.1, 2.0"
Invalid value for "t_star_min_max": "0.0, 0.1"
Invalid value for "Er_freq_range": "noise, noise"

I don't understand this one. The values for these parameters seem to be in agreement with the specs.
Any ideas?

Here is the configuration file:
source_spec.conf.txt

@krisvanneste
Copy link
Collaborator Author

OK, I solved it by splitting strings containing lists into string lists before validating.
Maybe I'm missing something, but this should probably be done in setup.configobj_helpers.read_config_file.

@claudiodsf
Copy link
Member

Thanks Kris, I fixed a problem which prevented running tests on pull requests against v2 and force-pushed.

Will come back later today, when the tests are done (and hopefully pass) 😉

@krisvanneste
Copy link
Collaborator Author

Does it work now?
If you have time, could you also have a look at my PR's for sourcespec_testruns?

@claudiodsf
Copy link
Member

Hi Kris,

Yes the tests are passing now. But I would like to make some improvements and simplify the code from the CLI module, based on the .read() and .write() methods of the Config() class.

Hopefully, I'll have time on Monday for this 😉

@krisvanneste
Copy link
Collaborator Author

Hi Kris,

Yes the tests are passing now. But I would like to make some improvements and simplify the code from the CLI module, based on the .read() and .write() methods of the Config() class.

Hopefully, I'll have time on Monday for this 😉

OK, that's fine.
Once this will be done, we will be able to make some progress with the notebooks.
Have a nice weekend!

This allows using it safely from a non-CLI context, such as a notebook
The write() method of the _Config class has been optimized to avoid
unnecessary conversions and to handle the free_surface_amplification
parameter.
@claudiodsf
Copy link
Member

Hi Kris,

I’ve just pushed a few commits to optimize your excellent ideas.

We actually don’t need to convert lists back to strings on .write(), since ConfigObj already handles this for us. The only remaining exception is free_surface_amplification, which I’m not entirely happy with yet; I’m hoping to come up with a cleaner solution in the next few days.

Let me know when you'll have a chance to test these changes. Thanks!

@krisvanneste
Copy link
Collaborator Author

I rebased the branch and ran my new notebook. Everything seems to work.
When writing configuration to a file, I only notice something strange in the free_surface_amplification line:

free_surface_amplification = *: 2.0

But this is probably the problem you were referring to.

@claudiodsf
Copy link
Member

When writing configuration to a file, I only notice something strange in the free_surface_amplification line:

free_surface_amplification = *: 2.0

This is the syntax quirk with free_surface_amplification:

 free_surface_amplification = 2.0

is equivalent to:

 free_surface_amplification = *: 2.0

i.e., all SNCL codes (*) get a value of 2.0. When validating the config, the more verbose syntax is used.

I don't like this too much, but I'm short of ideas for the moment... It's on my TODO list!

Anyway, merging this PR. Thanks for the contribution!

@claudiodsf claudiodsf merged commit 2a98cb3 into SeismicSource:v2-rebase-main Mar 3, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants