163 changes: 159 additions & 4 deletions pyKst/pykst.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,36 @@ def image(self, name):
"""
return Image(self, "", "", name, new=False)

def new_linear_fit(self, xVector, yVector, weightvector = 0, name = ""):
""" Create a New Linear Fit in kst.
See :class:`LinearFit`
"""
return LinearFit(self, xVector, yVector, weightvector, name)

def linear_fit(self, name):
""" Returns a linear fit from kst given its name.
See :class:`LinearFit`
"""
return LinearFit(self, "", "", 0, name, new=False)


def new_polynomial_fit(self, order, xVector, yVector, weightvector = 0, name = ""):
""" Create a New Polynomial Fit in kst.
See :class:`PolynomialFit`
"""
return PolynomailFit(self, order, xVector, yVector, weightvector, name)

def polynomial_fit(self, name):
""" Returns a polynomial fit from kst given its name.
See :class:`PolynomialFit`
"""
return PolynomialFit(self, 0, "", "", 0, name, new=False)


def new_label(self, text, pos=(0.5,0.5), rot=0, fontSize=12,
bold=False, italic=False, fontColor="black",
fontFamily="Serif", name="") :
Expand Down Expand Up @@ -326,7 +356,7 @@ def new_ellipse(self,pos=(0.1,0.1), size=(0.1,0.1),
fixAspect, name)

def ellipse(self, name):
""" Returns a from kst given its name.
""" Returns an ellipse from kst given its name.
See :class:`Ellipse`
"""
Expand Down Expand Up @@ -1254,8 +1284,8 @@ def setMatrix(self, matrix):
""" change the matrix which is the source of the image. """
self.client.send_si(self.handle, "setMatrix("+matrix.handle+")")

def setPalette(self, pallet):
""" set the pallet, selected by index.
def setPalette(self, palette):
""" set the palette, selected by index.
0 Grey
1 Red
Expand All @@ -1267,8 +1297,132 @@ def setPalette(self, pallet):
Note: this is not the same order as the dialog.
"""
self.client.send_si(self.handle, "setPalette("+matrix.handle+")")
self.client.send_si(self.handle, "setPalette("+b2str(palette)+")")

def setRange(self, zmin, zmax):
""" sets the z range of the color map."""
self.client.send_si(self.handle, "setFixedColorRange("+
b2str(zmin)+","+b2str(zmax)+")")

def setAutoRange(self, saturated=0):
""" Automatically set the z range of the color map
:param saturated: The colormap range is set so that this fraction
of the points in the matrix are saturated.
Equal numbers of points are saturated at both ends of the color map.
"""
self.client.send_si(self.handle, "setAutoColorRange("+b2str(saturated) + ")")

def max_z(self):
""" Returns the max Z value of the curve or image. """
return self.client.send_si(self.handle, "maxZ()")

def min_z(self):
""" Returns the max Z value of the curve or image. """
return self.client.send_si(self.handle, "minZ()")

# FIT ###################################################################
class Fit(NamedObject) :
""" This is a class which provides some methods common to all fits """
def __init__(self,client) :
NamedObject.__init__(self,client)

def parameters(self) :
""" a vector containing the Parameters of the fit """
vec = VectorBase(self.client)
vec.handle = self.client.send_si(self.handle, "outputVector(Parameters Vector)")
return vec

def fit(self) :
""" a vector containing the fit """
vec = VectorBase(self.client)
vec.handle = self.client.send_si(self.handle, "outputVector(Fit)")
return vec

def residuals(self) :
""" a vector containing the Parameters of the fit """
vec = VectorBase(self.client)
vec.handle = self.client.send_si(self.handle, "outputVector(Residuals)")
return vec

def covariance(self) :
""" a vector containing the Covariance of the fit """
vec = VectorBase(self.client)
vec.handle = self.client.send_si(self.handle, "outputVector(Covariance)")
return vec

def reduced_chi2(self) :
""" a scalar containing the Parameters of the fit """
X = Scalar(self.client)
X.handle = self.client.send_si(self.handle, "outputScalar(chi^2/nu)")
return X


# LINEAR FIT ############################################################
class LinearFit(Fit) :
""" A linear fit inside kst.
If weightvector is 0, then the fit is unweighted.
"""
def __init__(self, client, xvector, yvector, weightvector=0, name="", new=True) :
Fit.__init__(self,client)

if (new == True):
if weightvector==0:
self.client.send("newPlugin(Linear Fit)")
else:
self.client.send("newPlugin(Linear Weighted Fit)")
self.client.send("setInputVector(Weights Vector,"+weightvector.handle+")")

self.client.send("setInputVector(X Vector,"+xvector.handle+")")
self.client.send("setInputVector(Y Vector,"+yvector.handle+")")
self.handle=self.client.send("endEdit()")
self.handle.remove(0,self.handle.indexOf("ing ")+4)
self.set_name(name)
else:
self.handle = name

def slope(self) :
""" The slope of the fit. """
vec = VectorBase(self.client)
vec.handle = self.client.send_si(self.handle, "outputVector(Parameters Vector)")
return vec.value(1)

def intercept(self) :
""" The intercept of the fit. """
vec = VectorBase(self.client)
vec.handle = self.client.send_si(self.handle, "outputVector(Parameters Vector)")
return vec.value(0)

# POLYNOMIAL FIT ############################################################
class PolynomailFit(Fit) :
""" A Polynomial fit inside kst.
:param order: The order of the fit
"""
def __init__(self, client, order, xvector, yvector, weightvector=0, name="", new=True) :
Fit.__init__(self,client)

if (new == True):
if weightvector==0:
self.client.send("newPlugin(Polynomial Fit)")
else:
self.client.send("newPlugin(Polynomial Weighted Fit)")
self.client.send("setInputVector(Weights Vector,"+weightvector.handle+")")

self.client.send("setInputVector(X Vector,"+xvector.handle+")")
self.client.send("setInputVector(Y Vector,"+yvector.handle+")")
self.client.send("setInputScalar(Order Scalar,"+order.handle+")")
self.handle=self.client.send("endEdit()")
self.handle.remove(0,self.handle.indexOf("ing ")+4)
self.set_name(name)
else:
self.handle = name



# View Items ################################################################
class ViewItem(NamedObject):
""" Convenience class. You should not use it directly."""
def __init__(self,client):
Expand Down Expand Up @@ -1447,6 +1601,7 @@ def remove(self):
""" This removes the object from Kst. """
self.client.send("eliminate("+self.handle+")")


# LABELS ######################################################################
class Label(ViewItem) :
""" A floating label inside kst.
Expand Down
2 changes: 1 addition & 1 deletion src/datasources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ endif()
if(cfitsio)
include_directories(${CFITSIO_INCLUDE_DIR})
kst_add_plugin(. fitsimage)
kst_add_plugin(. fitstable)
#kst_add_plugin(. fitstable)
#kst_add_plugin(. healpix)
kst_link(${CFITSIO_LIBRARIES})
endif()
Expand Down
3 changes: 2 additions & 1 deletion src/datasources/ascii/asciisource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ int AsciiSource::readField(double *v, const QString& field, int s, int n)
}

// TODO multi threading problem: could trigger a dead-lock
// FIXME: Debug::trace() here is a memory leak, which is serious for large quickly updating files.
//Debug::trace(QString("AsciiSource::readField() %1 s=%2 n=%3").arg(field.leftJustified(15)).arg(QString("%1").arg(s, 10)).arg(n));

int read = tryReadField(v, field, s, n);
Expand Down Expand Up @@ -571,7 +572,7 @@ void AsciiSource::updateFieldMessage(const QString& message)
void AsciiSource::updateFieldProgress(const QString& message)
{
if (_read_count_max == 0) {
//emitProgress(-1, ""); // indicate "busy" FIXME: doesn't go away so don't start it.
//emitProgress(-1, ""); // indicate "busy" FIXME: commented out because it doesn't go away.
} else {
if (_progressSteps != 0 && _read_count_max != -1) {
emitProgress(50 + 50 * _progress / _progressSteps, _actualField + ": " + message);
Expand Down
6 changes: 6 additions & 0 deletions src/libkst/vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "debug.h"
#include "objectstore.h"
#include "updatemanager.h"
#include "vectorscriptinterface.h"

namespace Kst {

Expand Down Expand Up @@ -95,6 +96,11 @@ Vector::~Vector() {
}


ScriptInterface* Vector::createScriptInterface() {
return new VectorSI(this);
}


void Vector::deleteDependents() {

for (QHash<QString, ScalarPtr>::Iterator it = _scalars.begin(); it != _scalars.end(); ++it) {
Expand Down
2 changes: 2 additions & 0 deletions src/libkst/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ class KSTCORE_EXPORT Vector : public Primitive
/** does the vector represent time? */
virtual bool isTime() const {return false;}

virtual ScriptInterface* createScriptInterface();

protected:
/** current number of samples */
int _size;
Expand Down
32 changes: 32 additions & 0 deletions src/libkst/vectorscriptinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,37 @@ QByteArray VectorGenSI::endEditUpdate() {
return ("Finished editing "+vector->Name()).toLatin1();
}

/******************************************************/
/* Plain (base) Vectors */
/******************************************************/
VectorSI::VectorSI(VectorPtr it) {
vector=it;
}

QString VectorSI::doCommand(QString command) {

QString v=doVectorScriptCommand(command, vector);
if (!v.isEmpty()) {
return v;
}

return "No such command";
}

bool VectorSI::isValid() {
return vector.isPtrValid();
}

ScriptInterface* VectorSI::newVector(ObjectStore *) {
return 0L;
}

QByteArray VectorSI::endEditUpdate() {
vector->registerChange();
UpdateManager::self()->doUpdates(true);
UpdateServer::self()->requestUpdateSignal();
return ("Finished editing "+vector->Name()).toLatin1();
}


}
14 changes: 14 additions & 0 deletions src/libkst/vectorscriptinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,19 @@ class KSTCORE_EXPORT VectorGenSI : public ScriptInterface
static ScriptInterface* newVector(ObjectStore *store);
};


class KSTCORE_EXPORT VectorSI : public ScriptInterface
{
Q_OBJECT
VectorPtr vector;
public:
explicit VectorSI(VectorPtr it);
QString doCommand(QString);
bool isValid();
QByteArray endEditUpdate();

static ScriptInterface* newVector(ObjectStore *);
};

}
#endif // VECTORSCRIPTINTERFACE_H
29 changes: 15 additions & 14 deletions src/libkstapp/scriptserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include "relationscriptinterface.h"

#include "dataobjectscriptinterface.h"

#include "sessionmodel.h"
#include "updateserver.h"
#include "datagui.h"
Expand Down Expand Up @@ -126,7 +128,7 @@ ScriptServer::ScriptServer(ObjectStore *obj) : _server(new QLocalServer(this)),
// _fnMap.insert("newPSD()",&ScriptServer::newPSD);

_fnMap.insert("getPluginList()", &ScriptServer::getPluginList);
// _fnMap.insert("newPlugin()",&ScriptServer::newPlugin);
_fnMap.insert("newPlugin()",&ScriptServer::newPlugin);

_fnMap.insert("getImageList()",&ScriptServer::getImageList);
_fnMap.insert("newImage()",&ScriptServer::newImage);
Expand Down Expand Up @@ -318,7 +320,6 @@ inline QByteArray join(const QByteArrayList&n,const char&r) {
/** The heart of the script server. This function is what performs all the actions. s may be null. */
QByteArray ScriptServer::exec(QByteArray command, QLocalSocket *s)
{
//qDebug() << "ScripteServerExec" << command;
if(command.isEmpty()) {
return handleResponse("",s);
}
Expand Down Expand Up @@ -638,19 +639,19 @@ QByteArray ScriptServer::getPluginList(QByteArray&, QLocalSocket* s,ObjectStore*
return outputObjectList<BasicPlugin>(s,_store);
}

/*
QByteArray ScriptServer::newPlugin(QByteArray& plugin, QLocalSocket* s,ObjectStore* store,const int&ifMode,

if(_interface) {
return handleResponse("To access this function, first call endEdit()",s);
} else {
plugin.replace("newPlugin(","");
plugin.remove(plugin.lastIndexOf(")"),1);
_interface = DialogLauncherSI::self->newPlugin(store, plugin);
return handleResponse("Ok",s);
}
QByteArray ScriptServer::newPlugin(QByteArray& plugin, QLocalSocket* s,ObjectStore* store) {

if(_interface) {
return handleResponse("To access this function, first call endEdit()",s);
} else {
plugin.replace("newPlugin(","");
plugin.remove(plugin.lastIndexOf(")"),1);
_interface = DataObjectSI::newPlugin(store, plugin);
return handleResponse("Ok",s);
}
}
*/


QByteArray ScriptServer::getImageList(QByteArray&, QLocalSocket* s,ObjectStore*_store) {

Expand Down Expand Up @@ -884,7 +885,7 @@ QByteArray ScriptServer::beginEdit(QByteArray&command, QLocalSocket* s,ObjectSto
if (view_item) {
_interface = view_item->scriptInterface();
return handleResponse("Ok",s);
} else {
} else {
ObjectPtr o=_store->retrieveObject(command);
if (o) {
_interface = o->scriptInterface();
Expand Down
2 changes: 1 addition & 1 deletion src/libkstapp/scriptserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public slots:
//QByteArray newPSD(QByteArray& command, QLocalSocket* s,ObjectStore*_store);

QByteArray getPluginList(QByteArray& command, QLocalSocket* s,ObjectStore*_store);
//QByteArray newPlugin(QByteArray& command, QLocalSocket* s,ObjectStore*_store);
QByteArray newPlugin(QByteArray& command, QLocalSocket* s,ObjectStore*_store);

QByteArray getImageList(QByteArray& command, QLocalSocket* s,ObjectStore*_store);
QByteArray newImage(QByteArray& command, QLocalSocket* s,ObjectStore*_store);
Expand Down
Loading