Skip to content

Add polarimeter#127

Closed
SoroushHoseini wants to merge 8 commits into
masterfrom
add_polarimeter
Closed

Add polarimeter#127
SoroushHoseini wants to merge 8 commits into
masterfrom
add_polarimeter

Conversation

@SoroushHoseini

@SoroushHoseini SoroushHoseini commented Nov 6, 2025

Copy link
Copy Markdown
Contributor

Description

Add polarimeter pax1000IR2

Motivation and Context

Need driver for polarimeter

How have these changes been tested?

Tested each function with device to make sure it works and reads correct values.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My code follows the code style of this project.
  • My changes require changes to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests pass.

Comment thread src/pqnstack/pqn/drivers/thorlabs_polarimeter.py
Comment thread src/pqnstack/pqn/drivers/thorlabs_polarimeter.py Outdated
Comment thread src/pqnstack/pqn/drivers/thorlabs_polarimeter.py Outdated
Comment on lines +45 to +51
@dataclass(slots=True)
class PAX1000IR2(Instrument):
name: str
desc: str
hw_address: str
parameters: set[str] = field(default_factory=set)
operations: dict[str, Any] = field(default_factory=dict)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As with other drivers, use an intermediate protocol between Instrument and the wrapper.

Suggested change
@dataclass(slots=True)
class PAX1000IR2(Instrument):
name: str
desc: str
hw_address: str
parameters: set[str] = field(default_factory=set)
operations: dict[str, Any] = field(default_factory=dict)
@runtime_checkable
@dataclass(slots=True)
class ThorlabsPolarimeterInstrument(Instrument, Protocol):
def __post_init__(self) -> None:
self.operations["read"] = self.read
self.operations["set_wavelength"] = self.set_wavelength
@log_operation
def read(self) -> ThorlabsPolarimeterInfo: ...
@log_operation
def set_wavelength(self, value: float) -> None: ...
@dataclass(slots=True)
class PAX1000IR2(ThorlabsPolarimeterInstrument):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

To be honest, seems superfluous for one off drivers like this. If in the future we find there is a lot of shared utility like with motors, we can make a parent class for the group, else no point in my opinion

Comment thread src/pqnstack/pqn/drivers/thorlabs_polarimeter.py Outdated
msg = f"VISA backend not available: {exc}"
raise RuntimeError(msg) from exc

resource_name = self.hw_address or self._discover_resource()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Resource discovery feels beyond the scope of a slim wrapper. Users can provide a hw_address with the other configuration just like every other Instrument.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I find it quite hard to connect to instruments via visa, I'd prefer this method whenever visa is absolutely necessary, though I prefer using USBTMC when possible.

Comment thread src/pqnstack/pqn/drivers/thorlabs_polarimeter.py
Comment on lines +232 to +265
@log_operation
def read(self) -> dict[str, float]:
if self._instr is None:
msg = "Start the device first."
raise DeviceNotStartedError(msg)
raw_reply = self._read_and_write(QRY_LATEST, expect_response=True)

token_strs = [p for p in raw_reply.replace(";", ",").split(",") if p]
parsed_values: list[float | str] = []
for token_str in token_strs:
try:
parsed_values.append(float(token_str))
except (ValueError, TypeError):
parsed_values.append(token_str)

def get_float_at(index: int) -> float:
try:
value = parsed_values[index]
return float(value) if isinstance(value, (float, int)) else float(str(value))
except (ValueError, TypeError, IndexError):
return float("nan")

self._last_theta_deg = get_float_at(9)
self._last_eta_deg = get_float_at(10)
self._last_dop = get_float_at(11)
self._last_power_w = get_float_at(12)

return {
"pax_theta_deg": self._last_theta_deg,
"pax_eta_deg": self._last_eta_deg,
"pax_dop": self._last_dop,
"pax_power_w": self._last_power_w,
"pax_wavelength_nm": self._wavelength_nm_cache,
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consider my original implementation:

def read(self) -> ThorlabsPolarimeterInfo:
        # See https://www.thorlabs.com/_sd.cfm?fileName=MTN007790-D04.pdf&partumber=PAX1000IR2
        data_keys = (
            "revs",
            "timestamp",
            "paxOpMode",
            "paxFlags",
            "paxTIARange",
            "adcMin",
            "adcMax",
            "revTime",
            "misAdj",
            "theta",
            "eta",
            "DOP",
            "Ptotal",
        )
        data = self._device.query("SENS:DATA:LAT?").strip().split(",")

        hw_status: dict[str, str] = dict(zip(data_keys, data, strict=True))
        theta, eta, dop, power = (float(val) for val in data[9:13])

        wavelength = float(self._device.query("SENS:CORR:WAV?"))
        return ThorlabsPolarimeterInfo(
            name=self.name,
            desc=self.desc,
            hw_address=self.hw_address,
            hw_status=hw_status,
            theta=theta,
            eta=eta,
            dop=dop,
            power=power,
            wavelength=wavelength,
        )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, that was what I did originally. Most of that info returned is useless

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Specifically in the context of what I want out of the read. And I suppose not most, more like half.

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