Ejemplo n.º 1
0
def add_model(model):
    def from_polar(kwargs_dict=None, *, model=model, **actual_kwargs):
        try:
            if kwargs_dict:
                return model.objects.get(**kwargs_dict)
            else:
                return model.objects.get(**actual_kwargs)
        except ObjectDoesNotExist:
            return None

    setattr(model, "_from_polar", from_polar)
    Polar().register_class(model, from_polar=model._from_polar)
Ejemplo n.º 2
0
 def setupCore(self):
     '''
     status:
         0 :     waiting
         1 :     running
         -1 :    crashed
     '''
     self.status = 0
     self.fpath_mrls = ''
     self.fpath_urls = ''
     self.fpath_mls = ''
     self.sapp = Polar()
Ejemplo n.º 3
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("filename", nargs="+")

    args = parser.parse_args()
    polar = Polar()

    for filename in args.filename:
        if filename.endswith(".py"):
            load_python(filename, polar)

    for filename in args.filename:
        if filename.endswith(".polar"):
            load(filename, polar)
def route_rings(board, **kwargs):
    kwargs['skip_start'] = False
    kwargs['include_end'] = True
    for net in board.netlist.values():
        if net.flag_routed or len(net.terminals) != OPT.lines.n_lines + 1:
            continue
        # Check if this has n LED or ring terminals
        cnt_led = len(filter(lambda x: x.component.name.startswith(OPT.lines.led_pfx) and x.component.flag_placed,
                             net.terminals))
        cnt_res = len(filter(lambda x: x.component.name.startswith(OPT.lines.res_pfx) and x.component.flag_placed,
                             net.terminals))
        if cnt_res == 0 and cnt_led == OPT.lines.n_lines:
            # Ok that's one of the two ring nets.
            OPT.rings.gnd_net = net.name
            radius = OPT.rings.gnd_radius
            overhang = OPT.rings.overhang
        elif cnt_res == OPT.lines.n_lines and cnt_led == 0:
            # Ok that's one of the two ring nets.
            OPT.rings.pwr_net = net.name
            radius = OPT.rings.pwr_radius
            overhang = -OPT.rings.overhang
        else:
            continue
        del net.tracks[:]
        intersection_angles = []
        for t in filter(lambda x: x.component.flag_placed, net.terminals):
            # Get the pad position
            term_pol = t.position.to_polar()
            # Decide the endpoint for the arc
            arc_endpt = Polar(term_pol.a + overhang, term_pol.r)
            # Draw an arc to that point
            net.tracks.append(Track(map(Polar.to_point, apx_arc_through_polars(term_pol, arc_endpt, **kwargs))))
            # Draw a segment down to the given radius
            net.tracks.append(Track([arc_endpt.to_point(), Polar(arc_endpt.a, radius).to_point()]))
            # Angle at which it intersects the ring
            intersection_angles.append(arc_endpt.a)
        # Ok now join all the pieces. Add all pieces at multiples of 15 degrees so that we can attach at several angles
        intersection_angles += list(map(lambda x: float(x) * math.pi / 6., range(24)))
        intersection_angles = list(sorted(map(normalize_angle, intersection_angles)))
        # Ok pairwise arcs
        p1 = Polar(intersection_angles[-1], radius)
        for angle in intersection_angles:
            p2 = Polar(angle, radius)
            net.tracks.append(Track(map(Polar.to_point, apx_arc_through_polars(p1, p2, **kwargs))))
            p1 = p2
Ejemplo n.º 5
0
def polar():
    """ Set up a polar instance and tear it down after the test."""
    p = Polar()
    yield p
    del p
def project_on_ring(pt, radius):
    a = math.asin(pt.y / radius)
    return Polar(a if pt.x >= 0. else math.pi - a, radius).to_point()
Ejemplo n.º 7
0
 def addPolar(self, id, home, sources, callback=None):
     self._polars[id] = Polar(id, home, sources, callback)
     return self._polars[id]
Ejemplo n.º 8
0
class MainWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self,parent)
        self.setupCore()
        self.setupUI()
    
    def setupCore(self):
        '''
        status:
            0 :     waiting
            1 :     running
            -1 :    crashed
        '''
        self.status = 0
        self.fpath_mrls = ''
        self.fpath_urls = ''
        self.fpath_mls = ''
        self.sapp = Polar()

    def setupUI(self):
        # 定义组件
        self.button_start = QtGui.QPushButton(u'启动')
        self.button_exit = QtGui.QPushButton(u'退出')
        self.button_fopen_urefer = QtGui.QPushButton(u'选择未调制参考光')
        self.button_fopen_mrefer = QtGui.QPushButton(u'选择已调制参考光')
        self.button_fopen_meassure = QtGui.QPushButton(u'选择实测光谱')
        self.button_plot_mls = QtGui.QPushButton(u'实测')
        self.button_plot_mf = QtGui.QPushButton(u'载波')
        self.button_plot_af = QtGui.QPushButton(u'自相关函数')
        self.button_plot_ch = QtGui.QPushButton(u'调制各通道')
        self.button_plot_sv = QtGui.QPushButton(u'STOKES矢量')
        self.button_plot_dop = QtGui.QPushButton(u'偏振')
        self.button_plot_urls = QtGui.QPushButton(u'未调制参考光')
        self.button_plot_mrls = QtGui.QPushButton(u'已调制参考光')
        self.button_plot_nsv = QtGui.QPushButton(u'归一化矢量')
        self.button_plot_sffti = QtGui.QPushButton(u'参考光自相关')
        self.label_isset_urefer = QtGui.QLabel(u'未设置')
        self.label_isset_mrefer = QtGui.QLabel(u'未设置')
        self.label_isset_meassure = QtGui.QLabel(u'未设置')
        self.label_title_plot = QtGui.QLabel(u'绘制图像')
        self.label_conf_ns = QtGui.QLabel(u'插值点数: 2^')
        self.label_conf_lens_1 = QtGui.QLabel(u'LENS_1:')
        self.label_conf_lens_2 = QtGui.QLabel(u'LENS_2:')
        self.label_status = QtGui.QLabel(u'=等待=')
        self.lineedit_conf_ns = QtGui.QLineEdit()
        self.lineedit_conf_lens_1 = QtGui.QLineEdit()
        self.lineedit_conf_lens_2 = QtGui.QLineEdit()
        self.checkbox_conf_autofix = QtGui.QCheckBox(u'载波自动修正')
        self.checkbox_conf_highfreq = QtGui.QCheckBox(u'使用高频成分')
        self.checkbox_mf = QtGui.QCheckBox(u'重新计算载波')

        self.lineedit_conf_ns.setText('10')
        self.lineedit_conf_lens_1.setText('1.9')
        self.lineedit_conf_lens_2.setText('3.6')
        self.checkbox_mf.setCheckState(2)
        self.checkbox_conf_autofix.setCheckState(2)
        self.checkbox_conf_highfreq.setCheckState(2)

        # 设置布局
        layout_main = QtGui.QVBoxLayout()
        layout_settings = QtGui.QVBoxLayout()
        layout_buttons = QtGui.QHBoxLayout()
        layout_settings_grid = QtGui.QGridLayout()
        layout_status_bar = QtGui.QHBoxLayout()

        layout_settings_grid.addWidget(self.label_isset_urefer,0,0,1,1)
        layout_settings_grid.addWidget(self.label_isset_mrefer,1,0,1,1)
        layout_settings_grid.addWidget(self.button_fopen_urefer,0,1,1,1)
        layout_settings_grid.addWidget(self.button_fopen_mrefer,1,1,1,1)
        layout_settings_grid.addWidget(self.checkbox_mf,2,0,1,2)
        layout_settings_grid.addWidget(self.label_isset_meassure,3,0,1,1)
        layout_settings_grid.addWidget(self.button_fopen_meassure,3,1,1,1)

        layout_settings_grid.addWidget(self.label_conf_ns,4,0,1,1)
        layout_settings_grid.addWidget(self.label_conf_lens_1,5,0,1,1)
        layout_settings_grid.addWidget(self.label_conf_lens_2,6,0,1,1)
        layout_settings_grid.addWidget(self.lineedit_conf_ns,4,1,1,1)
        layout_settings_grid.addWidget(self.lineedit_conf_lens_1,5,1,1,1)
        layout_settings_grid.addWidget(self.lineedit_conf_lens_2,6,1,1,1)
        layout_settings_grid.addWidget(self.checkbox_conf_autofix,7,0,1,2)
        layout_settings_grid.addWidget(self.checkbox_conf_highfreq,8,0,1,2)

        layout_settings_grid.addWidget(self.label_title_plot,9,0,1,2)
        layout_settings_grid.addWidget(self.button_plot_mls,10,0,1,1)
        layout_settings_grid.addWidget(self.button_plot_mf,11,0,1,1)
        layout_settings_grid.addWidget(self.button_plot_dop,12,0,1,1)
        layout_settings_grid.addWidget(self.button_plot_af,10,1,1,1)
        layout_settings_grid.addWidget(self.button_plot_ch,11,1,1,1)
        layout_settings_grid.addWidget(self.button_plot_sv,12,1,1,1)
        layout_settings_grid.addWidget(self.button_plot_mrls,13,0,1,1)
        layout_settings_grid.addWidget(self.button_plot_urls,13,1,1,1)
        layout_settings_grid.addWidget(self.button_plot_sffti,14,0,1,1)
        layout_settings_grid.addWidget(self.button_plot_nsv,14,1,1,1)
        layout_settings_grid.setColumnStretch(2, 1);

        layout_settings.addLayout(layout_settings_grid)
        layout_settings.addStretch(1)
        layout_buttons.addStretch(1)
        layout_buttons.addWidget(self.label_status)
        layout_buttons.addWidget(self.button_start)
        layout_buttons.addWidget(self.button_exit)
        layout_main.addLayout(layout_settings)
        layout_main.addLayout(layout_buttons)
        self.setLayout(layout_main)

        # 设置信号与槽
        self.connect(self.button_start, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('startup()'))
        self.connect(self.button_exit, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('exit()'))
        self.connect(self.button_fopen_mrefer, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('chooseMRLS()'))
        self.connect(self.button_fopen_urefer, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('chooseURLS()'))
        self.connect(self.button_fopen_meassure, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('chooseMLS()'))
        self.connect(self.button_plot_sv, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('plotSV()'))
        self.connect(self.button_plot_ch, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('plotCHX()'))
        self.connect(self.button_plot_af, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('plotFFTX()'))
        self.connect(self.button_plot_dop, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('plotDOP()'))
        self.connect(self.button_plot_mf, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('plotMF()'))
        self.connect(self.button_plot_mls, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('plotMLS()'))
        self.connect(self.button_plot_urls, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('plotURLS()'))
        self.connect(self.button_plot_mrls, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('plotMRLS()'))
        self.connect(self.button_plot_nsv, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('plotNSV()'))
        self.connect(self.button_plot_sffti, QtCore.SIGNAL('clicked()'), self, QtCore.SLOT('plotSFFTX()'))
        self.connect(self.checkbox_mf, QtCore.SIGNAL('stateChanged(int)'), self, QtCore.SLOT('disableRefer()'))

    def figPlot(self, datagram, title='', color='black'):
        fig = pylab.figure(title)
        pylab.plot(datagram.getXaxis(),datagram.getYaxis(),label=title,color=color,linestyle='-')

    @QtCore.pyqtSlot()
    def plotDOP(self):
        pylab.close()
        self.figPlot(self.sapp.dop,'DOP', 'blue')
        pylab.show()

    @QtCore.pyqtSlot()
    def plotSV(self):
        pylab.close()
        self.figPlot(self.sapp.s0, 'STOKES VECTOR 0', 'red')
        self.figPlot(self.sapp.s1, 'STOKES VECTOR 1', 'black')
        self.figPlot(self.sapp.s2, 'STOKES VECTOR 2', 'blue')
        self.figPlot(self.sapp.s3, 'STOKES VECTOR 3', 'green')
        pylab.show()

    @QtCore.pyqtSlot()
    def plotFFTX(self):
        pylab.close()
        pylab.plot(self.sapp.rffti[0].getXaxis(),self.sapp.rffti[0].getYaxis(),label='FFT CHANNEL 0')
        pylab.plot(self.sapp.rffti[1].getXaxis(),self.sapp.rffti[1].getYaxis(),label='FFT CHANNEL 1')
        pylab.plot(self.sapp.rffti[2].getXaxis(),self.sapp.rffti[2].getYaxis(),label='FFT CHANNEL 2')
        pylab.plot(self.sapp.rffti[3].getXaxis(),self.sapp.rffti[3].getYaxis(),label='FFT CHANNEL 3')
        pylab.show()

    @QtCore.pyqtSlot()
    def plotMF(self):
        pylab.close()
        self.figPlot(self.sapp.cs0, 'MODULATION FACTOR 0', 'red')
        self.figPlot(self.sapp.cs1, 'MODULATION FACTOR 1', 'black')
        self.figPlot(self.sapp.cs2, 'MODULATION FACTOR 2', 'blue')
        self.figPlot(self.sapp.cs3, 'MODULATION FACTOR 3', 'green')
        pylab.show()

    @QtCore.pyqtSlot()
    def plotCHX(self):
        pylab.close()
        self.figPlot(self.sapp.fa0, 'CHANNEL 0', 'red')
        self.figPlot(self.sapp.fa1, 'CHANNEL 1', 'black')
        self.figPlot(self.sapp.fa2, 'CHANNEL 2', 'blue')
        self.figPlot(self.sapp.fa3, 'CHANNEL 3', 'green')
        pylab.show()

    @QtCore.pyqtSlot()
    def plotMLS(self):
        pylab.close()
        self.figPlot(self.sapp.rdata, 'MEASURED DATA', 'blue')
        pylab.show()

    @QtCore.pyqtSlot()
    def plotMRLS(self):
        pylab.close()
        self.figPlot(self.sapp.sdata, 'MODULATED REFERENCE LIGHT SPECTRUM', 'blue')
        pylab.show()

    @QtCore.pyqtSlot()
    def plotURLS(self):
        pylab.close()
        self.figPlot(self.sapp.idata, 'MODULATED REFERENCE LIGHT SPECTRUM', 'blue')
        pylab.show()

    @QtCore.pyqtSlot()
    def plotNSV(self):
        pylab.close()
        self.figPlot(self.sapp.nsv1, 'NOMALIZED STOKES VECTOR 1', 'red')
        self.figPlot(self.sapp.nsv2, 'NOMALIZED STOKES VECTOR 2', 'green')
        self.figPlot(self.sapp.nsv3, 'NOMALIZED STOKES VECTOR 3', 'blue')
        pylab.show()

    @QtCore.pyqtSlot()
    def plotSFFTX(self):
        pylab.close()
        pylab.plot(self.sapp.sffti[0].getXaxis(),self.sapp.sffti[0].getYaxis(),label='RLS FFT CHANNEL 0')
        pylab.plot(self.sapp.sffti[1].getXaxis(),self.sapp.sffti[1].getYaxis(),label='RLS FFT CHANNEL 1')
        pylab.plot(self.sapp.sffti[2].getXaxis(),self.sapp.sffti[2].getYaxis(),label='RLS FFT CHANNEL 2')
        pylab.plot(self.sapp.sffti[3].getXaxis(),self.sapp.sffti[3].getYaxis(),label='RLS FFT CHANNEL 3')
        pylab.show()

    @QtCore.pyqtSlot()
    def disableRefer(self):
        checked = self.checkbox_mf.isChecked()
        self.button_fopen_urefer.setEnabled(checked)
        self.button_fopen_mrefer.setEnabled(checked)
        if checked:
            self.label_isset_urefer.setText(u'未设置')
            self.label_isset_mrefer.setText(u'未设置')
        else:
            self.label_isset_urefer.setText(u'已禁用')
            self.label_isset_mrefer.setText(u'已禁用')

    @QtCore.pyqtSlot()
    def exit(self):
        QtGui.qApp.quit()

    @QtCore.pyqtSlot()
    def chooseMRLS(self):
        self.fpath_mrls = QtGui.QFileDialog.getOpenFileName(self, u'选取已调制参考光', '', '', '', QtGui.QFileDialog.DontUseNativeDialog)
        self.label_isset_mrefer.setText(u'已设置')

    @QtCore.pyqtSlot()
    def chooseURLS(self):
        self.fpath_urls = QtGui.QFileDialog.getOpenFileName(self, u'选取未调制参考光', '', '', '', QtGui.QFileDialog.DontUseNativeDialog)
        self.label_isset_urefer.setText(u'已设置')

    @QtCore.pyqtSlot()
    def chooseMLS(self):
        self.fpath_mls = QtGui.QFileDialog.getOpenFileName(self, u'选取实测光谱', '', '', '', QtGui.QFileDialog.DontUseNativeDialog)
        self.label_isset_meassure.setText(u'已设置')

    @QtCore.pyqtSlot()
    def startup(self):
        self.label_status.setText(u'*运行*')
        mf = self.checkbox_mf.isChecked()
        ns = 2**(int)(self.lineedit_conf_ns.text());
        d1 = (float)(self.lineedit_conf_lens_1.text())
        d2 = (float)(self.lineedit_conf_lens_2.text())
        autofix = self.checkbox_conf_autofix.isChecked()
        if self.checkbox_conf_highfreq.isChecked():
            freq = 'high'
        else:
            freq = 'low'
        urls = unicode(self.fpath_urls)
        mrls = unicode(self.fpath_mrls)
        mls = unicode(self.fpath_mls)
        conf = {
            'mf': mf,
            'ns': ns,
            'd1': d1,
            'd2': d2,
            'autofix': autofix,
            'freq': freq,
            'urls': urls,
            'mrls': mrls,
            'mls': mls
        }
        self.sapp.config(conf)
        self.sapp.start()
        print('***DONE***')
        self.label_status.setText(u'=等待=')
Ejemplo n.º 9
0
from boat import *
from environment import Environment
from simulator import Simulator
from polar import Polar

# import configuration
if os.path.isfile('config.py'):
    import config
else:
    print(
        "Please copy config_default.py to config.py to add your own strategies"
    )
    import config_default as config

# load polar
polar = Polar(os.path.join('data', 'polars', 'first-27.csv'))

# setup boat
real_boat = len(sys.argv) == 2 and sys.argv[1] == 'real'

if real_boat:
    print('Using real boat, happy sailing!')
    env = Environment(wind_speed_var=1, wind_direction_var=0)
    boat = RealBoat(env, ip_address='192.168.43.185')
    shuffle_interval = 0
else:
    print('Using simulator boat')
    env = Environment()
    boat = SimBoat(env, polar=polar)
    shuffle_interval = 20
Ejemplo n.º 10
0
def test_register_constants_with_decorator():
    @polar_class
    class RegisterDecoratorTest:
        x = 1

    p = Polar()
    p.load_str("foo_rule(x: RegisterDecoratorTest, y) if y = 1;")
    p.load_str("foo_class_attr(y) if y = RegisterDecoratorTest.x;")
    assert (next(
        p.query_rule("foo_rule", RegisterDecoratorTest(),
                     Variable("y")))["bindings"]["y"] == 1)
    assert next(p.query_rule("foo_class_attr",
                             Variable("y")))["bindings"]["y"] == 1

    p = Polar()
    p.load_str("foo_rule(x: RegisterDecoratorTest, y) if y = 1;")
    p.load_str("foo_class_attr(y) if y = RegisterDecoratorTest.x;")
    assert (next(
        p.query_rule("foo_rule", RegisterDecoratorTest(),
                     Variable("y")))["bindings"]["y"] == 1)
    assert next(p.query_rule("foo_class_attr",
                             Variable("y")))["bindings"]["y"] == 1
Ejemplo n.º 11
0
def test_load_and_query():
    p = Polar()
    p.load_str("f(1); f(2); ?= f(1); ?= not f(3);")

    with pytest.raises(exceptions.PolarException):
        p.load_str("g(1); ?= g(2);")
Ejemplo n.º 12
0
	def renew(self):

		####### prepare points
		planesPoints = {}
		polarsPoints = {}

		for qid, queue in self._queues.items():
			points = queue.dequeue()
			if not points:
				continue

			for point in points:
				code = point.info('code')
				if code not in planesPoints:
					planesPoints[code] = []
				planesPoints[code].append(point)

				if qid not in polarsPoints:
					polarsPoints[qid] = []
				polarsPoints[qid].append(point)

		####### handle planes
		added = {}
		updated = {}

		for code, points in planesPoints.items():
			if code not in self._planes:
				self._planes[code] = Plane(points)
				added[code] = self._planes[code]
			else:
				updated[code] = (self._planes[code], self._planes[code].pointsCount())
				self._planes[code].addPoints(points)

		if added:
			self.planesAdded.emit(added)
		if updated:
			self.planesUpdated.emit(updated)


		####### handle polars
		added = {}
		updated = {}

		for qid, points in polarsPoints.items():
			if qid not in self._polars:
				self._polars[qid] = Polar(self._home, points)
				added[qid] = self._polars[qid]
			else:
				if self._polars[qid].addPoints(points):
					updated[qid] = self._polars[qid]

		if added:
			self.polarsAdded.emit(added)
		if updated:
			self.polarsUpdated.emit(updated)

		####### remove timed out planes
		t = time()
		removed = []
		for id, plane in self._planes.items():
			if t > plane.lastSeen() + PLANE_TIMEOUT:
				self._planes.pop(id)
				removed.append(id)
		self.planesRemoved.emit(removed)