class ProbeNotificationListener(BaseNotificationListener): initialize_called = Bool(False) deliver_called = Bool(False) finalize_called = Bool(False) initialize_function = Function(default_value=pass_function) deliver_function = Function(default_value=pass_function) finalize_function = Function(default_value=pass_function) initialize_call_args = Any() deliver_call_args = Any() finalize_call_args = Any(([], {})) def initialize(self, model): self.initialize_called = True self.initialize_call_args = ([model], {}) self.initialize_function(model) def deliver(self, event): self.deliver_called = True self.deliver_call_args = ([event], {}) self.deliver_function(event) def finalize(self): self.finalize_called = True self.finalize_function()
class ProbeNotificationListenerFactory(BaseNotificationListenerFactory): initialize_function = Function(default_value=pass_function) deliver_function = Function(default_value=pass_function) finalize_function = Function(default_value=pass_function) raises_on_create_model = Bool(False) raises_on_create_listener = Bool(False) raises_on_initialize_listener = Bool(False) raises_on_finalize_listener = Bool(False) raises_on_deliver_listener = Bool(False) def get_name(self): return "test_notification_listener" def get_identifier(self): return "probe_notification_listener" def get_listener_class(self): return ProbeNotificationListener def get_model_class(self): return ProbeNotificationListenerModel def create_model(self, model_data=None): if self.raises_on_create_model: raise Exception("ProbeNotificationListenerFactory.create_model") if model_data is None: model_data = {} return self.model_class(self, **model_data) def create_listener(self): if self.raises_on_create_listener: raise Exception("ProbeNotificationListenerFactory.create_listener") if self.raises_on_initialize_listener: initialize_function = raise_exception else: initialize_function = self.initialize_function if self.raises_on_finalize_listener: finalize_function = raise_exception else: finalize_function = self.finalize_function if self.raises_on_deliver_listener: deliver_function = raise_exception else: deliver_function = self.deliver_function return self.listener_class( self, initialize_function=initialize_function, deliver_function=deliver_function, finalize_function=finalize_function, )
class ProbeMCO(BaseMCO): run_function = Function(default_value=run_func) run_called = Bool(False) def run(self, evaluator): self.run_called = True return self.run_function(evaluator)
class ProbeDataSourceFactory(BaseDataSourceFactory): run_function = Function(default_value=run_func) input_slots_type = CUBAType("PRESSURE") output_slots_type = CUBAType("PRESSURE") input_slots_size = Int(1) output_slots_size = Int(1) raises_on_create_model = Bool(False) raises_on_create_data_source = Bool(False) raises_on_data_source_run = Bool(False) def get_identifier(self): return "probe_data_source" def get_name(self): return "test_data_source" def get_model_class(self): return ProbeDataSourceModel def get_data_source_class(self): return ProbeDataSource def create_model(self, model_data=None): if self.raises_on_create_model: raise Exception("ProbeDataSourceFactory.create_model") if model_data is None: model_data = {} return self.model_class(factory=self, input_slots_type=self.input_slots_type, output_slots_type=self.output_slots_type, input_slots_size=self.input_slots_size, output_slots_size=self.output_slots_size, **model_data) def create_data_source(self): if self.raises_on_create_data_source: raise Exception("ProbeDataSourceFactory.create_data_source") if self.raises_on_data_source_run: run_function = raise_exception else: run_function = self.run_function return self.data_source_class(factory=self, run_function=run_function)
class DerivativeFactor(Factor): Operator = Function(default_value=_DefaultOperator, arg_type="Function", label="算子", order=0) ModelArgs = Dict(arg_type="Dict", label="参数", order=1) DataType = Enum("double", "string", "object", arg_type="SingleOption", label="数据类型", order=2) def __init__(self, name="", descriptors=[], sys_args={}, **kwargs): self._Descriptors = descriptors self.UserData = {} if descriptors: kwargs.setdefault("logger", descriptors[0]._QS_Logger) return super().__init__(name=name, ft=None, sys_args=sys_args, config_file=None, **kwargs) @property def Descriptors(self): return self._Descriptors def getMetaData(self, key=None, args={}): DataType = args.get("数据类型", self.DataType) if key is None: return pd.Series({"DataType": DataType}) elif key == "DataType": return DataType return None def start(self, dts, **kwargs): for iDescriptor in self._Descriptors: iDescriptor.start(dts=dts, **kwargs) return 0 def end(self): for iDescriptor in self._Descriptors: iDescriptor.end() return 0
class ProbeDataSource(BaseDataSource): run_function = Function(default_value=run_func) run_called = Bool(False) slots_called = Bool(False) def run(self, model, parameters): self.run_called = True return self.run_function(model, parameters) def slots(self, model): self.slots_called = True return ( (tuple( Slot(type=model.input_slots_type) for _ in range(model.input_slots_size))), (tuple( Slot(type=model.output_slots_type) for _ in range(model.output_slots_size))), )
class Figure(HasTraits): """For displaying images, note that grayscale uint8 does not seem to work because of a bug in numpy 1.4.x graysale float works! By default grayscale images are displayed as a RGB grayscale (much faster) >>> from scipy.misc.pilutil import imread >>> im = imread('testdata/noise.png') >>> f = Figure() >>> f.plot_image(im) >>> result = f.configure_traits() >>> f.plot_image(im[:,:,0]) >>> result = f.configure_traits() """ pd = Instance(ArrayPlotData, transient=True) plot = Instance(Plot, transient=True) process_selection = Function(transient=True) #: defines which color map to use for grayscale images ('gray' or 'color') colormap = Enum('gray', 'color') traits_view = View(Group(Item('plot', editor=ComponentEditor(size=size, bgcolor=bg_color), show_label=False), orientation="vertical"), resizable=True) def __init__(self, **kwds): super(Figure, self).__init__(**kwds) self.pd = self._pd_default() self.plot = self._plot_default() def _process_selection_default(self): def process(point0, point1): print('selection', point0, point1) return process def _pd_default(self): image = numpy.array([]) pd = ArrayPlotData() pd.set_data("imagedata", image) return pd def _plot_default(self): return self._create_plot_component() def _create_plot_component(self): # Create the plot pd = self.pd plot = Plot(pd, default_origin="top left", orientation="h") plot.x_axis.orientation = "top" plot.padding = 20 #plot.y_axis.orientation = "top" # Tweak some of the plot properties #plot.bgcolor = "white" plot.bgcolor = bg_color # Attach some tools to the plot # plot.tools.append(PanTool(plot,constrain_key="shift", drag_button = 'right')) printer = DataPrinter(component=plot, process=self.process_selection) plot.tools.append(printer) plot.overlays.append( ZoomTool(component=plot, tool_mode="box", always_on=False)) return plot def plot_image(self, data): """Plots image from a given array :param array data: Input image array """ self._plot_image(self.plot, data) def _plot_image(self, plot, data): if self.colormap == 'gray': image = to_RGB(data) else: image = data self.pd.set_data("imagedata", image) plot.aspect_ratio = float(image.shape[1]) / image.shape[0] if not plot.plots: img_plot = plot.img_plot("imagedata", name='image')[0] img_plot = plot.plots['image'][0] img_plot.edit_traits() plot.request_redraw() def update_image(self, data): import warnings warnings.warn('Use plot_image insteead!', DeprecationWarning, 2) self.plot_image(data) def plot_data(self, x, y, name='data 0', color='black'): """Plots additional line data on top of the image. :param x: x data :param y: y data :param name: name of a plot, you can call :meth:`del_plot` to delete it later :param color: color of plotted data """ self._plot_data(self.plot, x, y, name, color) def _plot_data(self, plot, x, y, name='data 0', color='black'): xname = 'x_' + name yname = 'y_' + name self.pd.set_data(xname, x) self.pd.set_data(yname, y) self._del_plot(plot, name) plot.plot((xname, yname), name=name, color=color) plot.request_redraw() def del_plot(self, name): """Delete a plot """ self._del_plot(self.plot, name) def _del_plot(self, plot, name): try: plot.delplot(name) except: pass
class _AnnTable(_TSTable): """公告信息表""" #ANNDate = Enum(None, arg_type="SingleOption", label="公告日期", order=0) Operator = Function(None, arg_type="Function", label="算子", order=1) LookBack = Int(0, arg_type="Integer", label="回溯天数", order=2) def __init__(self, name, fdb, sys_args={}, **kwargs): FactorInfo = fdb._FactorInfo.loc[name] IDInfo = FactorInfo[FactorInfo["FieldType"] == "ID"] if IDInfo.shape[0] == 0: self._IDField = self._IDType = None else: self._IDField, self._IDType = IDInfo.index[0], IDInfo[ "Supplementary"].iloc[0] self._AnnDateField = FactorInfo[FactorInfo["FieldType"] == "ANNDate"].index[0] # 公告日期 self._ConditionFields = FactorInfo[ FactorInfo["FieldType"] == "Condition"].index.tolist() # 所有的条件字段列表 return super().__init__(name=name, fdb=fdb, sys_args=sys_args, **kwargs) def __QS_initArgs__(self): super().__QS_initArgs__() FactorInfo = self._FactorDB._FactorInfo.loc[self.Name] for i, iCondition in enumerate(self._ConditionFields): self.add_trait( "Condition" + str(i), Str("", arg_type="String", label=iCondition, order=i + 2)) self[iCondition] = str(FactorInfo.loc[iCondition, "Supplementary"]) @property def FactorNames(self): FactorInfo = self._FactorDB._FactorInfo.loc[self.Name] return FactorInfo[FactorInfo["FieldType"] == "因子"].index.tolist() + [ self._AnnDateField ] def getID(self, ifactor_name=None, idt=None, args={}): if self._IDField is None: return ["000000.HST"] TableType = self._FactorDB._TableInfo.loc[self.Name, "Supplementary"] if TableType == "A股": return self._FactorDB.getStockID(index_id="全体A股", is_current=False) elif TableType == "期货": return self._FactorDB.getFutureID(future_code=None, is_current=False) return [] def getDateTime(self, ifactor_name=None, iid=None, start_dt=None, end_dt=None, args={}): return self._FactorDB.getTradeDay(start_date=start_dt, end_date=end_dt, exchange="", output_type="datetime") def __QS_genGroupInfo__(self, factors, operation_mode): ConditionGroup = {} for iFactor in factors: iConditions = ";".join([ iArgName + ":" + iFactor[iArgName] for iArgName in iFactor.ArgNames if iArgName not in ("回溯天数", "算子") ]) if iConditions not in ConditionGroup: ConditionGroup[iConditions] = { "FactorNames": [iFactor.Name], "RawFactorNames": {iFactor._NameInFT}, "StartDT": operation_mode._FactorStartDT[iFactor.Name], "args": iFactor.Args.copy() } else: ConditionGroup[iConditions]["FactorNames"].append(iFactor.Name) ConditionGroup[iConditions]["RawFactorNames"].add( iFactor._NameInFT) ConditionGroup[iConditions]["StartDT"] = min( operation_mode._FactorStartDT[iFactor.Name], ConditionGroup[iConditions]["StartDT"]) ConditionGroup[iConditions]["args"]["回溯天数"] = max( ConditionGroup[iConditions]["args"]["回溯天数"], iFactor.LookBack) EndInd = operation_mode.DTRuler.index(operation_mode.DateTimes[-1]) Groups = [] for iConditions in ConditionGroup: StartInd = operation_mode.DTRuler.index( ConditionGroup[iConditions]["StartDT"]) Groups.append((self, ConditionGroup[iConditions]["FactorNames"], list(ConditionGroup[iConditions]["RawFactorNames"]), operation_mode.DTRuler[StartInd:EndInd + 1], ConditionGroup[iConditions]["args"])) return Groups def __QS_prepareRawData__(self, factor_names, ids, dts, args={}): FactorInfo = self._FactorDB._FactorInfo.loc[self.Name] StartDate, EndDate = dts[0].date(), dts[-1].date() StartDate -= dt.timedelta(args.get("回溯天数", self.LookBack)) Fields = [self._AnnDateField] + factor_names if self._IDField is not None: Fields.insert(0, self._IDField) DBFields = FactorInfo["DBFieldName"].loc[Fields].tolist() DBTableName = self._FactorDB._TableInfo.loc[self.Name, "DBTableName"] RawData = pd.DataFrame(columns=DBFields) FieldStr = ",".join(DBFields) Conditions = {} for iConditionField in self._ConditionFields: iDataType = FactorInfo.loc[iConditionField, "DataType"] if iDataType == "str": Conditions[FactorInfo.loc[iConditionField, "DBFieldName"]] = args.get( iConditionField, self[iConditionField]) elif iDataType == "float": Conditions[FactorInfo.loc[iConditionField, "DBFieldName"]] = float( args.get(iConditionField, self[iConditionField])) elif iDataType == "int": Conditions[FactorInfo.loc[iConditionField, "DBFieldName"]] = int( args.get(iConditionField, self[iConditionField])) StartDate, EndDate = StartDate.strftime("%Y%m%d"), EndDate.strftime( "%Y%m%d") if self._IDField is None: RawData = self._FactorDB._ts.query(DBTableName, start_date=StartDate, end_date=EndDate, fields=FieldStr, **Conditions) RawData.insert(0, "ID", "000000.HST") DBFields.insert(0, "ID") elif pd.isnull(self._IDType): for iID in self._FactorDB.ID2DBID(ids): Conditions[DBFields[0]] = iID iData = self._FactorDB._ts.query(DBTableName, start_date=StartDate, end_date=EndDate, fields=FieldStr, **Conditions) while iData.shape[0] > 0: RawData = RawData.append(iData) iEndDate = iData[DBFields[1]].min() iData = self._FactorDB._ts.query(DBTableName, start_date=StartDate, end_date=iEndDate, fields=FieldStr, **Conditions) iEndDateData = iData[iData[DBFields[1]] == iEndDate] iRawEndDateMask = (RawData[DBFields[1]] == iEndDate) if iEndDateData.shape[0] > iRawEndDateMask.sum(): RawData = RawData[~iRawEndDateMask] RawData = RawData.append(iEndDateData) iData = iData[iData[DBFields[1]] < iEndDate] elif self._IDType == "Non-Finite": RawData = self._FactorDB._ts.query(DBTableName, start_date=StartDate, end_date=EndDate, fields=FieldStr, **Conditions) RawData = RawData.loc[:, DBFields] RawData.columns = ["ID", "日期"] + factor_names RawData["ID"] = self._FactorDB.DBID2ID(RawData["ID"]) return RawData.sort_values(by=["ID", "日期"]) def __QS_calcData__(self, raw_data, factor_names, ids, dts, args={}): if raw_data.shape[0] == 0: return pd.Panel(items=factor_names, major_axis=dts, minor_axis=ids) raw_data = raw_data.set_index(["日期", "ID"]) Operator = args.get("算子", self.Operator) if Operator is None: Operator = (lambda x: x.tolist()) Data = {} for iFactorName in factor_names: Data[iFactorName] = raw_data[iFactorName].groupby( axis=0, level=[0, 1]).apply(Operator).unstack() Data = pd.Panel(Data).loc[factor_names, :, ids] Data.major_axis = [ dt.datetime.strptime(iDate, "%Y%m%d") for iDate in Data.major_axis ] LookBack = args.get("回溯天数", self.LookBack) if LookBack == 0: return Data.loc[:, dts, ids] AllDTs = Data.major_axis.union(dts).sort_values() Data = Data.loc[:, AllDTs, ids] Limits = LookBack * 24.0 * 3600 for i, iFactorName in enumerate(Data.items): Data.iloc[i] = fillNaByLookback(Data.iloc[i], lookback=Limits) return Data.loc[:, dts]
class GGPlot(HasTraits): """ Represents a Grammar of Graphics plot. Most of the ggplot functions extend or manipulate this object, by adding new renderers and changing the data pipeline (or extracting statistics from data). The most common operation on this object is the '+' operator, which adds renderers to the plot. """ # Pandas dataframe dataset = Any() # Instance of Aesthetic which usually defines the data columns aes = Any() # list of Geom objects that define the actual things to plot on the # graph itself geoms = List() facet_layout = Any() # The window object in the current session that contains the plot # corresponding to this plot window = Any() # The redraw function to call display_hook = Function() def __init__(self, dataset, **kw): super(GGPlot, self).__init__(dataset=dataset, **kw) def __add__(self, obj): """ Appends another graphical element to this plot, or, in the case of facets, causes this plot to perform a re-layout. """ # Check to see if a facet or a geom is being passed in if isinstance(obj, Geom): print "added geom:", obj self.geoms.append(obj) if self.window is not None: self._update_plot() elif isinstance(obj, Facet): print "setting facet:", obj self.facet_layout = obj elif isinstance(obj, Aesthetic): if self.aes is None: self.aes = obj # Use the first aesthetic we get to set the window title if self.window: self.window.set_title(obj.x + " * " + obj.y) else: self.aes + obj elif isinstance(obj, Tool): self._add_tool(obj) if self.window is not None: self._redraw() return self def show_plot(self): from chaco import shell if self.window is None: win_num = shell.session.new_window() self.window = shell.session.get_window(win_num) if self.aes is not None: self.window.set_title(self.aes.x + " * " + self.aes.y) plot = self.window.get_container() self._initialize_plot(plot) plot.request_redraw() else: self._update_plot() def _initialize_plot(self, plotcontainer): # Create the data source ppd = PandasPlotData(self.dataset) plotcontainer.data = ppd if self.facet_layout is None: [g.plot(plotcontainer, self.aes) for g in self.geoms] else: # Use the PandasPlotData to create a list of faceted data sources facet_pds = ppd.facet(self.facet_layout.factors) container = None if self.facet_layout.ftype == "grid": # Determine the shape by looking at the first faceted datasource. # Reaching into the facet_pds is sort of gorpy; need to think # of a better interface for PandasPlotData. levels = facet_pds[0]._groupby.grouper.levels grid_shape = (len(levels[0]), len(levels[1])) print "Factors:", self.facet_layout.factors print "Grid of shape:", grid_shape print "Levels:", levels[0], levels[1] container = chaco.GridContainer(padding=20, fill_padding=True, bgcolor="lightgray", use_backbuffer=False, shape=grid_shape, spacing=(10,10)) pd_dict = dict((pd.group_key, pd) for pd in facet_pds) factors = self.facet_layout.factors title = factors[0] + "=%d, " + factors[1] + "=%d" for i in levels[0]: for j in levels[1]: if (i,j) in pd_dict: plot = chaco.Plot(pd_dict[(i,j)], title=title%(i,j), padding=15) plot.index_range.tight_bounds = False plot.value_range.tight_bounds = False [g.plot(plot, self.aes) for g in self.geoms] else: plot = chaco.OverlayPlotContainer(bgcolor="lightgray") container.add(plot) elif self.facet_layout.ftype == "wrap": # This is not really wrapping, instead just using a horizontal # plot container. container = chaco.HPlotContainer(padding=40, fill_padding=True, bgcolor="lightgray", use_backbuffer=True, spacing=20) self.window.set_container(container) container.request_redraw() def _update_plot(self): # Very simple right now: check our list of Geoms and if they # don't have a renderer, then plot them. for g in self.geoms: if g._renderer is None: print "Updating plot with geom", g g.plot(self.window.get_container(), self.aes) self._redraw() def _add_tool(self, toolspec): # depending on the kind of tool, we have to attach it to different # things in the plot component hierarchy. if toolspec.type == "regression": # Find the first scatterplot for g in self.geoms: if isinstance(g._renderer, chaco.ScatterPlot): plot = g._renderer tool = RegressionLasso(plot, selection_datasource=plot.index) plot.tools.append(tool) plot.overlays.append(RegressionOverlay(plot, lasso_selection=tool)) break else: print "Unable to find a suitable scatterplot for regression tool" elif toolspec.type == "pan": cont = self.window.get_container() tool = PanTool(cont) if toolspec.button is not None: tool.drag_button = toolspec.button cont.tools.append(tool) elif toolspec.type == "zoom": cont = self.window.get_container() zoom = ZoomTool(cont, tool_mode="box", always_on=False) cont.overlays.append(zoom) def _redraw(self): """ Private method that is called whenever a change is made that should cause an interactive display update. By default this is a NOP so that this module can be used in library form, and not merely in interactive mode. """ if self.window: self.window.get_container().invalidate_and_redraw() return
def test_function_deprecated(self): with self.assertWarnsRegex(DeprecationWarning, "Function trait type"): Function()