This file is indexed.

/usr/share/pyshared/swignifit/utility.py is in python-pypsignifit 3.0~beta.20120611.1-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#/usr/bin/env python
# encoding: utf-8
# vi: set ft=python sts=4 ts=4 sw=4 et:

######################################################################
#
#   See COPYING file distributed along with the psignifit package for
#   the copyright and license terms
#
######################################################################

"""Variety of utilities for working with swig generated code."""

__docformat__ = "restructuredtext"

import operator as op
import re
import numpy as np
import swignifit_raw as sfr

class PsignifitException(Exception):
    pass

def extract_subclasses(base, sub_func):
    """Recursively extract subclasses, given a swig base class.

    Parameters
    ----------
    base : swig class
        The base class from which to start.
    sub_func : string
        The function or attribute to use as name for subclass.

    Returns
    -------
    subs : dict
        A dictionary mapping subclass names to constructors.

    """
    to_visit = base.__subclasses__()
    subclasses = dict()
    for cl in to_visit:
        descriptor = eval("cl."+sub_func)
        if descriptor not in subclasses.keys():
            subclasses[descriptor] = cl
            to_visit.extend(cl.__subclasses__())
    return subclasses

def extract_subclasses_descriptor(base):
    """Recursively extract subclasses, using the `getDescriptor()` method."""
    return extract_subclasses(base, "getDescriptor()")

def extract_subclasses_reflection(base):
    """Recursively extract subclasses, using the `__name__` attribute."""
    return extract_subclasses(base, "__name__")

sig_dict = extract_subclasses_descriptor(sfr.PsiSigmoid)
core_dict = extract_subclasses_descriptor(sfr.PsiCore)
prior_dict = extract_subclasses_reflection(sfr.PsiPrior)
sampler_dict = extract_subclasses_reflection(sfr.PsiSampler)

def available_cores():
    print "The following cores are availabe:"
    print core_dict.keys()

def available_sigmoids():
    print "The following sigmoids are available:"
    print sig_dict.keys()

def available_priors():
    print "The following priors are available:"
    print prior_dict.keys()

def available_samplers():
    print "The following mcmc samplers are available:"
    print sampler_dict.keys()

def make_dataset(data, nafc):
    """Create a PsiData object from column based input.

    Parameters
    ----------
    data : sequence on length 3 sequences
        Psychometric data in colum based input,
        e.g.[[1, 1, 5], [2, 3, 5] [3, 5, 5]].
    nafc : int
        Number of alternative choices in forced choice procedure.

    Returns
    -------
    data: PsiData
        Dataset object.

    """
    data = np.array(data).T
    x = sfr.vector_double(map(float, data[0]))
    k = sfr.vector_int(map(int, data[1]))
    N = sfr.vector_int(map(int, data[2]))
    return sfr.PsiData(x,N,k,nafc)

def make_pmf(dataset, nafc, sigmoid, core, priors, gammaislambda=False):
    """Assemble PsiPsychometric object from model parameters.

    Parameters
    ----------
    dataset: sequence of length 3 sequences
        Psychometric data in colum based input,
        e.g.[[1, 1, 5], [2, 3, 5] [3, 5, 5]].
    nafc : int
        Number of alternative choices in forced choice procedure.
    sigmoid : string
        Description of model sigmoid.
    core : string
        Description of model core.
    priors : sequence of strings
        The model priors.
    gammaislambda : bool
        Constrain guessing rate and lapsing rate to be equal

    Returns
    -------
    pmf : PsiPsychometric
        Model object.
    nparams : int
        Number of free parameters in model..

    """
    sigmoid = get_sigmoid(sigmoid)
    core = get_core(core, dataset, sigmoid)
    if not priors is None:
        pr = "".join(priors)
    else:
        pr = ""
    if pr in ["Jeffreys","jeffreys","Jeffrey","jeffrey"]:
        pmf = sfr.PMF_with_JeffreysPrior ( nafc, core, sigmoid );
    else:
        pmf = sfr.PsiPsychometric(nafc, core, sigmoid)
    if gammaislambda:
        pmf.setgammatolambda()
    nparams = pmf.getNparams()
    if not pr in ["Jeffreys","jeffreys","Jeffrey","jeffrey"]:
        set_priors(pmf,priors)
    return pmf, nparams

def make_dataset_and_pmf(data, nafc, sigmoid, core, priors, gammaislambda=False ):
    """Assemble PsiData and PsiPsychometric objects simultaneously.

    Parameters
    ----------
    see: make_dataset and make_pmf

    Returns
    -------
    data : PsiData
        Dataset object.
    pmf : PsiPsychometric
        Model object.
    nparams : int
        Number of free parameters.

    """
    dataset = make_dataset(data, nafc)
    pmf, nparams = make_pmf(dataset, nafc, sigmoid, core, priors, gammaislambda)
    return dataset, pmf, nparams

def get_sigmoid(descriptor):
    """Convert string representation of sigmoid to PsiSigmoid object.

    Parameters
    ----------
    descriptor : string
        Description of sigmoid.

    Returns
    -------
    sigmoid : subclass of PsiSigmoid
        An instantiated sigmoid of the requested type.

    Raises
    ------
    ValueError
        If the requested sigmoid is not available.

    See Also
    --------
    `available_sigmoids()`

    """
    if not sig_dict.has_key(descriptor):
        raise ValueError("The sigmoid '%s' you requested, is not available." %
                descriptor)
    return sig_dict[descriptor]()

def get_core(descriptor, data, sigmoid):
    """Convert string representation of core to PsiCore object.

    Parameters
    ----------
    descriptor : string
        Description of core.
    data : PsiData
        Instantiated dataset.
    sigmoid : PsiSigmoid
        Instantiated sigmoid.

    Returns
    -------
    prior : subclass of PsiCore
        An instantiated core of the requested type.

    Raises
    ------
    ValueError
        If the requested core is not available.

    See Also
    --------
    `available_cores()`

    Notes
    -----
    The core objects may require a dataset and a sigmoid type identifier to be
    instantiated. See the Psi++ code for details.

    """
    descriptor, parameter = re.match('([a-z]+)([\d\.]*)', descriptor).groups()
    sigmoid_type = sigmoid.getcode()
    if descriptor not in core_dict.keys():
        raise ValueError("The core '%s' you requested, is not available." %
                descriptor)
    if len(parameter) > 0:
        return core_dict[descriptor](data, sigmoid_type, float(parameter))
    else:
        return core_dict[descriptor](data, sigmoid_type)

def get_prior(prior):
    """Convert string based representation of prior to PsiPrior object.

    Parameters
    ----------
    prior : string
        Description of prior, with paramters.

    Returns
    -------
    prior : PsiPrior
        An instantiated prior of the requested type.

    See Also
    --------
    `available_priors()`

    Notes
    -----
    This function does not raise any error and silently returns `None` if the
    prior does not exist.

    """
    try:
        prior = "sfr."+"Prior(".join(prior.split('('))
        return eval(prior)
    except Exception, e:
        return None

def set_priors(pmf, priors):
    """Set the priors to be used in the model object.

    Parameters
    ----------
    pmf : PsiPsychometric object
        Instantiated model.
    priors : Sequence of strings of length of free parameters of `pmf`
         Model priors.

    Raises
    ------
    ValueError
        If the number of priors is not equal to the number of free parameters in
        the model.

    """
    if priors is not None:
        nparams = pmf.getNparams()
        if len(priors) != nparams:
            raise ValueError("You specified %d priors, " % len(priors) +
                    "but there are %d free parameters in the model." % nparams)
        for (i,p) in enumerate((get_prior(p) for p in priors)):
            if p is not None:
                pmf.setPrior(i, p)

def get_start(start, nparams):
    """Convert sequence of starting values to vector_double type.

    Parameters
    ----------
    start : sequence of numbers
        Starting values.
    nparams : int
        Number of free parameters of the model.

    Returns
    -------
    start: vector_double
        Starting values.

    Raises
    ------
    ValueError
        If the length of the sequence is not equal to the number of free
        parameters.

    """
    if len(start) != nparams:
            raise ValueError("You specified %d starting value(s), " % len(start)
                    +"but there are %d parameters." % nparams)
    else:
        return sfr.vector_double(start)

def get_params(params, nparams):
    """Convert sequence of parameter values to vector_double type.

    Parameters
    ----------
    params : sequence of numbers
        Parameter values.
    nparams : int
        Number of free parameters in the model.

    Returns
    -------
    params : vector_double
        Parameter values.

    Raises
    ------
    ValueError
        If the length of the sequence is not equal to the number of free
        parameters.

    """
    if len(params) != nparams:
                raise ValueError("You specified %d parameters, " % len(params) +
                        "but the model has parameters." % nparams)
    else:
        return sfr.vector_double(params)

def get_cuts(cuts):
    """ Convert `cuts` argument to vector_double type.

    Argument can be None, a number or a sequence of numbers. If None, there is
    only one cut at 0.5. If `cuts` is a number, function returns a vector_double
    with that number as a single element. If its a sequence, that sequence will
    be converted to vector_double type.

    Parameters
    ----------
    cuts : None, number or sequence of numbers
        Cut values

    Returns
    -------
    cuts : vector_double
        Cut values.

    Raises
    ------
    TypeError
        If `cuts` is not None, a number or a sequence of numbers.

    """
    if cuts is None:
        return sfr.vector_double([0.5])
    elif op.isSequenceType(cuts) and np.array([op.isNumberType(a) for a in cuts]).all():
        return sfr.vector_double(cuts)
    elif op.isNumberType(cuts):
        return sfr.vector_double([cuts])
    else:
        raise TypeError("'cuts' must be either None, a number or a "+
                "sequence of numbers.")

def make_pilotsample ( mcsamples ):
    """create an MCList from a set of pilot samples

    Deviances of the pilot list will be meaningless!

    Parameters
    ----------
    mcsamples : array of Nsamples x Nparams
        pilot samples

    Returns
    -------
    pilot : PsiMClist
        MClist with the pilot samples
    """
    N,nprm = mcsamples.shape
    pilot = sfr.PsiMClist ( N, nprm )
    for i in xrange ( N ):
        pilot.setEst ( i, mcsamples[i,:], -1 )
    return pilot