\section{Introduction}
\label{sec:introduction}
\IEEEPARstart{S}{egmentation} and/or classification of medical images plays a vital role in disease diagnosis, therapy planning and follow-up tracking of therapy efficiency. Therefore, segmentation/classification by using deep learning (DL) models, i.e. deep neural networks (NNs), is steadily in focus in biomedical image analysis \cite{shen2017deep,litjens2016deep,de2018clinically,mishra2022data,gu2021net,gunesli2020attentionboost,li2021pathal,yang2020guided,zhu2021hard,guo2021learn}, to cite a few. Thus, a constant need for performance improvement results in many specialized and extremely complex DL models, whereat some of them are cited above. Their decision-making process is often hard to explain and sometimes even harder to interpret in terms of features understandable to medical experts \cite{rudin2019stop,tjoa2020survey}. When high-stakes judgments must be made, it is critical to comprehend the reasons behind the decision-making process.
As a result, rather than explaining black box models, it is preferable to create interpretable models  \cite{rudin2019stop}. Furthermore, there is currently a scarcity of expert-annotated datasets in many medical imaging modalities. For sophisticated models like convolutional NNs  (CNNs), this causes a generalization challenge  \cite{litjens2016deep,veta2014breast,qiu2018deep}. This issue is also present in histopathological image segmentation of intraoperatively collected frozen sections \cite{komura2018machine}. One reason is that sample collection during surgery is distracting. Also, another reason is highly time-consuming effort for pathologists to label cancerous pixels \cite{sitnik2021cocahis}.

\begin{figure*}[ht]
\centering
\includegraphics[width=0.9\textwidth]{figures/LEFM-Net.png}
\caption{Proposed framework comprising the slightly modified existing deep networks such as UNet, UNet++, DeepLabv3+, and MA-net, embedded in a space induced by the learnable explicit feature map (LEFM) layer. Increased number of learnable weights in the LEFM layer is represented as $\mathbf{a}_{m}$. Additional deep network's weights are introduced through modification to accept expanded features. $\phi_{m}(\mathbf{X})$ is defined as Hadamard product ($\otimes$) of: $\psi_{m}(\mathbf{x_t})$ - the spatial location dependent map of monomials of the input data space features, and $\mathbf{a}_{m}$ - the spatial location invariant vector of coefficients. The first part contributes prior knowledge in terms of monomial-based predefined algebraic structure. The second part learns the importance of explicit features.}
\label{figure:adafel}
\end{figure*}

The issues outlined above motivated us to propose an inclusive framework for DL-based segmentation/classification of medical images in general, and color histopathological images of frozen sections in particular. Instead of designing specialized DL models, we aim to improve segmentation/classification performance of the existing NNs, such as UNet \cite{ronneberger2015u}, UNet++ \cite{zhou2018unet++,zhou2020unet++}, DeepLabv3+ \cite{chen2018encoder} and MA-net \cite{fan2020manet}. To achieve that goal, we embed the existing network in a space induced by the learnable explicit feature map (LEFM) layer (Fig \ref{figure:adafel}). The LEFM layer is an analog to learnable kernels \cite{wang2021bridging,pan2011learning,kulis2009low,li2015adaptive,bach2004multiple,rebai2016deep,wilson2016deep,bohn2019representer}, and its use is motivated by the need to create low-dimensional subspace in data-adaptable Hilbert space where existing network will yield improved performance. To reduce risk of overfitting, the aim was to only modestly increase the complexity relative to the original network. That is achieved by using the LEFM of low order, whereat the LEFM order is the only new hyperparameter introduced by the proposed framework. Its suggested value is either 2 or 3, which simplifies the cross-validation procedure.

The explanation why the LEFM layer improves performance of deep network is in representation of the LEFM. The LEFM is represented as Hadamard (entry-wise) product of the spatial location invariant vector of coefficients and spatial location dependent map composed of the monomials of features from the input data space. Thus, the predefined (known) algebraic structure of the EFM contains \textit{a priori} knowledge that contributes to improved performance of the proposed framework. For example, \textit{a priori} knowledge in a form of translation symmetry in images is incorporated in CNNs \cite{lecun1998gradient}, and enables them to achieve translation equivariance by re-using convolutional filters at all spatial locations. Such design highly reduces the number of learnable parameters in CNNs. Analogously, the learned vector of coefficients, in proposed LEFM, can be re-used at all spatial locations with location dependent map of monomials. Learnable EFM alleviates the analogous problem in kernel-based nonlinear methods: the choice of kernel. After learning, the coefficients emphasize the importance of the features in LEFM. Since all features are known polynomial functions of the original features from the input data space, that contributes to explainability and interpretability of the decision-making process of the proposed framework. The main contributions of this work are listed as follows:

\begin{itemize}
    \item Framework for DL-based segmentation/classification of low-dimensional medical images comprising slightly modified existing deep network embedded into a subspace induced by learnable EFM layer.
    \item Representation of learnable EFM in terms of spatial location dependent map of monomials of features from the input data space and spatial location invariant vector of coefficients. The first part brings prior knowledge in terms of predefined algebraic structure with monomial-based explicit features. The second part learns the importance of explicit features. That contributes to medically explainable and interpretabe decision-making. The overall representation modestly increases the number of learnable parameters compared to the original network.
    \item Extensive validation of the proposed framework where we used existing deep architectures: DeepLabv3+, UNet, UNet++, and MA-net on two problems. The first one was image segmentation of adenocarcinoma of a colon in a liver from hematoxylin and eosin (H\&E) stained frozen sections. The second problem was the segmentation of nuclei from H\&E stained frozen section images of ten human organs. Proposed framework was compared to the original networks. In the first problem, statistically significant improvements in terms of micro balanced accuracy, micro $F_{1}$ score and micro precision are obtained. In the second, the proposed framework yielded only better results.
\end{itemize}

\newpage
\section{Related work}

\subsection{Brief overview of some specialized deep neural networks}

Herein, we briefly comment on recently proposed deep networks with task- or imaging modality specific architectures with an emphasis on histopathological image analysis. New deep CNN was proposed in \cite{mishra2022data} for medical image segmentation. It exploits specific attributes in the input datasets. That is achieved with auxiliary supervision implemented in terms of object perceptive field and layer-wise effective receptive fields. Comprehensive attention-based CNN (CA-Net) is proposed in \cite{gu2021net} for more accurate and more explainable medical image segmentation. That is achieved by using multiple attention modules in CNN architecture aware of the most important spatial positions, channels and scales\cite{gu2021net}. Although the explainability of decision is improved, the interpretability of the features is still problematic. AttentionBoost network is proposed in \cite{gunesli2020attentionboost} for gland instance segmentation in histopathological images. It is based on a multi-attention learning model that, based on adaptive boosting, adjusts loss of fully convolutional networks to adaptively learn what to attend at each stage. In \cite{li2015adaptive}, a new methodology, named PathAL, is presented aiming to improve classification performance in histopathology image analysis with a reduced number of expert annotations. Guided soft attention network for classification of histopathological breast cancer images is proposed in \cite{yang2020guided}. In particular, region-level supervision is used to guide attention of CNN. The attention mechanism activates neurons in diagnostically relevant regions and suppresses activation in diagnostically irrelevant regions. Hard sample aware noise robust learning deep network for classification of histopathology images is proposed in \cite{zhu2021hard}. The method is aimed at distinguishing informative hard samples from the harmful noisy ones. That lowers the noise rate and yields an almost clean dataset for training network in a self-supervised manner. Novel ThresholdNet \cite{guo2021learn} is designed for automatic segmentation of polyp in endoscopy images such that segmentation threshold is learnable. Proposed LEFM-Net framework is directly applicable to methods proposed in \cite{mishra2022data,gu2021net,gunesli2020attentionboost,li2021pathal,yang2020guided,zhou2020unet++,guo2021learn}. In this paper, however, we demonstrated performance improvement of the LEFM-Net framework using well know deep networks: DeepLabv3+, UNet, UNet++ and MA-net.

\subsection{Brief overview of kernel-based learning}

Let us assume there is a function $f:=\mathbb{R}^{d} \rightarrow \mathbb{R}$ that assigns the class label $y \in S_{C}$ to the sample $\mathbf{x} \in \mathbb{R}^{d}$. Here, $d$ stands for dimension of the ambient input space, $C$ stands for the number of classes and $S_{C}$ is a set containing all possible labels. As an example, for binary classification problem we have $C=2$ and $S_{2}=\{0,1\}$ or $S_{2}=\{-1,1\}$. The classifier learning problem is to estimate $f$ from the given set of training data $\left\{\left(\mathbf{x}_{t}, y_{t}\right)\right\}_{t=1}^{T}$. The representer theorem \cite{scholkopf2018learning,kimeldorf1971some}, states that each function $f$ in Hilbert space $\mathcal{H}$, a.k.a. feature space, that minimizes an arbitrary loss function, admits representation:

\begin{equation}
    \label{eq:repre}
    f(\mathbf{x})=\sum_{t=1}^{T} \alpha_{t} \kappa\left(\mathbf{x}_{t}, \mathbf{x}\right)
\end{equation}

\noindent where $\{ \alpha_t \}_{t=1}^{T}$, are estimated from the training data. That gave rise to the development of many machine learning algorithms, such as support vector machines and kernel principal component analysis, to name a few. These methods work well when the underlying problem fits $\mathcal{H}$ properly, i.e. when the given empirical values stem from $f \in \mathcal{H}$. In other words, the kernel function $\kappa(\circ, \circ)$ has to be chosen properly. If the appropriate $\mathcal{H}$ is not known, one can resort to data adaptive kernel methods \cite{pan2011learning,kulis2009low,li2015adaptive}, multiple kernel learning methods \cite{bach2004multiple}, multi-layer (deep) multiple kernel learning method \cite{rebai2016deep}, and deep kernel method \cite{wilson2016deep}. In particular, the last two approaches combine flexibility of deep networks. One where feature detection is done automatically with the approximation power of kernel methods. The second one is where the feature map is determined implicitly by the kernel function. Moreover, in \cite{bohn2019representer} the representer theorem is proven for a multilayer network of $L$ concatenated kernel functions. The power of kernel methods is their capability to implicitly learn potentially infinite dimensional nonlinear features in the reproducing kernel Hilbert space $\mathcal{H}$. The computation is made tractable by leveraging the kernel trick \cite{bohn2019representer}. However, implicitly learned features are hard to interpret.

\section{Learnable feature maps and deep networks}

As pointed out in \cite{wilson2016deep}, one of the main reasons to exploit deep kernel learning was growing frustration among researchers with too many design options associated with deep networks. Furthermore, the decision-making process of deep networks is also difficult to interpret. However, deep networks composed of multiple layers of nonlinear functions can approximate a rich set of naturally occurring input-output dependencies \cite{wang2021bridging}. Thus, they can achieve high classification performance. Therefore, we aim to propose a framework that further improves the performance of deep networks, contributes to the interpretability of the results and brings a modest increase in the number of learnable parameters compared to original networks. Towards that goal, we propose to embed existing deep networks in a subspace induced by the LEFM of small order (typically 2 or 3). Our approach relies on the well know kernel trick \cite{scholkopf2018learning}: for any positive definite kernel function $\kappa(\mathbf{x}, \mathbf{y})$ there exists a function $\phi(\mathbf{x})$ that maps data $\mathbf{x}$ to a Hilbert space $\mathcal{H}$, such that $\kappa(\mathbf{x}, \mathbf{y})=\langle\phi(\mathbf{x}), \phi(\mathbf{y})\rangle_{\mathcal{H}} \ \cdot \  \phi(\mathbf{x})$ is known as EFM. Thus, we can recast the representer theorem (\ref{eq:repre}) in terms of kernel associated EFM:

\begin{equation}
    f(\mathbf{x})=\langle\mathbf{w}, \phi(\mathbf{x})\rangle_{\mathcal{H}}
\end{equation}

\noindent where $\mathbf{w}=\sum_{t=1}^{T} \alpha_t \phi\left(\mathbf{x}_{t}\right)$. Thus, the nonlinear approximation problem (\ref{eq:repre}) in the input data space becomes the linear one in the feature space $\mathcal{H}$. Hence, kernel-based nonlinear algorithms can be seen as linear algorithms operating in appropriate feature space \cite{vedaldi2012efficient}. That is formalized by the Cover’s theorem \cite{cover1965geometrical}. It states that the number of separating hyperplanes is proportional to the dimension of the feature space. Thus, it is important for the dimension of expanded feature space $D$ to satisfy $D>>d$. That is fulfilled easily since EFMs associated with many popular kernels are mostly high dimensional or even infinite dimensional (e.g. EFM associated with the Gaussian kernel). That is also a reason feature maps are not used explicitly in nonlinear algorithms \cite{vedaldi2012efficient}. Nevertheless, we can partially circumvent this obstacle by using the approximate EFM of order $m$, i.e. $\phi_{m}(\mathbf{x})\in\mathbb{R}^D$. In that regard, we emphasize that EFMs can be written in a common algebraic form:

\begin{equation}
\phi_{m}(\mathbf{x})=\left[\left\{a_{q_{1}, \ldots ,q_{d}} x_{1}^{q_{1}} \ldots x_{d}^{q_{d}}\right\}_{q_{1}, \ldots, q_{d}=0}^{m}\right]^{\mathrm{T}}
\end{equation}

\begin{equation*}
\text{such that} \sum_{i=1}^{d} q_{i} \leq m.
\end{equation*}
When $m \rightarrow \infty$ we have: $\phi_{m}(\mathbf{x}) \rightarrow \phi(\mathbf{x})$. Bearing in mind that the dimension of induced feature space $D$ depends on $d$ and $m$ through:

\begin{equation}
\label{eq:binom}
D = \binom{d+m}{m}
\end{equation}

\noindent it becomes clear that we need to keep $m$ low if we do not want to introduce too many learnable parameters in the proposed framework. Thus, in the experiments conducted in this paper, we have $m \in\{2,3\}$. Hence, the formulation (2) with approximate EFM $\phi_{m}(\mathbf{x})$ will only approximate the original nonlinear approximation problem (1). That is why we need to embed the deep network in a subspace $\mathcal{H}_{D} \subset \mathcal{H}$ induced by $\phi_{m}(\mathbf{x})$. By doing so, we expect to further boost segmentation/classification performance of the network. Nevertheless, the problem related to the proper fit of the underlying problem to $\mathcal{H}$ still exists. Thus, we propose the following computationally efficient representation of $\phi_{m}(\mathbf{x})$:

\begin{equation}
\label{eq:phim}
    \phi_{m}(\mathbf{x}):= \psi_{m}(\mathbf{x}) \otimes \mathbf{a}_{m}
\end{equation}

\noindent where $\otimes$ denotes Hadamard (entry wise) product, and:

\begin{equation}
\label{eq:psim}
\psi_{m}(\mathbf{x}):=\left[\left\{x_{1}^{q_{1}} \ldots x_{d}^{q_{d}}\right\}_{q_{1}, \ldots, q_{d}=0}^{m}\right]^{\mathrm{T}} \text{s.t.} \sum_{i=1}^{d} q_{i} \leq m ,
\end{equation}

\begin{equation}
\label{eq:am}
\mathbf{a}_{m}:=\left[\left\{a_{q_{1}, \ldots , q_{d}}\right\}_{q_{1}, \ldots, q_{d}=0}^{m}\right]^{\mathrm{T}} \text {s.t.} \sum_{i=1}^{d} q_{i} \leq m .
\end{equation}

Hence, when existing deep network is trained with $\phi_{m}(\mathbf{x})$, the coefficients $\mathbf{a}_{m}$ are learned together with the rest of network parameters. Since $\mathbf{a}_{m}$ is spatial location invariant, learning is computationally efficient. As opposed to that, $\psi_{m}(\mathbf{x})$ is a spatial location dependent function of the input features and it is not learnable. Thus, when $d$ and $m$ are small, the number $D$ of learnable coefficients in $\mathbf{a}_{m}$ is modest when compared to the millions of parameters in existing deep networks. Number of trainable parameteres is additionally increased in amounts specific to the deep architecture and its backbone. Thus, all newly introduced parameters are directly related to the increase from $d$ to $D$ dimensional input. However, vast majority of the original network remains the same.

\noindent As an example, in the problem considered in the paper, we apply the proposed framework to segmentation of color (RGB) histopathological images of H\&E stained frozen sections. Thus, we have $d=3$. As we worked with approximate EFM of order $m \in\{2,3\}$, that respectively yields $D \in\{10,20\}$. The rationale why the deep network, embedded in a subspace $\mathcal{H}_{D} \subset \mathcal{H}$ induced by learnable EFM, outperforms the original network operating in the input data space is due to the explicit character of features up to the selected order $m$. Through learning, a deep network can determine their importance. Thus, in the proposed framework, the network will use more explicit features than it is available in the input data space. Since features in $\phi_{m}(\mathbf{x})$ are polynomials comprising the originals features from the input data space, it is possible to interpret them.
\section{Experiments and Results}
\label{sec:expRes}
\subsection{Data adaptive feature expansion layer}
\label{subsec:layer}
For implementing an LEFM layer, fundamental functionality is described in (\ref{eq:phim}) - (\ref{eq:am}). As mentioned, the coefficients are data adaptive (i.e. learnable) and, for LEFM layer construction, we are only interested in monomials. Thus, we need to find monomials (\ref{eq:psim}) using all combinations of $\boldsymbol{q}$ such that $\sum_{i=1}^d{q_i}\leq m,\ \forall q_i \geq 0$. Thus, the number of terms in this setting becomes (\ref{eq:binom}). Optionally, if the LEFM layer is followed by batch normalization layer \cite{ioffe2015batch}, two parameters per each term (mean and standard deviation) are introduced.

The core functionality of LEFM layer is developed using two matrices, term- and power mask. Both matrices are $D\times d$ dimensional. The term mask matrix $\mathbf{TM}$ is associated with selecting features $\{x_i \}^d_{i=1}$ and the power mask matrix $\mathbf{PM}$ with exponantiating selected features by $\{q_i \}^d_{i=1}$. A resulting monomial matrix $\mathbf{MM}$ is made by element-wise exponentiation $\mathbf{MM}=\mathbf{TM}^{\mathbf{PM}}$. Thus, when the resulting matrix is squeezed by elements-product across the $2^{nd}$ dimension, we get the monomials of the input features $\psi_m(\mathbf{x})$. Using the randomly initialized vector of learnable coefficients $\mathbf{a}_{m}$ we apply the Hadamard product as in (\ref{eq:phim}).

Thus, with $d$ features as an input, the LEFM layer gives $D$ output features with learnable coefficients $\{ a_i \}^D_{i=1}$. As illustrated in Fig. \ref{figure:adafel}, the expanded output is passed to the arbitrary deep NN to jointly train its weights and LEFM layer’s coefficients.

\begin{figure*}[ht!]
\centering
\includegraphics[width=\textwidth]{figures/fleiss_kappa_vis.png}
\caption{Fleiss kappa statistics for evaluating inter-annotators agreement. Left and right plots represent statistics per image in CryoNuSeg and CoCaHis datasets, respectively. The Fleiss kappa for the entire CryoNuSeg is 0.8012 and for CoCaHis 0.7405. Former indicates an almost perfect inter-annotator agreement, whereas the latter indicates a substantial inter-annotator agreement.}
\label{figure:fleisskappa}
\end{figure*}

\subsection{Deep neural networks}
\label{subsec:dnn}
Within the scope of this paper, the LEFM layer is combined with several types of slightly modified neural networks. In DeepLabv3+ \cite{chen2018encoder}, a deep network has the spatial pyramid pooling module combined with the encoder-decoder structure. Feature extraction at arbitrary resolution was enabled by atrous convolution layers contained in the encoder module \cite{chen2018encoder}. On the other hand, a simple decoder recovers detailed boundaries of an object. A popular UNet model \cite{ronneberger2015u} employs a symmetric U-shaped encoder-decoder for precise feature localization. Skip connections help in preserving context at the same level of the model’s depth \cite{ronneberger2015u}. Natural extension of the UNet is a nested model. Introduced in \cite{zhou2018unet++}, UNet++ model proposes additional nodes and skip connections to the original UNet. That facilitates predicting the output from different levels of depth. Training the entire network by combining all outputs, the UNet++ model outperforms UNet with a cost of a larger number of parameters. Finally, MA-net \cite{fan2020manet} incorporates a self-attention mechanism. Based on the attention mechanism, it collects rich contextual dependencies while adaptively integrating local features. Model implements Position-wise Attention Block and Multi-scale Fusion Attention Block. Prior is used to model the feature interdependencies spatially, and the former to capture channel dependencies using semantic feature fusion \cite{fan2020manet}.

All mentioned models can implement different backbones for feature extraction to their core. When comparing a model with and without the LEFM layer, it is important to keep the backbone fixed. MobileNetV2 \cite{sandler2018mobilenetv2} was implemented in DeepLabv3+ model, whereas other models have DenseNet201 \cite{huang2017densely} backbone. All backbone’s weights are pretrained on Imagenet classification problem \cite{deng2009imagenet} and fine-tuned with the rest of the model. Thus, the total number of trainable parameters in DeepLabv3+ model is \textasciitilde4.4M, in UNet \textasciitilde28.5M, in UNet++ \textasciitilde48.5M, and in MA-net \textasciitilde133.7M. Furthermore, LEFM-Nets, with mentioned architectures and $m=3$, contribute to an increase of the number of learnable parameters in the amount of: 0.11\%, 0.18\%, 0.11\%, and 0.04\% relative to the original networks. In the application considered herein, it is modest when compared to the reported total number of trainable parameters of original models.

\subsection{Datasets}
This paper is focused on segmentation of color images of H\&E stained frozen sections. Thereby, we used the only two publicly available datasets: CoCaHis \cite{sitnik2021cocahis,cocahis2021dataset} and CryoNuSeg \cite{mahbod2021cryonuseg}. Datasets are divided into the train and test sets. Furthermore, during the training process, 20\% of the train set is left for validation and early stopping purposes.

\subsubsection{CoCaHis}
Dataset comprises 82 images with dimensions $1037 \times 1388$. Training set has 58 images belonging to 13 patients. The rest of 24 images from 6 patients are contained in the test set. Sets are disjointed for both images and patients. Seven annotators generated the pixel-wise labels for each image. One and zero indicate cancerous and non-cancerous pixels, respectively. Resulting average Fleiss kappa statistic \cite{fleiss1971measuring}, presented in Section \ref{sec:results} and Fig. \ref{figure:fleisskappa}, supports usage of majority voting due to the substantial inter-annotator agreement \cite{landis1977measurement}.

\renewcommand{\thefootnote}{\arabic{footnote}}
\subsubsection{CryoNuSeg} Collected from The Cancer Genome Atlas\footnote[1]{https://www.cancer.gov/tcga}, the dataset contains 30 whole-slide frozen section images. Afterwards, they are cropped to the most representative window. Hence, the dataset comprises 30 images of the size $515\times 512$ pixels that correspond to 10 organs. The problem formulated by this dataset is segmentation of nuclei in different images. Therefore, the ground truth generation is similar to CoCaHis. The Fleiss kappa statistic, presented in Section \ref{sec:results} and Fig. \ref{figure:fleisskappa}, suggests an almost perfect agreement between three annotations, and that is why the majority vote is used. To ensure the representation of every organ in both train and test set, one image per each organ is left out for the test set (10 images), whereas the remaining images are included in the training set (20 images).

\subsection{Statistics and Metrics}
\subsubsection{Statistical significance analysis}
To determine if inclusion of the LEFM layer yields statistically better performance when compared to the original models, we used a one-way analysis of variance (ANOVA) test. All models were trained 10 times with different initial weights and different subsets for validation. Null hypothesis was that two groups of results, obtained by the LEFM-Net and the original deep network, have equal means. For all tests, a level of significance $\alpha=0.05$ was used.

\subsubsection{Evaluation metrics}
\label{sec:metrics}
All metrics used for evaluation were calculated using standard $TP$, $TN$, $FN$, and $FP$ notation. $TP$ stands for a number of correctly diagnosed cancerous pixels. $TN$ indicates a number of  non-cancerous pixels correctly diagnosed as non-cancerous. $FN$ stands for a number of cancerous pixels incorrectly diagnosed as non-cancerous. Finally, $FP$ indicates the number of noncancerous pixels incorrectly diagnosed as cancerous. Thus, the following micro measures were used for models’ performance validation:
\begin{itemize}

\item a harmonic mean of precision and sensitivity (recall) presented as a $F_1$ score:
\begin{equation}
\label{eq:f1}
    F_1 = \frac{2\cdot TP}{2\cdot TP+FP+FN},
\end{equation}

\item balanced accuracy (BACC) as an arithmetic mean of sensitivity (SE) and specificity (SP):
\begin{equation*}
SE = \frac{TP}{TP+FN},\ SP = \frac{TN}{{TN}+{FP}},
\end{equation*}

\begin{equation}
\label{eq:bacc}
BACC = \frac{SE+SP}{2},
\end{equation}

\item and Precision score, which measures the classifier's capacity not to categorize a sample as positive if it is negative:
\begin{equation}
\label{eq:prec}
    PREC = \frac{TP}{TP+FP}.
\end{equation}
\end{itemize}

\noindent For all the metrics, 0 indicates the worst performance and 1 indicates the best performance.

\begin{figure}[H]
    \centering
    \includegraphics[width=0.35\textwidth]{figures/target2.png}
    \caption{Target image for stain normalization chosen by pathologist. Selection is based on the quality-of-staining criterion.}
    \label{figure:target_image}
\end{figure}

\subsection{Data preprocessing}
\label{sec:preprocessing}

As an important part of the computational pathology pipeline, stain normalization was performed to eliminate the variability of staining and experimental variations. The structure preserved color normalization method \cite{vahadane2016structure} was used on both CoCaHis and CryoNuSeg datasets. A target image, shown in Fig. \ref{figure:target_image}, was carefully chosen by an experienced pathologist, ensuring a well stained specimen for the mentioned algorithm. Fig. \ref{figure:panel} shows original and stain-normalized images from the mentioned datasets. Stain-transfer from the target image helps models during the training to cope with the color variation problem.

Prior to the training, every image was scaled to the range [0,1]. Additionally, random shifting, scaling, rotating and flipping (horizontal, vertical) were applied as part of the data augmentation procedure. Chosen parameters are presented in Section \ref{sec:implDetail}.

\begin{figure}[H]
     \centering
     \begin{subfigure}[b]{0.24\textwidth}
         \centering
         \includegraphics[width=\textwidth]{figures/img_80_raw.png}
         \caption{}
         \label{figure:subplot1}
     \end{subfigure}
     \hfill
     \begin{subfigure}[b]{0.24\textwidth}
         \centering
         \includegraphics[width=\textwidth]{figures/img_80_sn.png}
         \caption{}
         \label{figure:subplot2}
     \end{subfigure}

    \bigskip

    \begin{subfigure}[b]{0.24\textwidth}
         \centering
         \includegraphics[width=\textwidth]{figures/Human_LymphNodes_03.png}
         \caption{}
         \label{figure:subplot3}
     \end{subfigure}
     \hfill
     \begin{subfigure}[b]{0.24\textwidth}
         \centering
         \includegraphics[width=\textwidth]{figures/img_9_sn.png}
         \caption{}
         \label{figure:subplot4}
     \end{subfigure}
     \caption{Visualization of raw and stain normalized images. Subfigures (a) and (c) show original images from CoCaHis and CryoNuSeg, respectively. Their stain-normalized equivalents are shown in subfigures (b) and (d).}
     \label{figure:panel}
\end{figure}

\begin{table*}[ht]
\caption{Performance scores of four different models on CryoNuSeg and CoCaHis datasets. Each model had 10 runs with different initial weights. Backbones were initialized on the pretrained ImageNet classification problem. Variable $m\in \{2,3\}$ indicates level of expansion. The best result for each metric and each dataset is in bold font.}
\label{tab:cryonusegPerf}
\resizebox{\textwidth}{!}{%
\begin{tabular}{c|l|cc|cc|cc|}
\cline{2-8}
\multicolumn{1}{l|}{{\color[HTML]{3D3D3D} \textbf{}}} &
  \multicolumn{1}{c|}{{\color[HTML]{3D3D3D} }} &
  \multicolumn{2}{c|}{{\color[HTML]{3D3D3D} \textbf{Original network}}} &
  \multicolumn{2}{c|}{{\color[HTML]{3D3D3D} \textbf{LEFM-Net m=2}}} &
  \multicolumn{2}{c|}{{\color[HTML]{3D3D3D} \textbf{LEFM-Net m=3}}} \\ \cline{3-8}
\textbf{} &
  \multicolumn{1}{c|}{\multirow{-2}{*}{{\color[HTML]{3D3D3D} \textbf{Metric}}}} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}\textbf{CryoNuSeg}} &
  \cellcolor[HTML]{C0C0C0}\textbf{CoCaHis} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}\textbf{CryoNuSeg}} &
  \cellcolor[HTML]{C0C0C0}\textbf{CoCaHis} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}\textbf{CryoNuSeg}} &
  \cellcolor[HTML]{C0C0C0}\textbf{CoCaHis} \\ \hline
\multicolumn{1}{|c|}{{\color[HTML]{3D3D3D} }} &
  {\color[HTML]{3D3D3D} BACC} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}{\color[HTML]{3D3D3D} 87.24 $\pm$ 0.54}} &
  \cellcolor[HTML]{C0C0C0}86.06 $\pm$ 1.60 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}{\color[HTML]{3D3D3D} 86.99 $\pm$ 0.64}} &
  \cellcolor[HTML]{C0C0C0}87.24 $\pm$ 1.55 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}{\color[HTML]{3D3D3D} 87.52 $\pm$ 0.74}} &
  \cellcolor[HTML]{C0C0C0}88.19 $\pm$ 0.70 \\ \cline{2-8}
\multicolumn{1}{|c|}{{\color[HTML]{3D3D3D} }} &
  {\color[HTML]{3D3D3D} F\textsubscript{1}} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}{\color[HTML]{3D3D3D} 82.34 $\pm$ 0.60}} &
  \cellcolor[HTML]{C0C0C0}80.75 $\pm$ 1.44 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}{\color[HTML]{3D3D3D} 82.11 $\pm$ 0.67}} &
  \cellcolor[HTML]{C0C0C0}81.89 $\pm$ 1.69 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}{\color[HTML]{3D3D3D} 82.60 $\pm$ 0.69}} &
  \cellcolor[HTML]{C0C0C0}82.85 $\pm$ 1.38 \\ \cline{2-8}
\multicolumn{1}{|c|}{\multirow{-3}{*}{{\color[HTML]{3D3D3D} \textbf{DeepLabv3+}}}} &
  {\color[HTML]{3D3D3D} PREC} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}{\color[HTML]{3D3D3D} 85.49 $\pm$ 0.39}} &
  \cellcolor[HTML]{C0C0C0}84.48 $\pm$ 4.70 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}{\color[HTML]{3D3D3D} 85.74 $\pm$ 0.60}} &
  \cellcolor[HTML]{C0C0C0}83.29 $\pm$ 2.96 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}{\color[HTML]{3D3D3D} 85.29 $\pm$ 0.79}} &
  \cellcolor[HTML]{C0C0C0}82.76 $\pm$ 3.96 \\ \hline
\multicolumn{1}{|c|}{} &
  {\color[HTML]{3D3D3D} BACC} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}88.99 $\pm$ 0.87} &
  \cellcolor[HTML]{C0C0C0}86.55 $\pm$ 1.46 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}89.05 $\pm$ 0.95} &
  \cellcolor[HTML]{C0C0C0}85.58 $\pm$ 2.32 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}88.73 $\pm$ 0.95} &
  \cellcolor[HTML]{C0C0C0}88.67 $\pm$ 1.64 \\ \cline{2-8}
\multicolumn{1}{|c|}{} &
  {\color[HTML]{3D3D3D} F\textsubscript{1}} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}84.95 $\pm$ 0.82} &
  \cellcolor[HTML]{C0C0C0}79.94 $\pm$ 1.70 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}85.02 $\pm$ 0.87} &
  \cellcolor[HTML]{C0C0C0}78.53 $\pm$ 3.72 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}84.79 $\pm$ 0.95} &
  \cellcolor[HTML]{C0C0C0}82.59 $\pm$ 1.67 \\ \cline{2-8}
\multicolumn{1}{|c|}{\multirow{-3}{*}{\textbf{UNet}}} &
  {\color[HTML]{3D3D3D} PREC} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}88.12 $\pm$ 0.74} &
  \cellcolor[HTML]{C0C0C0}78.60 $\pm$ 5.71 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}88.17 $\pm$ 1.37} &
  \cellcolor[HTML]{C0C0C0}77.35 $\pm$ 9.59 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}\textbf{88.73 $\pm$ 1.28}} &
  \cellcolor[HTML]{C0C0C0}80.08 $\pm$ 4.19 \\ \hline
\multicolumn{1}{|c|}{} &
  {\color[HTML]{3D3D3D} BACC} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}88.98 $\pm$ 0.57} &
  \cellcolor[HTML]{C0C0C0}82.01 $\pm$ 6.57 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}89.29 $\pm$ 0.60} &
  \cellcolor[HTML]{C0C0C0}86.99 $\pm$ 4.66 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}88.88 $\pm$ 0.56} &
  \cellcolor[HTML]{C0C0C0}88.23 $\pm$ 2.13 \\ \cline{2-8}
\multicolumn{1}{|c|}{} &
  {\color[HTML]{3D3D3D} F\textsubscript{1}} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}84.94 $\pm$ 0.52} &
  \cellcolor[HTML]{C0C0C0}74.04 $\pm$ 9.71 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}85.27 $\pm$ 0.61} &
  \cellcolor[HTML]{C0C0C0}79.94 $\pm$ 7.37 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}84.93 $\pm$ 0.61} &
  \cellcolor[HTML]{C0C0C0}81.55 $\pm$ 4.38 \\ \cline{2-8}
\multicolumn{1}{|c|}{\multirow{-3}{*}{\textbf{UNet++}}} &
  {\color[HTML]{3D3D3D} PREC} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}88.09 $\pm$ 0.72} &
  \cellcolor[HTML]{C0C0C0}75.64 $\pm$ 17.35 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}88.04 $\pm$ 0.57} &
  \cellcolor[HTML]{C0C0C0}76.69 $\pm$ 11.94 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}88.48 $\pm$ 1.08} &
  \cellcolor[HTML]{C0C0C0}78.23 $\pm$ 10.51 \\ \hline
\multicolumn{1}{|c|}{} &
  {\color[HTML]{3D3D3D} BACC} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}89.30 $\pm$ 0.44} &
  \cellcolor[HTML]{C0C0C0}88.02 $\pm$ 1.22 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}89.31 $\pm$ 0.38} &
  \cellcolor[HTML]{C0C0C0}88.69 $\pm$ 1.63 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}\textbf{89.41 $\pm$ 0.29}} &
  \cellcolor[HTML]{C0C0C0}\textbf{89.36 $\pm$ 1.27} \\ \cline{2-8}
\multicolumn{1}{|c|}{} &
  {\color[HTML]{3D3D3D} F\textsubscript{1}} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}85.12 $\pm$ 0.51} &
  \cellcolor[HTML]{C0C0C0}82.75 $\pm$ 1.10 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}85.27 $\pm$ 0.39} &
  \cellcolor[HTML]{C0C0C0}84.33 $\pm$ 1.98 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}\textbf{85.35 $\pm$ 0.25}} &
  \cellcolor[HTML]{C0C0C0}\textbf{84.96 $\pm$ 1.14} \\ \cline{2-8}
\multicolumn{1}{|c|}{\multirow{-3}{*}{\textbf{MA-net}}} &
  {\color[HTML]{3D3D3D} PREC} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}87.36 $\pm$ 0.58} &
  \cellcolor[HTML]{C0C0C0}83.16 $\pm$ 2.44 &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}87.93 $\pm$ 0.46} &
  \cellcolor[HTML]{C0C0C0}\textbf{86.52 $\pm$ 1.60} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}87.76 $\pm$ 0.53} &
  \cellcolor[HTML]{C0C0C0}86.15 $\pm$ 2.00 \\ \hline
\end{tabular}%
}
\end{table*}

\subsection{Implementation with training and test details}
\label{sec:implDetail}
LEFM layer is implemented in Python using the Pytorch framework \cite{paszke2019pytorch}. All existing deep networks are inherited and modified from \textit{segmentation-models-pytorch} library \cite{yakubovskiy2019segmentationmodels}. For faster convergence \cite{sitnik2021cocahis}, weights of backbones are initialized on ImageNet classification problem \cite{deng2009imagenet} and then fine-tuned with the rest of the LEFM-Net.

Before the training, images were preprocessed as described in Section \ref{sec:preprocessing}. For the evaluation of the statistical significance, all models were trained with 10 different random seeds. Thus, the initialization of newly added weights, coefficients, and choice of patches withheld for validation set were different in every run. In Table \ref{tab:cryonusegPerf}, we show the average performance of models on selected metrics (see Section \ref{sec:metrics}). For each dataset, the corresponding test sets are the same for every model.

We selected Adam \cite{kingma2014adam} optimizer for the training procedure. To equally punish the underperformance in terms of false positives and false negatives, we used Dice loss function \cite{seo2021closing,eelbode2020optimization}. The hyperparameters for every model were set as follows:
\begin{itemize}
    \item maximum number of epochs equals to $3\cdot 10^4$
    \item starting learning rate was set to $10^{-3}$
    \item learning rate is reduced when the loss plateau is reached
    \begin{itemize}
        \item waiting patience for improvement is set to 20 epochs
        \item reduction factor is set to 0.5
        \item minimum learning rate equals $10^{-6}$

    \end{itemize}
    \item factor for $L_2$ weight decay is set to $10^{-4}$
    \item Early stopping after 40 epochs when there is no improvement on the validation set
    \item data augmentation
    \begin{itemize}
        \item shift limit = 0.2
        \item scale limit = 0.2
        \item rotation limit = 30$^{\circ}$
        \item horizontal and vertical flip
        \item probability = 0.5
    \end{itemize}
    \item LEFM order $m\in\{2,3\}$
\end{itemize}

\subsection{Results}
\label{sec:results}
Results of exhaustive training and testing of four different models, with and without the LEFM layer, on two datasets and 10 random seeds are shown in Table \ref{tab:cryonusegPerf}. Models are ordered by the number of parameters. As mentioned in Section \ref{subsec:dnn}, DeepLabv3+ has the least and MA-net the most trainable weights. When comparing these two models, a noticeable increase in scores can always be found in the model with larger capacity, i.e. MA-net.

It can be seen that four different versions of LEFM-Nets, compared to the original networks, yield better scores on two datasets. Higher order features are important for better end results. As it can be seen in Table \ref{tab:cryonusegPerf}, regardless of the level of expansion, LEFM-Net outperforms original deep models. Also, nearly half of the observed metrics for CryoNuSeg dataset and almost all of them for CoCaHis dataset are better when the level of expansion is $m=3$.

Tests performed on CryoNuSeg show that all LEFM-Nets are on average better than the original network. ANOVA statistical testing indicates that, with statistical significance $\alpha = 0.05$, all LEFM-Nets significantly outperform original models on CoCaHis dataset for $m=3$. Tests are performed on $F_1$ and BACC metrics. Hence, introduction of the LEFM layer seems to be justified for intraoperative pixel-based diagnostics with deep learning models.

The training time depends on the number of training parameters in the model, dataset, batch- and patch size, and on the degree of expansion $m$. With NVIDIA Tesla V100S GPU, training time for one run ranges from 2 hours, when training DeepLabv3+, to 20 hours when training MA-net.
\section{Discussion and Conclusion}
\label{sec:discAndCon}

\begin{figure*}[ht]
\centering
\includegraphics[width=\textwidth]{figures/cryonuseg_d3.png}
\caption{Visualization of coefficients learned during the training on CryoNuSeg dataset. DeepLabv3+, UNet, UNet++, and MA-net were trained with expansion layer of degree $m=3$. During the training, $L_2$ norm was used for weight decay. Red ($R$), green ($G$) and blue ($B$) stand for $x_1$, $x_2$ and $x_3$ features, respectively.}
\label{figure:coefficientsD3}
\end{figure*}

The core idea behind the LEFM layer is generation of more explicit features in a data-driven manner, i.e. letting the network to decide on their importance for the image segmentation problem at hand. Towards that, we illustrate in Fig. \ref{figure:coefficientsD3} the relative importance of explicit features induced by the LEFM or order $m=3$ on the CryNuSeg dataset. For the sake of interpretability, we name the abstract features in the input space $\{x_1, x_2, x_3\}$ by the colors they represent \{red, green, blue\}. To interpret Fig. \ref{figure:coefficientsD3} it is important to notice that in images of H\&E stained specimens, nucleus, cytoplasm and glandular structures appear respectively blue-purple, pink and white. Intensely red structures may also represent red blood cells \cite{kothari2014removing}. Since red (R) and blue (B) colors are dominant in the purple color formation, they are dominantly expressed in Fig. \ref{figure:coefficientsD3}. However, justification for using the LEFM layer is because some explicit features, that do not exist in the input data space (monomials of the original features), are also important when the model is segmenting the image. As seen in Fig. \ref{figure:coefficientsD3}, the networks are using them up to certain extents. Hence, when the new features were added, i.e. number of channels is increased, LEFM-Net is expected to be guided towards better predictions while training. Thus, the level of expansion $m$ is a hyperparameter for cross-validation. However, there is a memory limitation to what degree can features be expanded (see Eq. (\ref{eq:binom})). Thus, we limited our experiments to the $m\in \{2,3\}$.

Interpretability of features in the domain of histopathological pixel-wise diagnostics is straightforward. Newly added features are nonlinear mixtures of the original features from the input space. Therefore, they can be interpreted as mixtures of colors needed for discriminating between cancer and non-cancer pixels. Using the previously explained coloring for nuclei, cytoplasm and glandular structures, it is clear that blue-purple, pink and white colors cannot be explained in terms of $R$, $G$ and $B$ features only. Thus, networks also used $RG$, $RB$ and $GB$ features, as seen in Fig. \ref{figure:coefficientsD3}. Since those features do not exist in the input data space, the LEFM layer helps the original network by providing the new medically meaningful features.

It is important to mention that feature expansion brings slightly increased computational complexity as there are more channels to the input of existing neural networks. Therefore, besides newly introduced coefficients in the LEFM layer, new neurons are added in every existing network to adapt for the expanded input. Thus, specific number of new learnable weights depends on the network's architecture. However, as discussed in  Section IV.B the increase is modest.

To conclude, this work introduced a data-adaptive and learnable explicit feature map layer. Besides the features from the input data space, it generates higher order features and helps the existing network to improve segmentation/classification performance. New features are predefined by order of expansion and their importance is learned through the training process. Learned coefficients identify whether the feature is important for classification. Overall, if the original features are interpretable, then their monomials should be interpretable as well. The same applies to color mixtures of H\&E stains in the domain of histopathological image segmentation. By performing exhaustive validation on two different datasets and across multiple runs, introducing the LEFM layer to existing deep models seems to be justified. Presented statistical significance analysis further supports this statement.

\bibliographystyle{IEEEtran}


\section{Introduction \label{sec:introduction}}
Recently, the use of a Wien filter operated at radio frequency (RF) has been proposed as a tool to  search for electric dipole moments (EDMs) of protons and deuterons in storage rings~\cite{Rathmann:2013rqa,morse}. The occurrence of EDMs of elementary particles is intimately connected to the matter-antimatter asymmetry observed in the universe~\cite{doi:10.1146/annurev.nucl.49.1.35}, which the Standard Model of elementary particle physics fails to describe. A non-zero EDM measurement would point to new physics beyond the Standard Model~\cite{Dekens2014}.

A Wien filter provides orthogonal electric and magnetic fields, usually generated by a parallel plate capacitor and encircling coils, such that the Lorentz force for charged particles traveling with a specific velocity orthogonal to both fields vanishes~\cite{salomaa}. This principle has found widespread applications not only in mass spectro\-meters, but also in electron microscopes and ion optics~\cite{wienmicro}. A few reports have been dedicated to its use in accelerator facilities, but most of them describe static Wien filters~\cite{orloff2008handbook,steiner}.  The electric and magnetic fields of an RF Wien filter can be used to manipulate the spins of particles in a storage ring, and, as shown in~\cite{Rathmann:2013rqa,morse}, open up the possibility for a measurement of the EDMs of protons and deuterons.

The objective of the present publication is to describe the design of a novel waveguide RF Wien filter for the search for the EDMs of protons and deuterons at COSY~\cite{Maier19971,PhysRevSTAB.18.020101}, pursued by the JEDI collaboration\footnote{JEDI collaboration \url{http://collaborations.fz-juelich.de/ikp/jedi}}. The fact that proton and deuteron EDMs are expected to be very small calls for precise design and manufacturing. We must admit that the spin-tracking tools required to provide specifications for the design of the RF Wien filter have not yet been fully developed. Recently, spin-tracking studies with an \textit{ideal} RF Wien filter were carried out~\cite{Rosenthal:2015jzr,doi:10.1142/S2010194516600995}, but these do not yet take into account a number of important systematic effects, such as fringe fields, unwanted field components, positioning errors, and non-vanishing Lorentz forces.  Therefore, an approach was adopted here to try to realize the best possible device based on state-of-the-art technologies.

A prototype RF Wien filter, based on an already existing RF dipole with radial magnetic field ~\cite{PhysRevLett.93.224801,CERN:Courier}, was recently developed and used at COSY.  (The use of RF dipoles and solenoids to manipulate stored polarized beams is discussed in~\cite{lee}.) The RF dipole was equipped with horizontal electric field plates in order to provide an RF Wien filter configuration with vertical electric and horizontal magnetic field~\cite{Mey:2015xbq,doi:10.1142/S2010194516600946}. The RF electromagnetic field was generated using two coupled resonators; one that generates the electric field and another one that generates the magnetic field. The approach of using separate systems to generate electric and magnetic fields, however, neglected the inherent coupling between the electric and magnetic fields. Therefore, the approach described here, is based on a novel waveguide system where by design the orthogonality between electric and magnetic fields is accomplished.

The paper is organized as follows:
\begin{itemize}
\item Section~\ref{sec:design} describes the mechanical design of the  RF Wien filter. The parallel-plates wave\-guide and its mechanical structure are described in Section~\ref{sec:waveguide}, the driving circuit in Section~\ref{sec:driving_circuit}. The results of the electromagnetic field simulations, the Lorentz force compensation, and the optimization of the electric and magnetic field homogeneity by shaping  the electrodes is discussed in Section~\ref{sec:field_homogenity}.

\item Section~\ref{sec:beam_dynamics_simulations} describes the beam dynamics simulations. A Monte Carlo simulation was carried out in order to quantify the unwanted field components of the RF Wien filter with a realistic phase-space distribution of the beam. A comparison of the prototype RF Wien filter~\cite{Mey:2015xbq,doi:10.1142/S2010194516600946} and the wave\-guide RF Wien filter is given in Section~\ref{sec:comparison}.

\item Section~\ref{sec:thermal} presents results of thermal simulations based on the power losses in the different materials of the device.
\end{itemize}

\section{Design \label{sec:design}}
\subsection{Waveguide design \label{sec:waveguide}}
The RF Wien filter shall be operated at frequencies of about 100\,kHz to 2\,MHz. The maximum acceptable length for the vacuum vessel, given by space restrictions at COSY, is 870\,mm, which corresponds to approximately $1/300$ of the length of the electromagnetic wave (at $\SI{1}{MHz}$). The transverse electromagnetic (TEM) mode of a parallel-plate wave\-guide fulfills the requirement of orthogonal electric and magnetic fields.

In order to maintain an overall vanishing Lorentz-force, and to provide minimal unwanted field components, it is a priori not clear whether a small or a large beam size is advantageous. Within the COSY ring, the PAX low-$\beta$ section~\cite{PhysRevSTAB.18.020101} offers the possibility to vary the beam size by about a factor of three, therefore the RF Wien filter will be installed at this location.

The design calculations for the waveguide RF Wien filter were carried out using a full-wave simulation with CST Microwave Studio\footnote{CST - Computer Simulation Technology AG, Darmstadt, Germany, \url{http://www.cst.com}}, and the electric and magnetic fields were modeled with an accuracy of $10^{-6}$. Because of the  high expectations on field homogeneity, an approach was adopted that allows one to calculate the electric and magnetic fields without additional assumptions, such as quasi-static approximations or the like\footnote{Each simulation required up to 12 hours of computing time on a 4-Tesla C2075 GPU cluster\footnotemark[4],  with 2 six-core Xeon E5 processors\footnotemark[5] and a RAM capacity of 94\,GB.}.
\footnotetext[4]{Nvidia Corporation, Santa Clara, California, USA, \url{http://www.nvidia.com/object/tesla-workstations.html}}\addtocounter{footnote}{+1}
\footnotetext[5]{Intel Corporation, Santa Clara, California, USA, \url{http://www.intel.com/content/www/us/en/processors/xeon/xeon-processor-e5-family.html}}\addtocounter{footnote}{+1}

The system comprises a power source, a parallel plate waveguide and a load. The plates are fed on their front and rear sides by an RF current, which distributes on the surfaces, thereby generating the required electromagnetic field in the enclosed space. Deviations from the ideal orthogonality and homogeneity of the fields are due to the finite size of the plates and their limited conductivities.

The intended working frequencies of the RF Wien filter are calculated according to
\begin{equation}
f_{\text{RF}}=f_{\text{rev}} \lvert {k+\gamma G} \rvert, k \in \mathbb{Z}\,
\end{equation}
where $k$ is the harmonic number, $G$ the gyromagnetic anomaly, $\gamma G$ the spin tune~\cite{PhysRevLett.115.094801}, and $f_{\text{rev}}$ the revolution frequency. With respect to the search for the EDMs of deute\-rons and protons, experiments at a number of harmonics shall be carried out for systematic reasons. Table~\ref{tab:harmonic} summarizes the resonance frequencies for deuterons at a momentum of $\SI{970}{MeV/c}$ and for protons at $\SI{520}{MeV/c}$, for which electron-cooled beams with compensated cooler solenoid are available at COSY. As indicated in Table~\ref{tab:harmonic} for deuterons (protons), five (four) harmonics are in the operating frequency range of $\SI{100}{kHz}$ to $\SI{2}{MHz}$. In the following, we restrict the discussion to deuterons at a momentum of $\SI{970}{MeV/c}$.

\begin{table*}[htb]
\renewcommand{\arraystretch}{1.2}
\centering
\caption{Operating frequencies $f_\text{RF}$ of the waveguide RF Wien filter for deuterons ($d$) at a momentum of 970\,MeV/c and for protons ($p$) at $\SI{520}{MeV/c}$ in COSY for the harmonic numbers $k$. The frequencies $f_\text{RF}$ shown in bold fit in the frequency range from $\SI{100}{kHz}$ to 2\,MHz. The revolution frequency $f_\text{rev}$, G factors, Lorentz $\beta$ and $\gamma$, and the spin tune $\gamma G$ are given as well.}
\label{tab:harmonic}
\begin{tabular}{cccccc|ccccccc}\hline
         & &         &            &         &          &       & \multicolumn{5}{c}{$f_\text{RF}$\,[kHz]}\\
 & $f_\text{rev}$\,[kHz] & G          & $\beta$ & $\gamma$ & $\gamma G$ & $\scriptstyle k=-4$ & $\scriptstyle k=-3$&  $\scriptstyle k=-2$      & $\scriptstyle k=-1$     & $\scriptstyle k=0$     & $\scriptstyle k=+1$    & $\scriptstyle k=+2$ \\\hline
$d$      & $750.2$ & $-0.143$   & $0.459$ & $1.126$ & $-0.161$   & $3121.6$ & $2371.4$ &  $\mathbf{1621.2}$  & $\mathbf{871.0}$  & $\mathbf{120.8}$ & $\mathbf{629.4}$ & $\mathbf{1379.6}$\\
$p$      & $791.6$ & $1.793$    & $0.485$ & $1.143$  & $2.050$    & $\mathbf{1543.9}$ & $\mathbf{752.2}$&  $39.4$  & $\mathbf{831.0}$ & $\mathbf{1622.7}$ & $2414.3$ & $3206.0$ \\\hline
\end{tabular}
\end{table*}

The Lorentz force is given by
\begin{equation}
\vec F_\text{L}  = q \left(\vec E + \vec v \times \vec B\right)\,,
\label{eq:lorentz}
\end{equation}
where $q$ is the charge of the particle, $\vec{v}=c(0,0,\beta)$ is the velocity vector,  $\vec E=(E_x,E_y,E_z)$ and $\vec B=\mu_0(H_x,H_y,H_z)$ denote the components of the electric and magnetic fields, and $\mu_0$ the vacuum permeability. For a vanishing Lorentz force $\vec{F}_\text{L}=0$, the required field quotient $Z_q$ is determined, which yields
\begin{align}
E_x & = -c \cdot \beta \cdot \mu_0 \cdot H_y\,, \nonumber \\
Z_q=-\frac{E_x}{H_y} & = c \cdot \beta \cdot \mu_0 \approx 173 \, \, \Omega\,.
\label{eq:zq}
\end{align}

Figure~\ref{fig:structure} shows a cross section of the parallel-plates wave\-guide RF Wien filter. The axis of the waveguide points along the beam direction ($z$). The plates are separated by $\SI{100}{mm}$ along the $x$-direction. The width of the plates is $\SI{182}{mm}$. This setup ensures that during the EDM studies, the main component of the electric field ($E_x$) points radially inwards in $-x$-direction, and the main component of the magnetic field ($H_y$)  upwards in $y$-direction with respect to the stored beam.  The system is placed inside a cylindrical vacuum vessel. The electrodes are surrounded by ferrite blocks made of CMD5005\footnote{National Magnetics Group, Inc., Pennsylvania, USA,  \url{http://www.magneticsgroup.com/m_ferr_nizn.htm}}. The copper electrodes are shaped in order to improve the homogeneity of the electric and magnetic fields and to minimize the Lorentz force, as explained in detail in Section~\ref{sec:EM-simulation}.

\begin{figure*}[!]
\centering
\includegraphics[width=11cm]{structure.eps}
\caption{\label{fig:structure} Design model of the RF Wien filter showing the parallel-plates waveguide and the support structure. 1: beam position monitor (BPM); 2: copper electrodes; 3: vacuum vessel; 4: clamps to hold the ferrite cage; 5: belt drive for $\ang{90}$ rotation, with a precision of $\ang{0.01}$ ($\SI{0.17}{mrad}$); 6: ferrite cage; 7: CF160 rotatable flange; 8: support structure of the electrodes; 9: inner support tube.  The axis of the waveguide points along the $z$-direction, the plates are separated along $x$, and the plate width extends along $y$. During the EDM studies, the main field component $E_x$ points radially outwards and $H_y$  upwards with respect to the stored beam.}
\end{figure*}

A sophisticated support structure, shown in Fig.~\ref{fig:support}, was designed to ensure a high degree of rigidity, precision alignment and most importantly, low distortion of the generated electromagnetic field. The electrodes are made from double T-shaped copper plates for better stiffness and stability. They are mounted with 12 stainless-steel screws on the main support structure of the electrodes. An inner stainless-steel tube is used to hold, align and mount the structure with high precision ($\approx 10$\,$\mu$m).

The angular position of the RF Wien filter with respect to the beam axis can be chosen to allow for a rotation of the field direction by $\ang{90}$ with an angular precision of $\ang{0.01}$ ($\SI{0.175}{mrad}$) using a belt drive (see label 5 in Fig.~\ref{fig:structure}), without breaking the vacuum. This feature is foreseen in order to fine tune the orientation of the RF Wien filter~\cite{doi:10.1142/S2010194516600995}, and also to exchange the role of electric and magnetic fields during the EDM measurements for  investigations of the systematics.

\begin{figure*}[!t]
\centering
\includegraphics[width=10cm]{support.eps}
\caption{\label{fig:support} Inner support structure of the RF Wien Filter. 1: copper electrodes with the trapezium shaping at the edges; 2: specially designed connector; 3: ceramic insulator between the electrodes and the support structure. A stainless-steel screw is located inside to connect the electrodes to the support structure; 4: support of the electrodes; 5: clamps to support the ferrite cage; 6: inner tube support structure.}
\end{figure*}

The parallel-plates waveguide constitutes a transmission line structure, which has been analyzed in detail in~\cite{slimpstp2015}. For the waveguide to be used as a Wien filter, wave mismatch must be introduced into the structure via a reflection, ensured by adding a resistor that controls the reflection coefficient. Thereby, the characteristic field quotient $Z_q$, given in Eq.~(\ref{eq:zq}), can be adjusted to the required value.

The multi-input feedthroughs ensure a homogeneous current distribution over the electrodes, the two parallel plates are connected to the amplifier and the load via four high-frequency, high-power CF 40 feedthroughs\footnote{VACOM Vakuum Komponenten $\&$ Messtechnik GmbH, Jena, Germany, \url{http://www.vacom.de}}. The electrodes are connected to the feedthroughs via specially designed connectors, and $3$\,dB power splitters are installed to feed each plate at the edges.

\begin{figure}[hbt]
\centering
\includegraphics[width=0.9\columnwidth]{circuit.eps}
\caption{\label{fig:circuit} Schematic of the driving circuit for the RF Wien filter. The condition for minimal Lorentz force is met using two adjustable devices, the resistor $R_\text{m}$ and the inductance $L_\text{p}$.}
\end{figure}

The RF Wien filter comprises a cage of ferrite blocks from CMD5005, which contains the electromagnetic fields within the Wien filter and enhances the field homogeneity.  CMD5005 is a NiZn-type high-permeability ferrite material. Ref.~\cite{Hahn_rhicabort} provides data of a dispersive model of the complex permeability $\mu_r$ that have been used in the full-wave simulator at frequencies from $\SI{100}{kHz}$ to 2\,MHz. At a frequency of $\SI{871}{kHz}$, for instance, $\mu_r=1449 + i 467$. The relative permittivity $\epsilon_r$ is around 25~\cite{zhang}. CMD5005 elements come in blocks with a maximum length of 330\,mm. The ferrites are held together by clamps in a symmetric arrangement. To make a connection between the electrodes and the main support structure possible, 12 boreholes are drilled in the ferrites. The screws supporting the electrodes are insulated from the ferrites by ceramic cylindrical insulators.

On each side of the CF100 entry and exit ports, 4 CF16 feedthroughs are connected for the beam position monitors (BPMs)~\cite{hinder}, especially designed to ensure the alignment of the beam with respect to the axis of the RF Wien filter.

\subsection{Driving circuit \label{sec:driving_circuit}}
Ideally, the driving circuit connects a load resistor directly to the electrodes, as shown in~\cite{slimpstp2015}. The amplifier (with 50 $\Omega$ internal impedance) is connected to the Wien filter via a ($1:0.5$) Balun~\cite{hickman} which transforms the impedance and converts the unbalanced output of the amplifier into a balanced one. A schematic of the driving circuit is shown in Fig.~\ref{fig:circuit}. One of the main characteristics of Balun transformers is their broadband response. The return loss $L_\text{r}$ of the driving circuit (Fig.~\ref{fig:circuit}), calculated using CST Design Studio, is depicted in Fig.~\ref{fig:s_11}.
\begin{figure}[hbt]
\centering
\includegraphics[width=0.9\columnwidth]{s11.eps}
\caption{\label{fig:s_11} Return loss $L_\text{r}$  of the electromagnetic simulation of the circuit, shown Fig.~\ref{fig:circuit}. For the entire range of spin harmonics (from 0 to $\SI{2}{MHz}$), $L_\text{r}$ is below $\SI{-15}{dB}$. Wideband matching is  achieved with a $1:0.5$ Balun.}
\end{figure}

Another ($1:1$) Balun is used to connect the Wien filter to the load resistor. Introducing the cables into the schematic induces a phase shift between the electric and magnetic field,
consequently, the field quotient $Z_q$, given in Eq.~(\ref{eq:zq}), becomes complex.
This effect can be compensated with an additional inductance $L_\text{p} = 8\,\mu\text{H}$  (as indicated in Fig.~\ref{fig:circuit}), and an almost purely real-valued field quotient is obtained, \textit{i.e.}, $Z_q = 173\,\angle 0.1 ^\circ \Omega$.

\subsection{Optimization of field homogeneity \label{sec:field_homogenity}}
\label{sec:EM-simulation}
For minimal Lorentz forces, the electric and magnetic forces must be matched at the center and the edges of the RF Wien filter. As mentioned before, the ratio between the electric and magnetic fields inside the RF Wien filter can be controlled via the load resistor $R_\text{m}$, and the inductance $L_\text{p}$ (see Fig.~\ref{fig:circuit}). A special solution, however, is required at the edges, where the slope of the curves of the electric and magnetic forces are not the same. According to our simulations, a simple parallel-plates waveguide deflects particles passing through the device in the same directions at the exit and entry points. Under these cir\-cum\-stances, the overall Lorentz force \textit{cannot} be zero when the Lorentz force in the center is zero.

Our solution aims at a decomposition of the kicks at each side of the RF Wien filter into two deflections of opposite sign in such a way that  both deflections average out. Decoupling the fields at the edges, keeping the electric field unchanged while manipulating the magnetic field, is accomplished by retaining the plate separation (spacing between the plates), while altering the width of the plates~\cite{pozar}. In doing so, the slope of the curves of the electric field remains constant while the magnetic field crosses the electric field, as shown in Fig.~\ref{fig:lorentz}. This approach results in trapezoid-shaped edges of the parallel plates, where the field crossing can now be optimized via the geometrical properties of the trapezoid-shaped edges. After performing a series of simulations, an optimum trapezoid depth of $\SI{50}{mm}$, with a short base of $\SI{50.5}{mm}$  and a long base of $\SI{100}{mm}$ was found, as shown in Fig.~\ref{fig:support} (label 1).
\begin{figure}[t]
\centering
\includegraphics[width=0.9\columnwidth]{lorentz.eps}
\caption{Electric force $F_\text{e}$, magnetic force $F_\text{m}$, and Lorentz force $F_\text{L}$  inside the RF Wien filter, for the geometry shown in Fig.~\ref{fig:structure}.   The integral Lorentz force is of the order of $10^{-3}$\,eV/m. The trapezoid-shaped electrodes at the entrance and exit of the RF Wien filter determine the crossing of electric and magnetic forces.}
\label{fig:lorentz}
\end{figure}

Integrating and averaging the Lorentz force $\vec F_\text{L}$, given in Eq.~(\ref{eq:lorentz}), along the axis of the RF Wien filter for the geometry shown in Fig.~\ref{fig:structure} at an input power of $\SI{1}{kW}$, yields
\begin{equation}
\frac{q}{\ell}\int_{-\ell/2}^{\ell/2} \left( \begin{array}{ccc}
                                       E_x - c \beta B_y\\
                                       E_y + c \beta B_x\\
                                       E_z
                                      \end{array} \right) dz=
\left(\begin{array}{ccc}
5.97 \times 10^{-3}\\
7.97 \times 10^{-3}\\
1.27 \times 10^{-21}\\
\end{array}\right) \, \text{eV/m}\,,
\label{eq:lorentz-force-value}
\end{equation}
where $\ell=\SI{1550}{mm}$ denotes the active length of the RF Wien filter, defined as the region where the fields are non-zero. The momentum variation in the beam of about $\Delta p / p = 10^{-4}$ translates into a variation of the required field quotient $Z_q$ of the same order of magnitude [see Eq.~(\ref{eq:zq})], and therefore, the resulting values for the Lorentz forces, given in Eq.~(\ref{eq:lorentz-force-value}), are acceptable.

The waveguide RF Wien filter can be rated according to its ability to manipulate the spins of the stored particles, and as a figure of merit, the field integral of $\vec B$ along the beam axis is evaluated, yielding for an input power of $\SI{1}{\,kW}$,
\begin{equation}
 \int_{-\ell/2}^{\ell/2} \vec B dz =
 \left(\begin{array}{ccc}
  2.73 \times 10^{-9}\\
  2.72 \times 10^{-2}\\
  6.96 \times 10^{-7}\\
 \end{array}
\right)\,\text{T\,mm}\,.
\end{equation}
Under these conditions, the corresponding integrated electric field components are given by
\begin{equation}
 \int_{-\ell/2}^{\ell/2} \vec E dz =
 \left(\begin{array}{rrr}
  3324.577 \\
  0.018\\
  0.006\\
 \end{array}
\right)\,\text{V}\,.
\end{equation}
Figure~\ref{fig:main_fields} shows the main (wanted) components of the electric and magnetic fields, $E_x$ and $B_y$, in the $xz$ plane.
\begin{figure*}[htb]
\centering
 \includegraphics[width=0.45\textwidth]{e_field_em.eps}
 \includegraphics[width=0.45\textwidth]{h_field_em.eps}
\caption{\label{fig:main_fields} Main components of the electric field $E_x$ (left panel) and magnetic field $H_y$ (right) of the waveguide RF Wien filter, for the geometry shown in Figs.~\ref{fig:structure} and \ref{fig:support}.}
\end{figure*}

In order to further increase the field homogeneity, the geometric parameters such as the width and the surface shape of the copper electrodes, and also the geometric parameters of the ferrite blocks and their distance to the metallic support structure, including the surrounding vacu\-um vessel were optimized. The simulations showed that parabolically-shaped electrode surfaces instead of flat ones substantially improve the local homogeneity of the electric field along the RF Wien filter. The parameters of the optimized parabolic electrodes are indicated in Fig.~\ref{fig:parabolic_design}, with a major radius of $\SI{91}{mm}$, and a minor radius of $\SI{6}{mm}$, where the sharp edges have been rounded using a $1$\,mm radius.  In the case of flat electrodes, the electric field varies up to $\SI{8}{\text{V/m}}$, and by parabolically shaping the electrodes, as shown in Fig.~\ref{fig:parabolic_design}, the electric field variation does not exceed $\SI{0.1}{\text{V/m}}$.
\begin{figure}[hbt]
\centering
\includegraphics[width=1\columnwidth]{para.eps}
\caption{\label{fig:parabolic_design} Schematic view of the cross section of the bottom electrode. The double T-shaped support is used to fix the electrode to the support structure. The parabolic shape is constructed using an ellipse with a major radius of $a = \SI{91}{\,mm}$, and a minor radius of $b=\SI{6}{mm}$. Sharp edges are avoided using a rounding radius of $1\,$mm.}
\end{figure}

The advantage of using parabolic electrodes is illustrated in Fig.~\ref{fig:parabolic_vs_flat}, where the relative variation of $E_x$ and $H_y$, \textit{e.g.}, $\left| \frac{|E_x|-\langle E_x \rangle}{|E_x|}\right|$, is shown in the $xy$ plane in  the range $x=\pm 5$\,mm and   $y=\pm 5$\,mm in the center of the RF Wien filter. The results are summarized in Table~\ref{tab:c_variation}, where the relative standard deviation of  panels a), b), d), and e) of Fig.~\ref{fig:parabolic_vs_flat} is listed. With respect to the Lorentz force $\vec F_\text{L}$, parabolically-shaped electrodes show a better homogeneity along the beam trajectory.

\begin{table}[b]
\renewcommand{\arraystretch}{1.1}
\centering
\caption{Calculated relative standard deviation (RSD) of the electric and magnetic fields in the cases of flat and parabolically-shaped electrodes from Fig.~\ref{fig:parabolic_vs_flat}, quantitatively indicating the achieved field homogeneity.}
\begin{tabular}{ccc}\hline
RSD & flat shape & parabolic shape\\ \hline
$|\frac{\sigma(E_x)}{\langle E_x\rangle}|$ & $4.74 \times 10^{-4}$ & $2.33 \times 10^{-5}$\\
$|\frac{\sigma(H_y)}{\langle H_y\rangle}|$ & $3.17 \times 10^{-5}$ & $ 3.53 \times 10^{-4}$\\\hline
\end{tabular}
\label{tab:c_variation}
\end{table}

The parabolically-shaped electrodes yield an improve\-ment of up to a factor of $20$ in terms of local electric field homogeneity while reducing the local homogeneity of the magnetic field  by a factor of $11$. In total, the Lorentz forces are about a factor $5$ smaller for parabolically-shaped electrodes with respect to flat-shaped electrodes.

\begin{figure*}[htb]
\centering
\subfigure[Flat: $\left|\frac{|E_x|- \left\langle E_x \right\rangle}{|E_x|} \right|$]{\includegraphics[width=0.32\textwidth]{e_flat.eps}}
\subfigure[Flat: $\left|\frac{|H_y|- \left\langle H_y \right\rangle}{|H_y|} \right|$]{\includegraphics[width=0.32\textwidth]{h_flat.eps}}
\subfigure[Flat: Lorentz force in $\text{eV/m}$]
{\includegraphics[width=0.32\textwidth]{lorentz_flat.eps}}
\subfigure[Parabolic: $\left|\frac{|E_x|- \left\langle E_x \right\rangle}{|E_x|} \right|$]{\includegraphics[width=0.32\textwidth]{e_para.eps}}
\subfigure[Parabolic:  $\left|\frac{|H_y|- \left\langle H_y \right\rangle}{|H_y|} \right|$]{\includegraphics[width=0.32\textwidth]{h_para.eps}}
\subfigure[Parabolic: Lorentz force in $\text{eV/m}$]
{\includegraphics[width=0.32\textwidth]{lorentz_para.eps}}
\caption{\label{fig:parabolic_vs_flat} Comparison of the field homogeneity of flat-shaped and parabolically-shaped electrodes across the beam extension in the range $\pm x=5$\,mm and   $\pm y=5$\,mm around the beam axis in the center of the RF Wien filter ($z=0$). The top row shows the fields variation of $E_x$ (panel a) and $H_y$ (b), and the Lorentz force (c) for flat-shaped electrodes, and the bottom row the corresponding results for parabolically-shaped electrodes, $E_x$ (d), $H_y$ (e), and the Lorentz force (f). (Note the different in the graphs.)}
\end{figure*}

\section{Beam dynamics simulations \label{sec:beam_dynamics_simulations}}
\subsection{Definition of unwanted field components}
The finite beam size induces non-ideal field components of $\vec{E}$ and $\vec{H}$ in undesired directions. The system is designed such that \textit{unwanted} field components average out at the exit and entry of the RF Wien filter. The unwanted field components are given by
\begin{equation}
\vec{E}_\perp = \left(\begin{array}{ccc}
  0\\
  E_y \\
  E_z\\
 \end{array}\right)  \,, \, \text{and}\,\,
\vec{H}_\perp = \left(\begin{array}{ccc}
  H_x\\
  0 \\
  H_z\\
 \end{array}\right) \label{eq:EH_perp} \,.
\end{equation}
The index $\perp$ is introduced to indicate that the unwanted electric and magnetic field components are perpendicular to the  main field components $E_x$ and $H_y$.

The particle beam that enters the RF Wien filter has a defined phase-space distribution~\cite{PhysRevSTAB.18.020101}, therefore the particles do not travel along straight lines, parallel to the beam axis. In order to quantify the effect of unwanted field components, a Monte-Carlo simulation based on solving the relativistic equation of motion using the time- and space-dependent fields has been carried out, with subsequent integration of the field components along the trajectories. The electromagnetic simulation yields the complex fields $\tilde{\vec{E}}$ and $\tilde{\vec{H}}$. The real fields $\vec{E} \left(\vec{r},t\right)$ and $\vec{H} \left(\vec{r},t\right)$ are obtained from
\begin{eqnarray}
\vec{E} \left(\vec{r},t\right) & = & \Re\left( \tilde{\vec{E}}e^{i\omega t} \right) \,,\text{and} \nonumber\\
\vec{H} \left(\vec{r},t\right) & = & \Re\left( \tilde{\vec{H}}e^{i\omega t} \right)\,.
\end{eqnarray}
The relativistic equations of motion~\cite{humphries2013charged,wiedemann2015particle},
\begin{eqnarray}
\frac{d\vec{v}}{dt} &=&\frac{q}{m \gamma}\left[ \vec{E}(\vec r,t)+\vec{v}\times \vec{B}(\vec r,t)\right]\nonumber\\
&&-\frac{q}{m \gamma c^2} \vec{v} \left[\vec{v} \cdot \vec{E}(\vec r,t) \right]\,,\text{and} \nonumber\\
\frac{d\vec{r}}{dt} &=&\vec{v}\,,
\end{eqnarray}
were solved in Matlab$\footnote{Mathworks, Inc. Natick, Massachusetts, USA, \url{http://de.mathworks.com}}$ using the field maps imported from the full-wave simulations. The mesh accuracy in the ${xy}$ plane amounts to $\SI{0.1}{mm}$ and $\SI{11}{mm}$ in ${z}$-direction. The phase-space distribution of the beam at the entry of the RF Wien filter is known~\cite{PhysRevSTAB.18.020101}. The phase space is expressed in terms of positions $x$ and $y$, and transverse angles $x'= v_x/v_z$ and $y'= v_y/v_z$.

For each phase space, 5000 particles have been simulated with a $2\sigma$ beam emittance of $\varepsilon_{x,y}=1$\,$\mu$m. The initial ${(x,x')}$  phase-space distributions are shown in red in Fig.~\ref{fig:pse_beta}. The blue points represent the phase-space distribution of the particles after passing through the RF Wien filter. The $(y,y')$ distributions are not shown, but are very similar. With low-$\beta$ section ON and OFF, the areas of the initial and final ellipses are the same, and consistent with the behavior of a field free (drift) region. Thus the RF Wien filter does not appear to alter the phase-space distributions of the beam.

\begin{figure*}[htb]
\centering
\subfigure[Low-$\beta$ section ON: $\beta = \SI{0.4}{m}$]{\includegraphics[width=0.40\textwidth]{pse_x_bl.eps}}
\subfigure[Low-$\beta$ section OFF: $\beta = \SI{4}{m}$]{\includegraphics[width=0.40\textwidth]{pse_x_bh.eps}}
\caption{\label{fig:pse_beta} Phase-space distributions $(x,x')$ of a beam with an emittance of $\varepsilon_{x,y}=1$\,$\mu$m entering (red) and exiting (blue) the RF Wien filter with low-$\beta$ section ON:  $\beta = \SI{0.4}{m}$ (panel a) and OFF: $\beta = \SI{4}{m}$ (panel b). The ellipses show the $2 \sigma$ results of a fit with a 2D Gaussian distribution, respectively.}
\end{figure*}

Based on the simulations, the effects of unwanted field components, given in Eq.~(\ref{eq:EH_perp}), are quantified by integration along the particle trajectories, and expressed as ratios with respect to the total field integrals, yielding
\begin{eqnarray}
f^\text{int}_{E_\perp} &=& \frac{\int |\vec E_\perp|ds}{\int |\vec E| ds} \,\,, \text{and}    \label{eq:e_field_err} \\
f^\text{int}_{H_\perp} &=& \frac{\int |\vec H_\perp|ds}{\int |\vec H| ds}  \label{eq:b_field_err}\,,
\end{eqnarray}
where $ds$ denotes a differential element of the particle's path, which does not necessarily point along the $z$ axis.

\subsection{Results of beam dynamics simulations \label{sec:results}}
The integrals in Eqs.~(\ref{eq:e_field_err}) and (\ref{eq:b_field_err}) are evaluated for each of the 5000 simulated particles and for the two cases with low-$\beta$ section ON ($\beta=\SI{0.4}{m}$) and OFF ($\beta=\SI{4}{m}$). The results are shown in the left panels of Fig.~\ref{fig:parasitic}, respectively, and summarized in Table~\ref{tab:RF-WF-comparison}. With low-$\beta$ section ON, the beam size is small, and the angular variation is large. As a consequence, the unwanted field components $E_\perp$ and $H_\perp$ do not completely cancel at the edges, leading to a wider distribution. When the low-$\beta$ section is OFF, the beam size is large, but with small angular spread, leading to a better cancellation of the unwanted field components at the edges.
\begin{figure*}[!]
\centering
\subfigure[Unwanted electric field components $f^\text{int}_{E_\perp}$ from Eq.~(\ref{eq:e_field_err}).]
{\includegraphics[width=0.48\textwidth]{e_err.eps}}
\hspace{0.5cm}
\subfigure[Unwanted magnetic field components $f^\text{int}_{H_\perp}$ from Eq.~(\ref{eq:b_field_err}).]
{\includegraphics[width=0.48\textwidth]{b_err.eps}}
\caption{\label{fig:parasitic} Comparison of the unwanted electric (a) and magnetic (b) field components with low-$\beta$ section ON (red) and low-$\beta$ section OFF (blue). The right panels display Gaussian fits to the projections of the simulated distributions, respectively, summarized in Table~\ref{tab:RF-WF-comparison}.}
\end{figure*}

The distributions in Fig.~\ref{fig:parasitic} exhibit a line structure that stems from the finite mesh ($\SI{0.1}{mm}$ in the $xy$ plane, and $\SI{11}{mm}$ along $z$).
Gaussian fits to the projections of the resulting distributions $f^\text{int}_{E_\perp}$ and $f^\text{int}_{H_\perp}$ are shown in the right panels of Fig.~\ref{fig:parasitic}, the fit results are summarized in Table~\ref{tab:RF-WF-comparison}. For the low-$\beta$ section either switched ON or OFF, the mean values of the integrated unwanted electric and magnetic field components are zero within the errors. The widths of the distributions, however, indicate that with low-$\beta$ section OFF, the unwanted electric and magnetic field components picked up by the beam are smaller by about a factor of two to three compared to when the low-$\beta$ section is ON. These findings should be further investigated using spin and particle tracking of a stored beam. In addition, once the device is installed, the predictions should be verified experimentally.

\subsection{Comparison of prototype  and waveguide RF Wien filter \label{sec:comparison}}
The waveguide RF Wien filter is compared to the prototype RF Wien filter~\cite{Mey:2015xbq,doi:10.1142/S2010194516600946} (see Section~\ref{sec:introduction}) in terms of the unwanted field components, evaluated according to Eqs.~(\ref{eq:e_field_err}) and (\ref{eq:b_field_err}), listed in Table~\ref{tab:RF-WF-comparison}. With the novel waveguide RF Wien filter, unwanted electric and magnetic field components can be reduced by one to two orders of magnitude.
\begin{table*}[hbt]
\renewcommand{\arraystretch}{1.1}
\centering
\caption{Comparison of the prototype RF Wien filter~\cite{Mey:2015xbq,doi:10.1142/S2010194516600946} and the waveguide design in terms of  unwanted field components $f^\text{int}_{E_\perp}$ and $f^\text{int}_{H_\perp}$ (see Eqs.~(\ref{eq:e_field_err}) and (\ref{eq:b_field_err})) using the simulated data, shown in Fig.~\ref{fig:parasitic}.
}
\begin{tabular}{lccccc}\hline
     RF Wien filter    &  & \multicolumn{2}{c}{$f^\text{int}_{E_\perp}$} & \multicolumn{2}{c}{$f^\text{int}_{H_\perp}$}\\
             &                                &  mean $\mu$ & width $\sigma$ & mean $\mu$ & width $\sigma$ \\\hline
\multirow{ 2}{*}{Waveguide} & low-$\beta$ ON  & $(-1.1 \pm 0.8) \times 10^{-5}$ & $5.7 \times 10^{-4}$ & $(1.4 \pm 0.8) \times 10^{-5}$  & $5.5 \times 10^{-4}$\\
                            & low-$\beta$ OFF & $(4.1 \pm 2.5) \times 10^{-6}$ & $1.8 \times 10^{-4}$ & $(0.8 \pm 2.6) \times 10^{-6}$ & $1.8 \times 10^{-4}$\\\hline
Prototype &  & $(1.454 \pm 0.009) \times 10^{-2}$ & $8.9 \times 10^{-3}$ & $(2.927 \pm 0.003) \times 10^{-2}$   & $3.0\times 10^{-3}$\\
\hline
 \end{tabular}
\label{tab:RF-WF-comparison}
\end{table*}

Spin tracking simulation are required to quantify the systematic errors induced by unwanted field components, and by other systematic effects, \textit{e.g.}, positioning errors, and non-vanishing Lorentz forces   in the determination of proton and deuteron EDMs.

\section{Thermal response of the RF Wien filter during operation \label{sec:thermal}}
Because of the thermal insulation by the vacuum, it was investigated whether even small power losses could lead to a temperature rise during operation. Therefore, for the corresponding thermal simulations using the FEM software ANSYS\footnote{ANSYS, Inc. Canonsburg, USA \url{http://http://www.ansys.com}} only radiative heat exchange between the internal surfaces was considered. The entire power in the copper plates was assumed to be generated at the locations of the feedthroughs. The calculated power loss densities with an input power of $\SI{1}{kW}$ were integrated over the structural elements and are summarized in Table~\ref{tbl_2}. The temperature rise for the copper electrodes is $6$\,K, and $0.5$\,K for the ferrites, respectively. Based on the temperature distribution, the simulated thermal expansion of the copper plates amounts to $48$\,$\mu $m, and to $8.6$\,$\mu$m for the ferrite blocks. These values are small compared to the structure's dimensions, and we do not expect problems with respect to the electromagnetic performance and the suspension of the mechanical structure during operation of the RF Wien filter.

\begin{table}[htb]
\renewcommand{\arraystretch}{1.1}
  \centering
\caption{\label{tbl_2} Losses per material computed using the electromagnetic solver of CST Microwave Studio at a frequency $f_\text{RF}=\SI{1}{MHz}$.}
  \begin{tabular}{rl}
\hline
   Material        & Thermal losses [W] \\\hline
   Copper Plates   & 0.0957 \\
   Ferrites        & 0.0734  \\
   Steel           & 0.0255 \\
   Ceramic         & 0.0010 \\\hline
 \end{tabular}
\end{table}

\section{Conclusion and Outlook \label{sec:conclusion}}
The paper presents the electromagnetic design calculations for a novel type of RF Wien filter that shall be used at the COSY storage ring to determine the EDMs of deuterons and protons. The main emphasis of the work was to design a device that exhibits a high level of homogeneity of the electric and magnetic fields. The optimization of the electromagnetic design was performed in close cooperation with the mechanical design, taking into account all the details of the mechanical construction.

A waveguide structure was selected because in this case the orthogonality of $E$ and $B$ fields, required for a Wien filter, can be ensured to a high level of precision. Minimizing the overall Lorentz force, while still providing  sizable electric and magnetic field integrals, leads to parabolically-shaped electrodes, equipped with trapezoid-shaped entrance and exit partitions, surrounded by a closed box of ferrites. Using the above described electrodes, the overall Lorentz force is reduced by about a factor of five, compared to flat-shaped electrodes.

The RF Wien filter will be installed in a section at COSY, where it is possible to vary the beam size by adjusting the $\beta$ function between about $\beta=0.4$ and $\SI{4}{m}$. Single-pass tracking calculations were performed in order to quantify the effect of unwanted electric and magnetic field components picked up by the beam, realistically distributed in phase space. The calculations indicate that unwanted field components picked up by the beam can be reduced by about a factor of three when the RF Wien filter is operated at $\beta=\SI{4}{m}$.

Thermal simulations show that the heat load in the different materials of the device, when operated with an input power of $\SI{1}{kW}$ is tolerable. Thus during operation, the mechanical accuracy of the device does not appear to substantially deteriorate the performance.

Forthcoming work will address improvements of the driving circuit, which will allow us to reach larger field values with the same input power. In addition, a novel semi-analytical approach to assess quantitatively the effect of mechanical tolerances on the electromagnetic performance of the RF Wien filter is presently being developed. In addition, spin tracking studies are planned to quantify the impact of the waveguide RF Wien filter on the systematic error of the planned proton and deuteron EDM experiments.

\section*{Acknowledgement}
This work has been performed in the framework of the JEDI (J{\"u}lich Electric Dipole Moment Investigations) collaboration. The authors would like to thank Martin Gaisser, Volker Hejny, Kirill Grigoryev, and J{\"o}rg Pretz for valuable comments on the manuscript.

\section*{References}