Skip to content

Conversation

@jl-wynen
Copy link
Member

Fixes #97

Comment on lines 239 to 242
).value
return OutputCalibrationData(
sc.DataArray(
sc.array(dims=['calibration'], values=[difc]),
Copy link
Member

Choose a reason for hiding this comment

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

Why are why taking the value (stripping units?) and put it back into a variable?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because the underlying CIF writer wants the coefficients as a data array (https://scipp.github.io/scippneutron/generated/modules/scippneutron.io.cif.CIF.html#scippneutron.io.cif.CIF.with_powder_calibration):

cal = sc.DataArray(
    sc.array(dims=['cal'], values=[3.4, 0.2]),
    coords={'power': sc.array(dims=['cal'], values=[0, 1])},
)

It is unfortunate that we have to remove the unit but that is how CIF wants the data.

We could instead keep a dict until the last moment where we need to convert to the above data array:

{
   0: sc.scalar(3.4, unit='us'),
   1: sc.scalar(0.2, unit='us/angstrom')
}

Would you prefer this?

Copy link
Member

Choose a reason for hiding this comment

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

I don't mind much, but can you explain what is going on in the comments?

Copy link
Member Author

Choose a reason for hiding this comment

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

Have a look at the new implementation.

def convert_to_calibrated_tof(
data: IofDspacing, calibration: OutputCalibrationData
) -> IofTof:
difc = calibration['power', sc.scalar(1)].data
Copy link
Member

Choose a reason for hiding this comment

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

Is the plan to have other powers as well? Is a DataArray the right data structure here? Maybe a DataGroup or simple dataclass would be more intuitive?

Copy link
Member Author

Choose a reason for hiding this comment

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

Is the plan to have other powers as well?

Ultimately, yes, as far as I know. @celinedurniak ?

See #106 (comment) why it is a data array. A data group would not work because the keys should be powers (int) and not names (str). A dataclass would have a fixed set of fields. Plus it would also need names instead of powers.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@jl-wynen your assumption is correct. There could be, for example, these terms to consider
difc * dspacing + difa * dispacing **2 + difb/dspacing +Zero

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok. Do we need to support them now or can this wait until we actually need them?

data: IofDspacing, calibration: OutputCalibrationData
) -> IofTof:
difc = calibration['power', sc.scalar(1)].data
difc.unit = 'us / angstrom'
Copy link
Member

Choose a reason for hiding this comment

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

We stripped the unit in the function above, and now we put it back?

Comment on lines 253 to 255
res = data.drop_coords('dspacing').bins.drop_coords('dspacing')
res.coords['tof'] = sc.to_unit(difc * data.coords['dspacing'], unit='us')
res.bins.coords['tof'] = sc.to_unit(difc * data.bins.coords['dspacing'], unit='us')
Copy link
Member

Choose a reason for hiding this comment

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

I presume you have considered using transform_coords but decided to do this manually?

Copy link
Member Author

Choose a reason for hiding this comment

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

I hadn't. Becuase the callibration coefficients are not coordinates. I could use a closure to capture them as a transformation function?

Copy link
Member

Choose a reason for hiding this comment

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

It would avoid having to manually handle dense and binned coords. Give it a try, but this is also fine?

Copy link
Member Author

Choose a reason for hiding this comment

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

See update

)


OutputCalibrationData = NewType('OutputCalibrationData', ScalarCalibrationData)
Copy link
Member

Choose a reason for hiding this comment

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

Can there be different ones? Why do we need the alias?

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess not

@jl-wynen jl-wynen merged commit 522f00f into main Nov 15, 2024
4 checks passed
@jl-wynen jl-wynen deleted the back-to-tof branch November 15, 2024 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Save data in TOF to CIF

4 participants