ColwynGulliford / distgen

Particle distribution generator
https://colwyngulliford.github.io/distgen/
Apache License 2.0
12 stars 8 forks source link

Assigning numpy array directly #7

Closed jacquelinegarrahan closed 2 years ago

jacquelinegarrahan commented 2 years ago

Hi Colwyn,

I'm wondering if there is a way to directly assign a numpy array as the xy distribution on the Generator object rather than loading from a file. I see this image2d dytpe for get_dist here. Am I able to assign this directly using a key?

Thanks!

ColwynGulliford commented 2 years ago

Hi Jacqueline,

If I understand correctly, you have a NumPy array representing rho(x,y) the 2d distribution for particles, not a file?

There is some support for that already, please see distgen/examples/example_dists.ipynb -> 2d image.

Screen Shot 2022-03-11 at 2 01 18 PM

Here I load an image using a distgen helper function. That should create the 2d NumPy array needed (the other objects returned there aren't used). Then the xy_distribution is defined in the dictionary input:

'xy_dist':{ 'type' : 'image2d', 'min_x': {'value':-2.0, 'units':'mm'}, 'max_x': {'value':+2.0, 'units':'mm'}, 'min_y': {'value':-2.0, 'units':'mm'}, 'max_y': {'value':+2.0, 'units':'mm'}, 'P':Pxy}

The min{x,y} and max{x,y} define the length scale corresponding to the data in Pxy. With that input, create a Generator object and print it:

Screen Shot 2022-03-11 at 2 08 01 PM

The yaml conversation isn't so pretty for the NumPy array, but basically I believe what you need is stored in gen['xy_dist:P'] (short form for gen['xy_dist]['P']), and you should be able to update that dict item as you see fit.

Can you try that?

jacquelinegarrahan commented 2 years ago

I've also noticed that if file isn't defined in the yaml :


xy_dist:
  type: file2d

And subsequently assigned using a key:

G["xy_dist:file"]

The following error occurs during run:

Traceback (most recent call last):
  File "/Users/jgarra/miniconda3/envs/lume-orchestration-demo/lib/python3.9/site-packages/prefect/engine/task_runner.py", line 876, in get_task_run_state
    value = prefect.utilities.executors.run_task_with_timeout(
  File "/Users/jgarra/miniconda3/envs/lume-orchestration-demo/lib/python3.9/site-packages/prefect/utilities/executors.py", line 467, in run_task_with_timeout
    return task.run(*args, **kwargs)  # type: ignore
  File "/Users/jgarra/sandbox/distgen-impact-cu-inj-ex/distgen_impact_cu_inj_ex/flow/flow2.py", line 72, in run_distgen
    output_variables = distgen_model.evaluate(distgen_input_variables)
  File "/Users/jgarra/sandbox/distgen-impact-cu-inj-ex/distgen_impact_cu_inj_ex/model.py", line 76, in evaluate
    self._G.run()
  File "/Users/jgarra/miniconda3/envs/lume-orchestration-demo/lib/python3.9/site-packages/distgen/generator.py", line 412, in run
    beam = self.beam()
  File "/Users/jgarra/miniconda3/envs/lume-orchestration-demo/lib/python3.9/site-packages/distgen/generator.py", line 315, in beam
    dist = get_dist('xy', dist_params['xy'], verbose=verbose)
  File "/Users/jgarra/miniconda3/envs/lume-orchestration-demo/lib/python3.9/site-packages/distgen/dist.py", line 106, in get_dist
    dist = File2d("x","y",verbose=verbose, **params)
  File "/Users/jgarra/miniconda3/envs/lume-orchestration-demo/lib/python3.9/site-packages/distgen/dist.py", line 2375, in __init__
    self.check_inputs(params)
  File "/Users/jgarra/miniconda3/envs/lume-orchestration-demo/lib/python3.9/site-packages/distgen/dist.py", line 139, in check_inputs
    assert req in params, f'Required input parameter {req} to {self.__class__.__name__}.__init__(**kwargs) was not found.'
AssertionError: Required input parameter file to File2d.__init__(**kwargs) was not found.

This doesn't occur if a dummy file is assigned in the yaml.

jacquelinegarrahan commented 2 years ago

@ColwynGulliford Thanks for your message- that structure is exactly what I was looking for. I'll test it out now

ColwynGulliford commented 2 years ago

No problem, I was just looking a little further and it looks like the Pxy object is a Pint Quantity (NumPy array + units):

Screen Shot 2022-03-11 at 2 20 25 PM

If so, let me see how best to set it...

ColwynGulliford commented 2 years ago

Ok, I believe that it should work as you require. Here's a distribution I made from a file:

Screen Shot 2022-03-11 at 2 32 56 PM

Now instead of the file I used image2d as described above, that is I load the distribution first to a NumPy array and then create the Generator. I then manipulate the NumPy array, and stretch the x scale by setting

gen['xy_dist:min_x:value'], gen['xy_dist:max_x:value'], as well as setting gen['xy_dist:P']:

Screen Shot 2022-03-11 at 2 36 30 PM

So I think it works as needed - nice to have tried this, I hadn't used it quite that way before!

jacquelinegarrahan commented 2 years ago

Thanks for your help @ColwynGulliford ! This works well for my application :)

ColwynGulliford commented 2 years ago

Glad to hear!