Skip to content

Add support for counter rotating choppers#47

Merged
nvaytet merged 16 commits intomainfrom
document-counter-rotating-choppers
Aug 10, 2023
Merged

Add support for counter rotating choppers#47
nvaytet merged 16 commits intomainfrom
document-counter-rotating-choppers

Conversation

@nvaytet
Copy link
Copy Markdown
Member

@nvaytet nvaytet commented Aug 8, 2023

Add support for counter-rotating choppers via the direction='anticlockwise' argument.

After discussions in #44 , I decided that using a classmethod for this is not so useful because the computation of the times when the windows are open/closed are not done upon construction.
They depend on how many rotations need to be performed.

I think the easiest is to have the direction argument, set to clockwise by default so that current users do not have to change all their notebooks.

We will most probably still use a classmethod to create a chopper from nexus parameters.

Supersedes #44

Fix #5

@nvaytet nvaytet requested a review from SimonHeybrock August 8, 2023 15:59
Comment thread src/tof/chopper.py Outdated
Comment on lines +29 to +30
The phase of the chopper. The phase offset is applied in the opposite direction
to the chopper rotation direction.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you clarify this a bit more? Do I read this correctly that the meaning of phase changes when we change the rotation direction?

Comment thread src/tof/chopper.py Outdated
Comment thread src/tof/chopper.py Outdated
phases = sc.arange(uuid.uuid4().hex, -1, nrot) * two_pi + self.phase.to(
unit='rad'
)
) * (int(self.direction == 'clockwise') * 2 - 1)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Like this, any typos in direction will mean "anticlockwise". How about either validating the direction, or using a dict:

Suggested change
) * (int(self.direction == 'clockwise') * 2 - 1)
) * {'clockwise': 1, 'anticlockwise': -1}[self.direction]

Copy link
Copy Markdown
Member Author

@nvaytet nvaytet Aug 10, 2023

Choose a reason for hiding this comment

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

Yeah it's partly because I wasn't sure about the naming. I don't know if anticlockwise is more intuitive than counter-clockwise.
Also, is it anticlockwise or anti-clockwise?

The best would probably to use an enum, but I don't know if that makes it impractical to use?

chopper = tof.Chopper(
    frequency=14*Hz,
    direction=tof.AntiClockwise,
    ...
    )

Or maybe we should have two classes? ChopperClockwise and ChopperAnticlockwise?
A bit verbose but hard to get wrong?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In the end, I used

Clockwise = Literal["clockwise"]
AntiClockwise = Literal["anticlockwise"]

to define the two options. It seems to work fine, but I am not sure if there is anything bad/dangerous about doing it like that.

nvaytet and others added 4 commits August 10, 2023 08:20
Comment thread src/tof/chopper.py Outdated
distance: sc.Variable,
phase: sc.Variable,
direction: Literal['clockwise', 'anticlockwise'] = 'clockwise',
direction: Union[Clockwise, AntiClockwise] = Clockwise,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Very unusual, using a union of Literal...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I know, I didn't even know that it would work.
Do you have a more common suggestion?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is using NewType better?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Btw, using Literal was a suggestion by Copilot... ;-)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why don't you use a single literal, Literal['clockwise', 'anticlockwise']? Or an enum?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, I went with Enum in the end.

Comment thread src/tof/chopper.py Outdated
phases = sc.arange(uuid.uuid4().hex, -1, nrot) * two_pi + self.phase.to(
unit='rad'
) * (int(self.direction == 'clockwise') * 2 - 1)
) * (int(self.direction is Clockwise) * 2 - 1)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So if I pass clokcwise it will still be interpreted as AntiClockwise?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes, i need to change

@nvaytet nvaytet merged commit 1959736 into main Aug 10, 2023
@nvaytet nvaytet deleted the document-counter-rotating-choppers branch August 10, 2023 09:58
@nvaytet nvaytet mentioned this pull request Aug 10, 2023
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.

Handle counter-rotating choppers

2 participants