Пример #1
0
def new_task(args):
    name = ' '.join(args.name)

    if not name:
        print(INVALID_NAME_MSG)
        return

    date_str = args.date

    if date_str:
        try:
            date = interpret_date(date_str)
        except ValueError as e:
            print(e)
            return
    else:
        date = None

    estimated_hours = float(args.est_hours)

    manager = TaskManager(fileio.load())
    id = manager.new_task(name, estimated_hours, date)
    fileio.save(manager.task_dict)

    print_table(manager, id)
Пример #2
0
  def load(self, fname = 'save.dcb'):
    ''''''

    # TODO check if this is dirty.
    try:
      self.lec = fileio.load(fname)
      return True
    except fileio.FormatError:
      return False
Пример #3
0
 def doOpen(self):
     self.__resetFileInfo()
     fileName = unicode(QtGui.QFileDialog.getOpenFileName())
     try:
         self.__rawValues = fileio.load(fileName)
     except Exception, e:
         msg = QtGui.QMessageBox()
         msg.setText('Error while loading file: %s (%s)' % (fileName, str(e)))
         msg.exec_()
Пример #4
0
    def load(self, fname='save.dcb'):
        ''''''

        # TODO check if this is dirty.
        try:
            self.lec = fileio.load(fname)
            return True
        except fileio.FormatError:
            return False
Пример #5
0
def clock(args):
    manager = TaskManager(fileio.load())
    task_id = args.id
    to_deduct = args.to_deduct
    # TODO Complete if less than/eq 0?
    try:
        old_task: task.Task = manager.task_dict[task_id]
        old_task.hours_remaining -= to_deduct

        fileio.save(manager.task_dict)
        print_table(manager, task_id)
    except KeyError:
        print(INVALID_ID_MSG)
Пример #6
0
def load_response_vars(datafile, maskfile=None, vol=True):
    """ load response variables (of any data type)"""

    if fileio.file_type(datafile) == 'nifti':
        dat = fileio.load_nifti(datafile, vol=vol)
        volmask = fileio.create_mask(dat, mask=maskfile)
        Y = fileio.vol2vec(dat, volmask).T
    else:
        Y = fileio.load(datafile)
        volmask = None
        if fileio.file_type(datafile) == 'cifti':
            Y = Y.T

    return Y, volmask
Пример #7
0
def complete_task(args):
    manager = TaskManager(fileio.load())
    ids = args.id
    if not ids:
        print(INVALID_ID_MSG)
    else:
        completed_tasks_dict = dict()
        for i in ids:
            try:
                completed_tasks_dict[i] = manager.task_dict[i]
                manager.complete(i)
                fileio.save(manager.task_dict)
            except KeyError:
                print(INVALID_ID_MSG)
        print_table(manager)
        print()
        print_completed_tasks(completed_tasks_dict)
Пример #8
0
def modify_task(args):
    manager = TaskManager(fileio.load())
    task_id = args.id
    try:
        old_task: task.Task = manager.task_dict[task_id]
        new_name = args.name if args.name else old_task.name
        if args.date:
            new_date = interpret_date(args.date)
        elif args.floating or isinstance(old_task, task.FloatingTask):
            new_date = None
        else:
            new_date = old_task.due_date
        new_hours_remaining = args.est_hours if args.est_hours else old_task.hours_remaining
        manager.new_task(new_name, new_hours_remaining, new_date, task_id)
        fileio.save(manager.task_dict)
        print_table(manager, task_id)
    except KeyError:
        print(INVALID_ID_MSG)
    except ValueError as e:
        print(e)
Пример #9
0
def load_jams_range(filename,
                    feature_name,
                    annotator=0,
                    annotator_name=None,
                    converter=None,
                    context='large_scale',
                    confidence=False):
    """Import specific data from a JAMS annotation file. It imports range
		data, i.e., data that spans within two time points and it has a label
		associated with it.

		:parameters:
		- filename : str
		Path to the annotation file.

		- feature_name: str
		The key to the JAMS range feature to be extracted
		(e.g. "sections", "chords")

		- annotator: int
		The idx of the annotator from which to extract the annotations.

		- annotator_name: str
		The name of the annotator from which to extract the annotations. If not
		None, this parameter overwrites the "annotator".

		- converter : function
		Function to convert time-stamp data into numerics. Defaults to float().

		- context : str
		Context of the labels to be extracted (e.g. "large_scale", "function").

		:returns:
		- event_times : np.ndarray
		array of event times (float).

		- event_labels : list of str
		list of corresponding event labels.
	"""

    if converter is None:
        converter = float

    jam = load(filename)
    if annotator_name is not None:
        annotator = get_annotator_idx(jam, feature_name, annotator_name,
                                      filename)

    try:
        jam = load(filename)
    except:
        print "Error: could not open %s (JAMS module not installed?)" % \
         filename
        return [], []

    times = []
    labels = []
    conf = []
    if len(jam[feature_name]) == 0:
        print "Warning: %s empty in file %s" % (feature_name, filename)
        return []

    for data in jam[feature_name][annotator].data:
        if data.label.context == context:
            times.append(
                [converter(data.start.value),
                 converter(data.end.value)])
            conf.append([
                converter(data.start.confidence),
                converter(data.end.confidence)
            ])
            labels.append(data.label.value)

    times = np.asarray(times)
    print 'times', times
    if confidence:
        return times, labels, conf
    else:
        return times, labels
Пример #10
0
#!/usr/bin/python

import sys
sys.path.append('..')
import fileio
from datatypes import *

if len(sys.argv) <= 0: sys.exit()

lec = fileio.load('../saves/ll2.dcb')
ofile = open('tmp.csv', 'w')

it = iter(lec)
try:
  last_point = None
  while True:
    e = it.next()
    if isinstance(e, Point):
      if last_point is None or last_point.x() != e.x() and last_point.y() != e.y():
        ofile.write("%d,%d,%d," % (e.x() * 800, e.y() * 600, e.t * 1000))
      last_point = e
    elif last_point is not None:
      last_point = None
      ofile.write("\n")

except StopIteration:
  pass

print 'Done!'
ofile.close()
Пример #11
0
def load_jams_range(filename, feature_name, annotator=0, annotator_name=None,
					converter=None, context='large_scale', confidence=False):
	"""Import specific data from a JAMS annotation file. It imports range
		data, i.e., data that spans within two time points and it has a label
		associated with it.

		:parameters:
		- filename : str
		Path to the annotation file.

		- feature_name: str
		The key to the JAMS range feature to be extracted
		(e.g. "sections", "chords")

		- annotator: int
		The idx of the annotator from which to extract the annotations.

		- annotator_name: str
		The name of the annotator from which to extract the annotations. If not
		None, this parameter overwrites the "annotator".

		- converter : function
		Function to convert time-stamp data into numerics. Defaults to float().

		- context : str
		Context of the labels to be extracted (e.g. "large_scale", "function").

		:returns:
		- event_times : np.ndarray
		array of event times (float).

		- event_labels : list of str
		list of corresponding event labels.
	"""

	if converter is None:
		converter = float
	jam = load(filename) # error introduced since here.

	if annotator_name is not None:
		annotator = get_annotator_idx(jam, feature_name, annotator_name, filename)

	try:
		jam = load(filename)
	except:
		print "Error: could not open %s (JAMS module not installed?)" % \
			filename
		return [], []
		
	times	= []
	labels	= []
	conf	= []
	if len(jam[feature_name]) == 0:
		print "Warning: %s empty in file %s" % (feature_name, filename)
		return []
	
	for data in jam[feature_name][annotator].data:
		if data.label.context == context:
			times.append([converter(data.start.value),
						  converter(data.end.value)])
			conf.append([converter(data.start.confidence),
						 converter(data.end.confidence)])
			labels.append(data.label.value)
		else:
			print '%s != %s, convert not excecuted.' %(data.label.context,context)

	times = np.asarray(times)

	if confidence:
		return times, labels, conf
	else:
		return times, labels
Пример #12
0
# -------------------------------- Testing --------------------------------- #
##############################################################################


def _load_from_file(fname):
    f = open(fname, "r")
    times = []
    try:
        while True:
            line = f.next().strip()
            times.append(int(line[:-3]) * 60 + int(line[-2:]))
    except StopIteration:
        pass
    finally:
        f.close()
    return times


if __name__ == "__main__":
    import sys, fileio

    iname = sys.argv[1]
    trace, audio = fileio.load(iname)
    oname = iname[:-4] if iname[:-1].lower().endswith(".dc") else iname
    times = _load_from_file(sys.argv[2]) if len(sys.argv) == 3 else None
    if times is not None:
        tofe = min(trace.get_time_of_first_event(), audio[0][0])
        times = map(lambda x: x + tofe, times)
    to_pdf(trace, oname, (480, 320), times)
    to_png(trace, oname, (480, 320), times)
Пример #13
0
if __name__ == '__main__':
    # valid video modules in preferred order
    VALID_V_MODULES = ['gtkgui', 'macgui', 'qtgui', 'dummy']

    # valid audio modules in preferred order
    VALID_A_MODULES = ['alsa', 'macaudio', 'qtaudio', 'dummy']

    config = parse_args(sys.argv[1:])

    if config.help_req:
        Configuration.print_usage()
        sys.exit(0)

    if config.export_fmt is not None:
        if config.export_fmt == 'swf':
            lec = fileio.load(config.file_to_load)
            exporter.to_swf(lec, lec.adats, config.file_to_load[:-4] + '.swf')
        elif config.export_fmt == 'pdf':
            lec = fileio.load(config.file_to_load)
            exporter.to_pdf(lec, config.file_to_load[:-4] + '.swf')
        elif config.export_fmt in ['dcd', 'dcb', 'dcx', 'dar', 'dct']:
            lec = fileio.load(config.file_to_load)
            fileio.save(config.file_to_load[:-3] + config.export_fmt, lec,
                        lec.adats)
        else:
            print 'Unknown flag "--exp-%s"' % config.export_fmt
        sys.exit(0)

    # Something was passed, so use that to
    if config.audio_module is not None:
        try:
Пример #14
0
##############################################################################
# -------------------------------- Testing --------------------------------- #
##############################################################################


def _load_from_file(fname):
    f = open(fname, 'r')
    times = []
    try:
        while True:
            line = f.next().strip()
            times.append(int(line[:-3]) * 60 + int(line[-2:]))
    except StopIteration:
        pass
    finally:
        f.close()
    return times


if __name__ == "__main__":
    import sys, fileio
    iname = sys.argv[1]
    trace, audio = fileio.load(iname)
    oname = iname[:-4] if iname[:-1].lower().endswith('.dc') else iname
    times = _load_from_file(sys.argv[2]) if len(sys.argv) == 3 else None
    if times is not None:
        tofe = min(trace.get_time_of_first_event(), audio[0][0])
        times = map(lambda x: x + tofe, times)
    to_pdf(trace, oname, (480, 320), times)
    to_png(trace, oname, (480, 320), times)
Пример #15
0
def estimate(respfile, covfile, maskfile=None, cvfolds=None,
             testcov=None, testresp=None, alg='gpr', configparam=None,
             saveoutput=True, outputsuffix=None):
    """ Estimate a normative model

    This will estimate a model in one of two settings according to the
    particular parameters specified (see below):

    * under k-fold cross-validation
        required settings 1) respfile 2) covfile 3) cvfolds>2
    * estimating a training dataset then applying to a second test dataset
        required sessting 1) respfile 2) covfile 3) testcov 4) testresp
    * estimating on a training dataset ouput of forward maps mean and se
        required sessting 1) respfile 2) covfile 3) testcov

    The models are estimated on the basis of data stored on disk in ascii or
    neuroimaging data formats (nifti or cifti). Ascii data should be in
    tab or space delimited format with the number of subjects in rows and the
    number of variables in columns. Neuroimaging data will be reshaped
    into the appropriate format

    Basic usage::

        estimate(respfile, covfile, [extra_arguments])

    where the variables are defined below. Note that either the cfolds
    parameter or (testcov, testresp) should be specified, but not both.

    :param respfile: response variables for the normative model
    :param covfile: covariates used to predict the response variable
    :param maskfile: mask used to apply to the data (nifti only)
    :param cvfolds: Number of cross-validation folds
    :param testcov: Test covariates
    :param testresp: Test responses
    :param alg: Algorithm for normative model
    :param configparam: Parameters controlling the estimation algorithm
    :param saveoutput: Save the output to disk? Otherwise returned as arrays
    :param outputsuffix: Text string to add to the output filenames

    All outputs are written to disk in the same format as the input. These are:

    :outputs: * yhat - predictive mean
              * ys2 - predictive variance
              * Hyp - hyperparameters
              * Z - deviance scores
              * Rho - Pearson correlation between true and predicted responses
              * pRho - parametric p-value for this correlation
              * rmse - root mean squared error between true/predicted responses
              * smse - standardised mean squared error

    The outputsuffix may be useful to estimate multiple normative models in the
    same directory (e.g. for custom cross-validation schemes)
    """

    # load data
    print("Processing data in " + respfile)
    X = fileio.load(covfile)
    Y, maskvol = load_response_vars(respfile, maskfile)
    if len(Y.shape) == 1:
        Y = Y[:, np.newaxis]
    if len(X.shape) == 1:
        X = X[:, np.newaxis]
    Nmod = Y.shape[1]

    if testcov is not None:
        # we have a separate test dataset
        Xte = fileio.load(testcov)
        testids = range(X.shape[0], X.shape[0]+Xte.shape[0])
        trainids = range(0, X.shape[0])
        if len(Xte.shape) == 1:
            Xte = Xte[:, np.newaxis]
        if testresp is not None:
            Yte, testmask = load_response_vars(testresp, maskfile)
            if len(Yte.shape) == 1:
                Yte = Yte[:, np.newaxis]
        else:
            sub_te = Xte.shape[0]
            Yte = np.zeros([sub_te, Nmod])

        # treat as a single train-test split
        splits = CustomCV((range(0, X.shape[0]),), (testids,))

        Y = np.concatenate((Y, Yte), axis=0)
        X = np.concatenate((X, Xte), axis=0)

        # force the number of cross-validation folds to 1
        if cvfolds is not None and cvfolds != 1:
            print("Ignoring cross-valdation specification (test data given)")
        cvfolds = 1
    else:
        # we are running under cross-validation
        splits = KFold(n_splits=cvfolds)
        testids = range(0, X.shape[0])

    # find and remove bad variables from the response variables
    # note: the covariates are assumed to have already been checked
    nz = np.where(np.bitwise_and(np.isfinite(Y).any(axis=0),
                                 np.var(Y, axis=0) != 0))[0]

    # Initialise normative model
    nm = norm_init(X, alg=alg, configparam=configparam)

    # run cross-validation loop
    Yhat = np.zeros_like(Y)
    S2 = np.zeros_like(Y)
    Hyp = np.zeros((Nmod, nm.n_params, cvfolds))

    Z = np.zeros_like(Y)
    nlZ = np.zeros((Nmod, cvfolds))

    for idx in enumerate(splits.split(X)):
        fold = idx[0]
        tr = idx[1][0]
        te = idx[1][1]

        # standardize responses and covariates, ignoring invalid entries
        iy, jy = np.ix_(tr, nz)
        mY = np.mean(Y[iy, jy], axis=0)
        sY = np.std(Y[iy, jy], axis=0)
        Yz = np.zeros_like(Y)
        Yz[:, nz] = (Y[:, nz] - mY) / sY
        mX = np.mean(X[tr, :], axis=0)
        sX = np.std(X[tr, :],  axis=0)
        Xz = (X - mX) / sX

        # estimate the models for all subjects
        for i in range(0, len(nz)):  # range(0, Nmod):
            print("Estimating model ", i+1, "of", len(nz))      
            try:
                nm = norm_init(Xz[tr, :], Yz[tr, nz[i]], alg=alg, configparam=configparam)
                Hyp[nz[i], :, fold] = nm.estimate(Xz[tr, :], Yz[tr, nz[i]])

                # Work around to get stats for subject in th emodel and out. Instead of te for all :
                #yhat, s2 = nm.predict(Xz[tr, :], Yz[tr, nz[i]], Xz[te, :], Hyp[nz[i], :, fold])
                yhat, s2 = nm.predict(Xz[tr, :], Yz[tr, nz[i]], Xz, Hyp[nz[i], :, fold])

                #Yhat[te, nz[i]] = yhat * sY[i] + mY[i]
                Yhat[:, nz[i]] = yhat * sY[i] + mY[i]
                #S2[te, nz[i]] = s2 * sY[i]**2
                S2[:, nz[i]] = s2 * sY[i] ** 2
                nlZ[nz[i], fold] = nm.neg_log_lik
                if testcov is None:
                    #Z[te, nz[i]] = (Y[te, nz[i]] - Yhat[te, nz[i]]) / np.sqrt(S2[te, nz[i]])
                    Z[:, nz[i]] = (Y[:, nz[i]] - Yhat[:, nz[i]]) / np.sqrt(S2[:, nz[i]])
                else:
                    if testresp is not None:
                        #Z[te, nz[i]] = (Y[te, nz[i]] - Yhat[te, nz[i]]) / np.sqrt(S2[te, nz[i]])
                        Z[:, nz[i]] = (Y[:, nz[i]] - Yhat[:, nz[i]]) / np.sqrt(S2[:, nz[i]])

            except Exception as e:
                exc_type, exc_obj, exc_tb = sys.exc_info()
                fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
                print("Model ", i+1, "of", len(nz),
                      "FAILED!..skipping and writing NaN to outputs")
                print("Exception:")
                print(e)
                print(exc_type, fname, exc_tb.tb_lineno)
                Hyp[nz[i], :, fold] = float('nan')

                Yhat[te, nz[i]] = float('nan')
                S2[te, nz[i]] = float('nan')
                nlZ[nz[i], fold] = float('nan')
                if testcov is None:
                    Z[te, nz[i]] = float('nan')
                else:
                    if testresp is not None:
                        Z[te, nz[i]] = float('nan')


    # compute performance metrics
    if testcov is None:
        MSE = np.mean((Y[testids, :] - Yhat[testids, :])**2, axis=0)
        MSE_tr = np.mean((Y[0, X.shape[0], :] - Yhat[0, X.shape[0], :])**2, axis=0)
        RMSE = np.sqrt(MSE)
        # for the remaining variables, we need to ignore zero variances
        SMSE = np.zeros_like(MSE)
        Rho = np.zeros(Nmod)
        pRho = np.ones(Nmod)
        iy, jy = np.ix_(testids, nz)  # ids for tested samples nonzero values
        SMSE[nz] = MSE[nz] / np.var(Y[iy, jy], axis=0)
        Rho[nz], pRho[nz] = compute_pearsonr(Y[iy, jy], Yhat[iy, jy])
    else:
        if testresp is not None:
            MSE = np.mean((Y[testids, :] - Yhat[testids, :])**2, axis=0)
            MSE_tr = np.mean((Y[0:X.shape[0], :] - Yhat[0:X.shape[0], :]) ** 2, axis=0)

            RMSE = np.sqrt(MSE)
            RMSE_tr = np.sqrt(MSE_tr)

            # for the remaining variables, we need to ignore zero variances
            SMSE = np.zeros_like(MSE)
            SMSE_tr = np.zeros_like(RMSE_tr)

            Rho = np.zeros(Nmod)
            Rho_tr = np.zeros(Nmod)

            pRho = np.ones(Nmod)
            pRho_tr = np.ones(Nmod)

            iy, jy = np.ix_(testids, nz)  # ids tested samples nonzero values
            iy_tr, jy_tr = np.ix_(range(0, X.shape[0]), nz)

            SMSE[nz] = MSE[nz] / np.var(Y[iy, jy], axis=0)
            SMSE_tr[nz] = MSE[nz] / np.var(Y[iy_tr, jy_tr], axis=0)

            Rho[nz], pRho[nz] = compute_pearsonr(Y[iy, jy], Yhat[iy, jy])
            Rho_tr[nz], pRho_tr[nz] = compute_pearsonr(Y[iy_tr, jy_tr], Yhat[iy_tr, jy_tr])

    # Set writing options
    if saveoutput:
        print("Writing output ...")
        if fileio.file_type(respfile) == 'cifti' or \
           fileio.file_type(respfile) == 'nifti':
            exfile = respfile
        else:
            exfile = None
        if outputsuffix is not None:
            ext = str(outputsuffix) + fileio.file_extension(respfile)
        else:
            ext = fileio.file_extension(respfile)

        # Write output
        if testcov is None:
            fileio.save(Yhat[testids, :].T, 'yhat' + ext,
                        example=exfile, mask=maskvol)
            fileio.save(S2[testids, :].T, 'ys2' + ext,
                        example=exfile, mask=maskvol)
            fileio.save(Z[testids, :].T, 'Z' + ext, example=exfile,
                        mask=maskvol)
            fileio.save(Rho, 'Rho' + ext, example=exfile, mask=maskvol)
            fileio.save(pRho, 'pRho' + ext, example=exfile, mask=maskvol)
            fileio.save(RMSE, 'rmse' + ext, example=exfile, mask=maskvol)
            fileio.save(SMSE, 'smse' + ext, example=exfile, mask=maskvol)
            if cvfolds is None:
                fileio.save(Hyp[:,:,0], 'Hyp' + ext, example=exfile, mask=maskvol)
            else:
                for idx in enumerate(splits.split(X)):
                    fold = idx[0]
                    fileio.save(Hyp[:, :, fold], 'Hyp_' + str(fold+1) +
                                ext, example=exfile, mask=maskvol)
        else:
            if testresp is None:
                fileio.save(Yhat[testids, :].T, 'yhat' + ext,
                            example=exfile, mask=maskvol)
                fileio.save(S2[testids, :].T, 'ys2' + ext,
                            example=exfile, mask=maskvol)
                fileio.save(Hyp[:,:,0], 'Hyp' + ext,
                            example=exfile, mask=maskvol)
            else:
                fileio.save(Yhat[testids, :].T, 'yhat' + ext, example=exfile, mask=maskvol)
                fileio.save(Yhat[trainids, :].T, 'yhat_controls' + ext, example=exfile, mask=maskvol)

                fileio.save(S2[testids, :].T, 'ys2' + ext, example=exfile, mask=maskvol)
                fileio.save(S2[trainids, :].T, 'ys2_controls' + ext, example=exfile, mask=maskvol)

                fileio.save(Z[testids, :].T, 'Z' + ext, example=exfile, mask=maskvol)
                fileio.save(Z[trainids, :].T, 'Z_controls' + ext, example=exfile, mask=maskvol)

                fileio.save(Rho, 'Rho' + ext, example=exfile, mask=maskvol)
                fileio.save(Rho_tr, 'Rho_controls' + ext, example=exfile, mask=maskvol)

                fileio.save(pRho, 'pRho' + ext, example=exfile, mask=maskvol)
                fileio.save(pRho_tr, 'pRho_controls' + ext, example=exfile, mask=maskvol)

                fileio.save(RMSE, 'rmse' + ext, example=exfile, mask=maskvol)
                fileio.save(RMSE_tr, 'rmse_controls' + ext, example=exfile, mask=maskvol)

                fileio.save(SMSE, 'smse' + ext, example=exfile, mask=maskvol)
                fileio.save(SMSE_tr, 'smse_controls' + ext, example=exfile, mask=maskvol)
                if cvfolds is None:
                    fileio.save(Hyp[:,:,0], 'Hyp' + ext,
                                example=exfile, mask=maskvol)
                else:
                    for idx in enumerate(splits.split(X)):
                        fold = idx[0]
                        fileio.save(Hyp[:, :, fold], 'Hyp_'+ str(fold+1) +
                                    ext, example=exfile, mask=maskvol)
    else:
        if testcov is None:
            output = (Yhat, S2, Hyp, Z, Rho, pRho, RMSE, SMSE)
        else:
            if testresp is None:
                output = (Yhat, S2, Hyp)
            else:
                output = (Yhat, S2, Hyp, Z, Rho, pRho, RMSE, SMSE)
        return output
Пример #16
0
def estimate(respfile,
             covfile,
             maskfile=None,
             cvfolds=None,
             testcov=None,
             testresp=None,
             saveoutput=True,
             outputsuffix=None):
    """ Estimate a normative model

    This will estimate a model in one of two settings according to the
    particular parameters specified (see below):

    * under k-fold cross-validation
    * estimating a training dataset then applying to a second test dataset

    The models are estimated on the basis of data stored on disk in ascii or
    neuroimaging data formats (nifti or cifti). Ascii data should be in
    tab or space delimited format with the number of subjects in rows and the
    number of variables in columns. Neuroimaging data will be reshaped
    into the appropriate format

    Basic usage::

        estimate(respfile, covfile, [extra_arguments])

    where the variables are defined below. Note that either the cfolds
    parameter or (testcov, testresp) should be specified, but not both.

    :param respfile: response variables for the normative model
    :param covfile: covariates used to predict the response variable
    :param maskfile: mask used to apply to the data (nifti only)
    :param cvfolds: Number of cross-validation folds
    :param testcov: Test covariates
    :param testresp: Test responses
    :param saveoutput: Save the output to disk? Otherwise returned as arrays
    :param outputsuffix: Text string to add to the output filenames

    All outputs are written to disk in the same format as the input. These are:

    :outputs: * yhat - predictive mean
              * ys2 - predictive variance
              * Z - deviance scores
              * Rho - Pearson correlation between true and predicted responses
              * pRho - parametric p-value for this correlation
              * rmse - root mean squared error between true/predicted responses
              * smse - standardised mean squared error

    The outputsuffix may be useful to estimate multiple normative models in the
    same directory (e.g. for custom cross-validation schemes)
    """

    # load data
    print("Processing data in " + respfile)
    X = fileio.load(covfile)
    Y, maskvol = load_response_vars(respfile, maskfile)
    if len(Y.shape) == 1:
        Y = Y[:, np.newaxis]
    if len(X.shape) == 1:
        X = X[:, np.newaxis]
    Nmod = Y.shape[1]

    if testcov is not None:
        # we have a separate test dataset
        Xte = fileio.load(testcov)
        Yte, testmask = load_response_vars(testresp, maskfile)
        testids = range(X.shape[0], X.shape[0] + Xte.shape[0])

        if len(Yte.shape) == 1:
            Yte = Yte[:, np.newaxis]
        if len(Xte.shape) == 1:
            Xte = Xte[:, np.newaxis]

        # treat as a single train-test split
        splits = CustomCV((range(0, X.shape[0]), ), (testids, ))

        Y = np.concatenate((Y, Yte), axis=0)
        X = np.concatenate((X, Xte), axis=0)

        # force the number of cross-validation folds to 1
        if cvfolds is not None and cvfolds != 1:
            print("Ignoring cross-valdation specification (test data given)")
        cvfolds = 1
    else:
        # we are running under cross-validation
        splits = KFold(n_splits=cvfolds)
        testids = range(0, X.shape[0])

    # find and remove bad variables from the response variables
    # note: the covariates are assumed to have already been checked
    nz = np.where(
        np.bitwise_and(np.isfinite(Y).any(axis=0),
                       np.var(Y, axis=0) != 0))[0]

    # starting hyperparameters. Could also do random restarts here
    covfunc = CovSum(X, ('CovLin', 'CovSqExpARD'))
    hyp0 = np.zeros(covfunc.get_n_params() + 1)

    # run cross-validation loop
    Yhat = np.zeros_like(Y)
    S2 = np.zeros_like(Y)
    Z = np.zeros_like(Y)
    nlZ = np.zeros((Nmod, cvfolds))
    Hyp = np.zeros((Nmod, len(hyp0), cvfolds))
    for idx in enumerate(splits.split(X)):
        fold = idx[0]
        tr = idx[1][0]
        te = idx[1][1]

        # standardize responses and covariates, ignoring invalid entries
        iy, jy = np.ix_(tr, nz)
        mY = np.mean(Y[iy, jy], axis=0)
        sY = np.std(Y[iy, jy], axis=0)
        Yz = np.zeros_like(Y)
        Yz[:, nz] = (Y[:, nz] - mY) / sY
        mX = np.mean(X[tr, :], axis=0)
        sX = np.std(X[tr, :], axis=0)
        Xz = (X - mX) / sX

        # estimate the models for all subjects
        for i in range(0, len(nz)):  # range(0, Nmod):
            print("Estimating model ", i + 1, "of", len(nz))
            gpr = GPR(hyp0, covfunc, Xz[tr, :], Yz[tr, nz[i]])
            Hyp[nz[i], :, fold] = gpr.estimate(hyp0, covfunc, Xz[tr, :],
                                               Yz[tr, nz[i]])

            yhat, s2 = gpr.predict(Hyp[nz[i], :, fold], Xz[tr, :],
                                   Yz[tr, nz[i]], Xz[te, :])

            Yhat[te, nz[i]] = yhat * sY[i] + mY[i]
            S2[te, nz[i]] = np.diag(s2) * sY[i]**2
            Z[te, nz[i]] = (Y[te, nz[i]] - Yhat[te, nz[i]]) / \
                           np.sqrt(S2[te, nz[i]])
            nlZ[nz[i], fold] = gpr.nlZ

    # compute performance metrics
    MSE = np.mean((Y[testids, :] - Yhat[testids, :])**2, axis=0)
    RMSE = np.sqrt(MSE)
    # for the remaining variables, we need to ignore zero variances
    SMSE = np.zeros_like(MSE)
    Rho = np.zeros(Nmod)
    pRho = np.ones(Nmod)
    iy, jy = np.ix_(testids, nz)  # ids for tested samples with nonzero values
    SMSE[nz] = MSE[nz] / np.var(Y[iy, jy], axis=0)
    Rho[nz], pRho[nz] = compute_pearsonr(Y[iy, jy], Yhat[iy, jy])

    # Set writing options
    if saveoutput:
        print("Writing output ...")
        if fileio.file_type(respfile) == 'cifti' or \
           fileio.file_type(respfile) == 'nifti':
            exfile = respfile
        else:
            exfile = None
        if outputsuffix is not None:
            ext = str(outputsuffix) + fileio.file_extension(respfile)
        else:
            ext = fileio.file_extension(respfile)

        # Write output
        fileio.save(Yhat[testids, :].T,
                    'yhat' + ext,
                    example=exfile,
                    mask=maskvol)
        fileio.save(S2[testids, :].T,
                    'ys2' + ext,
                    example=exfile,
                    mask=maskvol)
        fileio.save(Z[testids, :].T, 'Z' + ext, example=exfile, mask=maskvol)
        fileio.save(Rho, 'Rho' + ext, example=exfile, mask=maskvol)
        fileio.save(pRho, 'pRho' + ext, example=exfile, mask=maskvol)
        fileio.save(RMSE, 'rmse' + ext, example=exfile, mask=maskvol)
        fileio.save(SMSE, 'smse' + ext, example=exfile, mask=maskvol)
        if cvfolds is None:
            fileio.save(Hyp, 'Hyp' + ext, example=exfile, mask=maskvol)
        else:
            for idx in enumerate(splits.split(X)):
                fold = idx[0]
                fileio.save(Hyp[:, :, fold],
                            'Hyp_' + str(fold + 1) + ext,
                            example=exfile,
                            mask=maskvol)
    else:
        output = (Yhat, S2, Z, Rho, pRho, RMSE, SMSE)
        return output
Пример #17
0
if __name__ == '__main__':
  # valid video modules in preferred order
  VALID_V_MODULES = ['gtkgui', 'macgui', 'qtgui', 'dummy']

  # valid audio modules in preferred order
  VALID_A_MODULES = ['alsa', 'macaudio', 'qtaudio', 'dummy']

  config = parse_args(sys.argv[1:])

  if config.help_req:
    Configuration.print_usage()
    sys.exit(0)

  if config.export_fmt is not None:
    if config.export_fmt == 'swf':
      lec = fileio.load(config.file_to_load)
      exporter.to_swf(lec, lec.adats, config.file_to_load[:-4] + '.swf')
    elif config.export_fmt == 'pdf':
      lec = fileio.load(config.file_to_load)
      exporter.to_pdf(lec, config.file_to_load[:-4] + '.swf')
    elif config.export_fmt in ['dcd', 'dcb', 'dcx', 'dar', 'dct']:
      lec = fileio.load(config.file_to_load)
      fileio.save(config.file_to_load[:-3] + config.export_fmt, lec, lec.adats)
    else:
      print 'Unknown flag "--exp-%s"' % config.export_fmt
    sys.exit(0)

  # Something was passed, so use that to 
  if config.audio_module is not None:
    try:
      Audio = __import__(config.audio_module).Audio
Пример #18
0
 def testOcc(self):
     name = "health1"
     checkOcc(load(name))
Пример #19
0
 def testNoWar(self):
     name = "nowar"
     expect = State.WAIT
     handler = AlertHandle(load(name))
     self.assertEqual(handler.state, expect)
Пример #20
0
def list_tasks(args):
    manager = TaskManager(fileio.load())
    print_table(manager)
Пример #21
0
 def testHealthOne(self):
     name = "health1"
     handler = AlertHandle(load(name))
     self.assertEqual(handler.state, State.RUN)
     self.assertEqual(handler.health, Health.NOFULL)