text:
package cucumber.cukeulator.test;

import android.test.ActivityInstrumentationTestCase2;
import cucumber.api.CucumberOptions;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import cucumber.cukeulator.CalculatorActivity;
import cucumber.cukeulator.R;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

/**
 * We extend ActivityInstrumentationTestCase2 in order to have access to methods like getActivity
 * and getInstrumentation. Depending on what methods we are going to need, we can put our
 * step definitions inside classes extending any of the following Android test classes:
 *

 * ActivityInstrumentationTestCase2
 * InstrumentationTestCase
 * AndroidTestCase
 *

 * The CucumberOptions annotation is mandatory for exactly one of the classes in the test project.
 * Only the first annotated class that is found will be used, others are ignored. If no class is
 * annotated, an exception is thrown.
 *

 * The options need to at least specify features = "features". Features must be placed inside
 * assets/features/ of the test project (or a subdirectory thereof).
 */
@CucumberOptions(features = "features")
public class CalculatorActivitySteps extends ActivityInstrumentationTestCase2<CalculatorActivity> {

    public CalculatorActivitySteps(SomeDependency dependency) {
        super(CalculatorActivity.class);
        assertNotNull(dependency);
    }

    @Given("^I have a CalculatorActivity$")
    public void I_have_a_CalculatorActivity() {
        assertNotNull(getActivity());
    }

    @When("^I press (\\d)$")
    public void I_press_d(final int d) {
        switch (d) {
            case 0:
                onView(withId(R.id.btn_d_0)).perform(click());
                break;
            case 1:
                onView(withId(R.id.btn_d_1)).perform(click());
                break;
            case 2:
                onView(withId(R.id.btn_d_2)).perform(click());
                break;
            case 3:
                onView(withId(R.id.btn_d_3)).perform(click());
                break;
            case 4:
                onView(withId(R.id.btn_d_4)).perform(click());
                break;
            case 5:
                onView(withId(R.id.btn_d_5)).perform(click());
                break;
            case 6:
                onView(withId(R.id.btn_d_6)).perform(click());
                break;
            case 7:
                onView(withId(R.id.btn_d_7)).perform(click());
                break;
            case 8:
                onView(withId(R.id.btn_d_8)).perform(click());
                break;
            case 9:
                onView(withId(R.id.btn_d_9)).perform(click());
                break;
        }
    }

    @When("^I press ([+–x\\/=])$")
    public void I_press_op(final char op) {
        switch (op) {
            case '+':
                onView(withId(R.id.btn_op_add)).perform(click());
                break;
            case '–':
                onView(withId(R.id.btn_op_subtract)).perform(click());
                break;
            case 'x':
                onView(withId(R.id.btn_op_multiply)).perform(click());
                break;
            case '/':
                onView(withId(R.id.btn_op_divide)).perform(click());
                break;
            case '=':
                onView(withId(R.id.btn_op_equals)).perform(click());
                break;
        }
    }

    @Then("^I should see (\\S+) on the display$")
    public void I_should_see_s_on_the_display(final String s) {
        onView(withId(R.id.txt_calc_display)).check(matches(withText(s)));
    }
}


text:
package org.apache.abdera.model;

import java.net.URI;
import java.net.URISyntaxException;

/**
 *
An Atom Person Construct

 *
 *
Per RFC4287:

 *
 *

 *   A Person construct is an element that describes a person,
 *   corporation, or similar entity (hereafter, 'person').
 *
 *   atomPersonConstruct =
 *     atomCommonAttributes,
 *     (element atom:name { text }
 *      & element atom:uri { atomUri }?
 *      & element atom:email { atomEmailAddress }?
 *      & extensionElement*)
 *
 *

 */
public interface Person
  extends ExtensibleElement, Element {

  /**
   * The "atom:name" element's content conveys a human-readable name for
   * the person.  The content of atom:name is Language-Sensitive.  Person
   * constructs MUST contain exactly one "atom:name" element.
   */
  Element getNameElement();

  /**
   * The "atom:name" element's content conveys a human-readable name for
   * the person.  The content of atom:name is Language-Sensitive.  Person
   * constructs MUST contain exactly one "atom:name" element.
   */
  void setNameElement(Element element);

  /**
   * The "atom:name" element's content conveys a human-readable name for
   * the person.  The content of atom:name is Language-Sensitive.  Person
   * constructs MUST contain exactly one "atom:name" element.
   */
  Element setName(String name);

  /**
   * The "atom:name" element's content conveys a human-readable name for
   * the person.  The content of atom:name is Language-Sensitive.  Person
   * constructs MUST contain exactly one "atom:name" element.
   */
  String getName();

  /**
   * The "atom:email" element's content conveys an e-mail address
   * associated with the person.  Person constructs MAY contain an
   * atom:email element, but MUST NOT contain more than one.  Its content
   * MUST conform to the "addr-spec" production in [RFC2822].
   */
  Element getEmailElement();

  /**
   * The "atom:email" element's content conveys an e-mail address
   * associated with the person.  Person constructs MAY contain an
   * atom:email element, but MUST NOT contain more than one.  Its content
   * MUST conform to the "addr-spec" production in [RFC2822].
   */
  void setEmailElement(Element element);

  /**
   * The "atom:email" element's content conveys an e-mail address
   * associated with the person.  Person constructs MAY contain an
   * atom:email element, but MUST NOT contain more than one.  Its content
   * MUST conform to the "addr-spec" production in [RFC2822].
   */
  Element setEmail(String email);

  /**
   * The "atom:email" element's content conveys an e-mail address
   * associated with the person.  Person constructs MAY contain an
   * atom:email element, but MUST NOT contain more than one.  Its content
   * MUST conform to the "addr-spec" production in [RFC2822].
   */
  String getEmail();

  /**
   * The "atom:uri" element's content conveys an IRI associated with the
   * person.  Person constructs MAY contain an atom:uri element, but MUST
   * NOT contain more than one.  The content of atom:uri in a Person
   * construct MUST be an IRI reference [RFC3987].
   */
  IRI getUriElement();

  /**
   * The "atom:uri" element's content conveys an IRI associated with the
   * person.  Person constructs MAY contain an atom:uri element, but MUST
   * NOT contain more than one.  The content of atom:uri in a Person
   * construct MUST be an IRI reference [RFC3987].
   */
  void setUriElement(IRI uri);

  /**
   * The "atom:uri" element's content conveys an IRI associated with the
   * person.  Person constructs MAY contain an atom:uri element, but MUST
   * NOT contain more than one.  The content of atom:uri in a Person
   * construct MUST be an IRI reference [RFC3987].
   * @throws URISyntaxException
   */
  IRI setUri(String uri) throws URISyntaxException;

  /**
   * The "atom:uri" element's content conveys an IRI associated with the
   * person.  Person constructs MAY contain an atom:uri element, but MUST
   * NOT contain more than one.  The content of atom:uri in a Person
   * construct MUST be an IRI reference [RFC3987].
   * @throws URISyntaxException
   */
  URI getUri() throws URISyntaxException;
}

redpajama_set_name: RedPajamaGithub


text:
docker rm -f releasr-db

docker run --name releasr-db \
    -p 27017:27017 \
    -d \
    -v `pwd`/init/:/opt/ \
    mongo --auth

# give the container some time to start
sleep 2

docker exec -it releasr-db mongo admin /opt


text:
<?php

namespace Aws\ElastiCache\Exception;

/**
 * The requested reserved cache node was not found.
 */
class ReservedCacheNodeNotFoundException extends ElastiCacheException {}


text:
class Application::HeaderPresenter < Curly::Presenter
  def logo
    image_tag "something.png"
  end

  def login
    if user_signed_in?
      "Welcome, #{current_user.name}!"

    else
      link_to(
        image_tag("sign_in.png"),
        main_app.user_omniauth_authorize_path(:steam)
      ).html_safe
    end
  end

  def avatar_image
    if user_signed_in?
      image_tag current_user.avatar
    else
      ""
    end
  end

  def admin_link
    if user_signed_in? && current_user.forem_admin
      link_to forem.admin_root_path do
        content_tag :li, "Admin"
      end
    end
  end
end


text:
set -eu

swagger generate model -f api/swagger.yaml \
    -t api -m types --skip-validator \
    -n Volume \
    -n Port \
    -n ImageSummary \
    -n Plugin -n PluginDevice -n PluginMount -n PluginEnv -n PluginInterfaceType


text:
package edu.gemini.obslog.config.model;

import java.io.Serializable;
import java.util.logging.Logger;

//
// Gemini Observatory/AURA
// $Id: OlBasicLogItem.java,v 1.3 2005/02/22 19:21:42 gillies Exp $
//

public class OlBasicLogItem implements OlLogItem, Serializable {
    public static final Logger LOG = Logger.getLogger(OlBasicLogItem.class.getName());

    private String _key;
    private String _heading = OlLogItem.DEFAULT_ENTRY;
    private String _property = OlLogItem.DEFAULT_ENTRY;
    private String _sequenceName = OlLogItem.DEFAULT_ENTRY;
    private boolean _isVisible = true;

    /**
     * Creates a basic observing log entry cell model.
     *
     * @param key the key for this entry
     * @throws java.lang.NullPointerException if key or group is null.
     * @throws java.lang.IllegalArgumentException
     *                                        if the length of key or group is zero.
     */
    public OlBasicLogItem(String key) {
        if (key == null) throw new NullPointerException();
        if (key.length() == 0) throw new IllegalArgumentException("Entry must have a non-zero length key.");

        _key = key;
    }

    /**
     * Return the entry key that for this entry.
     */
    public String getKey() {
        return _key;
    }

    /**
     * Set the column heading.
     *
     * @param heading the name of the heading that will appear in a column that has this entry
     */
    public void setColumnHeading(String heading) {
        _heading = heading;
    }

    /**
     * Return the column heading that matches entries of this type.
     */
    public String getColumnHeading() {
        return _heading;
    }

    /**
     * Set the item name for this entry
     *
     * @param itemName the low-level name for this entry
     */
    public void setProperty(String itemName) {
        _property = itemName;
    }

    /**
     * Return the item name for this entry.
     *
     * @return a string name for this entry
     */
    public String getProperty() {
        return _property;
    }

    /**
     * Set the sequence name for this item.
     *
     * @param sequenceName
     */
    public void setSequenceName(String sequenceName) {
        _sequenceName = sequenceName;
    }

    /**
     * Return the sequence name for this item.
     *
     * @return
     */
    public String getSequenceName() {
        return _sequenceName;
    }

    /**
     * Should this item be visible in the log.
     *
     * @return true if it should be visible
     */
    public boolean isVisible() {
        return _isVisible;
    }

    /**
     * Set the visibility in the logs.
     *
     * @param visible true means is visible
     */
    public void setVisible(boolean visible) {
        _isVisible = visible;
    }

    public void dump() {
        LOG.info("property:     " + getProperty());
        LOG.info("heading:      " + getColumnHeading());
        LOG.info("sequenceName: " + getSequenceName());
        LOG.info("isVisible:    " + isVisible());
    }

}


text:
        Privacy Policy
        Copyright © {{ year }} All rights reserved


text:
#ifndef __ASM_MACH_LOONGSON_LOONGSON_H
#define __ASM_MACH_LOONGSON_LOONGSON_H

#include <linux/io.h>
#include <linux/init.h>

/* there is an internal bonito64-compatiable northbridge in loongson2e/2f */
#include <asm/mips-boards/bonito64.h>

/* loongson internal northbridge initialization */
extern void bonito_irq_init(void);

/* machine-specific reboot/halt operation */
extern void mach_prepare_reboot(void);
extern void mach_prepare_shutdown(void);

/* environment arguments from bootloader */
extern unsigned long bus_clock, cpu_clock_freq;
extern unsigned long memsize, highmemsize;

/* loongson-specific command line, env and memory initialization */
extern void __init prom_init_memory(void);
extern void __init prom_init_cmdline(void);
extern void __init prom_init_env(void);

/* irq operation functions */
extern void bonito_irqdispatch(void);
extern void __init bonito_irq_init(void);
extern void __init set_irq_trigger_mode(void);
extern void __init mach_init_irq(void);
extern void mach_irq_dispatch(unsigned int pending);

/* PCI Configuration Registers */
#define LOONGSON_PCI_ISR4C  BONITO_PCI_REG(0x4c)

/* PCI_Hit*_Sel_* */

#define LOONGSON_PCI_HIT0_SEL_L     BONITO(BONITO_REGBASE + 0x50)
#define LOONGSON_PCI_HIT0_SEL_H     BONITO(BONITO_REGBASE + 0x54)
#define LOONGSON_PCI_HIT1_SEL_L     BONITO(BONITO_REGBASE + 0x58)
#define LOONGSON_PCI_HIT1_SEL_H     BONITO(BONITO_REGBASE + 0x5c)
#define LOONGSON_PCI_HIT2_SEL_L     BONITO(BONITO_REGBASE + 0x60)
#define LOONGSON_PCI_HIT2_SEL_H     BONITO(BONITO_REGBASE + 0x64)

/* PXArb Config & Status */

#define LOONGSON_PXARB_CFG      BONITO(BONITO_REGBASE + 0x68)
#define LOONGSON_PXARB_STATUS       BONITO(BONITO_REGBASE + 0x6c)

/* loongson2-specific perf counter IRQ */
#define LOONGSON2_PERFCNT_IRQ   (MIPS_CPU_IRQ_BASE + 6)

#endif /* __ASM_MACH_LOONGSON_LOONGSON_H */


text:
var streams = ["freecodecamp", "storbeck", "terakilobyte", "habathcx", "RobotCaleb", "comster404", "brunofin", "thomasballinger", "noobs2ninjas", "beohoff", "medrybw"],
    twitchUrl = "https://api.twitch.tv/kraken/";

// Creates an array with all future ajax calls
var ajaxArr = streams.map(function(stream) {
    return $.ajax({
        url: twitchUrl + "channels/" + stream,
        dataType: "jsonp",
        success: function() {
            $(".loading").append(".");
        }
    });
}).concat(streams.map(function(stream) {
    return $.ajax({
        url: twitchUrl + "streams/" + stream,
        dataType: "jsonp",
        success: function() {
            $('.loading').append(".");
        }
    });
}));

// Wait for document to load until document dependent code is run.
$(document).ready(function() {

    $('main').empty().append("
Loading
");

    // Runs all ajax calls and calls the callback with their results as arguments
    $.when.apply($, ajaxArr).done(function() {

        // Create array from arguments
        args = Array.prototype.slice.call(arguments);

        // Remove loading message
        $('main').empty();

        // Create an array with appropriate HTML for every stream
        args.forEach(function(stream) {
            // Return if ajax request was unsuccessfull or
            // if it's not a channel request (i.e. if it's a stream request)
            if(stream[1] !== "success" || stream[0].display_name === undefined) {
                return false;
            }

            // Get stream information
            profilePicture = stream[0].logo ? stream[0].logo : "http://static-cdn.jtvnw.net/jtv_user_pictures/xarth/404_user_150x150.png";
            displayName = stream[0].display_name;
            channelUrl = stream[0].url;
            info = stream[0].status ? stream[0].status : "No information available";
            id = displayName.toLowerCase();

            // Loop through ajax responses...
            args.forEach(function(stream) {
                // ...and find the one related to this stream
                if(stream[1] === "success" && stream[0].display_name === undefined && stream[0]._links !== undefined) {
                    var streamSplit = stream[0]._links.channel.split('/');
                    if(id === streamSplit[streamSplit.length - 1]) {

                        // Check the online status of the stream and set variables accordingly
                        if(stream[0].stream) {
                            displayName = "• " + displayName;
                            info = "" + info + "";
                            onlineStatus = "online";
                        } else {
                            info = "";
                            onlineStatus = "offline";
                        }
                    }
                }
            });

            // Render current stream on screen
            $('main').append("
" + displayName + info + "
");

        });

    });

    // Add button functionality
    //
    // Bugish: If #offline is clicked before rendering all .online elements
    // the .online elements will be renered on with #offline active.
    // Can be prevented by not allowing these to be called until
    // all ajax and rendering functions are finished.
    $('#all').click(function() {
        $('.online').removeClass('hidden');
        $('.offline').removeClass('hidden');
        $('#all').addClass('active');
        $('#online').removeClass('active');
        $('#offline').removeClass('active');
    });

    $('#online').click(function() {
        $('.online').removeClass('hidden');
        $('.offline').addClass('hidden');
        $('#all').removeClass('active');
        $('#online').addClass('active');
        $('#offline').removeClass('active');
    });

    $('#offline').click(function() {
        $('.online').addClass('hidden');
        $('.offline').removeClass('hidden');
        $('#all').removeClass('active');
        $('#online').removeClass('active');
        $('#offline').addClass('active');
    });

    // Search functionality
    $('input[name=search]').keyup(function() {
        var children = Array.prototype.slice.call($('main').children());
        children.forEach(function(foo) {
            if(foo.id.match(new RegExp($('input[name=search]').val(), 'ig'))) {
                $('#' + foo.id).show();
            } else {
                $('#' + foo.id).hide();
            }
        });
    });

    // Prevent reloading on 'Enter' in search field
    $('input[name=search]').keypress(function(event) {
        if(event.keyCode === 10 || event.keyCode === 13) {
            event.preventDefault();
        }
    });

});


text:
"""Qt implementation of _Renderer and GUI."""

# Authors: Guillaume Favelier <guillaume.favelier@gmail.com>
#          Eric Larson <larson.eric.d@gmail.com>
#          Alex Rockhill <aprockhill@mailbox.org>
#
# License: Simplified BSD

from contextlib import contextmanager
import os
import platform
import sys
import weakref

import pyvista
from pyvistaqt.plotting import FileDialog, MainWindow
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvas

from qtpy.QtCore import (Qt, QTimer, QLocale, QLibraryInfo, QEvent,
                         # non-object-based-abstraction-only, deprecate
                         Signal, QObject)
from qtpy.QtGui import QIcon, QCursor, QKeyEvent
from qtpy.QtWidgets import (QComboBox, QGroupBox, QHBoxLayout, QLabel,
                            QSlider, QDoubleSpinBox, QVBoxLayout, QWidget,
                            QSizePolicy, QProgressBar, QScrollArea,
                            QLayout, QCheckBox, QButtonGroup, QRadioButton,
                            QLineEdit, QGridLayout, QFileDialog, QPushButton,
                            QMessageBox,
                            # non-object-based-abstraction-only, deprecate
                            QDockWidget, QToolButton, QMenuBar,
                            QSpinBox, QStyle, QStyleOptionSlider)

from ._pyvista import _PyVistaRenderer
from ._pyvista import (_close_3d_figure, _check_3d_figure, _close_all,  # noqa: F401,E501 analysis:ignore
                       _set_3d_view, _set_3d_title, _take_3d_screenshot)  # noqa: F401,E501 analysis:ignore
from ._abstract import (_AbstractAppWindow, _AbstractHBoxLayout,
                        _AbstractVBoxLayout, _AbstractGridLayout,
                        _AbstractWidget, _AbstractCanvas,
                        _AbstractPopup, _AbstractLabel, _AbstractButton,
                        _AbstractSlider, _AbstractCheckBox, _AbstractSpinBox,
                        _AbstractComboBox, _AbstractRadioButtons,
                        _AbstractGroupBox, _AbstractText, _AbstractFileButton,
                        _AbstractPlayMenu, _AbstractProgressBar)
from ._abstract import (_AbstractDock, _AbstractToolBar, _AbstractMenuBar,
                        _AbstractStatusBar, _AbstractLayout, _AbstractWdgt,
                        _AbstractWindow, _AbstractMplCanvas, _AbstractPlayback,
                        _AbstractBrainMplCanvas, _AbstractMplInterface,
                        _AbstractWidgetList, _AbstractAction, _AbstractDialog,
                        _AbstractKeyPress)
from ._utils import (_qt_disable_paint, _qt_get_stylesheet, _qt_is_dark,
                     _qt_detect_theme, _qt_raise_window, _init_mne_qtapp,
                     _qt_app_exec)
from ..utils import safe_event
from ...utils import _check_option, get_config
from ...fixes import _compare_version

# Adapted from matplotlib
if (sys.platform == 'darwin' and
        _compare_version(platform.mac_ver()[0], '>=', '10.16') and
        QLibraryInfo.version().segments() <= [5, 15, 2]):
    os.environ.setdefault("QT_MAC_WANTS_LAYER", "1")

# fix for qscroll needing two layouts, one parent, one child
def _get_layout(layout):
    if hasattr(layout, '_parent_layout'):
        return layout._parent_layout
    return layout

# -------
# Widgets
# -------
# The metaclasses need to share a base class in order for the inheritance
# not to conflict, http://www.phyast.pitt.edu/~micheles/python/metatype.html
# https://stackoverflow.com/questions/28720217/multiple-inheritance-metaclass-conflict

class _BaseWidget(type(QWidget), type(_AbstractWidget)):
    pass

# The inheritance has to be in this order for the _Widget and the opposite for
# the widgets (e.g. _PushButton) that inherit from it, not sure why
class _Widget(_AbstractWidget, QWidget, metaclass=_BaseWidget):

    tooltip = None
    _to_qt = dict(
        escape=Qt.Key_Escape,
        up=Qt.Key_Up,
        down=Qt.Key_Down,
        left=Qt.Key_Left,
        right=Qt.Key_Right,
        page_up=Qt.Key_PageUp,
        page_down=Qt.Key_PageDown,
    )
    _from_qt = {v: k for k, v in _to_qt.items()}

    def __init__(self):
        _AbstractWidget.__init__()
        # QWidget.__init__(self)

    def _show(self):
        self.show()

    def _hide(self):
        self.hide()

    def _set_enabled(self, state):
        self.setEnabled(state)

    def _is_enabled(self):
        return self.isEnabled()

    def _update(self, repaint=True):
        self.update()
        if repaint:
            self.repaint()

    def _get_tooltip(self):
        return self.toolTip()

    def _set_tooltip(self, tooltip):
        self.setToolTip(tooltip)

    def _set_style(self, style):
        stylesheet = ""
        for key, val in style.items():
            stylesheet = stylesheet + f"{key}:{val};"
        self.setStyleSheet(stylesheet)

    def _add_keypress(self, callback):
        self.keyPressEvent = lambda event: callback(
            self._from_qt[event.key()] if event.key() in self._from_qt else
            event.text())

    def _trigger_keypress(self, key):
        if key in self._to_qt:
            key_int = self._to_qt[key]
        else:
            key_int = getattr(Qt, f'Key_{key.upper()}')
        self.keyPressEvent(
            QKeyEvent(QEvent.KeyRelease, key_int, Qt.NoModifier, text=key))

    def _set_focus(self):
        self.setFocus()

    def _set_layout(self, layout):
        self.setLayout(_get_layout(layout))

    def _set_theme(self, theme=None):
        if theme is None:
            default_theme = _qt_detect_theme()
        else:
            default_theme = theme
        theme = get_config('MNE_3D_OPTION_THEME', default_theme)
        stylesheet = _qt_get_stylesheet(theme)
        self.setStyleSheet(stylesheet)
        if _qt_is_dark(self):
            QIcon.setThemeName('dark')
        else:
            QIcon.setThemeName('light')

    def _set_size(self, width=None, height=None):
        if width:
            self.setMinimumWidth(width)
            self.setMaximumWidth(width)
        if height:
            self.setMinimumHeight(height)
            self.setMaximumHeight(height)

class _Label(QLabel, _AbstractLabel, _Widget, metaclass=_BaseWidget):

    def __init__(self, value, center=False, selectable=False):
        _AbstractLabel.__init__(value, center=center, selectable=selectable)
        _Widget.__init__(self)
        QLabel.__init__(self)
        self.setText(value)
        if center:
            self.setAlignment(Qt.AlignCenter)
        self.setWordWrap(True)
        if selectable:
            self.setTextInteractionFlags(Qt.TextSelectableByMouse)

class _Text(QLineEdit, _AbstractText, _Widget, metaclass=_BaseWidget):

    def __init__(self, value=None, placeholder=None, callback=None):
        _AbstractText.__init__(value=value, placeholder=placeholder,
                               callback=callback)
        _Widget.__init__(self)
        QLineEdit.__init__(self, value)
        self.setPlaceholderText(placeholder)
        if callback is not None:
            self.textChanged.connect(callback)

    def _set_value(self, value):
        self.setText(value)

class _Button(QPushButton, _AbstractButton, _Widget, metaclass=_BaseWidget):

    def __init__(self, value, callback, icon=None):
        _AbstractButton.__init__(value=value, callback=callback)
        _Widget.__init__(self)
        QPushButton.__init__(self)
        self.setText(value)
        self.released.connect(callback)
        if icon:
            self.setIcon(QIcon.fromTheme(icon))

    def _click(self):
        self.click()

    def _set_icon(self, icon):
        self.setIcon(QIcon.fromTheme(icon))

class _Slider(QSlider, _AbstractSlider, _Widget, metaclass=_BaseWidget):

    def __init__(self, value, rng, callback, horizontal=True):
        _AbstractSlider.__init__(value=value, rng=rng, callback=callback,
                                 horizontal=horizontal)
        _Widget.__init__(self)
        QSlider.__init__(self, Qt.Horizontal if horizontal else Qt.Vertical)
        self.setMinimum(rng[0])
        self.setMaximum(rng[1])
        self.setValue(value)
        self.valueChanged.connect(callback)

    def _set_value(self, value):
        self.setValue(value)

    def _get_value(self):
        return self.value()

    def _set_range(self, rng):
        self.setRange(int(rng[0]), int(rng[1]))

class _ProgressBar(QProgressBar, _AbstractProgressBar, _Widget,
                   metaclass=_BaseWidget):

    def __init__(self, count):
        _AbstractProgressBar.__init__(count=count)
        _Widget.__init__(self)
        QProgressBar.__init__(self)
        self.setMaximum(count)

    def _increment(self):
        if self.value() + 1 > self.maximum():
            return
        self.setValue(self.value() + 1)
        return self.value()

class _CheckBox(QCheckBox, _AbstractCheckBox, _Widget, metaclass=_BaseWidget):

    def __init__(self, value, callback):
        _AbstractCheckBox.__init__(value=value, callback=callback)
        _Widget.__init__(self)
        QCheckBox.__init__(self)
        self.setChecked(value)
        self.stateChanged.connect(lambda x: callback(bool(x)))

    def _set_checked(self, checked):
        self.setChecked(checked)

    def _get_checked(self):
        return self.checkState() != Qt.Unchecked

class _SpinBox(QDoubleSpinBox, _AbstractSpinBox, _Widget,
               metaclass=_BaseWidget):

    def __init__(self, value, rng, callback, step=None):
        _AbstractSpinBox.__init__(value=value, rng=rng, callback=callback,
                                  step=step)
        _Widget.__init__(self)
        QDoubleSpinBox.__init__(self)
        self.setAlignment(Qt.AlignCenter)
        self.setMinimum(rng[0])
        self.setMaximum(rng[1])
        self.setKeyboardTracking(False)
        if step is None:
            self.setSingleStep((rng[1] - rng[0]) / 20.)
        else:
            self.setSingleStep(step)
        self.setValue(value)
        self.valueChanged.connect(callback)

    def _set_value(self, value):
        self.setValue(value)

    def _get_value(self):
        return self.value()

class _ComboBox(QComboBox, _AbstractComboBox, _Widget, metaclass=_BaseWidget):

    def __init__(self, value, items, callback):
        _AbstractComboBox.__init__(value=value, items=items, callback=callback)
        _Widget.__init__(self)
        QComboBox.__init__(self)
        self.addItems(items)
        self.setCurrentText(value)
        self.currentTextChanged.connect(callback)
        self.setSizeAdjustPolicy(QComboBox.AdjustToContents)

    def _set_value(self, value):
        self.setCurrentText(value)

    def _get_value(self):
        return self.currentText()

class _RadioButtons(QVBoxLayout, _AbstractRadioButtons, _Widget,
                    metaclass=_BaseWidget):

    def __init__(self, value, items, callback):
        _AbstractRadioButtons.__init__(
            value=value, items=items, callback=callback)
        _Widget.__init__(self)
        QVBoxLayout.__init__(self)
        self._button_group = QButtonGroup()
        self._button_group.setExclusive(True)
        for val in items:
            button = QRadioButton(val)
            if val == value:
                button.setChecked(True)
            self._button_group.addButton(button)
            self.addWidget(button)
        self._button_group.buttonClicked.connect(
            lambda button: callback(button.text()))

    def _set_value(self, value):
        for button in self._button_group.buttons():
            if button.text() == value:
                button.click()

    def _get_value(self):
        return self.checkedButton().text()

class _GroupBox(QGroupBox, _AbstractGroupBox, _Widget, metaclass=_BaseWidget):

    def __init__(self, name, items):
        _AbstractGroupBox.__init__(name=name, items=items)
        _Widget.__init__(self)
        QGroupBox.__init__(self, name)
        self._layout = _VBoxLayout()
        for item in items:
            self._layout._add_widget(item)
        self.setLayout(self._layout)

class _FileButton(_Button, _AbstractFileButton, _Widget,
                  metaclass=_BaseWidget):

    def __init__(self, callback, content_filter=None, initial_directory=None,
                 save=False, is_directory=False, icon='folder', window=None):
        _AbstractFileButton.__init__(
            callback=callback, content_filter=content_filter,
            initial_directory=initial_directory, save=save,
            is_directory=is_directory, window=window)
        _Widget.__init__(self)

        def fp_callback():
            if is_directory:
                name = QFileDialog.getExistingDirectory(
                    parent=window, directory=initial_directory
                )
            elif save:
                name = QFileDialog.getSaveFileName(
                    parent=window, directory=initial_directory,
                    filter=content_filter)
            else:
                name = QFileDialog.getOpenFileName(
                    parent=window, directory=initial_directory,
                    filter=content_filter)
            name = name[0] if isinstance(name, tuple) else name
            # handle the cancel button
            if len(name) == 0:
                return
            callback(name)

        _Button.__init__(self, '', callback=fp_callback, icon=icon)

class _PlayMenu(QVBoxLayout, _AbstractPlayMenu, _Widget,
                metaclass=_BaseWidget):

    def __init__(self, value, rng, callback):
        _AbstractPlayMenu.__init__(
            value=value, rng=rng, callback=callback)
        _Widget.__init__(self)
        QVBoxLayout.__init__(self)
        self._slider = QSlider(Qt.Horizontal)
        self._slider.setMinimum(rng[0])
        self._slider.setMaximum(rng[1])
        self._slider.setValue(value)
        self._slider.setTracking(False)
        self._slider.valueChanged.connect(callback)
        self._nav_hbox = QHBoxLayout()
        self._play_button = QPushButton()
        self._play_button.setIcon(QIcon.fromTheme('play'))
        self._nav_hbox.addWidget(self._play_button)
        self._pause_button = QPushButton()
        self._pause_button.setIcon(QIcon.fromTheme('pause'))
        self._nav_hbox.addWidget(self._pause_button)
        self._reset_button = QPushButton()
        self._reset_button.setIcon(QIcon.fromTheme('reset'))
        self._nav_hbox.addWidget(self._reset_button)
        self._loop_button = QPushButton()
        self._loop_button.setIcon(QIcon.fromTheme('restore'))
        self._loop_button.setStyleSheet('background-color : lightgray;')
        self._loop_button._checked = True

        def loop_callback():
            self._loop_button._checked = not self._loop_button._checked
            color = 'lightgray' if self._loop_button._checked else 'darkgray'
            self._loop_button.setStyleSheet(f'background-color : {color};')

        self._loop_button.released.connect(loop_callback)
        self._nav_hbox.addWidget(self._loop_button)
        self._timer = QTimer()

        def timer_callback():
            value = self._slider.value() + 1
            if value > rng[1]:
                if self._loop._checked:
                    self._timer.stop()
                value = rng[0]
            self._slider.setValue(value)

        self._timer.timeout.connect(timer_callback)
        self._timer.setInterval(250)
        self._play_button.released.connect(self._timer.start)
        self._pause_button.released.connect(self._timer.stop)
        self._reset_button.released.connect(
            lambda: self._slider.setValue(rng[0]))
        self.addWidget(self._slider)
        self.addLayout(self._nav_hbox)

    def _play(self):
        self._play_button.click()

    def _pause(self):
        self._pause_button.click()

    def _reset(self):
        self._reset_button.click()

    def _loop(self):
        self._loop_button.click()

    def _set_value(self, value):
        self._slider.setValue(value)

class _Popup(QMessageBox, _AbstractPopup, _Widget, metaclass=_BaseWidget):

    def __init__(self, title, text, info_text=None, callback=None,
                 icon='warning', buttons=None, window=None):
        _AbstractPopup.__init__(
            self, title=title, text=text, info_text=info_text,
            callback=callback, icon=icon, buttons=buttons, window=window)
        _Widget.__init__(self)
        QMessageBox.__init__(self)
        self.setWindowTitle(title)
        self.setText(text)
        # icon is one of _Dialog.supported_icon_names
        if icon is not None:
            self.setIcon(getattr(QMessageBox, icon.title()))
        if info_text:
            self.setInformativeText(info_text)

        if buttons is None:
            buttons = ['Ok']

        button_ids = list()
        for button in buttons:
            # button is one of _Dialog.supported_button_names
            button_id = getattr(QMessageBox, button)
            button_ids.append(button_id)
        standard_buttons = default_button = button_ids[0]
        for button_id in button_ids[1:]:
            standard_buttons |= button_id
        self.setStandardButtons(standard_buttons)
        self.setDefaultButton(default_button)
        if callback:
            self.buttonClicked.connect(
                lambda button: callback(button.text().title()))
        _qt_raise_window(self)
        self._show()

    def _click(self, value):
        self.button(getattr(QMessageBox, value)).click()

class _ScrollArea(QScrollArea):

    def __init__(self, width, height, widget):
        QScrollArea.__init__(self)
        self.setWidget(widget)
        self.setFixedSize(width, height)
        self.setWidgetResizable(True)

class _HBoxLayout(QHBoxLayout, _AbstractHBoxLayout, _Widget,
                  metaclass=_BaseWidget):

    def __init__(self, height=None, scroll=None):
        _AbstractHBoxLayout.__init__(self, height=height, scroll=scroll)
        _Widget.__init__(self)
        QHBoxLayout.__init__(self)

        if scroll is not None:
            self._scroll_widget = QWidget()
            self._parent_layout = QHBoxLayout()
            self._parent_layout.addWidget(
                _ScrollArea(scroll[0], scroll[1], self._scroll_widget))
            self._scroll_widget.setLayout(self)

        self._height = height

    def _add_widget(self, widget):
        """Add a widget to an existing layout."""
        if isinstance(widget, QLayout):
            self.addLayout(widget)
        else:
            if self._height is not None:
                widget.setMinimumHeight(self._height)
                widget.setMaximumHeight(self._height)
            self.addWidget(widget)

    def _add_stretch(self, amount=1):
        self.addStretch(amount)

class _VBoxLayout(QVBoxLayout, _AbstractVBoxLayout, _Widget,
                  metaclass=_BaseWidget):

    def __init__(self, width=None, scroll=None):
        _AbstractVBoxLayout.__init__(self, width=width, scroll=scroll)
        _Widget.__init__(self)
        QVBoxLayout.__init__(self)

        if scroll is not None:
            self._scroll_widget = QWidget()
            self._parent_layout = QHBoxLayout()
            self._parent_layout.addWidget(
                _ScrollArea(scroll[0], scroll[1], self._scroll_widget))
            self._scroll_widget.setLayout(self)

        self._width = width

    def _add_widget(self, widget):
        """Add a widget to an existing layout."""
        if isinstance(widget, QLayout):
            self.addLayout(widget)
        else:
            if self._width is not None:
                widget.setMinimumWidth(self._width)
                widget.setMaximumWidth(self._width)
            self.addWidget(widget)

    def _add_stretch(self, amount=1):
        self.addStretch(amount)

class _GridLayout(QGridLayout, _AbstractGridLayout, _Widget,
                  metaclass=_BaseWidget):

    def __init__(self, height=None, width=None):
        _AbstractGridLayout.__init__(self)
        _Widget.__init__(self)
        QGridLayout.__init__(self)
        if height:
            self.setMinimumHeight(height)
            self.setMaximumHeight(height)
        if width:
            self.setMinimumWidth(width)
            self.setMaximumWidth(width)

    def _add_widget(self, widget, row=None, col=None):
        """Add a widget to an existing layout."""
        if isinstance(widget, QLayout):
            self.addLayout(widget, row, col)
        else:
            self.addWidget(widget, row, col)

class _BaseCanvas(type(FigureCanvas), type(_AbstractCanvas)):
    pass

class _Canvas(FigureCanvas, _AbstractCanvas, metaclass=_BaseCanvas):

    def __init__(self, width, height, dpi):
        _AbstractCanvas.__init__(
            self, width=width, height=height, dpi=dpi)
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        self.ax = self.fig.add_subplot(111, position=[0.15, 0.15, 0.75, 0.75])
        FigureCanvas.__init__(self, self.fig)
        self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.setMinimumWidth(width)
        self.setMinimumHeight(height)

    def _set_size(self, width=None, height=None):
        if width:
            self.setMinimumWidth(width)
            self.setMaximumWidth(width)
        if height:
            self.setMinimumHeight(height)
            self.setMaximumHeight(height)

# %%
# Windows
# -------

# In theory we should be able to do this later (e.g., in _pyvista.py when
# initializing), but at least on Qt6 this has to be done earlier. So let's do
# it immediately upon instantiation of the QMainWindow class.
# TODO: This should eventually allow us to handle
# https://github.com/mne-tools/mne-python/issues/9182

class _MNEMainWindow(MainWindow):
    def __init__(self, parent=None, title=None, size=None):
        MainWindow.__init__(self, parent=parent, title=title, size=size)
        self.setAttribute(Qt.WA_ShowWithoutActivating, True)

class _AppWindow(_AbstractAppWindow, _MNEMainWindow, _Widget,
                 metaclass=_BaseWidget):

    def __init__(self, size=None, fullscreen=False):
        self._app = _init_mne_qtapp()
        _AbstractAppWindow.__init__(self)
        _MNEMainWindow.__init__(self, size=size)
        _Widget.__init__(self)

        if fullscreen:
            self.setWindowState(Qt.WindowFullScreen)

        self._set_theme()
        self.setLocale(QLocale(QLocale.Language.English))
        self.signal_close.connect(self._clean)
        self._before_close_callbacks = list()
        self._after_close_callbacks = list()

        # patch closeEvent
        def closeEvent(event):
            # functions to call before closing
            accept_close_event = True
            for callback in self._before_close_callbacks:
                ret = callback()
                # check if one of the callbacks ignores the close event
                if isinstance(ret, bool) and not ret:
                    accept_close_event = False

            if accept_close_event:
                self.signal_close.emit()
                self._clean()
                event.accept()
            else:
                event.ignore()

            # functions to call after closing
            for callback in self._after_close_callbacks:
                callback()
        self.closeEvent = closeEvent

    def _set_central_layout(self, central_layout):
        central_widget = QWidget()
        central_widget.setLayout(_get_layout(central_layout))
        self.setCentralWidget(central_widget)

    def _get_dpi(self):
        return self.windowHandle().screen().logicalDotsPerInch()

    def _get_size(self):
        return (self.width(), self.height())

    def _get_cursor(self):
        return self.cursor()

    def _set_cursor(self, cursor):
        self.setCursor(cursor)

    def _new_cursor(self, name):
        return QCursor(getattr(Qt, name))

    def _close_connect(self, callback, *, after=True):
        if after:
            self._after_close_callbacks.append(callback)
        else:
            self._before_close_callbacks.append(callback)

    def _close_disconnect(self, after=True):
        if after:
            self._after_close_callbacks.clear()
        else:
            self._before_close_callbacks.clear()

    def _clean(self):
        self._app = None

    def _show(self, block=False):
        _qt_raise_window(self)
        _Widget._show(self)
        if block:
            _qt_app_exec(self._app)

    def _close(self):
        self.close()

class _3DRenderer(_PyVistaRenderer):
    _kind = 'qt'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def show(self):
        super().show()
        with _qt_disable_paint(self.plotter):
            self.plotter.app_window.show()
        self._update()
        for plotter in self._all_plotters:
            plotter.updateGeometry()
            plotter._render()
        # Ideally we would just put a `splash.finish(plotter.window())` in the
        # same place that we initialize this (_init_qt_app call). However,
        # the window show event is triggered (closing the splash screen) well
        # before the window actually appears for complex scenes like the coreg
        # GUI. Therefore, we close after all these events have been processed
        # here.
        self._process_events()
        splash = getattr(self.figure, 'splash', False)
        if splash:
            splash.close()
        _qt_raise_window(self.plotter.app_window)

    def _clean(self):
        self.figure._plotter = None
        self._interactor = None

# ------------------------------------
# Non-object-based Widget Abstractions
# ------------------------------------
# These are planned to be deprecated in favor of the simpler, object-
# oriented abstractions above when time allows.

class _QtKeyPress(_AbstractKeyPress):
    _widget_id = 0
    _callbacks = dict()
    _to_qt = dict(
        escape=Qt.Key_Escape,
        up=Qt.Key_Up,
        down=Qt.Key_Down,
        left=Qt.Key_Left,
        right=Qt.Key_Right,
        comma=Qt.Key_Comma,
        period=Qt.Key_Period,
        page_up=Qt.Key_PageUp,
        page_down=Qt.Key_PageDown,
    )

    def _keypress_initialize(self, widget=None):
        widget = self._window if widget is None else widget
        self._widget_id = _QtKeyPress._widget_id
        _QtKeyPress._widget_id += 1
        _QtKeyPress._callbacks[self._widget_id] = dict()

        def keyPressEvent(event):
            text = event.text()
            widget_callbacks = _QtKeyPress._callbacks[self._widget_id]
            if text in widget_callbacks:
                callback = widget_callbacks[text]
                callback()
            else:
                key = event.key()
                if key in widget_callbacks:
                    callback = widget_callbacks[key]
                    callback()

        widget.keyPressEvent = keyPressEvent

    def _keypress_add(self, shortcut, callback):
        widget_callbacks = _QtKeyPress._callbacks[self._widget_id]
        if len(shortcut) > 1:  # special key
            shortcut = _QtKeyPress._to_qt[shortcut]
        widget_callbacks[shortcut] = callback

    def _keypress_trigger(self, shortcut):
        widget_callbacks = _QtKeyPress._callbacks[self._widget_id]
        if len(shortcut) > 1:  # special key
            shortcut = _QtKeyPress._to_qt[shortcut]
        widget_callbacks[shortcut]()

class _QtDialog(_AbstractDialog):
    # from QMessageBox.StandardButtons
    supported_button_names = [
        "Ok", "Open", "Save", "Cancel", "Close", "Discard", "Apply",
        "Reset", "RestoreDefaults", "Help", "SaveAll", "Yes",
        "YesToAll", "No", "NoToAll", "Abort", "Retry", "Ignore"
    ]
    # from QMessageBox.Icon
    supported_icon_names = [
        "NoIcon", "Question", "Information", "Warning", "Critical"
    ]

    def _dialog_create(self, title, text, info_text, callback, *,
                       icon='Warning', buttons=[], modal=True, window=None):
        window = self._window if window is None else window
        widget = QMessageBox(window)
        widget.setWindowTitle(title)
        widget.setText(text)
        # icon is one of _QtDialog.supported_icon_names
        icon_id = getattr(QMessageBox, icon)
        widget.setIcon(icon_id)
        widget.setInformativeText(info_text)

        if not buttons:
            buttons = ["Ok"]

        button_ids = list()
        for button in buttons:
            # button is one of _QtDialog.supported_button_names
            button_id = getattr(QMessageBox, button)
            button_ids.append(button_id)
        standard_buttons = default_button = button_ids[0]
        for button_id in button_ids[1:]:
            standard_buttons |= button_id
        widget.setStandardButtons(standard_buttons)
        widget.setDefaultButton(default_button)

        @safe_event
        def func(button):
            button_id = widget.standardButton(button)
            for button_name in _QtDialog.supported_button_names:
                if button_id == getattr(QMessageBox, button_name):
                    widget.setCursor(QCursor(Qt.WaitCursor))
                    try:
                        callback(button_name)
                    finally:
                        widget.unsetCursor()
                        break

        widget.buttonClicked.connect(func)
        return _QtDialogWidget(widget, modal)

class _QtLayout(_AbstractLayout):
    def _layout_initialize(self, max_width):
        pass

    def _layout_add_widget(self, layout, widget, stretch=0,
                           *, row=None, col=None):
        """Add a widget to an existing layout."""
        if isinstance(widget, QLayout):
            layout.addLayout(widget)
        else:
            if isinstance(layout, QGridLayout):
                layout.addWidget(widget, row, col)
            else:
                layout.addWidget(widget, stretch)

    def _layout_create(self, orientation='vertical'):
        if orientation == 'vertical':
            layout = QVBoxLayout()
        elif orientation == 'horizontal':
            layout = QHBoxLayout()
        else:
            assert orientation == 'grid'
            layout = QGridLayout()
        return layout

class _QtDock(_AbstractDock, _QtLayout):
    def _dock_initialize(self, window=None, name="Controls",
                         area="left", max_width=None):
        window = self._window if window is None else window
        qt_area = getattr(Qt, f'{area.capitalize()}DockWidgetArea')
        self._dock, self._dock_layout = _create_dock_widget(
            window, name, qt_area, max_width=max_width
        )
        if area == "left":
            window.setCorner(Qt.BottomLeftCorner, Qt.LeftDockWidgetArea)
        else:
            window.setCorner(Qt.BottomRightCorner, Qt.RightDockWidgetArea)

    def _dock_finalize(self):
        self._dock.setMinimumSize(self._dock.sizeHint().width(), 0)
        self._dock_add_stretch(self._dock_layout)

    def _dock_show(self):
        self._dock.show()

    def _dock_hide(self):
        self._dock.hide()

    def _dock_add_stretch(self, layout=None):
        layout = self._dock_layout if layout is None else layout
        layout.addStretch()

    def _dock_add_layout(self, vertical=True):
        layout = QVBoxLayout() if vertical else QHBoxLayout()
        return layout

    def _dock_add_label(
        self, value, *, align=False, layout=None, selectable=False
    ):
        layout = self._dock_layout if layout is None else layout
        widget = QLabel()
        if align:
            widget.setAlignment(Qt.AlignCenter)
        widget.setText(value)
        widget.setWordWrap(True)
        if selectable:
            widget.setTextInteractionFlags(Qt.TextSelectableByMouse)
        self._layout_add_widget(layout, widget)
        return _QtWidget(widget)

    def _dock_add_button(
        self, name, callback, *, style='pushbutton', icon=None, tooltip=None,
        layout=None
    ):
        _check_option(
            parameter='style',
            value=style,
            allowed_values=('toolbutton', 'pushbutton')
        )
        if style == 'toolbutton':
            widget = QToolButton()
            widget.setText(name)
        else:
            widget = QPushButton(name)
            # Don't change text color upon button press
            widget.setStyleSheet(
                'QPushButton:pressed {color: none;}'
            )
        if icon is not None:
            widget.setIcon(self._icons[icon])

        _set_widget_tooltip(widget, tooltip)
        widget.clicked.connect(callback)

        layout = self._dock_layout if layout is None else layout
        self._layout_add_widget(layout, widget)
        return _QtWidget(widget)

    def _dock_named_layout(self, name, *, layout=None, compact=True):
        layout = self._dock_layout if layout is None else layout
        if name is not None:
            hlayout = self._dock_add_layout(not compact)
            self._dock_add_label(
                value=name, align=not compact, layout=hlayout)
            self._layout_add_widget(layout, hlayout)
            layout = hlayout
        return layout

    def _dock_add_slider(self, name, value, rng, callback, *,
                         compact=True, double=False, tooltip=None,
                         layout=None):
        layout = self._dock_named_layout(
            name=name, layout=layout, compact=compact)
        slider_class = QFloatSlider if double else QSlider
        cast = float if double else int
        widget = slider_class(Qt.Horizontal)
        _set_widget_tooltip(widget, tooltip)
        widget.setMinimum(cast(rng[0]))
        widget.setMaximum(cast(rng[1]))
        widget.setValue(cast(value))
        if double:
            widget.floatValueChanged.connect(callback)
        else:
            widget.valueChanged.connect(callback)
        self._layout_add_widget(layout, widget)
        return _QtWidget(widget)

    def _dock_add_check_box(self, name, value, callback, *, tooltip=None,
                            layout=None):
        layout = self._dock_layout if layout is None else layout
        widget = QCheckBox(name)
        _set_widget_tooltip(widget, tooltip)
        widget.setChecked(value)
        widget.stateChanged.connect(callback)
        self._layout_add_widget(layout, widget)
        return _QtWidget(widget)

    def _dock_add_spin_box(self, name, value, rng, callback, *,
                           compact=True, double=True, step=None,
                           tooltip=None, layout=None):
        layout = self._dock_named_layout(
            name=name, layout=layout, compact=compact)
        value = value if double else int(value)
        widget = QDoubleSpinBox() if double else QSpinBox()
        _set_widget_tooltip(widget, tooltip)
        widget.setAlignment(Qt.AlignCenter)
        widget.setMinimum(rng[0])
        widget.setMaximum(rng[1])
        widget.setKeyboardTracking(False)
        if step is None:
            inc = (rng[1] - rng[0]) / 20.
            inc = max(int(round(inc)), 1) if not double else inc
            widget.setSingleStep(inc)
        else:
            widget.setSingleStep(step)
        widget.setValue(value)
        widget.valueChanged.connect(callback)
        self._layout_add_widget(layout, widget)
        return _QtWidget(widget)

    def _dock_add_combo_box(self, name, value, rng, callback, *, compact=True,
                            tooltip=None, layout=None):
        layout = self._dock_named_layout(
            name=name, layout=layout, compact=compact)
        widget = QComboBox()
        _set_widget_tooltip(widget, tooltip)
        widget.addItems(rng)
        widget.setCurrentText(value)
        widget.currentTextChanged.connect(callback)
        widget.setSizeAdjustPolicy(QComboBox.AdjustToContents)
        self._layout_add_widget(layout, widget)
        return _QtWidget(widget)

    def _dock_add_radio_buttons(self, value, rng, callback, *, vertical=True,
                                layout=None):
        layout = self._dock_layout if layout is None else layout
        group_layout = QVBoxLayout() if vertical else QHBoxLayout()
        group = QButtonGroup()
        for val in rng:
            button = QRadioButton(val)
            if val == value:
                button.setChecked(True)
            group.addButton(button)
            self._layout_add_widget(group_layout, button)

        def func(button):
            callback(button.text())
        group.buttonClicked.connect(func)
        self._layout_add_widget(layout, group_layout)
        return _QtWidgetList(group)

    def _dock_add_group_box(self, name, *, collapse=None, layout=None):
        layout = self._dock_layout if layout is None else layout
        hlayout = QVBoxLayout()
        widget = QGroupBox(name)
        widget.setLayout(hlayout)
        self._layout_add_widget(layout, widget)
        return hlayout

    def _dock_add_text(self, name, value, placeholder, *, callback=None,
                       layout=None):
        layout = self._dock_layout if layout is None else layout
        widget = QLineEdit(value)
        widget.setPlaceholderText(placeholder)
        self._layout_add_widget(layout, widget)
        if callback is not None:
            widget.textChanged.connect(callback)
        return _QtWidget(widget)

    def _dock_add_file_button(
        self, name, desc, func, *, filter=None, initial_directory=None,
        save=False, is_directory=False, icon=False, tooltip=None, layout=None
    ):
        layout = self._dock_layout if layout is None else layout

        def callback():
            if is_directory:
                name = QFileDialog.getExistingDirectory(
                    directory=initial_directory
                )
            elif save:
                name = QFileDialog.getSaveFileName(
                    directory=initial_directory,
                    filter=filter
                )
            else:
                name = QFileDialog.getOpenFileName(
                    directory=initial_directory,
                    filter=filter
                )
            name = name[0] if isinstance(name, tuple) else name
            # handle the cancel button
            if len(name) == 0:
                return
            func(name)

        if icon:
            kwargs = dict(style='toolbutton', icon='folder')
        else:
            kwargs = dict()
        button_widget = self._dock_add_button(
            name=desc,
            callback=callback,
            tooltip=tooltip,
            layout=layout,
            **kwargs
        )
        return button_widget  # It's already a _QtWidget instance

class QFloatSlider(QSlider):
    """Slider that handles float values."""

    floatValueChanged = Signal(float)

    def __init__(self, ori, parent=None):
        """Initialize the slider."""
        super().__init__(ori, parent)
        self._opt = QStyleOptionSlider()
        self.initStyleOption(self._opt)
        self._gr = self.style().subControlRect(
            QStyle.CC_Slider, self._opt, QStyle.SC_SliderGroove, self)
        self._sr = self.style().subControlRect(
            QStyle.CC_Slider, self._opt, QStyle.SC_SliderHandle, self)
        self._precision = 10000
        super().valueChanged.connect(self._convert)

    def _convert(self, value):
        self.floatValueChanged.emit(value / self._precision)

    def minimum(self):
        """Get the minimum."""
        return super().minimum() / self._precision

    def setMinimum(self, value):
        """Set the minimum."""
        super().setMinimum(int(value * self._precision))

    def maximum(self):
        """Get the maximum."""
        return super().maximum() / self._precision

    def setMaximum(self, value):
        """Set the maximum."""
        super().setMaximum(int(value * self._precision))

    def value(self):
        """Get the current value."""
        return super().value() / self._precision

    def setValue(self, value):
        """Set the current value."""
        super().setValue(int(value * self._precision))

    # Adapted from:
    # https://stackoverflow.com/questions/52689047/moving-qslider-to-mouse-click-position  # noqa: E501
    def mousePressEvent(self, event):
        """Add snap-to-location handling."""
        opt = QStyleOptionSlider()
        self.initStyleOption(opt)
        sr = self.style().subControlRect(
            QStyle.CC_Slider, opt, QStyle.SC_SliderHandle, self)
        if (event.button() != Qt.LeftButton or sr.contains(event.pos())):
            super().mousePressEvent(event)
            return
        if self.orientation() == Qt.Vertical:
            half = (0.5 * sr.height()) + 0.5
            max_ = self.height()
            pos = max_ - event.y()
        else:
            half = (0.5 * sr.width()) + 0.5
            max_ = self.width()
            pos = event.x()
        max_ = max_ - 2 * half
        pos = min(max(pos - half, 0), max_) / max_
        val = self.minimum() + (self.maximum() - self.minimum()) * pos
        val = (self.maximum() - val) if self.invertedAppearance() else val
        self.setValue(val)
        event.accept()
        # Process afterward so it's seen as a drag
        super().mousePressEvent(event)

class _QtToolBar(_AbstractToolBar, _QtLayout):
    def _tool_bar_initialize(self, name="default", window=None):
        self.actions = dict()
        window = self._window if window is None else window
        self._tool_bar = window.addToolBar(name)
        self._tool_bar_layout = self._tool_bar.layout()

    def _tool_bar_add_button(self, name, desc, func, *, icon_name=None,
                             shortcut=None):
        icon_name = name if icon_name is None else icon_name
        icon = self._icons[icon_name]
        self.actions[name] = _QtAction(self._tool_bar.addAction(
            icon, desc, func))
        if shortcut is not None:
            self.actions[name].set_shortcut(shortcut)

    def _tool_bar_update_button_icon(self, name, icon_name):
        self.actions[name].set_icon(self._icons[icon_name])

    def _tool_bar_add_text(self, name, value, placeholder):
        pass

    def _tool_bar_add_spacer(self):
        spacer = QWidget()
        spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
        self._tool_bar.addWidget(spacer)

    def _tool_bar_add_file_button(self, name, desc, func, *, shortcut=None):
        weakself = weakref.ref(self)

        def callback(weakself=weakself):
            weakself = weakself()
            if weakself is None:
                return
            return FileDialog(
                weakself._window,
                callback=func,
            )

        self._tool_bar_add_button(
            name=name,
            desc=desc,
            func=callback,
            shortcut=shortcut,
        )

    def _tool_bar_add_play_button(self, name, desc, func, *, shortcut=None):
        self._tool_bar_add_button(
            name=name, desc=desc, func=func, icon_name=None, shortcut=shortcut)

class _QtMenuBar(_AbstractMenuBar):
    def _menu_initialize(self, window=None):
        self._menus = dict()
        self._menu_actions = dict()
        self._menu_bar = QMenuBar()
        self._menu_bar.setNativeMenuBar(False)
        window = self._window if window is None else window
        window.setMenuBar(self._menu_bar)

    def _menu_add_submenu(self, name, desc):
        self._menus[name] = self._menu_bar.addMenu(desc)
        self._menu_actions[name] = dict()

    def _menu_add_button(self, menu_name, name, desc, func):
        menu = self._menus[menu_name]
        self._menu_actions[menu_name][name] = \
            _QtAction(menu.addAction(desc, func))

class _QtStatusBar(_AbstractStatusBar, _QtLayout):
    def _status_bar_initialize(self, window=None):
        window = self._window if window is None else window
        self._status_bar = window.statusBar()

    def _status_bar_add_label(self, value, *, stretch=0):
        widget = QLabel(value)
        self._layout_add_widget(self._status_bar.layout(), widget, stretch)
        return _QtWidget(widget)

    def _status_bar_add_progress_bar(self, stretch=0):
        widget = QProgressBar()
        self._layout_add_widget(self._status_bar.layout(), widget, stretch)
        return _QtWidget(widget)

    def _status_bar_update(self):
        self._status_bar.layout().update()

class _QtPlayback(_AbstractPlayback):
    def _playback_initialize(self, func, timeout, value, rng,
                             time_widget, play_widget):
        self.figure.plotter.add_callback(func, timeout)

class _QtMplInterface(_AbstractMplInterface):
    def _mpl_initialize(self):
        from qtpy import QtWidgets
        from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
        self.canvas = FigureCanvasQTAgg(self.fig)
        FigureCanvasQTAgg.setSizePolicy(
            self.canvas,
            QtWidgets.QSizePolicy.Expanding,
            QtWidgets.QSizePolicy.Expanding
        )
        FigureCanvasQTAgg.updateGeometry(self.canvas)

class _QtMplCanvas(_AbstractMplCanvas, _QtMplInterface):
    def __init__(self, width, height, dpi):
        super().__init__(width, height, dpi)
        self._mpl_initialize()

class _QtBrainMplCanvas(_AbstractBrainMplCanvas, _QtMplInterface):
    def __init__(self, brain, width, height, dpi):
        super().__init__(brain, width, height, dpi)
        self._mpl_initialize()
        if brain.separate_canvas:
            self.canvas.setParent(None)
        else:
            self.canvas.setParent(brain._renderer._window)
        self._connect()

class _QtWindow(_AbstractWindow):
    def _window_initialize(
        self, *, window=None, central_layout=None, fullscreen=False
    ):
        super()._window_initialize()
        self._interactor = self.figure.plotter.interactor
        if window is None:
            self._window = self.figure.plotter.app_window
        else:
            self._window = window

        if fullscreen:
            self._window.setWindowState(Qt.WindowFullScreen)

        if central_layout is not None:
            central_widget = self._window.centralWidget()
            if central_widget is None:
                central_widget = QWidget()
                self._window.setCentralWidget(central_widget)
            central_widget.setLayout(central_layout)
        self._window_load_icons()
        self._window_set_theme()
        self._window.setLocale(QLocale(QLocale.Language.English))
        self._window.signal_close.connect(self._window_clean)
        self._window_before_close_callbacks = list()
        self._window_after_close_callbacks = list()

        # patch closeEvent
        def closeEvent(event):
            # functions to call before closing
            accept_close_event = True
            for callback in self._window_before_close_callbacks:
                ret = callback()
                # check if one of the callbacks ignores the close event
                if isinstance(ret, bool) and not ret:
                    accept_close_event = False

            if accept_close_event:
                self._window.signal_close.emit()
                event.accept()
            else:
                event.ignore()

            # functions to call after closing
            for callback in self._window_after_close_callbacks:
                callback()
        self._window.closeEvent = closeEvent

    def _window_load_icons(self):
        self._icons["help"] = QIcon.fromTheme("help")
        self._icons["play"] = QIcon.fromTheme("play")
        self._icons["pause"] = QIcon.fromTheme("pause")
        self._icons["reset"] = QIcon.fromTheme("reset")
        self._icons["scale"] = QIcon.fromTheme("scale")
        self._icons["clear"] = QIcon.fromTheme("clear")
        self._icons["movie"] = QIcon.fromTheme("movie")
        self._icons["restore"] = QIcon.fromTheme("restore")
        self._icons["screenshot"] = QIcon.fromTheme("screenshot")
        self._icons["visibility_on"] = QIcon.fromTheme("visibility_on")
        self._icons["visibility_off"] = QIcon.fromTheme("visibility_off")
        self._icons["folder"] = QIcon.fromTheme("folder")

    def _window_clean(self):
        self.figure._plotter = None
        self._interactor = None

    def _window_close_connect(self, func, *, after=True):
        if after:
            self._window_after_close_callbacks.append(func)
        else:
            self._window_before_close_callbacks.append(func)

    def _window_close_disconnect(self, after=True):
        if after:
            self._window_after_close_callbacks.clear()
        else:
            self._window_before_close_callbacks.clear()

    def _window_get_dpi(self):
        return self._window.windowHandle().screen().logicalDotsPerInch()

    def _window_get_size(self):
        w = self._interactor.geometry().width()
        h = self._interactor.geometry().height()
        return (w, h)

    def _window_get_simple_canvas(self, width, height, dpi):
        return _QtMplCanvas(width, height, dpi)

    def _window_get_mplcanvas(self, brain, interactor_fraction, show_traces,
                              separate_canvas):
        w, h = self._window_get_mplcanvas_size(interactor_fraction)
        self._interactor_fraction = interactor_fraction
        self._show_traces = show_traces
        self._separate_canvas = separate_canvas
        self._mplcanvas = _QtBrainMplCanvas(
            brain, w, h, self._window_get_dpi())
        return self._mplcanvas

    def _window_adjust_mplcanvas_layout(self):
        canvas = self._mplcanvas.canvas
        self._mpl_dock, dock_layout = _create_dock_widget(
            self._window, "Traces", Qt.BottomDockWidgetArea)
        dock_layout.addWidget(canvas)

    def _window_get_cursor(self):
        return self._window.cursor()

    def _window_set_cursor(self, cursor):
        self._interactor.setCursor(cursor)
        self._window.setCursor(cursor)

    def _window_new_cursor(self, name):
        return QCursor(getattr(Qt, name))

    @contextmanager
    def _window_ensure_minimum_sizes(self):
        sz = self.figure.store['window_size']
        adjust_mpl = (self._show_traces and not self._separate_canvas)
        # plotter:            pyvista.plotting.qt_plotting.BackgroundPlotter
        # plotter.interactor: vtk.qt.QVTKRenderWindowInteractor.QVTKRenderWindowInteractor -> QWidget  # noqa
        # plotter.app_window: pyvista.plotting.qt_plotting.MainWindow -> QMainWindow  # noqa
        # plotter.frame:      QFrame with QVBoxLayout with plotter.interactor as centralWidget  # noqa
        # plotter.ren_win:    vtkXOpenGLRenderWindow
        self._interactor.setMinimumSize(*sz)
        # Lines like this are useful for debugging these issues:
        # print('*' * 80)
        # print(0, self._interactor.app_window.size().height(), self._interactor.size().height(), self._mpl_dock.widget().height(), self._mplcanvas.canvas.size().height())  # noqa
        if adjust_mpl:
            mpl_h = int(round((sz[1] * self._interactor_fraction) /
                              (1 - self._interactor_fraction)))
            self._mplcanvas.canvas.setMinimumSize(sz[0], mpl_h)
            self._mpl_dock.widget().setMinimumSize(sz[0], mpl_h)
        try:
            yield  # show
        finally:
            # 1. Process events
            self._process_events()
            self._process_events()
            # 2. Get the window and interactor sizes that work
            win_sz = self._window.size()
            ren_sz = self._interactor.size()
            # 3. Undo the min size setting and process events
            self._interactor.setMinimumSize(0, 0)
            if adjust_mpl:
                self._mplcanvas.canvas.setMinimumSize(0, 0)
                self._mpl_dock.widget().setMinimumSize(0, 0)
            self._process_events()
            self._process_events()
            # 4. Compute the extra height required for dock decorations and add
            win_h = win_sz.height()
            if adjust_mpl:
                win_h += max(
                    self._mpl_dock.widget().size().height() - mpl_h, 0)
            # 5. Resize the window and interactor to the correct size
            #    (not sure why, but this is required on macOS at least)
            self._interactor.window_size = (win_sz.width(), win_h)
            self._interactor.resize(ren_sz.width(), ren_sz.height())
            self._process_events()
            self._process_events()

    def _window_set_theme(self, theme=None):
        if theme is None:
            default_theme = _qt_detect_theme()
        else:
            default_theme = theme
        theme = get_config('MNE_3D_OPTION_THEME', default_theme)
        stylesheet = _qt_get_stylesheet(theme)
        self._window.setStyleSheet(stylesheet)
        if _qt_is_dark(self._window):
            QIcon.setThemeName('dark')
        else:
            QIcon.setThemeName('light')

    def _window_create(self):
        return _MNEMainWindow()

class _QtWidgetList(_AbstractWidgetList):
    def __init__(self, src):
        self._src = src
        self._widgets = list()
        if isinstance(self._src, QButtonGroup):
            widgets = self._src.buttons()
        else:
            widgets = src
        for widget in widgets:
            if not isinstance(widget, _QtWidget):
                widget = _QtWidget(widget)
            self._widgets.append(widget)

    def set_enabled(self, state):
        for widget in self._widgets:
            widget.set_enabled(state)

    def get_value(self, idx):
        return self._widgets[idx].get_value()

    def set_value(self, idx, value):
        if isinstance(self._src, QButtonGroup):
            self._widgets[idx].set_value(True)
        else:
            self._widgets[idx].set_value(value)

class _QtWidget(_AbstractWdgt):
    def set_value(self, value):
        if isinstance(self._widget, (QRadioButton, QToolButton, QPushButton)):
            self._widget.click()
        else:
            if hasattr(self._widget, "setValue"):
                self._widget.setValue(value)
            elif hasattr(self._widget, "setCurrentText"):
                self._widget.setCurrentText(value)
            elif hasattr(self._widget, "setChecked"):
                self._widget.setChecked(value)
            else:
                assert hasattr(self._widget, "setText")
                self._widget.setText(value)

    def get_value(self):
        if hasattr(self._widget, "value"):
            return self._widget.value()
        elif hasattr(self._widget, "currentText"):
            return self._widget.currentText()
        elif hasattr(self._widget, "checkState"):
            return self._widget.checkState() != Qt.Unchecked
        else:
            assert hasattr(self._widget, "text")
            return self._widget.text()

    def set_range(self, rng):
        self._widget.setRange(rng[0], rng[1])

    def show(self):
        self._widget.show()

    def hide(self):
        self._widget.hide()

    def set_enabled(self, state):
        self._widget.setEnabled(state)

    def is_enabled(self):
        return self._widget.isEnabled()

    def update(self, repaint=True):
        self._widget.update()
        if repaint:
            self._widget.repaint()

    def get_tooltip(self):
        assert hasattr(self._widget, 'toolTip')
        return self._widget.toolTip()

    def set_tooltip(self, tooltip):
        assert hasattr(self._widget, 'setToolTip')
        self._widget.setToolTip(tooltip)

    def set_style(self, style):
        stylesheet = ""
        for key, val in style.items():
            stylesheet = stylesheet + f"{key}:{val};"
        self._widget.setStyleSheet(stylesheet)

class _QtDialogCommunicator(QObject):
    signal_show = Signal()

    def __init__(self, parent=None):
        super().__init__(parent)

class _QtDialogWidget(_QtWidget):
    def __init__(self, widget, modal):
        super().__init__(widget)
        self._modal = modal
        self._communicator = _QtDialogCommunicator()
        self._communicator.signal_show.connect(self.show)

    def trigger(self, button):
        button_id = getattr(QMessageBox, button)
        for current_button in self._widget.buttons():
            if self._widget.standardButton(current_button) == button_id:
                current_button.click()

    def show(self, thread=False):
        if thread:
            self._communicator.signal_show.emit()
        else:
            if self._modal:
                self._widget.exec()
            else:
                self._widget.show()

class _QtAction(_AbstractAction):
    def trigger(self):
        self._action.trigger()

    def set_icon(self, icon):
        self._action.setIcon(icon)

    def set_shortcut(self, shortcut):
        self._action.setShortcut(shortcut)

class _Renderer(_PyVistaRenderer, _QtDock, _QtToolBar, _QtMenuBar,
                _QtStatusBar, _QtWindow, _QtPlayback, _QtDialog,
                _QtKeyPress):
    _kind = 'qt'

    def __init__(self, *args, **kwargs):
        fullscreen = kwargs.pop('fullscreen', False)
        super().__init__(*args, **kwargs)
        self._window_initialize(fullscreen=fullscreen)

    def show(self):
        super().show()
        with _qt_disable_paint(self.plotter):
            with self._window_ensure_minimum_sizes():
                self.plotter.app_window.show()
        self._update()
        for plotter in self._all_plotters:
            plotter.updateGeometry()
            plotter._render()
        # Ideally we would just put a `splash.finish(plotter.window())` in the
        # same place that we initialize this (_init_qt_app call). However,
        # the window show event is triggered (closing the splash screen) well
        # before the window actually appears for complex scenes like the coreg
        # GUI. Therefore, we close after all these events have been processed
        # here.
        self._process_events()
        splash = getattr(self.figure, 'splash', False)
        if splash:
            splash.close()
        _qt_raise_window(self.plotter.app_window)

def _set_widget_tooltip(widget, tooltip):
    if tooltip is not None:
        widget.setToolTip(tooltip)

def _create_dock_widget(window, name, area, *, max_width=None):
    # create dock widget
    dock = QDockWidget(name)
    # add scroll area
    scroll = QScrollArea(dock)
    dock.setWidget(scroll)
    # give the scroll area a child widget
    widget = QWidget(scroll)
    scroll.setWidget(widget)
    scroll.setWidgetResizable(True)
    dock.setAllowedAreas(area)
    dock.setTitleBarWidget(QLabel(name))
    window.addDockWidget(area, dock)
    dock_layout = QVBoxLayout()
    widget.setLayout(dock_layout)
    # Fix resize grip size
    # https://stackoverflow.com/a/65050468/2175965
    styles = ['margin: 4px;']
    if max_width is not None:
        styles.append(f'max-width: {max_width};')
    style_sheet = 'QDockWidget { ' + '  \n'.join(styles) + '\n}'
    dock.setStyleSheet(style_sheet)
    return dock, dock_layout

@contextmanager
def _testing_context(interactive):
    from . import renderer
    orig_offscreen = pyvista.OFF_SCREEN
    orig_testing = renderer.MNE_3D_BACKEND_TESTING
    orig_interactive = renderer.MNE_3D_BACKEND_INTERACTIVE
    renderer.MNE_3D_BACKEND_TESTING = True
    if interactive:
        pyvista.OFF_SCREEN = False
        renderer.MNE_3D_BACKEND_INTERACTIVE = True
    else:
        pyvista.OFF_SCREEN = True
        renderer.MNE_3D_BACKEND_INTERACTIVE = False
    try:
        yield
    finally:
        pyvista.OFF_SCREEN = orig_offscreen
        renderer.MNE_3D_BACKEND_TESTING = orig_testing
        renderer.MNE_3D_BACKEND_INTERACTIVE = orig_interactive


text:
// Copyright 2008 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
  An example running of this program:

  ./lda           \
  --num_topics 2 \
  --alpha 0.1    \
  --beta 0.01                                           \
  --training_data_file ./testdata/test_data.txt \
  --model_file /tmp/lda_model.txt                       \
  --burn_in_iterations 100                              \
  --total_iterations 150
*/

#include <fstream>
#include <set>
#include <sstream>
#include <string>
#include <map>
#include <time.h>

#include "common.h"
#include "document.h"
#include "model.h"
#include "accumulative_model.h"
#include "sampler.h"
#include "cmd_flags.h"

namespace learning_lda {

using std::ifstream;
using std::ofstream;
using std::ostream;
using std::istringstream;
using std::set;
using std::map;

void OutputAssignments(LDACorpus* corpus,vector<string>& index_word_map,string file_out1,string file_out2) {
  ofstream out1(file_out1.c_str());
  ofstream out2(file_out2.c_str());

  for (list<LDADocument*>::iterator iter = corpus->begin();
    iter != corpus->end();
    ++iter) {
      const vector<int64> topic_distributions_vec = (*iter)->topic_distribution();
      out1 << (*iter)->document_name();
      for (int topic = 0; topic < topic_distributions_vec.size(); ++topic) {
        out1 << " " << topic_distributions_vec[topic];
      }
      out1 << "\n";

      out2 << (*iter)->document_name();
      vector<pair<int,int> > word_assign;
      (*iter)->word_assignments(word_assign);
      for (int index = 0; index < word_assign.size(); ++index) {
        out2 << " " << index_word_map[word_assign[index].first]<< ":" << word_assign[index].second;
      }
      out2 << "\n";
  }
}

int LoadAndInitTrainingCorpus(const string& corpus_file,
                              int file_type,
                              int num_topics,
                              LDACorpus* corpus,
                              map<string, int>* word_index_map) {
  corpus->clear();
  ifstream fin(corpus_file.c_str());
  string line;
  while (getline(fin, line)) {  // Each line is a training document.
    if (line.size() > 0 &&      // Skip empty lines.
        line[0] != '\r' &&      // Skip empty lines.
        line[0] != '\n' &&      // Skip empty lines.
        line[0] != '#') {       // Skip comment lines.
      istringstream ss(line);
      string doc_name;
      ss >> doc_name;
      DocumentWordTopicsPB document;
      string word;
      int count;
      while (ss >> word ) {  // Load and init a document.
        if (0==file_type)
        {
          ss >> count;
        }
        else
        {
          count = 1;
        }
        vector<int32> topics;
        for (int i = 0; i < count; ++i) {
          topics.push_back(RandInt(num_topics));
        }
        int word_index;
        map<string, int>::const_iterator iter = word_index_map->find(word);
        if (iter == word_index_map->end()) {
//          word_index = word_index_map->size();
//          (*word_index_map)[word] = word_index;
            continue;
        } else {
          word_index = iter->second;
        }
        document.add_wordtopics(word, word_index, topics, true);
      }
      corpus->push_back(new LDADocument(doc_name,document, num_topics));
    }
  }
  return corpus->size();
}

void FreeCorpus(LDACorpus* corpus) {
  for (list<LDADocument*>::iterator iter = corpus->begin();
       iter != corpus->end();
       ++iter) {
    if (*iter != NULL) {
      delete *iter;
      *iter = NULL;
    }
  }
}

}  // namespace learning_lda

int main(int argc, char** argv) {
  using learning_lda::LDACorpus;
  using learning_lda::LDAModel;
  using learning_lda::LDAAccumulativeModel;
  using learning_lda::LDASampler;
  using learning_lda::LDATopicSplitRule;
  using learning_lda::LDADocument;
  using learning_lda::LoadWordIndex;
  using learning_lda::LoadWordSet;
  using learning_lda::LoadWordLex;
  using learning_lda::LoadAndInitTrainingCorpus;
  using learning_lda::OutputAssignments;
  using learning_lda::generate_model_name;
  using learning_lda::LDACmdLineFlags;
  using std::ifstream;
  using std::ofstream;
  using std::list;

  LDACmdLineFlags flags;
  flags.ParseCmdFlags(argc, argv);

//   if (!flags.CheckTrainingValidity()) {
//     return -1;
//   }

  srand(time(NULL));

  map<string, int> word_index_map;

  ifstream model_fin(flags.model_file_.c_str());
  LDAModel model(model_fin, &word_index_map);

  set<int> new_words;
  if (flags.new_words_file_.length() > 0 )
  {
    ifstream newwords_fin(flags.new_words_file_.c_str());
    LoadWordSet(newwords_fin,word_index_map,new_words);
  }

  vector<string> index_word_map(word_index_map.size());
  CHECK_GT(LoadWordLex(word_index_map,index_word_map), 0);

  LDAModel new_model(flags.num_topics_, word_index_map);
  LDASampler new_sampler(flags.alpha_, flags.beta_, &new_model, NULL);

  LDATopicSplitRule rule;
  ifstream rule_fin(flags.rule_file_.c_str());
  rule.LoadSplitRule(rule_fin ,word_index_map);

  LDACorpus corpus;
  CHECK_GT(LoadAndInitTrainingCorpus(flags.training_data_file_,
                                     flags.file_type_,
                                     model.num_topics(),
                                     &corpus, &word_index_map), 0);

  LDASampler sampler(flags.alpha_, flags.beta_, &model, NULL);

  for (int iter = 0; iter < flags.burn_in_iterations_; ++iter) {
    std::cout << "Iteration " << iter << " ...\n";
    sampler.DoIteration(&corpus, false, iter < flags.burn_in_iterations_);
  }

  sampler.AdjustCorpusWithRule(corpus,flags.num_topics_ ,new_words,rule);

  new_sampler.InitModelGivenTopics(corpus);

  for (int iter = 0; iter < flags.total_iterations_; ++iter) {
    std::cout << "Iteration " << iter << " ...\n";
    if (flags.compute_likelihood_ == "true") {
      double loglikelihood = 0;
      for (list<LDADocument*>::const_iterator iterator = corpus.begin();
        iterator != corpus.end();
        ++iterator) {
          loglikelihood += new_sampler.LogLikelihood(*iterator);
      }
      std::cout << "Loglikelihood: " << loglikelihood << std::endl;
    }
    new_sampler.DoIteration2(&corpus, true, iter < flags.burn_in_iterations_);

    if (flags.save_step_ > 0) {
      if (iter % flags.save_step_ == 0) {
        // saving the model
        printf("Saving the Assignments File at iteration %d ...\n", iter);
        string file_out1 = generate_model_name(flags.topic_distribution_file_, 0, iter);
        string file_out2 = generate_model_name(flags.topic_assignments_file_, 0, iter);
        OutputAssignments(&corpus,index_word_map,file_out1,file_out2);
      }
    }
  }

  string file_out1 = generate_model_name(flags.topic_distribution_file_, 0, -1);
  string file_out2 = generate_model_name(flags.topic_assignments_file_, 0, -1);
  OutputAssignments(&corpus,index_word_map,file_out1,file_out2);

  FreeCorpus(&corpus);

  string file_out3 = generate_model_name(flags.model_file_, 0, -1);
  std::ofstream fout(file_out3.c_str());
  new_model.AppendAsString(fout);

  return 0;
}


text:
import { EventEmitter } from "events";
import * as most from "most";
import * as assert from "assert";

class FakeKafka extends EventEmitter {

  constructor () {
    super();
  }

  fake() {
    this.emit("message", "one 1");
    this.emit("message", "two 1");
    this.emit("message", "three 1");
    this.emit("message", "two 2");
    this.emit("message", "one 2");
    this.emit("message", "two 3");
  }

  fake2() {
    this.emit("message", "hi 1");
    this.emit("message", "hu 1");
    this.emit("message", "hi 2");
  }
}

describe("Observable2 UNIT", function () {

  it("should be able to observe", function (done) {

    const countMap: any = {};
    function slowKeyCount(value) {
      return new Promise(resolve => {

        if (countMap[value.key]) {
          countMap[value.key]++;
        } else {
          countMap[value.key] = 1;
        }

        resolve({
          key: value.key,
          value: value.value,
          count: countMap[value.key]
        });
      });
    }

    function toKV(m) {
      m = m.toLowerCase().split(" ");
      return {
        key: m[0],
        value: m[1]
      };
    }

    const kafka1 = new FakeKafka();
    const kafka2 = new FakeKafka();

    const stream1$ = most.fromEvent("message", kafka1)
      .map(toKV)
      .flatMap(value => most.fromPromise(slowKeyCount(value)))
      .tap(kv => console.log("tap1"))
      .multicast();

    stream1$.forEach(kv => console.log(kv));

    const stream2$ = most.fromEvent("message", kafka2)
      .map(toKV)
      .tap(kv => console.log("tap2"))
      .multicast();

    stream2$.forEach(kv => console.log(kv));

    const stream3$ = stream1$.multicast().merge(stream2$.multicast());

    stream3$.forEach(value => {
      console.log(value);
    });

    setTimeout(() => {
      kafka1.fake();
      kafka2.fake2();
      setTimeout(() => {
        console.log();
        console.log(countMap);

        assert.equal(countMap.one, 2);
        assert.equal(countMap.two, 3);
        assert.equal(countMap.three, 1);

        done();
      }, 50);
    }, 100);
  });
});


text:
using System;
using System.IO;

namespace Umbraco.Core.IO.MediaPathSchemes
{
    /// <summary>
    /// Implements a combined-guids media path scheme.
    /// </summary>
    /// <remarks>
    /// <para>Path is "{combinedGuid}/{filename>}" where combinedGuid is a combination of itemGuid and propertyGuid.</para>
    /// </remarks>
    public class CombinedGuidsMediaPathScheme : IMediaPathScheme
    {
        /// <inheritdoc />
        public string GetFilePath(IMediaFileSystem fileSystem, Guid itemGuid, Guid propertyGuid, string filename, string previous = null)
        {
            // assumes that cuid and puid keys can be trusted - and that a single property type
            // for a single content cannot store two different files with the same name
            var directory = HexEncoder.Encode(GuidUtils.Combine(itemGuid, propertyGuid).ToByteArray()/*'/', 2, 4*/); // could use ext to fragment path eg 12/e4/f2/...
            return Path.Combine(directory, filename).Replace('\\', '/');
        }

        /// <inheritdoc />
        public string GetDeleteDirectory(IMediaFileSystem fileSystem, string filepath) => Path.GetDirectoryName(filepath);
    }
}


text:
EXPORT_API BITMAP_HANDLE Bitmap_Create(wchar_t* wstrFileName) {
	return Iris2D::Bitmap::Create(wstrFileName);
}

EXPORT_API BITMAP_HANDLE Bitmap_Create2(unsigned int nWidth, unsigned int nHeight) {
	return Iris2D::Bitmap::Create(nWidth, nHeight);
}

EXPORT_API BITMAP_HANDLE Bitmap_Create3(BITMAP_HANDLE hSrcBitmap) {
	const auto pCastedSrcBitmap = reinterpret_cast<Iris2D::Bitmap*>(hSrcBitmap);
	return Iris2D::Bitmap::Create(pCastedSrcBitmap);
}

EXPORT_API BITMAP_HANDLE Bitmap_CopyFrom(BITMAP_HANDLE hSrcBitmap) {
	const auto pCastedSrcBitmap = reinterpret_cast<Iris2D::Bitmap*>(hSrcBitmap);
	return Iris2D::Bitmap::CopyFrom(pCastedSrcBitmap);
}

EXPORT_API void Bitmap_Release(BITMAP_HANDLE* hBitmap) {
	auto pCastedBitmap = reinterpret_cast<Iris2D::Bitmap*&>(*hBitmap);
	return Iris2D::Bitmap::Release(pCastedBitmap);
}

EXPORT_API unsigned int Bitmap_GetWidth(BITMAP_HANDLE hHandle) {
	const auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);
	return pBitmap->GetWidth();
}
EXPORT_API unsigned Bitmap_GetHeight(BITMAP_HANDLE hHandle) {
	const auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);
	return pBitmap->GetHeight();
}

EXPORT_API ResultCode Bitmap_Blt(BITMAP_HANDLE hHandle, unsigned int nDestX, unsigned int nDestY, BITMAP_HANDLE hSrcBitmap, RECT_HANDLE hSrcRect, float fOpacity) {
	auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);
	const auto pCastedSrcBitmap = reinterpret_cast<Iris2D::Bitmap*>(hSrcBitmap);
	const auto pCastedSrcRect = reinterpret_cast<Iris2D::Rect*>(hSrcRect);

	return pBitmap->Blt(nDestX, nDestY, pCastedSrcBitmap, pCastedSrcRect, fOpacity);
}

EXPORT_API ResultCode Bitmap_StretchBlt(BITMAP_HANDLE hHandle, RECT_HANDLE hDestRect, BITMAP_HANDLE hSrcBitmap, RECT_HANDLE hSrcRect, float fOpacity) {
	auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);
	const auto pCastedDestRect = reinterpret_cast<Iris2D::Rect*>(hDestRect);
	const auto pCastedSrcRect = reinterpret_cast<Iris2D::Rect*>(hSrcRect);
	const auto pCastedSrcBitmap = reinterpret_cast<Iris2D::Bitmap*>(hSrcBitmap);

	return pBitmap->StretchBlt(pCastedDestRect, pCastedSrcBitmap, pCastedSrcRect, fOpacity);
}

EXPORT_API ResultCode Bitmap_FillRect(BITMAP_HANDLE hHandle, unsigned int nX, unsigned int nY, unsigned int nWidth, unsigned int nHeight, COLOR_HANDLE hColor) {
	auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);
	const auto pCastedColor = reinterpret_cast<Iris2D::Color*>(hColor);

	return pBitmap->FillRect(nX, nY, nWidth, nHeight, pCastedColor);
}

EXPORT_API ResultCode Bitmap_FillRect2(BITMAP_HANDLE hHandle, RECT_HANDLE hRect, COLOR_HANDLE hColor) {
	auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);
	const auto pCastedRect = reinterpret_cast<Iris2D::Rect*>(hRect);
	const auto pCastedColor = reinterpret_cast<Iris2D::Color*>(hColor);

	return pBitmap->FillRect(pCastedRect, pCastedColor);
}

EXPORT_API ResultCode Bitmap_Clear(BITMAP_HANDLE hHandle) {
	auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);

	return pBitmap->Clear();
}

EXPORT_API ResultCode Bitmap_ClearRect(BITMAP_HANDLE hHandle, unsigned int nX, unsigned int nY, unsigned int nWidth, unsigned int nHeight) {
	auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);

	return pBitmap->ClearRect(nX, nY, nWidth, nHeight);
}

EXPORT_API ResultCode Bitmap_ClearRect2(BITMAP_HANDLE hHandle, RECT_HANDLE hRect) {
	auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);
	const auto pCastedRect = reinterpret_cast<Iris2D::Rect*>(hRect);

	return pBitmap->ClearRect(pCastedRect);
}

EXPORT_API COLOR_HANDLE Bitmap_GetPixel(BITMAP_HANDLE hHandle, unsigned int nX, unsigned int nY) {
	const auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);

	return pBitmap->GetPixel(nX, nY);
}

EXPORT_API ResultCode Bitmap_SetPixel(BITMAP_HANDLE hHandle, unsigned int nX, unsigned int nY, COLOR_HANDLE hColor) {
	auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);
	const auto pCastedColor = reinterpret_cast<Iris2D::Color*>(hColor);

	return pBitmap->SetPixel(nX, nY, pCastedColor);
}

EXPORT_API ResultCode Bitmap_SaveToFile(BITMAP_HANDLE hHandle, wchar_t* wstrFilePath) {
	auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);

	return pBitmap->SaveToFile(wstrFilePath);
}

EXPORT_API ResultCode Bitmap_HueChange(BITMAP_HANDLE hHandle, float fHue) {
	auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);

	return pBitmap->HueChange(fHue);
}

EXPORT_API void Bitmap_SetFont(BITMAP_HANDLE hHandle, FONT_HANDLE* pFont) {
	auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);
	auto pCastedFont = reinterpret_cast<Iris2D::Font*&>(*pFont);

	pBitmap->SetFont(pCastedFont);
}
EXPORT_API FONT_HANDLE Bitmap_GetFont(BITMAP_HANDLE hHandle) {
	const auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);

	return pBitmap->GetFont();
}

EXPORT_API unsigned int Bitmap_TextSize(BITMAP_HANDLE hHandle, FONT_HANDLE hFont, wchar_t* wstrText) {
	auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);
	const auto pCastedFont = reinterpret_cast<Iris2D::Font*>(hFont);

	return pBitmap->TextSize(pCastedFont, wstrText);
}

EXPORT_API ResultCode Bitmap_DrawText(BITMAP_HANDLE hHandle, unsigned int nX, unsigned int nY, unsigned int nWidth, unsigned int nHeight, wchar_t* wstrText, Bitmap_AlignType nAlign) {
	auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);

	return pBitmap->DrawText(nX, nY, nWidth, nHeight, wstrText, static_cast<Iris2D::AlignType>(nAlign));
}

EXPORT_API ResultCode Bitmap_DrawText2(BITMAP_HANDLE hHandle, RECT_HANDLE hRect, wchar_t* wstrText, Bitmap_AlignType nAlign) {
	auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);
	const auto pCastedRect = reinterpret_cast<Iris2D::Rect*>(hRect);

	return pBitmap->DrawText(pCastedRect, wstrText, static_cast<Iris2D::AlignType>(nAlign));
}
EXPORT_API ResultCode Bitmap_Dispose(BITMAP_HANDLE hHandle) {
	auto pBitmap = reinterpret_cast<Iris2D::Bitmap*>(hHandle);

	return pBitmap->Dispose();
}