Example #1
0
    def on_join(self, c, e):
        """Hook for someone joining the channel (/join).
        --> e.target() == channel 
        --> e.source() == nick!~user@hostname
        """
        nick, channame, hostname = nm_to_n(
            e.source()), e.target().lower(), nm_to_h(e.source())
        if self.nick != nick:
            chan = self.chans[channame]

            self.users[nick] = user = User(nick, hostname, self.connection) if \
                not nick in self.users else self.users[nick]

            user.channels += [chan]
            chan.users += [user]

            data = Data(line_raw=nick,
                        reaction_type="join",
                        chan=chan,
                        user=user)
            self.dispatch(data)
        else:
            self.chans[channame] = chan = Channel(channame, self.connection)
            data = Data(line_raw=channame,
                        reaction_type="enter_channel",
                        chan=chan)
            self.dispatch(data)
Example #2
0
 def setUp(self):
     self.starting_recipes = get_starting_recipes()
     self.starting_recipes_plus_one = get_starting_recipes_plus_one(
         self.starting_recipes)
     self._filepath = "./tests/test_data/recipes.json"
     create_recipe_file(self._filepath)
     self.r = Recipe(name="peanut_butter_toast",
                     meal="breakfast",
                     servings=1,
                     category="veggie",
                     speed="very_fast",
                     ingredients=[{
                         "item": "bread",
                         "qty": 1,
                         "unit": "slice"
                     }, {
                         "item": "peanut_butter",
                         "qty": 1,
                         "unit": "tablespoon"
                     }, {
                         "item": "banana",
                         "qty": 1,
                         "unit": "amount"
                     }])
     self.d = Data(filepath=self._filepath)
Example #3
0
	def parse(self):
		if self.annot_file.endswith('.gz'):
			fh = gzip.open(self.annot_file, 'rt')
		else:
			fh = open(self.annot_file)

		for line in fh:
			if line[0] == '#': continue
			
			cols = line.strip().split('\t')
			
			record = Data(
				seqid = cols[0],
				feature = cols[2].upper(),
				start = int(cols[3]),
				end = int(cols[4]),
				attrs = Data()
			)
			
			for item in cols[-1].split(';'):
				if not item:
					continue
				
				#if _format == 'GFF':
				#	name, value = item.split('=')
				#else:
				#	name, value = item.strip().strip('"').split('"')

				name, value = self.split_val(item)
				
				record.attrs[name.strip().upper()] = value
			
			yield record

		fh.close()
Example #4
0
def analyse_patch_request_errors(request_validator: UserRequest,
                                 data: Data,
                                 filename: str,
                                 method_parameters: dict) \
        -> Union[tuple, None]:
    try:
        request_validator.existent_filename_validator(filename)
    except Exception as nonexistent_train_filename:
        return (
            jsonify(
                {Constants.MESSAGE_RESULT: str(nonexistent_train_filename)}),
            Constants.HTTP_STATUS_CODE_NOT_FOUND,
        )

    module_path, class_name = data.get_module_and_class_from_a_executor_name(
        filename)

    class_method = data.get_class_method_from_a_executor_name(filename)
    try:
        request_validator.valid_method_parameters_validator(
            module_path, class_name, class_method, method_parameters)
    except Exception as invalid_function_parameters:
        return (
            jsonify(
                {Constants.MESSAGE_RESULT: str(invalid_function_parameters)}),
            Constants.HTTP_STATUS_CODE_NOT_ACCEPTABLE,
        )

    return None
Example #5
0
def main():
    if opt.truncate:
        train_data = pickle.load(
            open('../datasets/' + opt.dataset + '/train_shortonly.txt', 'rb'))
    else:
        train_data = pickle.load(
            open('../datasets/' + opt.dataset + '/train.txt', 'rb'))
    if opt.validation:
        train_data, valid_data = split_validation(train_data,
                                                  opt.valid_portion)
        test_data = valid_data
    else:
        if opt.truncate:
            test_data = pickle.load(
                open('../datasets/' + opt.dataset + '/test_shortonly.txt',
                     'rb'))
        else:
            test_data = pickle.load(
                open('../datasets/' + opt.dataset + '/test.txt', 'rb'))
    # all_train_seq = pickle.load(open('../datasets/' + opt.dataset + '/all_train_seq.txt', 'rb'))
    # g = build_graph(all_train_seq)
    train_data = Data(opt, train_data, shuffle=True)
    test_data = Data(opt, test_data, shuffle=False)
    # del all_train_seq, g
    if opt.dataset == 'diginetica':
        n_node = 43098
    elif opt.dataset == 'yoochoose1_64' or opt.dataset == 'yoochoose1_4':
        n_node = 37484
    else:
        n_node = 310

    model = trans_to_cuda(SessionGraph(opt, n_node))
    model = torch.nn.DataParallel(model, device_ids=[0, 1])

    start = time.time()
    best_result = [0, 0]
    best_epoch = [0, 0]
    bad_counter = 0
    for epoch in range(opt.epoch):
        print('-------------------------------------------------------')
        print('epoch: ', epoch)
        hit, mrr = train_test(model, train_data, test_data)
        flag = 0
        if (hit - best_result[0]) > 0.0001:
            best_result[0] = hit
            best_epoch[0] = epoch
            flag = 1
        if (mrr - best_result[1]) > 0.0001:
            best_result[1] = mrr
            best_epoch[1] = epoch
            flag = 1
        print('Best Result:')
        print('\tRecall@20:\t%.4f\tMMR@20:\t%.4f\tEpoch:\t%d,\t%d' %
              (best_result[0], best_result[1], best_epoch[0], best_epoch[1]))
        bad_counter += 1 - flag
        if bad_counter >= opt.patience:
            break
    print('-------------------------------------------------------')
    end = time.time()
    print("Run time: %f s" % (end - start))
Example #6
0
def test_model():
    input_shape = (256, 256, 3)
    local_shape = (128, 128, 3)
    batch_size = 4

    test_datagen = Data(test_data, input_shape[:2], local_shape[:2])
    completion_model = load_model("model/completion.h5", compile=False)
    #completion_model.summary()

    cnt = 0
    for inputs, points, masks in test_datagen.flow(batch_size):
        completion_image = completion_model.predict([inputs, masks])
        num_img = min(5, batch_size)
        fig, axs = plt.subplots(num_img, 3)

        for i in range(num_img):
            axs[i, 0].imshow(inputs[i] * (1 - masks[i]))
            axs[i, 0].axis('off')
            axs[i, 0].set_title('Input')
            axs[i, 1].imshow(completion_image[i])
            axs[i, 1].axis('off')
            axs[i, 1].set_title('Output')
            axs[i, 2].imshow(inputs[i])
            axs[i, 2].axis('off')
            axs[i, 2].set_title('Ground Truth')
        fig.savefig(os.path.join("result", "result_test_%d.png" % cnt))
        plt.close()
        cnt += 1
Example #7
0
def main():

    # generate some dummy data for testing
    manager = Data()
    num_examples = 10**4
    max_val = 1
    train_batch_size = 16
    train_size = int(num_examples/2)
    eval_size = int(num_examples/2)
    X, y = manager.create_data_set(num_of_examples=num_examples, max_val=max_val,
                                   discriminator=lambda x: max_val*(1/(1+np.exp(-x)) + 1/(1+np.exp(x**2)))-max_val/2,
                                   one_hot=False, plot_data=False, load_saved_data=True,
                                   filename='dataset.npy')

    train_examples, train_labels = X[0:train_size, :], y[0:train_size]
    eval_examples, eval_labels = X[train_size:, :], y[train_size:]
    print('train examples = {}, train labels = {}, eval examples = {}, eval labels = {}'.format(train_examples.shape,
                                                                                                train_labels.shape,
                                                                                                eval_examples.shape,
                                                                                                eval_labels.shape))

    # get some small train batch
    indices = np.random.randint(low=0,high=train_size,size=train_batch_size)
    train_batch_examples, train_batch_labels = X[indices,:], y[indices]


    # start by defining your default graph
    graph = GRAPH()
    graph.getDefaultGraph()


    # declare your placeholders, to provide your inputs
    # print(int(train_batch_examples.shape[1]))
    input_features = placeholder(shape=(train_batch_size, int(train_batch_examples.shape[1])))
    input_labels = placeholder(shape=(train_batch_size))


    """
        Method #3
    """

    # this is defined using layers
    features = fully_connected(features=input_features, units=32)
    features = relu(features)
    features = fully_connected(features=features, units=64)
    features = relu(features)
    features = fully_connected(features=features, units=128)
    features = relu(features)
    features = fully_connected(features=features, units=64)
    features = relu(features)
    features = fully_connected(features=features, units=32)
    features = relu(features)
    features = fully_connected(features=features, units=2)
    logits = softmax_classifier(features)
    loss = CrossEntropyLoss(softmax_logits=logits, labels=input_labels)

    # compile and run
    graph.graph_compile(function=loss, verbose=True)
    loss = graph.run(input_matrices={input_features: train_batch_examples, input_labels: train_batch_labels})
    print(loss, logits.output.shape)
 def setUp(self):
     random.seed(1)
     self.rules = {
         "monday": {
             "breakfast_categories": ["veggie", "vegan", "fish"],
             "brunch_categories": [],
             "lunch_categories": ["veggie", "vegan", "fish"],
             "dinner_categories": ["veggie", "vegan", "fish"],
             "breakfast_speed": ["very_fast"],
             "brunch_speed": [],
             "lunch_speed": ["very_fast"],
             "dinner_speed": ["very_fast", "fast", "medium"]
         },
         "sunday": {
             "breakfast_categories": ["veggie", "vegan", "fish", "meat"],
             "brunch_categories": [],
             "lunch_categories": ["veggie", "vegan", "fish", "meat"],
             "dinner_categories": ["meat"],
             "breakfast_speed": ["very_fast"],
             "brunch_speed": [],
             "lunch_speed": ["very_fast"],
             "dinner_speed":
             ["very_fast", "fast", "medium", "slow", "very_slow"]
         }
     }
     self._filepath = "./tests/test_data/recipes.json"
     create_recipe_file(self._filepath)
     self.d = Data(self._filepath)
     self.p = Plan(self.d, self.rules, "2021-04-17", people=2)
     self.maxDiff = None
Example #9
0
def transmission_pixel_sinogram(rays, image, angles, pixel_i, pixel_j, rays_downsample=5, response_image=None):
    if response_image is None:
        response_image = Data(image.extent, np.zeros(image.data.shape, dtype=np.double))
        response_image.data[pixel_i, pixel_j] = 1

    response = rotation_sinogram(rays, response_image, angles)
    response = response.reshape(response.shape[0], -1, rays_downsample).mean(axis=2)

    return response
Example #10
0
def main():
    os.environ["CUDA_VISIBLE_DEVICES"] = "0"

    train_data = pickle.load(open('./dataset/train.txt', 'rb'))
    test_data = pickle.load(open('./dataset/test.txt', 'rb'))

    fp = open("./dataset/statistic.txt", "rb+")
    (all_user_count, all_item_count, all_cate_count) = pickle.load(fp)

    print("训练集sess数量:" + str(len(train_data[0])))
    print("测试集sess数量:" + str(len(test_data[0])))

    # ===========attention==============
    # train_data一开始只是一堆session + target,在Data这个类里才开始构建图之类的
    train_data = Data(train_data, shuffle=True)
    # print(train_data)
    test_data = Data(test_data, shuffle=False)
    # del all_train_seq, g

    n_node = all_item_count + 1

    model = trans_to_cuda(SessionGraph(opt, n_node))

    start = time.time()
    best_result = [0, 0]
    best_epoch = [0, 0]
    bad_counter = 0
    for epoch in range(opt.epoch):
        print('-------------------------------------------------------')
        print('epoch: ', epoch)

        # print("===param===")
        # for param in model.named_parameters():
        #     print(param)

        hit, mrr = train_test(model, train_data, test_data)
        flag = 0
        if hit >= best_result[0]:
            best_result[0] = hit
            best_epoch[0] = epoch
            flag = 1
        if mrr >= best_result[1]:
            best_result[1] = mrr
            best_epoch[1] = epoch
            flag = 1
        print('Best Result:')
        print('\tRecall@20:\t%.4f\tMMR@20:\t%.4f\tEpoch:\t%d,\t%d' %
              (best_result[0], best_result[1], best_epoch[0], best_epoch[1]))
        bad_counter += 1 - flag
        if bad_counter >= opt.patience:
            break
    print('-------------------------------------------------------')
    end = time.time()
    print("Run time: %f s" % (end - start))
Example #11
0
def main():
    train_data = pickle.load(
        open('../datasets/' + opt.dataset + '/train.txt', 'rb'))
    if opt.validation:
        train_data, valid_data = split_validation(train_data,
                                                  opt.valid_portion)
        test_data = valid_data
    else:
        test_data = pickle.load(
            open('../datasets/' + opt.dataset + '/test.txt', 'rb'))
    # all_train_seq = pickle.load(open('../datasets/' + opt.dataset + '/all_train_seq.txt', 'rb'))
    # g = build_graph(all_train_seq)
    train_data = Data(train_data, shuffle=True, opt=opt)
    test_data = Data(test_data, shuffle=False, opt=opt)
    # del all_train_seq, g
    if opt.dataset == 'diginetica':
        n_node = 43098
    elif opt.dataset == 'yoochoose1_64' or opt.dataset == 'yoochoose1_4':
        n_node = 37484
    elif opt.dataset == 'diginetica_users':
        n_node = 57070
    else:
        n_node = 310

    model = trans_to_cuda(
        SessionGraph(opt, n_node, max(train_data.len_max, test_data.len_max)))

    start = time.time()
    best_result = [0, 0]
    best_epoch = [0, 0]
    bad_counter = 0
    for epoch in range(opt.epoch):
        print('-------------------------------------------------------')
        print('epoch: ', epoch)
        hit, mrr = train_test(model, train_data, test_data)
        flag = 0
        if hit >= best_result[0]:
            best_result[0] = hit
            best_epoch[0] = epoch
            flag = 1
        if mrr >= best_result[1]:
            best_result[1] = mrr
            best_epoch[1] = epoch
            flag = 1
        print('Best Result:')
        print('\tRecall@20:\t%.4f\tMMR@20:\t%.4f\tEpoch:\t%d,\t%d' %
              (best_result[0], best_result[1], best_epoch[0], best_epoch[1]))
        bad_counter += 1 - flag
        if bad_counter >= opt.patience:
            break
    print('-------------------------------------------------------')
    end = time.time()
    print("Run time: %f s" % (end - start))
Example #12
0
def train_model(result_dir="result", data=data_train):

    train_datagen = Data(data_train, input_shape[:2], local_shape[:2])

    for n in range(n_epoch):
        progbar = generic_utils.Progbar(len(train_datagen))
        for inputs, points, masks in train_datagen.flow(batch_size):
            completion_image = completion_model.predict([inputs, masks])
            valid = np.ones((batch_size, 1))
            fake = np.zeros((batch_size, 1))

            g_loss = 0.0
            d_loss = 0.0
            if n < tc:
                g_loss = completion_model.train_on_batch([inputs, masks],
                                                         inputs)
            else:
                d_loss_real = d_model.train_on_batch([inputs, points], valid)
                d_loss_fake = d_model.train_on_batch(
                    [completion_image, points], fake)
                d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)
                if n >= tc + td:
                    g_loss = all_model.train_on_batch([inputs, masks, points],
                                                      [inputs, valid])
                    g_loss = g_loss[0] + alpha * g_loss[1]
            progbar.add(inputs.shape[0],
                        values=[("D loss", d_loss), ("G mse", g_loss)])

        num_img = min(5, batch_size)
        fig, axs = plt.subplots(num_img, 3)
        for i in range(num_img):
            axs[i, 0].imshow(inputs[i] * (1 - masks[i]))
            axs[i, 0].axis('off')
            axs[i, 0].set_title('Input')
            axs[i, 1].imshow(completion_image[i])
            axs[i, 1].axis('off')
            axs[i, 1].set_title('Output')
            axs[i, 2].imshow(inputs[i])
            axs[i, 2].axis('off')
            axs[i, 2].set_title('Ground Truth')
        fig.savefig(os.path.join(result_dir, "result_%d.png" % n))
        plt.close()

        # Save the checkpoints every 10 epochs
        if (n + 1) % 10 == 0:
            checkpoint.save(file_prefix=checkpoint_prefix)

    # save model
    generator.save(os.path.join("model", "generator.h5"))
    completion_model.save(os.path.join("model", "completion.h5"))
    discriminator.save(os.path.join("model", "discriminator.h5"))
Example #13
0
class TestData(TestCase):
    def setUp(self):
        self.starting_recipes = get_starting_recipes()
        self.starting_recipes_plus_one = get_starting_recipes_plus_one(
            self.starting_recipes)
        self._filepath = "./tests/test_data/recipes.json"
        create_recipe_file(self._filepath)
        self.r = Recipe(name="peanut_butter_toast",
                        meal="breakfast",
                        servings=1,
                        category="veggie",
                        speed="very_fast",
                        ingredients=[{
                            "item": "bread",
                            "qty": 1,
                            "unit": "slice"
                        }, {
                            "item": "peanut_butter",
                            "qty": 1,
                            "unit": "tablespoon"
                        }, {
                            "item": "banana",
                            "qty": 1,
                            "unit": "amount"
                        }])
        self.d = Data(filepath=self._filepath)

    def tearDown(self):
        remove_recipe_file(self._filepath)

    def test_init(self):
        self.assertEqual("./tests/test_data/recipes.json", self.d.filepath)
        self.assertEqual(self.starting_recipes, self.d._data)

    def test_load(self):
        self.d._data = {}
        self.assertEqual({}, self.d._data)
        self.assertEqual(self.starting_recipes, self.d._load())
        self.assertEqual(self.starting_recipes, self.d._data)

    def test_add_recipe(self):
        self.assertEqual(self.starting_recipes, self.d._data)
        self.d.add_recipe(self.r)
        self.assertEqual(self.starting_recipes_plus_one, self.d._data)

    def test_get_recipes(self):
        self.d._data = {}
        self.assertEqual({}, self.d.get_recipes())
        self.d._data = self.starting_recipes
        self.assertEqual(self.starting_recipes, self.d.get_recipes())
Example #14
0
def main():
    #Read and sort the data
    train_data = os.path.join(opt.data_folder, opt.train_data)
    x_train = pd.read_csv(train_data)
    x_train.sort_values(opt.sessionid, inplace=True)
    x_train = x_train.iloc[-int(len(x_train) / 4):]
    valid_data = os.path.join(opt.data_folder, opt.valid_data)
    x_valid = pd.read_csv(valid_data)
    x_valid.sort_values(opt.sessionid, inplace=True)
    #Extract Item Features column names (if exists)
    feats_columns = []
    ffeats = opt.item_feats.strip().split("#")
    if ffeats[0] != '':
        feats_columns.extend(ffeats)
        
    train_data, test_data, n_node, item_features = preprocess(x_train, x_valid, feats_columns, opt)
    del x_train
    del x_valid
    train_data = Data(train_data, shuffle=True)
    test_data = Data(test_data, shuffle=False)
    print('Number of Nodes:', n_node)
    model = trans_to_cuda(SessionGraph(opt, n_node, item_features))
    start = time.time()
    best_result = [0, 0]
    #bad_counter = 0
    f=open("outputlogs.txt","a+")
    print('START TRAINING........')
    for epoch in range(opt.epoch):
        print('-------------------------------------------------------')
        print('Epoch: ', epoch)
        hit, mrr = train_test(model, train_data, test_data, opt.K)
        #flag = 0
        if hit >= best_result[0] or mrr >= best_result[1]:
            #flag = 1
            best_result[0] = hit
            best_result[1] = mrr
            
        print('Epoch%d: \tRecall@%d:\t%.4f\tMMR@%d:\t%.4f'% (epoch, opt.K, hit, opt.K, mrr))
        f.write('Epoch%d: \tRecall@%d:\t%.4f\tMMR@%d:\t%.4f'% (epoch, opt.K, hit, opt.K, mrr))
        
        #bad_counter += 1 - flag
        #if bad_counter >= opt.patience:
        #    break
    print('-------------------------------------------------------')
    end = time.time()
    print("Run time: %f s" % (end - start))
    f.write("\nRun time: %f s" % (end - start))
    f.write('\n-----------------------------------------------------------')
    f.close()
Example #15
0
def transmission_response_sinogram(rays, image, angles, rays_downsample=5):
    nx, ny = image.data.shape
    response = np.zeros((nx * ny, angles.shape[0], int(rays.shape[0] / rays_downsample)), dtype=np.double)

    response_image = Data(image.extent, np.zeros(image.data.shape, dtype=np.double))

    for i in range(nx * ny):
        ix = i % image.data.shape[0]
        iy = i // image.data.shape[0]
        print(i, nx * ny, ix, iy)
        response_image.data[ix, iy] = 1
        response[i] = transmission_pixel_sinogram(rays, image, angles, ix, iy, rays_downsample, response_image)
        response_image.data[ix, iy] = 0

    return response
Example #16
0
def update_execution(filename: str) -> jsonify:
    service_type = request.args.get(Constants.TYPE_PARAM_NAME)
    description = request.json[Constants.DESCRIPTION_FIELD_NAME]
    class_method_name = request.json[Constants.METHOD_FIELD_NAME]
    method_parameters = request.json[Constants.METHOD_PARAMETERS_FIELD_NAME]

    storage = None
    if service_type == Constants.EXPLORE_TENSORFLOW_TYPE or \
            service_type == Constants.EXPLORE_SCIKITLEARN_TYPE:
        storage = explore_storage
    elif service_type == Constants.TRANSFORM_TENSORFLOW_TYPE or \
            Constants.TRANSFORM_SCIKITLEARN_TYPE:
        storage = transform_storage

    data = Data(database, storage)

    request_errors = analyse_patch_request_errors(request_validator, data,
                                                  filename, class_method_name,
                                                  method_parameters)

    if request_errors is not None:
        return request_errors

    module_path, class_name = data.get_module_and_class(filename)
    class_parameters = data.get_class_parameters(filename)

    parameters = Parameters(database, data)
    metadata_creator = Metadata(database)

    execution = Execution(database, filename, service_type, storage,
                          metadata_creator, module_path, class_name,
                          class_parameters, parameters)

    execution.update(class_method_name, method_parameters, description)

    response_params = None
    if service_type == Constants.TRANSFORM_TENSORFLOW_TYPE or \
            service_type == Constants.TRANSFORM_SCIKITLEARN_TYPE:
        response_params = Constants.MICROSERVICE_URI_GET_PARAMS

    return (
        jsonify({
            Constants.MESSAGE_RESULT:
            f'{Constants.MICROSERVICE_URI_SWITCHER[service_type]}'
            f'{filename}{response_params}'
        }),
        Constants.HTTP_STATUS_CODE_SUCCESS_CREATED,
    )
Example #17
0
    def setFileOff(self, file_off):
        """
        Straight from the Android's source code, mapping one to one
        the java code to the python one.
        -----------------------------------------------------------


        if (fileOffset < 0) {
            throw new IllegalArgumentException("fileOffset < 0");
        }
        if (this.fileOffset >= 0) {
            throw new RuntimeException("fileOffset already set");
        }
        int mask = alignment - 1;
        fileOffset = (fileOffset + mask) & ~mask;
        this.fileOffset = fileOffset;

        mask = self.alignment - 1
        file_off = ( file_off + mask ) & ~mask
        """
        res = Data.toAligned(self.alignment, file_off)

        self.file_off = res

        return res
Example #18
0
    def __init__(self, parent, size, plot, msg):
        self.parent = parent
        self.plot2d = plot
        self.msgwin = msg
        self.data = Data()
                
        x = Image.open(abs_path(photograph))	# Images are kept along with the python code
        im = x.resize(size)        

        self.size[0] = float(im.size[0])
        self.size[1] = float(im.size[1])
        self.image = ImageTk.PhotoImage(im)
        self.canvas = Canvas(parent, width = im.size[0], height = im.size[1])
        self.canvas.create_image(0,0,image = self.image, anchor = NW)

	self.adblock = self.canvas.create_rectangle(self.dotxy(self.adctl),fill='white')
        
        for k in self.douts:
            k[2] = self.canvas.create_oval(self.dotxy(k), outline="", fill = 'black')
        for k in self.dins:
            k[2] = self.canvas.create_oval(self.dotxy(k), outline="", fill = 'black')
        for k in self.adcs:
            k[2] = self.canvas.create_oval(self.dotxy(k), outline="", fill = 'black')
        for k in self.ls_ins:
            k[2] = self.canvas.create_oval(self.dotxy(k), outline="", fill = 'black')
        self.led[2] = self.canvas.create_oval(self.dotxy(self.led), outline="", fill = 'black')
        self.pwg[2] = self.canvas.create_oval(self.dotxy(self.pwg), outline="", fill = 'black')
        self.dac[2] = self.canvas.create_oval(self.dotxy(self.dac), outline="", fill = 'black')
        self.cntr[2] = self.canvas.create_oval(self.dotxy(self.cntr), outline="", fill = 'black')
        self.cmp[2] = self.canvas.create_oval(self.dotxy(self.cmp), outline="", fill = 'black')
        self.canvas.bind("<ButtonRelease-1>", self.mouse_click)
        self.canvas.pack()    
Example #19
0
    def lbp():
        # Import dataset
        X, Y, lb = Data.numbers()

        X_new = []
        for img in X:

            # Convert array in image
            image = img.reshape([35, 35])
            image = image.astype(np.uint8)

            # Number of points to be considered as neighbourers
            radius = 3
            no_points = 8 * radius
            # Uniform LBP is used
            lbp = local_binary_pattern(image,
                                       no_points,
                                       radius,
                                       method='uniform')

            lbp = np.array(lbp).flatten()
            #print(lbp)

            X_new.append(lbp)

        return np.array(X_new), Y, lb
Example #20
0
    def glcm():
        # Import dataset
        X, Y, lb = Data.numbers()

        X_new = []
        for img in X:

            # Convert array in image
            image = img.reshape([35, 35])
            image = image.astype(np.uint8)

            g = greycomatrix(image, [0, 1], [0, np.pi / 2], levels=256)

            contrast = greycoprops(g, 'contrast').flatten()
            energy = greycoprops(g, 'energy').flatten()
            homogeneity = greycoprops(g, 'homogeneity').flatten()
            correlation = greycoprops(g, 'correlation').flatten()
            dissimilarity = greycoprops(g, 'dissimilarity').flatten()
            ASM = greycoprops(g, 'ASM').flatten()

            features = np.concatenate((contrast, energy, homogeneity,
                                       correlation, dissimilarity, ASM))

            X_new.append(features)

        return X_new, Y, lb
Example #21
0
def show_maps():
    layer = model.h_conv
    data = Data(1)
    # print data._y.shape
    # print data._X.shape
    # layer = model.h_conv5
    batch_size = 40  # image that will be used, taken from test_data/X.npy
    batch = data.next_batch(batch_size)  # jump batch_size images
    batch = data.next_batch(batch_size)  # use the first image of this batch
    feature_maps = sess.run(layer,
                            feed_dict={
                                model.x: batch[0],
                                model.y_: batch[1],
                                model.keep_prob: 1.0
                            })
    plotFeatureMaps(feature_maps)
def testSingleImg():
    NetHelper.gpu()
    #submission()
    nh=NetHelper(deploy=cfgs.deploy_pt,model=cfgs.best_model_dir)
    img=Data.imFromFile(os.path.join(cfgs.train_mask_path,"1_1_mask.tif"))
    res=nh.bin_pred_map(img)
    print(np.histogram(res))
Example #23
0
    def huMoments():
        # Import dataset
        X, Y, lb = Data.numbers()

        X_new = []
        for img in X:

            # Convert array in image
            image = img.reshape([35, 35])
            image = image.astype(np.uint8)

            # Calculate Moments
            moments = cv2.moments(image)

            # Calculate Hu Moments
            huMoments = cv2.HuMoments(moments)

            # Log scale hu moments
            hm = [
                -1 * copysign(1.0, hu) * log10(abs(hu)) if hu != 0 else 0
                for hu in huMoments
            ]

            X_new.append(hm)

        return np.array(X_new), Y, lb
def testSingleImg():
    NetHelper.gpu()
    #submission()
    nh = NetHelper(deploy=cfgs.deploy_pt, model=cfgs.best_model_dir)
    img = Data.imFromFile(os.path.join(cfgs.train_mask_path, "1_1_mask.tif"))
    res = nh.bin_pred_map(img)
    print(np.histogram(res))
Example #25
0
    def make_memory_dataset(self, aug_crop=False):

        dataset = tf.data.Dataset.from_tensor_slices(Data(self.images_PH, self.labels_PH, self.nus_PH, self.singles_PH))
        c_map_func = None
        if aug_crop:
            c_map_func = self.augment

        self.dataset = self.shuf_rep_map_batch_fetch(dataset, self.shuffle_buff, self.epochs, self.batch_sz, c_map_func)
Example #26
0
    def extractParam(self):
        """Turn muti part encoded form into params."""
        params = []
        try:
            environ = {
                'CONTENT_TYPE': self.headers['content-type'],
                'CONTENT_LENGTH': self.headers['content-length'],
                'REQUEST_METHOD': 'POST',
                }
        except KeyError:
            trace('# Warning: missing header content-type or content-length'
                  ' in file: %s not an http request ?\n' % self.file_path)
            return params

        form = FieldStorage(fp=StringIO(self.body),
                            environ=environ,
                            keep_blank_values=True)
        try:
            keys = form.keys()
        except TypeError:
            trace('# Using custom data for request: %s ' % self.file_path)
            params = Data(self.headers['content-type'], self.body)
            return params

        for item in form.list:
            
            key = item.name
            value = item.value
            filename = item.filename
            
            if filename is None:
                params.append([key, value])
            else:
                # got a file upload
                filename = filename or ''
                params.append([key, 'Upload("%s")' % filename])
                if filename:
                    if os.path.exists(filename):
                        trace('# Warning: uploaded file: %s already'
                              ' exists, keep it.\n' % filename)
                    else:
                        trace('# Saving uploaded file: %s\n' % filename)
                        f = open(filename, 'w')
                        f.write(str(value))
                        f.close()
        return params
Example #27
0
def readDataSetFile(filename, F):
	with open(filename) as f:
		E = DataSet()
		for line in f:
			line =line.split()
			if len(line) != len(F):
				print("Number of attributes in the Training record:"+str(line)+"does not match the number in the attribute file")
				raise Exception('DataValidation')
			d  = Data()
			idx= 0
			for value in ine:
				if value in F[idx].values:
					d.addValue(F[idx].name,value)
				else:
					print('Value='+value+' is not a valid value for attribute='+ F[idx].name)
				idx += 1
			E.addData(d)
		f.close()
def func(filename, nh):
    _, idx, ext = Data.splitPath(filename)
    if ext != ".tif":
        return None
    cfgs.cnt += 1
    print(cfgs.cnt)
    #idx=int(idx)
    img = Data.imFromFile(filename)
    ready = prep(img, cfgs.inShape[1], cfgs.inShape[0])
    # print(np.histogram(ready))
    # ready*=0.00392156862745
    ready -= 128
    ready *= 0.0078431372549
    pred_bin, pred, img = classifier(ready, nh)
    # pred_bin,pred,output, img=classifier(ready,nh)

    result = run_length_enc(pred_bin)
    if debug:
        # print('org',np.histogram(ready))
        # print('data', np.histogram(img))
        hist = np.histogram(img)
        print(pd.DataFrame(hist[0], index=hist[1][1:]).T)
        hist = np.histogram(pred)
        print(pd.DataFrame(hist[0], index=hist[1][1:]).T)

        mask = plt.imread(os.path.join(cfgs.train_mask_path,
                                       idx + "_mask.tif"))
        plt.figure(1)
        plt.subplot(221)
        plt.title('mask')
        plt.imshow(mask)
        plt.subplot(222)
        plt.title('prediction')
        plt.imshow(pred_bin)
        plt.subplot(223)
        plt.title('img')
        plt.imshow(img)
        plt.subplot(224)
        plt.title('heatmap ')
        plt.imshow(pred)
        plt.show()
        # print(idx,result)

    return (idx, result)
Example #29
0
    def _initInternalDict(self):

        self.dict = CachedItemDictionary()

        strings_ids = self.strings_ids.getRawData()
        instance_type = Data.getInstance(strings_ids)

        self.dict.createTypeDictionary(instance_type)
        self.dict.fillPairsDict(instance_type, strings_ids,
                                lambda x: IndexedItemsValues(x.get_off()))

        type_ids = self.type_ids.getRawData()
        instance_type = Data.getInstance(type_ids)

        self.dict.createTypeDictionary(instance_type)
        self.dict.fillPairsDict(
            instance_type, type_ids,
            lambda x: IndexedItemsValues(x.get_descriptor_idx()))
        """
def main():
    data = Data('hyperspectral_data//san.mat')  # load data
    ecem = ECEM()
    ecem.parmset(**{'windowsize': [1/4, 2/4, 3/4, 4/4],   # window size
                    'num_layer': 10,  # the number of detection layers
                    'num_cem': 6,  # the number of CEMs per layer
                    'Lambda': 1e-6,  # the regularization coefficient
                    'show_proc': True})  # show the process or not
    result = ecem.detect(data, pool_num=4)  # detection (we recomemend to use multi-thread processing to speed up detetion)
    ecem.show([result], ['E-CEM'])  # show
Example #31
0
def main():
    train_data = pickle.load(open('./datasets/' + opt.dataset + '/train.txt', 'rb'))
    if opt.validation:
        train_data, valid_data = split_validation(train_data, opt.valid_portion)
        test_data = valid_data
    else:
        test_data = pickle.load(open('./datasets/' + opt.dataset + '/test.txt', 'rb'))

    train_data = Data(train_data, shuffle=True)
    test_data = Data(test_data, shuffle=False)
    if opt.dataset == 'diginetica':
        n_node = 43098
    else:
        n_node = 37484


    model = trans_to_cuda(SelfAttentionNetwork(opt, n_node))

    start = time.time()
    best_result = [0, 0]
    best_epoch = [0, 0]
    bad_counter = 0
    for epoch in range(opt.epoch):
        print('-------------------------------------------------------')
        print('epoch: ', epoch)
        hit, mrr = train_test(model, train_data, test_data)
        flag = 0
        if hit >= best_result[0]:
            best_result[0] = hit
            best_epoch[0] = epoch
            flag = 1
        if mrr >= best_result[1]:
            best_result[1] = mrr
            best_epoch[1] = epoch
            flag = 1
        print('Best Result:')
        print('\tRecall@20:\t%.4f\tMMR@20:\t%.4f\tEpoch:\t%d,\t%d'% (best_result[0], best_result[1], best_epoch[0], best_epoch[1]))
        bad_counter += 1 - flag
        if bad_counter >= opt.patience:
            break
    print('-------------------------------------------------------')
    end = time.time()
    print("Run time: %f s" % (end - start))
def func(filename, nh):
    _,idx,ext=Data.splitPath(filename)
    if ext!=".tif": 
        return None
    cfgs.cnt+=1
    print(cfgs.cnt)
    #idx=int(idx)
    img=Data.imFromFile(filename)
    ready=prep(img,cfgs.inShape[1],cfgs.inShape[0]) 
    # print(np.histogram(ready))
    # ready*=0.00392156862745
    ready-=128
    ready*=0.0078431372549
    pred_bin,pred, img=classifier(ready,nh)
    # pred_bin,pred,output, img=classifier(ready,nh)

    result=run_length_enc(pred_bin)
    if debug:
        # print('org',np.histogram(ready))
        # print('data', np.histogram(img))
        hist=np.histogram(img)
        print(pd.DataFrame(hist[0],index=hist[1][1:]).T)
        hist=np.histogram(pred)
        print(pd.DataFrame(hist[0],index=hist[1][1:]).T)

        mask=plt.imread(os.path.join(cfgs.train_mask_path,idx+"_mask.tif"))
        plt.figure(1)
        plt.subplot(221)
        plt.title('mask')
        plt.imshow(mask)
        plt.subplot(222)
        plt.title('prediction')
        plt.imshow(pred_bin)
        plt.subplot(223)
        plt.title('img')
        plt.imshow(img)
        plt.subplot(224)
        plt.title('heatmap ')
        plt.imshow(pred)
        plt.show()
        # print(idx,result)

    return (idx,result)
Example #33
0
def main():
    doc_content_list, doc_train_list, doc_test_list, vocab_dic, labels_dic, max_num_sentence, keywords_dic, class_weights = read_file(
        args.dataset, args.use_LDA)

    pre_trained_weight = []
    if args.dataset == 'mr':
        gloveFile = 'data/glove.6B.300d.txt'
        if not os.path.exists(gloveFile):
            print(
                'Please download the pretained Glove Embedding from https://nlp.stanford.edu/projects/glove/'
            )
            return
        pre_trained_weight = loadGloveModel(gloveFile, vocab_dic,
                                            len(vocab_dic) + 1)

    train_data, valid_data = split_validation(doc_train_list,
                                              args.valid_portion, SEED)
    test_data = split_validation(doc_test_list, 0.0, SEED)

    num_categories = len(labels_dic)

    train_data = Data(train_data, max_num_sentence, keywords_dic,
                      num_categories, args.use_LDA)
    valid_data = Data(valid_data, max_num_sentence, keywords_dic,
                      num_categories, args.use_LDA)
    test_data = Data(test_data, max_num_sentence, keywords_dic, num_categories,
                     args.use_LDA)

    model = trans_to_cuda(
        DocumentGraph(args, pre_trained_weight, class_weights,
                      len(vocab_dic) + 1, len(labels_dic)))

    for epoch in range(args.epoch):
        print('-------------------------------------------------------')
        print('epoch: ', epoch)

        train_model(model, train_data, args)

        valid_detail, valid_acc = test_model(model, valid_data, args, False)
        detail, acc = test_model(model, test_data, args, False)
        print('Validation Accuracy:\t%.4f, Test Accuracy:\t%.4f' %
              (valid_acc, acc))
Example #34
0
    def standardize_data(self, datatup):
        images = datatup.images
        labels = datatup.labels
        nus = datatup.nus
        singles = datatup.singles

        print('Inception preprocessing')
        images = (images - tf.reduce_min(images)) * (1.0 - 0.0) / (tf.reduce_max(images) - tf.reduce_min(images)) + 0.0
        images = (images - 0.5) * 2 # get values [-1, 1].  Shift and scale. For Resnet V2 and all tf models.

        return Data(images, labels, nus, singles)
def submission():

    NetHelper.gpu(2)
    #submission()
    nh = NetHelper(deploy=cfgs.deploy_pt, model=cfgs.best_model_dir)
    if debug:
        l = Data.folder_opt(cfgs.train_data_path, func, nh)
    else:
        l = Data.folder_opt(cfgs.test_data_path, func, nh)
    l = np.array(l, dtype=[('x', int), ('y', object)])
    l.sort(order='x')

    first_row = 'img,pixels'
    file_name = 'submission.csv'

    with open(file_name, 'w+') as f:
        f.write(first_row)
        for i in l:
            s = str(i[0]) + ',' + i[1]
            f.write(('\n' + s))
def submission():

    NetHelper.gpu(2)
    #submission()
    nh=NetHelper(deploy=cfgs.deploy_pt,model=cfgs.best_model_dir)
    if debug:
        l=Data.folder_opt(cfgs.train_data_path,func,nh)
    else:
        l=Data.folder_opt(cfgs.test_data_path,func,nh)
    l=np.array(l,dtype=[('x',int),('y',object)])
    l.sort(order='x')

    first_row = 'img,pixels'
    file_name = 'submission.csv'

    with open(file_name, 'w+') as f:
        f.write(first_row)
        for i in l:
            s = str(i[0]) + ',' + i[1]
            f.write(('\n'+s))
Example #37
0
    def saveSectionChanges(self, section):

        andro_obj = section.getAndroguardObj()
        if andro_obj:
            mapped_obj = Data.getInstance(andro_obj)
            if mapped_obj:
                # Really, what i am thinking.
                # Maybe the slowest, but i think it is unavoidable?
                # self.__map.get_item_type(TYPE_MAP_ITEM[mapped_obj.get_type()])
                # = copy.deepcopy(andro_obj)
                pass
Example #38
0
 def __init__(self, parent, size, plot, msg):
     self.parent = parent
     self.plot2d = plot
     self.msgwin = msg
     x = Image.open(abs_path(photograph))
     im = x.resize(size)
     self.size[0] = float(size[0])
     self.size[1] = float(size[1])
     self.image = ImageTk.PhotoImage(im)
     self.canvas = Canvas(parent, width = size[0], height = size[1])
     self.canvas.create_image(0,0,image = self.image, anchor = NW)
     self.data = Data()
Example #39
0
    def make_memory_feed_dict(self, in_memory_Data=None):
        if in_memory_Data is not None:
            c_data = in_memory_Data # if you just process the files and keep the Data tuple in memory

        else:
            with np.load(self.file_name) as c_file:
                c_data = Data(c_file['images'], c_file['labels'], c_file['nus'], c_file['singles'])

        self.feed_dict = {self.images_PH: c_data.images,
                          self.labels_PH: c_data.labels,
                          self.nus_PH: c_data.nus,
                          self.singles_PH: c_data.singles}
Example #40
0
 def on_pubmsg(self, c, e):
     """Hook for public not-prefixed written text inside a channel.
     -> e.target() == channel 
     -> e.source() == nick!~user@hostname  
     """
     nick, hostname = nm_to_n(e.source()), nm_to_h(e.source())
     if not nick in self.users:
         self.users[nick] = User(nick, hostname, self.connection)
     user, chan = self.users[nick], self.chans[e.target().lower()]
     
     line_raw = e.arguments()[0]
     data = Data(line_raw=line_raw, chan=chan, user=user)
     
     # execute plugins
     if line_raw.startswith(self.command_prefix):
         data.line_raw = data.line_raw[len(self.command_prefix):]
         data.reaction_type = "public_command"
         self.dispatch(data)
     else:
         data.reaction_type = "public"
         self.dispatch(data)
Example #41
0
class pend:
    NP = 400
    tmax = 5.0		# We digitize oscillations for 10 seconds
    looping = False
    xlabel = 'Seconds'
    ylabel = 'Volts'
    size = [100,100]
    scale = None    
    delay = 0.0
    
    def __init__(self, parent, size, plot, msg):
        self.parent = parent
        self.plot2d = plot
        self.msgwin = msg
        x = Image.open(abs_path(photograph))
        im = x.resize(size)
        self.size[0] = float(size[0])
        self.size[1] = float(size[1])
        self.image = ImageTk.PhotoImage(im)
        self.canvas = Canvas(parent, width = size[0], height = size[1])
        self.canvas.create_image(0,0,image = self.image, anchor = NW)
        self.data = Data()

    def enter(self, p):	
      self.ph = p 
      self.ph.select_adc(0)
      self.ph.set_adc_size(2)
      self.intro_msg()
      self.plot2d.setWorld(0, -5, self.tmax, 5)	
      self.plot2d.mark_axes(self.xlabel,self.ylabel)

    def exit(self):
      self.looping = False
      
    def set_tmax(self,w):
        d = self.scale.get()    
        self.tmax = float(d)
        self.plot2d.setWorld(0, -5, self.tmax, 5)	
        self.plot2d.mark_axes(self.xlabel,self.ylabel)
        
    def clear(self,e):
        if self.looping == True:
            return
        self.plot2d.delete_lines()  
        self.data.clear()

    def save(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save(fname)
        self.msgwin.msg('Data saved to '+ fname)

    def calc_g(self,e):
        if self.data.points == []:
            return
        x = self.lentext.get()
        try:
            L = float(x)
        except:
            self.msgwin.msg('Set the length of the rod', 'red')
            return

        import phmath, math
        dat = []
        for k in self.data.points:
           dat.append([k[0], k[1]])
        res = phmath.fit_dsine(dat)
        fit = []
        exp = []
        for k in res[0]: 
            fit.append([k[0],k[2]])
            exp.append([k[0],k[3]])
        self.data.traces.append(fit)
       	self.col = self.data.get_col()
        self.plot2d.line(fit, self.col)
       	self.col = self.data.get_col()
        self.plot2d.line(exp, self.col)

        D = res[1][4]
        T = 1.0 / res[1][1]
        r = 0.14		# radius of the rod
        R = 1.27		# radius of the ball
        density = 7.8		# density of iron
        m_rod = math.pi * r**2 * L * density
        m_ball = math.pi * 4 / 3 * R**3 * density
#        print m_rod,'',m_ball
        Lprime = ( (m_rod * L**2)/3 + (m_ball * 2/5 * R**2) + m_ball * \
          (L+R)**2 ) / ( (m_rod * L)/2 + m_ball*(L+R) )

        g = 4.0 * math.pi**2 * Lprime / (T * T)
#        print T, Lprime, g, D 
        ss = 'T = %4.3f seconds. Length = %4.2f | g = %4.0f \
cm/sec2 | Damping factor = %4.3f\n'%(T,L,g,D)
        self.msgwin.showtext(ss)
#        g2 = 4.0 * math.pi**2 * L / (T * T)
        
    def analyze(self,e):
        self.data.analyze(self.xlabel, self.ylabel)

    def save_all(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save_all(fname)
        self.msgwin.msg('Data saved to '+ fname)

    def show_all(self,e):
        self.plot2d.delete_lines()  
        self.data.index = 0
        for tr in self.data.traces:
          self.plot2d.line(tr,self.data.get_col())
#------------------------------------------------------------------        
        
    def start(self,e):
        if self.ph == None:
            self.msgwin.msg('Connection not made yet', 'red')
            return
        if self.looping == False:    		# Same color if restarted
        	self.col = self.data.get_col()
        self.msgwin.msg('Starting Pendulum Waveform digitization')
	self.looping = True

	v = self.ph.get_voltage_bip()		# First reading is taken
	self.start_time = v[0]			# Remember starting time
        self.data.points = [(0.0, v[1]/1000.0)]	# initialize the list
        
    def accept_trace(self):
        self.data.traces.append(self.data.points)

    def update(self):
        if self.looping == False:
            return
        val = self.ph.get_voltage_bip()
        elapsed = val[0] - self.start_time
        self.data.points.append( (elapsed, val[1]/1000.0) )
        self.plot2d.delete_lines()
        self.plot2d.line(self.data.points, self.col)
        if elapsed >= self.tmax:
          self.msgwin.msg('Digitization done for %2.1f seconds'%elapsed)
          self.accept_trace()
          self.looping = False

    def refresh(self,e):
        if self.looping == True:
            return
        self.intro_msg()

    def intro_msg(self):
        self.clear(None)
        self.msgwin.clear()
        self.msgwin.showtext('Connect DC motor output to Ch0, through an amplifier (gain = 100) and the '+\
        'Level Shifter.\nOscillate the pendulum and ')
        self.msgwin.showlink('Digitize', self.start)
        self.msgwin.showtext(' for ')
        if self.scale != None: self.scale.destroy()
        self.scale =  Scale(None, command = self.set_tmax, \
          from_ = 1, to=20, orient=HORIZONTAL, length=100, showvalue=1)
        self.scale.set(int(self.tmax))
        self.msgwin.showwindow(self.scale)
        self.msgwin.showtext(' Seconds.\n')
        self.msgwin.showlink('Click Here', self.calc_g)
        
        self.msgwin.showtext(' to calculate period "T" by fitting the graph.\n Value of "g" is calculated '+\
		'assuming Length of the rod =')
        self.lentext =  Entry(None, width = 4, fg = 'red')
        self.msgwin.showwindow(self.lentext)
        self.lentext.insert(END,'11.0')
        self.msgwin.showtext(' cm. and Bob diameter = 2.54 cm')
        
        self.msgwin.showlink('\nSave the latest trace', self.save)
        self.msgwin.showtext(' or ')
        self.msgwin.showlink('Save all Traces', self.save_all)
        self.msgwin.showtext(' to the text file')
        self.fntext =  Entry(None, width =15, fg = 'red')
        self.msgwin.showwindow(self.fntext)
        self.fntext.insert(END,'pend.dat')
        self.msgwin.showtext('  ')
        self.msgwin.showlink('Display all Traces', self.show_all)
        self.msgwin.showtext('   ')
        self.msgwin.showlink('Clear all Traces', self.clear)
        self.msgwin.showtext('   ')
        self.msgwin.showlink('Xmgrace', self.analyze)
        self.msgwin.showtext('   ')
        self.msgwin.showlink('Refresh', self.refresh)
        self.msgwin.showtext('\n')
Example #42
0
class mono:
    NP = 200
    adc_delay = 20
    delay_vals = [10,20,50,100,200,500,1000]
    looping = False
    xlabel = 'milli seconds'
    ylabel = 'Volts'
    adc_scale = None
    size = [100,100]
    
    def __init__(self, parent, size, plot, msg):
        self.parent = parent
        self.plot2d = plot
        self.msgwin = msg
        x = Image.open(abs_path(photograph))
        im = x.resize(size)
        self.size[0] = float(size[0])
        self.size[1] = float(size[1])
        self.image = ImageTk.PhotoImage(im)
        self.canvas = Canvas(parent, width = size[0], height = size[1])
        self.canvas.create_image(0,0,image = self.image, anchor = NW)
        self.data = Data()
      
    def enter(self, fd):	#Phoenix handler set by the caller
        self.ph = fd
        self.ph.select_adc(0)
        self.ph.set_adc_size(1)
        self.ph.set_pulse_width(1)
        self.plot2d.setWorld(0, 0, self.NP * self.adc_delay/1000.0, 5)
        self.plot2d.mark_axes(self.xlabel, self.ylabel)
        self.msg_intro()
      
    def exit(self):		# Do cleanup here
        self.ph.disable_set()

    def update(self):
        pass
      
    def clear(self,e):
        self.data.clear()
        self.plot2d.delete_lines()  
              
    def save(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save(fname)
        self.msgwin.msg('Data saved to '+ fname)

    def save_all(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save_all(fname)
        self.msgwin.msg('Data saved to '+ fname)
        
    def show_waveform(self,w):
        self.ph.enable_pulse_low(3)
        data = self.ph.read_block(self.NP, self.adc_delay, 0)
        self.data.points = []
        for k in data:
          self.data.points.append( (k[0]/1000.0, k[1]/1000.0) )
        self.plot2d.line(self.data.points, self.data.get_col())
        self.data.traces.append(self.data.points)
        self.ph.write_outputs(8);
        
    def set_adc_delay(self,w):
        d = self.adc_scale.get()    
        self.adc_delay = self.delay_vals[d]
        if self.ph == None:
            return
        self.ph.set_adc_delay(self.adc_delay)
        self.plot2d.setWorld(0, -5, self.NP * self.adc_delay/1000.0, 5)
        self.plot2d.mark_axes(self.xlabel, self.ylabel)

    def ms_delay(self,e):
        self.ph.write_outputs(8)
        self.ph.set_pulse_width(1)
        self.ph.set_pulse_polarity(1)
        t = self.ph.pulse2ftime(3,3)
        if t < 0:
            self.msgwin.showtext('\nTime out on Input D3','red')
            return
        self.msgwin.showtext('\nMonoshot Delay = ' + '%4.0f'%(t) + ' usec.')
    
    def refresh(self,e):
        self.msg_intro()

    def msg_intro(self):
        self.clear(None)
        self.msgwin.clear()
        self.msgwin.showtext('Power the Monostable Multivibrator circuit '+\
        'made using IC555 from the 5V Output socket. Connect the Trigger '+\
        'Input of 555 (pin number 2) to Digital Output D3.\n')

        self.msgwin.showlink('View Waveform', self.show_waveform)
        self.msgwin.showtext(' View the output of the circuit.\n')
        self.adc_scale =  Scale(None, command = self.set_adc_delay, \
          from_ = 0, to=6, orient=HORIZONTAL, length=100, showvalue=0)
        self.adc_scale.set(1)
        self.msgwin.showwindow(self.adc_scale)
        self.msgwin.showtext(' Change time scale if required.\n')
        
        self.msgwin.showlink('Save Last Trace', self.save)
        self.msgwin.showtext(' or ')
        self.msgwin.showlink('Save all Traces', self.save_all)
        self.msgwin.showtext(' to a text file named ') 
        self.fntext =  Entry(None, width =20, fg = 'red')
        self.msgwin.showwindow(self.fntext)
        self.fntext.insert(END,'mono555.dat')
        self.msgwin.showtext(' ')
        self.msgwin.showlink('Clear all Traces', self.clear)
        self.msgwin.showtext(' to remove all the plots.\n')
        self.msgwin.showtext('\nConnect 555 output to Digital input D3. ')
        self.msgwin.showlink('Measure Time Delay', self.ms_delay)

        self.msgwin.showtext('  .To Refresh Text Window ')
        self.msgwin.showlink('Click Here', self.refresh)
Example #43
0
class gm:
    NP = 400
    countrate = 100.0		# Assume a 100 Hz count rate
    numtrials = 5		# 5 trials default
    duration = 1		# count for one second
    tube_voltage = 0.0
    VMIN = 300.0
    VOPT = 500.0
    VMAX = 902.0
    looping = False
    xlabel = 'Trial'
    ylabel = 'Count'
    size = [100,100]
    scale = None    
    
    def __init__(self, parent, size, plot, msg):
        self.parent = parent
        self.plot2d = plot
        self.msgwin = msg
        x = Image.open(abs_path(photograph))
        im = x.resize(size)
        self.size[0] = float(size[0])
        self.size[1] = float(size[1])
        self.image = ImageTk.PhotoImage(im)
        self.canvas = Canvas(parent, width = size[0], height = size[1])
        self.canvas.create_image(0,0,image = self.image, anchor = NW)
        self.data = Data()

    def enter(self, p):	
      self.ph = p 
      self.ph.select_adc(0)
      self.ph.set_adc_size(2)
      self.plot2d.setWorld(0, 0, self.countrate, self.numtrials)	
      self.plot2d.mark_axes(self.xlabel,self.ylabel)
      self.tube_voltage = self.ph.gm_set_voltage(self.VOPT)
      self.intro_msg()
      
    def exit(self):
      self.looping = False
      
    def clear(self,e):
        if self.looping == True: return
        self.plot2d.delete_lines()  
        self.data.clear()

    def save(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save(fname)
        self.msgwin.msg('Data saved to '+ fname)

    def analyze(self,e):
        self.data.analyze(self.xlabel, self.ylabel)

    def save_all(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save_all(fname)
        self.msgwin.msg('Data saved to '+ fname)

    def show_all(self,e):
        self.plot2d.delete_lines()  
        self.data.index = 0
        for tr in self.data.traces:
          self.plot2d.line(tr,self.data.get_col())
#------------------------------------------------------------------        
        
    def start(self,e):
        x = self.durationtext.get()
        try:
            self.duration = int(x)
        except:
            self.msgwin.msg('Set the Duration of Counting', 'red')
            return

        x = self.trialstext.get()
        try:
            self.numtrials = int(x)
        except:
            self.numtrials = 5

        if self.ph == None:
            self.msgwin.msg('Connection not made yet', 'red')
            return
        if self.looping == False:    		# Same color if restarted
        	self.col = self.data.get_col()
        
        self.tube_voltage = self.ph.gm_set_voltage(self.tube_voltage)
        self.msgwin.msg('GM tube Counting Radiation.')
        self.msgwin.label.update()
	self.count = 0
        gmc = self.ph.gm_get_count(self.duration)
        self.msgwin.showtext('\nCounts : %d '%(gmc))
        self.data.points = [(self.count, gmc)]
        self.plot2d.setWorld(0, 0, self.numtrials, gmc * 1.5 + 5)
        self.xlabel = 'Trial'
        self.plot2d.mark_axes(self.xlabel,self.ylabel)
        self.count = self.count + 1
	self.looping = True
        self.doing_gmchar = False

    def start_gmgraph(self,e):
        x = self.durationtext.get()
        try:
            self.duration = int(x)
        except:
            self.msgwin.msg('Set the Duration of Counting', 'red')
            return
        if self.looping == False:    		# Same color if restarted
        	self.col = self.data.get_col()
        self.msgwin.msg('Drawing GM tube Characteristic (will take time)')
        self.msgwin.label.update()
        self.count = 0
        self.tube_voltage = self.ph.gm_set_voltage(self.VOPT)
        gmc_max = self.ph.gm_get_count(self.duration)
        self.plot2d.setWorld(self.VMIN, 0, self.VMAX*1.1, gmc_max * 2 + 5)	
        self.xlabel = 'Voltage'
        self.plot2d.mark_axes(self.xlabel,self.ylabel)
	self.tube_voltage = self.ph.gm_set_voltage(self.VMIN)
        gmc = self.ph.gm_get_count(self.duration)
        self.msgwin.showtext('\n(%4.0f,%d) '%(self.tube_voltage,gmc))
        self.data.points = [(self.tube_voltage, gmc)]
        self.count = self.count + 1
	self.looping = True
        self.doing_gmchar = True

    def accept_trace(self):
        self.data.traces.append(self.data.points)

    def update(self):
        if self.looping == False:
            return
        if self.doing_gmchar == True:
            if self.tube_voltage < 400:
                self.tube_voltage = self.ph.gm_set_voltage(self.tube_voltage + 20)
            else:
                self.tube_voltage = self.ph.gm_set_voltage(self.tube_voltage + 50)
            gmc = self.ph.gm_get_count(self.duration)
            self.data.points.append((self.tube_voltage, gmc))
            self.plot2d.delete_lines()
            self.plot2d.line(self.data.points, self.col, smooth=False)
            self.msgwin.showtext('(%4.0f,%d) '%(self.tube_voltage,gmc))
            self.count = self.count + 1
            if self.tube_voltage > self.VMAX:
                self.looping = False
                self.msgwin.msg('GM Tube Characteristic Done.')
#                self.ph.gm_set_voltage(self.VOPT)
                self.set_tv(None)
                self.accept_trace()
        else:
            gmc = self.ph.gm_get_count(self.duration)
            self.data.points.append((self.count, gmc))
            self.plot2d.delete_lines()
            self.plot2d.line(self.data.points, self.col, smooth=False)
            self.msgwin.showtext('%d '%(gmc))
            self.count = self.count + 1
            if self.count >= self.numtrials:
                self.looping = False
                self.msgwin.msg('Counting Over after %d trials'%self.numtrials)
                self.accept_trace()

    def set_tv(self, e):
        ss =  self.tv_text.get()
        try:
           vset = float(ss)
        except:
           vset = 0
        self.tube_voltage = self.ph.gm_set_voltage(vset)
        ss = '%5.0f'%self.tube_voltage
        self.gmtv_value.set(ss)
        
    def refresh(self,e):
        self.intro_msg()

    def intro_msg(self):
        self.msgwin.clear()
        self.msgwin.showtext('Connections: (1)Yellow - CNTR (2) Green - CH0 '+\
        '(3) Blue - PWG (4) Black - GND (5) Red - 5V.\n','red')
        self.msgwin.showtext('Enter the Tube voltage ')
        self.tv_text =  Entry(None, width = 5, fg = 'red')
        self.msgwin.showwindow(self.tv_text)
        self.tv_text.insert(END, '500')
        self.msgwin.showtext('Volts and ')
        self.msgwin.showlink('Click Here ', self.set_tv)
        self.msgwin.showtext(' to set it. Current Tube Voltage is ')
        self.gmtv_value = StringVar()
        self.gmtv_label =  Label(None, width=5, textvariable = self.gmtv_value)
        self.msgwin.showwindow(self.gmtv_label)
        ss = '%5.0f'%self.tube_voltage
        self.gmtv_value.set(ss)
        self.msgwin.showtext('Volts\n')
        
        self.msgwin.showtext('Set the duration of Counting to')
        self.durationtext =  Entry(None, width = 5, fg = 'red')
        self.msgwin.showwindow(self.durationtext)
        self.durationtext.insert(END,'1')
        self.msgwin.showtext('seconds and number of trials to')
        self.trialstext =  Entry(None, width = 5, fg = 'red')
        self.msgwin.showwindow(self.trialstext)
        self.trialstext.insert(END,str(self.numtrials))

        self.msgwin.showtext('.')
        self.msgwin.showlink('Click Here', self.start)

        self.msgwin.showtext(' to start counting. ')
        self.msgwin.showtext('For tube Characteristic ')
        self.msgwin.showlink('Click Here', self.start_gmgraph)
        self.msgwin.showtext('\n');

        self.msgwin.showlink('Save the latest trace', self.save)
        self.msgwin.showtext(' or ')
        self.msgwin.showlink('Save all Traces', self.save_all)
        self.msgwin.showtext(' to a text file named ')
        self.fntext =  Entry(None, width =15, fg = 'red')
        self.msgwin.showwindow(self.fntext)
        self.fntext.insert(END,'gmcount.dat')
        self.msgwin.showtext(' or ')
        self.msgwin.showlink('Analyze', self.analyze)
        self.msgwin.showtext(' data online using Xmgrace.\n')
        self.msgwin.showtext('You can also do ')
        self.msgwin.showlink('Display all Traces', self.show_all)
        self.msgwin.showtext(' , ')
        self.msgwin.showlink('Clear all Traces', self.clear)
        self.msgwin.showtext(' or ')
        self.msgwin.showlink('Refresh This Window', self.refresh)
        self.msgwin.showtext('\n')
Example #44
0
class rad:
    xmax = 255
    ymax = 100
    xlabel = "Channel Number"
    ylabel = "Count"
    size = [100, 100]
    running = False
    scale = None

    def __init__(self, parent, size, plot, msg):
        self.parent = parent
        self.plot2d = plot
        self.msgwin = msg
        x = Image.open(abs_path(photograph))
        im = x.resize(size)
        self.size[0] = float(size[0])
        self.size[1] = float(size[1])
        self.image = ImageTk.PhotoImage(im)
        self.canvas = Canvas(parent, width=size[0], height=size[1])
        self.canvas.create_image(0, 0, image=self.image, anchor=NW)
        self.data = Data()

    def enter(self, p):
        self.ph = p
        self.ph.clear_hist()
        self.intro_msg()
        self.plot2d.setWorld(0, 0, self.xmax, self.ymax)
        self.plot2d.mark_axes(self.xlabel, self.ylabel)
        self.plot2d.markerval = None  # Clear Markers
        self.calibrated = False
        try:
            self.plot2d.canvas.delete(self.plot2d.markertext)
        except:
            pass

    def exit(self):
        self.running = False

    def clear(self, e):
        self.plot2d.delete_lines()
        self.data.clear()

    def set_ymax(self, w):
        d = self.scale.get()
        self.ymax = float(d)
        self.plot2d.setWorld(0, 0, self.xmax, self.ymax)
        self.plot2d.mark_axes(self.xlabel, self.ylabel)

    def collect_hist(self, e):
        if self.running == False:
            return
        phdata = self.ph.read_hist()
        self.plot2d.setWorld(0, 0, self.xmax, self.ymax)
        self.plot2d.mark_axes(self.xlabel, self.ylabel)
        self.data.points = []
        for k in phdata:
            energy = k[0] * self.xmax / 255.0
            self.data.points.append((energy, k[1]))
        self.plot2d.delete_lines()
        self.plot2d.line(self.data.points, "black")
        #        self.data.traces.append(self.data.points)
        self.last_read_time = time.time()

    def update(self):
        if self.running == False:
            return
        self.collect_hist(0)

    def start(self, w):
        self.ph.start_hist()
        self.running = True
        self.msgwin.msg("Started Collecting Data")

    def stop(self, w):
        self.running = False
        self.ph.stop_hist()
        self.msgwin.msg("Stopped Collecting Data")

    def clear_hist(self, w):
        self.ph.clear_hist()
        self.plot2d.delete_lines()
        self.msgwin.msg("Cleared Histogram Data")

    def save(self, e):
        fname = self.fntext.get()
        if fname == None:
            return
        self.data.save(fname)
        self.msgwin.msg("Data saved to " + fname)

    def calibrate(self, e):
        if self.plot2d.markerval == None:
            self.msgwin.msg("Mark a Peak before calibration", "red")
            return
        try:
            chan = self.plot2d.markerval[0]
            s = self.energytext.get()
            energy = float(s)
            self.xmax = self.xmax * energy / chan
            self.xlabel = "Energy (MeV)"
            self.plot2d.setWorld(0, 0, self.xmax, self.ymax)
            self.plot2d.mark_axes(self.xlabel, self.ylabel)
            self.calibrated = True
            self.plot2d.canvas.delete(self.plot2d.markertext)
            self.plot2d.markerval = None
            self.msgwin.msg("Calibration done")
        except:
            self.msgwin.msg("Error occured during calibration", "red")

    def autoscale(self, e):
        ymax = 10.0
        for pt in self.data.points:
            if pt[1] > ymax:
                ymax = pt[1]
        self.ymax = ymax * 1.1
        self.plot2d.setWorld(0, 0, self.xmax, self.ymax)

    def intro_msg(self):
        self.clear(None)
        self.msgwin.clear()
        self.msgwin.showtext(
            "Connections: (1) Blue - CH0 (2) Black - GND " + "(3)Green - D3out (4) Yellow - CMP\n", "red"
        )
        self.msgwin.showtext(
            "Connect the Radiation Detection Accessoy as "
            + "shown in the figure. The micro controller inside Phoenix is "
            + "interrupted every time a particle enters the detector. While "
            + "enabled it digitizes the energy information of the particle and "
            + "stores it in a 256 channel histogram. The following commands "
            + "can be used to control the data acquisition process\n"
        )
        self.msgwin.showlink("Start", self.start)
        self.msgwin.showtext(" Collecting. ")
        self.msgwin.showlink("Stop", self.stop)
        self.msgwin.showtext(" Stop Collecting. ")
        self.msgwin.showlink("Update", self.collect_hist)
        self.msgwin.showtext(" Update the Display. ")
        self.msgwin.showlink("Clear", self.clear_hist)
        self.msgwin.showtext(" Clear data and Display.\n")
        self.msgwin.showlink("Save Histogram", self.save)
        self.msgwin.showtext(" to a text file named ")
        self.fntext = Entry(None, width=20, fg="red")
        self.msgwin.showwindow(self.fntext)
        self.fntext.insert(END, "hist.dat")
        self.msgwin.showtext("\n")

        self.scale = Scale(
            None, command=self.set_ymax, resolution=100, from_=100, to=10000, orient=HORIZONTAL, length=100, showvalue=0
        )
        self.scale.set(self.ymax)
        self.msgwin.showwindow(self.scale)
        self.msgwin.showtext(" Change the Vertical Axis. For auto scaling ")
        self.msgwin.showlink("Click Here\n", self.autoscale)

        self.msgwin.showtext(" To calibrate, click on a known peak, enter " + " the corresponding energy ")
        self.energytext = Entry(None, width=10, fg="red")
        self.msgwin.showwindow(self.energytext)
        self.energytext.insert(END, "5.485")
        self.msgwin.showtext(" MeV and ")
        self.msgwin.showlink("Click Here\n", self.calibrate)
Example #45
0
class cap:
    NP = 200
    adc_delay = 20
    delay_vals = [10,20,50,100,200,500,1000]
    xlabel = 'milli seconds'
    ylabel = 'Volts'
    size = [100,100]
    
    def __init__(self, parent, size, plot, msg):
        self.parent = parent
        self.plot2d = plot
        self.msgwin = msg
        x = Image.open(abs_path(photograph))
        im = x.resize(size)
        self.size[0] = float(size[0])
        self.size[1] = float(size[1])
        self.image = ImageTk.PhotoImage(im)
        self.canvas = Canvas(parent, width = size[0], height = size[1])
        self.canvas.create_image(0,0,image = self.image, anchor = NW)
        self.data = Data()
      
    def enter(self, fd):	#Phoenix handler 'ph' is set by the caller
        self.intro_msg()
        self.ph = fd
        try:
            self.ph.select_adc(0)
            self.ph.set_adc_size(2)
        except:
            self.msgwin.msg('Connection NOT Established','red')
                
    def exit(self):		# Do cleanup here
        try:
            self.ph.disable_set()
        except:
            pass
            
    def update(self):
        pass
      
    def clear(self,e):
        self.data.clear()
        self.plot2d.delete_lines()  
              
    def save(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save(fname)
        self.msgwin.msg('Data saved to '+ fname)

    def analyze(self,e):
        self.data.analyze(self.xlabel, self.ylabel)

    def save_all(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save_all(fname)
        self.msgwin.msg('Data saved to '+ fname)
        
    def capture_trace(self): # Collect data and store in object 'data'
        # All conversion to be done here. setWorld according to 'trace'
        # xscale and yscale are specified
        # if axes are shown in different units    
        phdata = self.ph.read_block(self.NP, self.adc_delay, 0)
        self.data.points = []
        for k in phdata:
          self.data.points.append( (k[0]/1000.0, k[1]/1000.0) )
            #microsec -> millisec ; millivolts -> volts
        self.data.traces.append(self.data.points)
        last = self.data.points[-1][0]
        second = self.data.points[-2][0]
        xmax = last + (last-second)

        self.plot2d.setWorld(0, 0, xmax, 5)	# Set scale factors
        self.plot2d.mark_axes(self.xlabel, self.ylabel)	 # axes & labels
        self.plot2d.line(self.data.points, self.data.get_col())
        
    def discharge(self,w):
        self.ph.write_outputs(8)
        time.sleep(1)
        self.ph.enable_set_low(3)
        self.capture_trace()

    def charge(self,w):
        self.ph.write_outputs(0)
        time.sleep(1)
        self.ph.enable_set_high(3)
        self.capture_trace()
        
    def set_adc_delay(self,w):
        d = self.adc_scale.get()    
        self.adc_delay = self.delay_vals[d]
        if self.ph == None:
            return
        self.ph.set_adc_delay(self.adc_delay)
        self.plot2d.setWorld(0, 0, self.NP * self.adc_delay/1000.0, 5)
        self.plot2d.mark_axes(self.xlabel, self.ylabel)

    def calc_cap(self,e):
        if self.data.points == []:
            return
        x = self.restext.get()
        try:
            R = float(x)
        except:
            self.msgwin.msg('Set the Resistance value', 'red')
            return

        import phmath, math
        dat = []
        for k in self.data.points:
           dat.append([k[0], k[1]])
        res = phmath.fit_exp(dat)
        fit = []
        for k in res[0]: 
            fit.append([k[0],k[2]])
        self.data.traces.append(fit)
       	self.col = self.data.get_col()
        self.plot2d.line(fit, self.col)

        RC = -1.0/res[1][1]
        C = RC/R
#        print res[1][1], RC, R, C
        ss = 'RC = %4.3f . C = %4.3f\n'%(RC,C)
        self.msgwin.showtext(ss)

    def refresh(self,e):
        self.intro_msg()

    def intro_msg(self):
        self.clear(None)
        self.msgwin.clear()
        self.msgwin.showtext('Connect the Capacitor from CH0 to GND and '+\
        'Resistor from D3out to CH0. The Blue Texts are the Commands.\n')
        self.msgwin.showlink('Charge', self.charge)
        self.msgwin.showtext('  or   ')
        self.msgwin.showlink('Discharge.', self.discharge)
        self.msgwin.showtext('       ')
        self.msgwin.showlink('Fit the Discharge Curve', self.calc_cap)
        self.msgwin.showtext(' and calculate the value of capacitance for R = ')
        self.restext =  Entry(None, width = 4, fg = 'red')
        self.msgwin.showwindow(self.restext)
        self.restext.insert(END,'1.0')
        self.msgwin.showtext(' KOhm.\n')
        
        self.msgwin.showlink('Save Last Trace', self.save)
        self.msgwin.showtext(' or ')
        self.msgwin.showlink('Save all Traces', self.save_all)
        self.msgwin.showtext(' to a text file named ') 
        self.fntext =  Entry(None, width =20, fg = 'red')
        self.msgwin.showwindow(self.fntext)
        self.fntext.insert(END,'cap.dat')
        self.msgwin.showtext(' ')
        self.msgwin.showlink('Clear Traces', self.clear)
        self.msgwin.showtext('   ')
        self.msgwin.showlink('Xmgrace', self.analyze)
        self.msgwin.showtext('    ')
        self.msgwin.showlink('Refresh', self.refresh)
        self.msgwin.showtext('\n')

        self.adc_scale =  Scale(None, command = self.set_adc_delay, \
          from_ = 0, to=6, orient=HORIZONTAL, length=100, showvalue=0)
        self.adc_scale.set(1)
        self.msgwin.showwindow(self.adc_scale)
        self.msgwin.showtext(' Change time scale according to RC time constant.\n')
Example #46
0
class gravity:
    NP = 400
    tmax = 10.0		# We digitize oscillations for 10 seconds
    maxcount = 10
    looping = False
    xlabel = 'Number'
    ylabel = 'g'
    size = [100,100]
    scale = None    
    
    def __init__(self, parent, size, plot, msg):
        self.parent = parent
        self.plot2d = plot
        self.msgwin = msg
        x = Image.open(abs_path(photograph))
        im = x.resize(size)
        self.size[0] = float(size[0])
        self.size[1] = float(size[1])
        self.image = ImageTk.PhotoImage(im)
        self.canvas = Canvas(parent, width = size[0], height = size[1])
        self.canvas.create_image(0,0,image = self.image, anchor = NW)
        self.data = Data()

    def enter(self, p):	
      self.ph = p 
      self.intro_msg()
      self.plot2d.setWorld(0, -5, self.tmax, 5)	
      self.plot2d.mark_axes(self.xlabel,self.ylabel)

    def exit(self):
      self.looping = False
      
    def update(self):
        pass

    def clear(self,e):
        self.plot2d.delete_lines()  
        self.data.clear()

    def save(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save(fname)
        self.msgwin.msg('Data saved to '+ fname)

    def analyze(self,e):
        self.data.analyze(self.xlabel, self.ylabel)

    def save_all(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save_all(fname)
        self.msgwin.msg('Data saved to '+ fname)

    def show_all(self,e):
        self.plot2d.delete_lines()  
        self.data.index = 0
        for tr in self.data.traces:
          self.plot2d.line(tr,self.data.get_col())
#------------------------------------------------------------------        
        
    def attach(self,e):
        x = self.lentext.get()
        try:
            self.length = float(x)
        except:
            self.msgwin.msg('Set the Height', 'red')
            return

        if self.ph == None:
            self.msgwin.msg('Connection not made yet', 'red')
            return
        self.ph.write_outputs(1)
        self.msgwin.msg('Powered the Electromagnet')
        
    def measure(self,e):
        t = self.ph.clr2rtime(0,0)
        if t < 0:
             self.msgwin.msg('Timeout Error', 'red')
             return
        elif t < 20:
             self.msgwin.msg('Connection Error', 'red')
             return
        t = t + 4000.0   # 4 ms correction for magnetic retention
        t = t * 1.0e-6   # usec to sec
        print t, self.length
        g = 2.0 * self.length / t ** 2
        self.msgwin.showtext('\n%7.6f\t%4.1f'%(t,g))
        self.msgwin.msg('Done')

    def refresh(self,e):
        self.intro_msg()

    def intro_msg(self):
        self.msgwin.clear()
        self.msgwin.showtext('Connect the Electromagnet between Digital '+\
        'Output D0 and GND. '+\
        'Connect the loudspeaker between GND and the input of the inverting '+\
        'amplifier and set the gain resistor to 100 Ohms. '+\
        ' Amplifier output goes to Digital Input D0 through a 1K resistor.\n')

        self.msgwin.showlink('Click Here', self.attach)
        self.msgwin.showtext(' to power the Electromagnet.')
        
        self.msgwin.showtext(' Height = ')
        self.lentext =  Entry(None, width = 5, fg = 'red')
        self.msgwin.showwindow(self.lentext)
        self.lentext.insert(END,'30.0')
        self.msgwin.showtext(' cm.')

        self.msgwin.showlink('Click Here', self.measure)
        self.msgwin.showtext(' to Release the Ball from the magnet.')
Example #47
0
class sound:
    NP = 200
    adc_delay = 10
    delay_vals = [10,20,50,100,200,500,1000]
    looping = False
    xlabel = 'milli seconds'
    ylabel = 'Volts'
    adc_scale = None
    size = [100,100]
    
    def __init__(self, parent, size, plot, msg):
        self.parent = parent
        self.plot2d = plot
        self.msgwin = msg
        x = Image.open(abs_path(photograph))
        im = x.resize(size)
        self.size[0] = float(size[0])
        self.size[1] = float(size[1])
        self.image = ImageTk.PhotoImage(im)
        self.canvas = Canvas(parent, width = size[0], height = size[1])
        self.canvas.create_image(0,0,image = self.image, anchor = NW)
        self.data = Data()
      
    def enter(self, fd):	#Phoenix handler set by the caller
        self.ph = fd
        self.ph.select_adc(0)
        self.ph.set_adc_size(1)
        self.ph.write_outputs(0)
        self.ph.set_pulse_width(13)
        self.plot2d.setWorld(0, -5, self.NP * self.adc_delay/1000.0, 5)
        self.plot2d.mark_axes(self.xlabel, self.ylabel)
        self.msg_intro()
      
    def exit(self):		# Do cleanup here
        self.ph.disable_set()

    def update(self):
        pass
      
    def clear(self,e):
        self.data.clear()
        self.plot2d.delete_lines()  
              
    def save(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save(fname)
        self.msgwin.msg('Data saved to '+ fname)

    def save_all(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save_all(fname)
        self.msgwin.msg('Data saved to '+ fname)
        
    def show_waveform(self,w):
        self.ph.enable_pulse_high(3)
        data = self.ph.read_block(self.NP, self.adc_delay, 1)
        self.data.points = []
        for k in data:
          self.data.points.append( (k[0]/1000.0, k[1]/1000.0) )
        self.plot2d.line(self.data.points, self.data.get_col())
        self.data.traces.append(self.data.points)
        self.ph.write_outputs(0);
        
    def set_adc_delay(self,w):
        d = self.adc_scale.get()    
        self.adc_delay = self.delay_vals[d]
        if self.ph == None:
            return
        self.ph.set_adc_delay(self.adc_delay)
        self.plot2d.setWorld(0, -5, self.NP * self.adc_delay/1000.0, 5)
        self.plot2d.mark_axes(self.xlabel, self.ylabel)

    def ms_delay(self,e):
        self.ph.write_outputs(0)
        self.ph.set_pulse_width(13)
        self.ph.set_pulse_polarity(0)
        t = self.ph.pulse2rtime(3,3)
        if t < 0:
            self.msgwin.showtext('\nTime out on Input D3','red')
            return
        self.msgwin.showtext('%4.0f'%t)
    
    def refresh(self,e):
        self.msg_intro()

    def msg_intro(self):
        self.clear(None)
        self.msgwin.clear()
        self.msgwin.showtext('Connect the Transmitter Piezo between Digital '+\
        'output D3 and Ground. Connect the Receiver Piezo between Ground '+\
        'and the Inverting Amplifier Input. Set a gain resistor of 100. '+\
        'Connect the Output of the amplifier to the level shifter and '+\
        'level shifter output to Ch0. If the Amplitude is less, use one '+\
        'more Inverting amplifier in series.')

        self.msgwin.showlink('View Waveform', self.show_waveform)
        self.msgwin.showtext(' View the output of the receiver piezo.\n')
        
        self.msgwin.showlink('Save Last Trace', self.save)
        self.msgwin.showtext(' or ')
        self.msgwin.showlink('Save all Traces', self.save_all)
        self.msgwin.showtext(' to a text file named ') 
        self.fntext =  Entry(None, width =20, fg = 'red')
        self.msgwin.showwindow(self.fntext)
        self.fntext.insert(END,'sound.dat')
        self.msgwin.showtext(' ')
        self.msgwin.showlink('Clear all Traces', self.clear)
        self.msgwin.showtext(' to remove all the plots.\n')
        self.msgwin.showtext('\nConnect the Inverting Amplifier Output '+\
        'to Digital Input D3 through a 1K Series resistance to.')
        self.msgwin.showlink('Measure Time Delay', self.ms_delay)

        self.msgwin.showtext(' or ')
        self.msgwin.showlink('Refresh', self.refresh)
        self.msgwin.showtext('\n')
Example #48
0
class osc:
    NP = 200
    adc_delay = 20
    delay_vals = [10,20,50,100,200,500,1000]
    looping = False
    xlabel = 'milli seconds'
    ylabel = 'Volts'
    adc_scale = None
    size = [100,100]
    
    def __init__(self, parent, size, plot, msg):
        self.parent = parent
        self.plot2d = plot
        self.msgwin = msg
        x = Image.open(abs_path(photograph))
        im = x.resize(size)
        self.size[0] = float(size[0])
        self.size[1] = float(size[1])
        self.image = ImageTk.PhotoImage(im)
        self.canvas = Canvas(parent, width = size[0], height = size[1])
        self.canvas.create_image(0,0,image = self.image, anchor = NW)
        self.data = Data()
      
    def enter(self, fd):	#Phoenix handler set by the caller
        self.ph = fd
        self.ph.select_adc(0)
        self.ph.set_adc_size(1)
        self.plot2d.setWorld(0, 0, self.NP * self.adc_delay/1000.0, 5)
        self.plot2d.mark_axes(self.xlabel, self.ylabel)
        self.msg_intro()
      
    def exit(self):		# Do cleanup here
        self.ph.disable_set()

    def update(self):
        pass
      
    def clear(self,e):
        self.data.clear()
        self.plot2d.delete_lines()  
              
    def save(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save(fname)
        self.msgwin.msg('Data saved to '+ fname)

    def save_all(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save_all(fname)
        self.msgwin.msg('Data saved to '+ fname)
        
    def show_waveform(self,w):
        data = self.ph.read_block(self.NP, self.adc_delay, 0)
        self.data.points = []
        for k in data:
          self.data.points.append( (k[0]/1000.0, k[1]/1000.0) )
        self.plot2d.line(self.data.points, self.data.get_col())
        self.data.traces.append(self.data.points)
        
    def set_adc_delay(self,w):
        d = self.adc_scale.get()    
        self.adc_delay = self.delay_vals[d]
        if self.ph == None:
            return
        self.ph.set_adc_delay(self.adc_delay)
        self.plot2d.setWorld(0, -5, self.NP * self.adc_delay/1000.0, 5)
        self.plot2d.mark_axes(self.xlabel, self.ylabel)

    def duty_cycle(self,e):
        sum = 0.0
        for k in range(100):
          t = self.ph.r2ftime(4,4)
          if t < 0:
            self.msgwin.showtext('\nTime out on CMP','red')
            return
          else:
            sum = sum + t    
        hi = sum / 100
        self.msgwin.showtext('\nHIGH = ' + '%4.1f'%(hi) + ' usec.  ')
        sum = 0.0
        for k in range(100):
          t = self.ph.f2rtime(4,4)
          if t < 0:
            self.msgwin.showtext('\nTime out on CMP','red')
            return
          else:
            sum = sum + t    
        low = sum / 100
        self.msgwin.showtext('LOW = ' + '%4.1f'%(low) + ' usec.  ')
        ds = hi * 100 / (low + hi)
        self.msgwin.showtext('Duty Cycle = ' + '%4.1f'%(ds)+ ' %. ')

    def frequency(self,e):
        fr = self.ph.measure_frequency()
        self.msgwin.showtext('\nFrequency = ' + '%4.0f'%(fr) + ' Hz')

    def msg_intro(self):
        self.clear(None)
        self.msgwin.clear()
        self.msgwin.showtext('Power the Astable Multivibrator circuit '+\
        'made using IC555 from the 5V Output socket. Connect the Output '+\
        'signal (pin number 3) to CH0.\n')

        self.msgwin.showlink('View Waveform', self.show_waveform)
        self.msgwin.showtext(' View the output of the circuit.\n')
        self.adc_scale =  Scale(None, command = self.set_adc_delay, \
          from_ = 0, to=6, orient=HORIZONTAL, length=100, showvalue=0)
        self.adc_scale.set(1)
        self.msgwin.showwindow(self.adc_scale)
        self.msgwin.showtext(' Change time scale if required.\n')
        
        self.msgwin.showlink('Save Last Trace', self.save)
        self.msgwin.showtext(' or ')
        self.msgwin.showlink('Save all Traces', self.save_all)
        self.msgwin.showtext(' to a text file named ') 
        self.fntext =  Entry(None, width =20, fg = 'red')
        self.msgwin.showwindow(self.fntext)
        self.fntext.insert(END,'osc555.dat')
        self.msgwin.showtext(' ')
        self.msgwin.showlink('Clear all Traces', self.clear)
        self.msgwin.showtext(' to remove all the plots.\n')
        self.msgwin.showtext('\nConnect 555 output to CMP and ')
        self.msgwin.showlink('Measure Duty Cycle', self.duty_cycle)
        self.msgwin.showtext('. Connect to CNTR and ')
        self.msgwin.showlink('Measure Frequency', self.frequency)
Example #49
0
class explore:
    NP = 100
    adc_delay = 10
    delay_vals = [10,20,50,100,200,500,1000]
    numchans = 1
    xlabel = 'milli seconds'
    ylabel = 'Volts'
    bw = ['black', 'white']	
    by = ['black', 'yellow']	
    br = ['black', 'red']	
    size = [350.0, 272.0]
    dotdx = 0.012
    dotdy = 0.015
    delta	= 0.03
    playing_tune = False

    # adc info [x, y, canvas_oval, status, color, coordinate data list]
    adcs  = [[0.620, 0.717, None, 0, 'black', [] ],\
             [0.620, 0.621, None, 0, 'red'  , [] ],\
             [0.620, 0.523, None, 0, 'green', [] ],\
             [0.620, 0.432, None, 0, 'blue',  [] ] ]

    #Digital have x,y,oval & value
    douts = [[0.21, 0.739, None, 0],\
         [0.286, 0.739, None, 0],\
         [0.360, 0.739, None, 0],\
         [0.434, 0.739, None, 0]]

    dins = [[0.212, 0.836, None, 0],\
         [0.285, 0.836, None, 0],\
         [0.359, 0.836, None, 0],\
         [0.433, 0.836, None, 0]]
         
    cmp   = [0.508, 0.432, None, 0]

    digin_data = 0		# will force the first update 
    cmp_stat = 0		# Comparator status	
         
    ccs   = [0.335, 0.246]
    led   = [0.245, 0.339, None, 0]	# x, y, oval & status
    dc5v  = [0.333, 0.342, None, 0]
    # DAC, CNTR & PWG has x, y, oval, status & value fields
    dac   = [0.434, 0.337, None, 0, 0.0]
    cntr  = [0.333, 0.431, None, 0, 0.0]
    pwg   = [0.433, 0.431, None, 0, 1000.0] # 1000 Hz 

    ls_ins = [[0.841, 0.715, None, 0],\
             [0.840, 0.620, None, 0]]
    ls_outs= [[0.701, 0.715],\
         [0.701, 0.619]]

    ia_in = [[0.615, 0.245],\
         [0.615, 0.33]]
    ia_out= [[0.838, 0.240],\
         [0.838, 0.337]]
    ia_gb = [[0.689, 0.243],\
         [0.689, 0.339]]
    ia_ge= [[0.763, 0.242],\
         [0.763, 0.337]]

    nia_in = [0.916, 0.830]
    nia_out= [0.916, 0.620]
    nia_gb = [0.702, 0.833]
    nia_ge = [0.841, 0.833]

    gnds = [[0.911, 0.242],[0.940, 0.339], [0.508, 0.738], [0.508, 0.834]]

    adctl   = [0.62,0.85]   # Text display of ADC outputs
    dispadc = False
    adctext = [0,0,0,0]
    adcbox  = [0,0,0,0]

    ph = None		# The phoenix handler set by main program
    plot2d = None	# The 2D plot window
    msgwin = None	# The message window
    adc_scale = None  
     

    def __init__(self, parent, size, plot, msg):
        self.parent = parent
        self.plot2d = plot
        self.msgwin = msg
        self.data = Data()
                
        x = Image.open(abs_path(photograph))	# Images are kept along with the python code
        im = x.resize(size)        

        self.size[0] = float(im.size[0])
        self.size[1] = float(im.size[1])
        self.image = ImageTk.PhotoImage(im)
        self.canvas = Canvas(parent, width = im.size[0], height = im.size[1])
        self.canvas.create_image(0,0,image = self.image, anchor = NW)

	self.adblock = self.canvas.create_rectangle(self.dotxy(self.adctl),fill='white')
        
        for k in self.douts:
            k[2] = self.canvas.create_oval(self.dotxy(k), outline="", fill = 'black')
        for k in self.dins:
            k[2] = self.canvas.create_oval(self.dotxy(k), outline="", fill = 'black')
        for k in self.adcs:
            k[2] = self.canvas.create_oval(self.dotxy(k), outline="", fill = 'black')
        for k in self.ls_ins:
            k[2] = self.canvas.create_oval(self.dotxy(k), outline="", fill = 'black')
        self.led[2] = self.canvas.create_oval(self.dotxy(self.led), outline="", fill = 'black')
        self.pwg[2] = self.canvas.create_oval(self.dotxy(self.pwg), outline="", fill = 'black')
        self.dac[2] = self.canvas.create_oval(self.dotxy(self.dac), outline="", fill = 'black')
        self.cntr[2] = self.canvas.create_oval(self.dotxy(self.cntr), outline="", fill = 'black')
        self.cmp[2] = self.canvas.create_oval(self.dotxy(self.cmp), outline="", fill = 'black')
        self.canvas.bind("<ButtonRelease-1>", self.mouse_click)
        self.canvas.pack()    

    def enter(self, fd):	# Called when entering this expt
        self.msg_intro()
        self.canvas.itemconfigure(self.led[2],  fill = 'red')
        for adc in self.adcs:			# initialize ADC data
            adc[3] = 0
            self.canvas.itemconfigure(adc[2],fill = 'black')
            adc[5] = []
            for xy in range(self.NP):
                adc[5].append( (0.0, 0.0) ) 
        self.updating = True
        self.plot2d.setWorld(0, -5, self.NP * self.adc_delay/1000.0, 5)
        self.plot2d.mark_axes(self.xlabel,self.ylabel,self.numchans)

        self.ph = fd
        try:
            self.ph.set_frequency(1000)
            for ch in range(4):
                self.ph.del_channel(ch)
        except:
            self.msgwin.msg('Connection NOT established', 'red')
            
    def exit(self):			# Clear popup etc. here
        self.updating = False
#        if self.playing_tune == True:
          
        

    def set_adc_delay(self,w):
        d = self.adc_scale.get()    
        self.adc_delay = self.delay_vals[d]
        if self.ph == None:
            return
        self.ph.set_adc_delay(self.adc_delay)
        self.plot2d.setWorld(0, -5, self.NP * self.adc_delay/1000.0, 5)
        self.plot2d.mark_axes(self.xlabel,self.ylabel,self.numchans)
        
    def calc_numchans(self):
        self.numchans = 0
        for k in self.adcs:
            if k[3] == 1:
                self.numchans = self.numchans  + 1
        self.set_adc_delay(None)   	
#--------------------------------------------------------------------
    def w2s(self,x,y):		#world to screen coordinates
	return x*self.size[0], y*self.size[1]

    def dotxy(self,socket):
        x1 = self.size[0] * (socket[0] - self.dotdx)
        y1 = self.size[1] * (socket[1] - self.dotdy)
        x2 = self.size[0] * (socket[0] + self.dotdx)
        y2 = self.size[1] * (socket[1] + self.dotdy)
        return(x1,y1,x2,y2)

    def set_pwg(self,w):
        freq = self.pwg_scale.get()
        f = float(freq)
        self.pwg[4] = self.ph.set_frequency(f)
        s = '%3.1f Hz'%(self.pwg[4])
        self.pwg_label.config(text=s)

    def add_pwg(self):
        self.pwg_frame = Frame()    
        self.pwg_scale =  Scale(self.pwg_frame, command = self.set_pwg, from_ = 0,\
		to=10000, orient=HORIZONTAL, length=100, showvalue=0)
        self.pwg_scale.set(self.pwg[4])
        self.pwg_scale.pack(side=TOP)
        self.pwg_label = Label(self.pwg_frame)
        self.pwg_label.pack(side=TOP)

        xc = self.size[0] * self.pwg[0] + 10
        yc = self.size[1] * self.pwg[1] + 10
        self.canvas.create_window(int(xc), int(yc), \
        anchor = NW, window = self.pwg_frame)

    def remove_pwg(self):
        self.canvas.itemconfigure(self.pwg[2],  fill = 'black')
        self.pwg_label.destroy()
        self.pwg_scale.destroy()
        self.pwg_frame.destroy()

#----------------------- DAC Control ------------------------       
    def set_dac(self,w):
        mv = self.dac_scale.get()
        self.dac[4] = float(mv)
        self.ph.set_voltage(self.dac[4])
        s = '%4.0f mV'%(self.dac[4])
        self.dac_label.config(text=s)

    def add_dac(self):
        self.dac_frame = Frame()    
        self.dac_scale =  Scale(self.dac_frame, command = self.set_dac, from_ = 0,\
		to=5000, orient=HORIZONTAL, length=100, showvalue=0)
        self.dac_scale.set(self.dac[4])
        self.dac_scale.pack(side=TOP)
        self.dac_label = Label(self.dac_frame)
        self.dac_label.pack(side=TOP)

        xc = self.size[0] * self.dac[0] + 10
        yc = self.size[1] * self.dac[1] + 10
        self.canvas.create_window(int(xc), int(yc), anchor = NE, \
            window = self.dac_frame)
        self.canvas.itemconfigure(self.dac[2],  fill = 'white')
        self.dac[3] = 1
    
    def remove_dac(self):
        self.dac_label.destroy()
        self.dac_scale.destroy()
        self.dac_frame.destroy()
        self.canvas.itemconfigure(self.dac[2],  fill = 'black')
        self.dac[3] = 0
    
#----------------------- CNTR Reading  ------------------------       
    def get_cntr(self):
        self.cntr[4] = self.ph.measure_frequency()
        s = '%4.0f Hz'%(self.cntr[4])
        self.cntr_value.set(s)

    def add_cntr(self):
        self.cntr_frame = Frame()    
        self.cntr_value = StringVar()
        self.cntr_value.set('0.0 Hz')
        self.cntr_button =  Button(self.cntr_frame, \
            textvariable = self.cntr_value, command = self.get_cntr)
        self.cntr_button.pack(side=TOP)
        xc = self.size[0] * self.cntr[0] + 10
        yc = self.size[1] * self.cntr[1] + 10
        self.canvas.create_window(int(xc), int(yc), anchor = NE,\
             window = self.cntr_frame)
        self.canvas.pack()
        self.get_cntr()
    
    def remove_cntr(self):
        self.cntr_button.destroy()
        self.cntr_frame.destroy()
        self.canvas.itemconfigure(self.cntr[2],  fill = 'black')
        self.cntr[3] = 0

#-------------------------------------------------------------
    def selected(self, socket, e):
        if abs(e.x / self.size[0] - socket[0]) < 0.03 and \
                abs(e.y / self.size[1] - socket[1]) < 0.03:
            return True
        return False
  
    def mouse_click(self, e):	# Runs when user clicks on the Photograph
        self.msg_intro()
        if self.ph == None:
            self.msgwin.msg('Connection NOT established', 'red')
            return

        if self.selected(self.adctl,e):
            self.msg_adcdisp()
            if self.dispadc == False:
                self.dispadc = True
                self.canvas.itemconfigure(self.adblock,fill='yellow')
                for i in range(4):
                    tl = self.w2s(0.65, self.adcs[i][1] - .03)
                    rb = self.w2s(0.76, self.adcs[i][1] + .03)
                    self.adcbox[i] = self.canvas.create_rectangle(tl[0], tl[1], rb[0], rb[1], fill = 'white')
                    txy = self.w2s(0.655, self.adcs[i][1])
                    self.adctext[i] = self.canvas.create_text(txy, text = 'TEXT', fill = 'black', anchor = 'w')
            else:
                self.dispadc = False
                self.canvas.itemconfigure(self.adblock,fill='white')
                for i in range(4):
                      self.canvas.delete(self.adcbox[i])
                      self.canvas.delete(self.adctext[i])
            return

        for k in range(4):			# ADC Inputs
            if self.selected(self.adcs[k], e):
              self.msg_adc()
              if self.adcs[k][3] == 0:		# Not selected
                self.adcs[k][3] = 1		# Add trace
                self.calc_numchans()
                self.canvas.itemconfigure(self.adcs[k][2], fill = 'white')
                self.ph.add_channel(k)
              else:
                self.adcs[k][3] = 0	# Remove trace
                self.calc_numchans()
                self.canvas.itemconfigure(self.adcs[k][2], fill = 'black')
                self.ph.del_channel(k)

        for ls in self.ls_ins:			# Level Shifters
            if self.selected(ls, e):
                  self.msg_adc()
                  ls[3] = ~ls[3] & 1		# Toggle status
                  self.canvas.itemconfigure(ls[2],fill = self.bw[ls[3]] )

        if self.selected(self.pwg, e):		# PWG
            self.msg_pwg()
            if self.dac[3] == 1:		# Either DAC or PWG at a time
                self.msgwin.msg('Remove DAC to use PWG','red')
                return
            if self.pwg[3] == 1:
                self.pwg[3] = 0
                self.remove_pwg()
            else:
                self.add_pwg()
                self.pwg[3] = 1
            self.canvas.itemconfigure(self.pwg[2], fill = self.bw[self.pwg[3]])
            
        if self.selected(self.dac, e):		# DAC
            self.msg_dac()
            if self.pwg[3] == 1:
                self.msgwin.msg( 'Remove PWG to use DAC','red')
                return
            if self.dac[3] == 1:
                self.dac[3] = 0
                self.remove_dac()
            else:
                self.add_dac()
                self.dac[3] = 1
            self.canvas.itemconfigure(self.dac[2], \
                fill = self.bw[self.dac[3]])

        if self.selected(self.cntr, e):		# CNTR
            self.msg_cntr()
            if self.cntr[3] == 1:
                self.cntr[3] = 0
                self.remove_cntr()
            else:
                self.cntr[3] = 1
                self.add_cntr()
            self.canvas.itemconfigure(self.cntr[2],\
                fill = self.bw[self.cntr[3]])

        data = 0			# Digital Outputs
        for k in range(4):	
            if self.selected(self.douts[k], e):
                self.douts[k][3] = (~self.douts[k][3]) & 1	# Toggle status
                self.msg_douts()
                self.canvas.itemconfigure(self.douts[k][2],\
                  fill = self.br[self.douts[k][3]] )
                self.msgwin.showtext('\nYou just toggled D%d'%(k))
            data = data | (self.douts[k][3] << k)        
        self.ph.write_outputs(data)
    

        for k in range(4):			# Digital Inputs
            if self.selected(self.dins[k], e):
                self.msg_dins()
                tp = self.ph.multi_r2rtime(k,99)
                if tp > 0:
                  fr = 1.0e8/tp
                else:
                  fr = 0.0
                self.msgwin.showtext('\nFrequency = %5.2f Hz'%(fr))

        if self.selected(self.cmp, e):
            self.msg_cmp()
            tp = self.ph.multi_r2rtime(4,99)
            if tp > 0:
              fr = 1.0e8/tp
            else:
              fr = 0.0
            self.msgwin.showtext('\nFrequency = %5.2f Hz'%(fr))

# No action other than help message required for the following items

        if self.selected(self.ccs, e):
            self.msg_ccs()

        if self.selected(self.dc5v, e):
            self.msg_dc5v()


        for k in range(2):			# Inverting Amps
            if self.selected(self.ia_in[k], e) or \
               self.selected(self.ia_out[k], e) or \
               self.selected(self.ia_gb[k], e) or \
               self.selected(self.ia_ge[k], e):
                 self.msg_ia()

                                               # Non Inverting amp
        if self.selected(self.nia_in, e) or \
               self.selected(self.nia_out, e) or \
               self.selected(self.nia_gb, e) or \
               self.selected(self.nia_ge, e):
                 self.msg_nia()
        
        for k in range(4):			# GND Sockets
            if self.selected(self.gnds[k], e):
                self.msg_gnds()
        
        self.canvas.pack()	
#---------------------Panel Click action ends here-----------------

    def update(self):
        try:
            data = self.ph.read_inputs() & 15	# Digital Inputs
        except:
            self.msgwin.msg('Connection NOT established', 'red')

        if self.dispadc == True:
            for i in range(2): 
                self.ph.select_adc(i)
                if self.ls_ins[i][3]:
                   ss = '%4.2f V'%(self.ph.get_voltage_bip()[1]*0.001)
                else:
                   ss = '%4.2f V'%(self.ph.get_voltage()[1]*0.001)
                self.canvas.itemconfigure(self.adctext[i],text = ss)
            for i in range(2,4): 
                self.ph.select_adc(i)
                ss = '%4.2f V'%(self.ph.get_voltage()[1]*0.001)
                self.canvas.itemconfigure(self.adctext[i],text = ss)

        if data != self.digin_data:
            self.digin_data = data
            for k in range(4):	
                self.canvas.itemconfigure(self.dins[k][2],  \
                fill = self.by[(data >> k) & 1])
            self.canvas.pack()
            
            
        cs = ~self.ph.read_acomp() & 1	# returns 1 when socket if < 1.23 V 
        if cs != self.cmp_stat:
            self.cmp_stat = cs 
            self.canvas.itemconfigure(self.cmp[2], fill = self.by[cs])
            self.canvas.pack()

        if self.updating == False:  return
          
        have_work = False
        for k in self.adcs:
          if k[3] == 1:
            have_work = True
 
        if have_work == False:	# No channels selected
          return

        try:
          self.phdata = self.ph.multi_read_block(self.NP, self.adc_delay, 0)
          if self.phdata == None:
                return
        except:
          return
 
        ch = 0;
        for k in range(4):			# Copy data to traces
            adc = self.adcs[k]
            if self.adcs[k][3] == 1:		# Active channel
                if k < 2:			# CH0 or CH1
                    for xy in range(self.NP):
	                if self.ls_ins[k][3] == 0:	# No Level Shifter
    		            self.adcs[k][5][xy] = (self.phdata[xy][0]/1000.0,\
                               self.phdata[xy][ch+1]/1000.0 )
                        else:
    		            self.adcs[k][5][xy] = (self.phdata[xy][0]/1000.0, \
    		              5.0 - 2.0 * self.phdata[xy][ch+1]/1000.0 )
                else:
	            for xy in range(self.NP):
    		            self.adcs[k][5][xy] = (self.phdata[xy][0]/1000.0,\
                               self.phdata[xy][ch+1]/1000.0)
                ch = ch + 1
        
        self.plot2d.delete_lines()
        for adc in self.adcs:
            if adc[3] == 1:			# Active channel
                self.plot2d.line(adc[5], adc[4])
  
#-----------------------------------------------------------------------
    def start(self,e):
        self.updating = True

    def stop(self,e):
        self.updating = False

    def save_all(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.traces = []
        for adc in self.adcs:
            if adc[3] == 1:			# Active channel
              self.data.traces.append(adc[5])
        self.data.save_all(fname)
        self.msgwin.msg('Data saved to '+ fname)

#-------------------------- Message routines -------------
    def msg_intro(self):
        self.msgwin.clear()
        self.msgwin.showtext('This section helps you to explore ' +\
          'the different building blocks of PHOENIX. ')
        self.msgwin.showtext('Click on the different Sockets ', 'blue')
        self.msgwin.showtext('shown in  the Photograph to explore the system. '+\
        'Select "Connect" from the main before proceeding.\n'+\
        'From here you can use Phoenix as a test equipment also. The ADC '+\
        'inputs acts like CRO inputs. Digital Inputs can measure frequency '+\
        'of the 0-5V range signals etc.')
    def msg_ccs(self):
        self.msgwin.clear()
        self.msgwin.showtext('This socket gives a constant current of 1 mA ' +\
          'to load resistors upto 1KOhm. This feature is mainly used for ' +\
          'temperature measurements using RTD sensor elements.')

    def msg_cmp(self):
        self.msgwin.clear()
        self.msgwin.showtext('This is one of the Inputs of an Analog ' +\
          'Comparator. The other input is internally connected to around ' +\
          '1.2 V. Connect the DAC output to CMP input and change the DAC ' +\
          'slider to see how it works. Similar to Digital Input Sockets ' +\
          'the CMP input also is used for time interval measurements. '+\
          'Clicking on CMP Socket will try to measure the '+\
          'frequency of the signal at the input. If nothing is connected '+\
          'the message will be displayed after the timeout and you will '+\
          'notice a delay.')

    def msg_dc5v(self):
        self.msgwin.clear()
        self.msgwin.showtext('Phoenix is powered by an unregulated 9V DC ' +\
          'supply which is regulated inside. The 5V DC output is available ' +\
          'on the Panel. This can only supply around 100 mA of current.')

    def msg_pwg(self):
        self.msgwin.clear()
        self.msgwin.showtext('The Programmable Waveform Generator ' +\
          'gives a square wave output. Frequency can be varied '+\
          'between 13 Hz and 10 KHz using the slider. ' +\
          'ou cannot set it to all the Using the set_frequency() '+\
          'function in the Phoenix library you can set it up to 4 MHz.'+\
          'The voltage level swings between 0 and 5V. To view this ' +\
          'waveform connect a wire from PWG to CH0 and click on CH0 to ' +\
          'add that channel to the plot window. Move the slider and ' +\
          'see the result. Frequency cannot be set to all the values '+\
          'between the limits due to the nature of the hardware.\n\n')

        self.msgwin.showtext('You can play a tune on PWG. Connect the '+\
        'Loudspeaker from PWG to Ground (with 100 Ohm series resisitor).\n'+\
        'Quit this and run /docs/phoenix/applications/TerminalProgs/jan.py')

    def msg_dac(self):
        self.msgwin.clear()
        self.msgwin.showtext('The DAC socket can be programmed to give ' +\
          'a DC voltage between 0 to 5V. The DAC us implemented using ' +\
          'Pulse Width Modulation technique. In fact the DAC socket is ' +\
          'the PWG socket output filtered. When in DAC mode the PWG will ' +\
          'show a 31.25 KHz squarewave whose duty cycle varies with the '+\
          'DAC voltage setting. Due to this reason, you can only use PWG '+\
          'or DAC at a time.')

    def msg_cntr(self):
        self.msgwin.clear()
        self.msgwin.showtext('Measures the frequency of the 5V signal ' +\
          'connected at the input. To test this feature, connect PWG output ' +\
          'to CNTR input using a cable and set PWG to some value. ' +\
          'CNTR can be updated by clicking on the currently displyed ' +\
          'frequency value.')

    def msg_douts(self):
        self.msgwin.clear()
        self.msgwin.showtext('The Digital Output Sockets can be made ' +\
          '0V or 5V under software control. Under this program, clicking ' +\
          'on a socket will toggle the state. The output D0 alone is' +\
          'transistor buffered to drive upto 100 mA. The rest can drive ' +\
          'only upto 5 mA ')

    def msg_dins(self):
        self.msgwin.clear()
        self.msgwin.showtext('The voltage level at the Digital Input sockets ' +\
          'can be read through software. This program makes the socket hole ' +\
          'Yellow if it is conencted to 0V, floating inputs are 5 Volts. ' +\
          'Connect one socket to GND using a cable and watch the picture above. ' +\
          'The time between voltage level transitions at Digital Input'+\
          'Sockets can be measured with microsecond accuracy. '+\
          'Clicking on a Digital input Socket will try to measure the '+\
          'frequency of the signal at the input. If nothing is connected '+\
          'the message will be displayed after the timeout and you will '+\
          'notice a delay.')


    def msg_ls(self):
        self.msgwin.clear()
        self.msgwin.showtext('Level Shifting Amplifiers convert a -5V to 5V ' +\
          'range signal to a 0 to 5V range signal. A 5V DC value is added ' +\
          'to the input signal and the amplitude is reduced to half. With ' +\
          'the input connected to GND, the output will be nearly 2.5 Volts.')


    def msg_ia(self):
        self.msgwin.clear()
        self.msgwin.showtext(' ' +\
          'The variable gain Inverting Amplifiers are implemented using '+\
          'TL084 op-amps. ' +\
          'The Feedback Resistor (Rf)is fixed at 10 KOhms. The Input Resistor ' +\
          '(Ri) can be changed by the user to adjust the gain. Remember that ' +\
          'TL084 Op-amps have around 2mV (multiply by the gain) offset. The '+\
          'Op-amps inside Phoenix are powered by a charge pump generating '+\
          '+8V and -7V supplies from +5V. The output of these amplifiers ' +\
          'saturate at nearly 5V due to this reason.')

    def msg_nia(self):
        self.msgwin.clear()
        self.msgwin.showtext(' ' +\
          'The variable gain Non-Inverting Amplifiers are implemented using '+\
          'LM358 op-amp, which is good only for low frequencies. ' +\
          'The Feedback Resistor (Rf)is fixed at 10 KOhms. The Gain Selection '+\
          ' Resistor (Rg) can be changed by the user to adjust the gain. '+\
          'Op-amps inside Phoenix are powered by a charge pump generating '+\
          '+8V and -7V supplies from +5V. The output of these amplifiers ' +\
          'saturate at nearly 5V due to this reason.')

    def msg_gnds(self):
        self.msgwin.clear()
        self.msgwin.showtext('These sockets are at zero volts. ')

    def msg_adcdisp(self):
        self.msgwin.clear()
        self.msgwin.showtext('Clicking on the white square below the ADC Inputs '+\
          'starts the display of voltages at the Analog inputs CH0 to CH3. '+\
          'Click again to stop displaying the voltage valeus.')

    def msg_adc(self):
        self.msgwin.clear()
        self.msgwin.showtext('The voltage input at the ADC input channels can be ' +\
          'read through software. They can be read at regular time intervals and ' +\
          'plotted to get the input waveform. Four channels are available. Clicking '+\
          'on the sockets will Enable/Disable scanning. The ADC inputs must be '+\
          'between 0 to 5V. However, CH0 and CH1 can be used with the level shifting '+\
          'amplifiers to display a -5V to 5V range signal. To display the voltage '+\
          'at the Level Shifter Input Socket click on it. The functions available are:\n')
        self.msgwin.showlink('Stop Scanning', self.stop)
        self.msgwin.showtext(' To stop updating.\n')
        self.msgwin.showlink('Save Traces', self.save_all)
        self.msgwin.showtext(' to save the data to text file named ') 
        self.fntext =  Entry(None, width =20, fg = 'red')
        self.msgwin.showwindow(self.fntext)
        self.fntext.insert(END,'cro.dat')
        self.msgwin.showtext('\n')
        self.msgwin.showlink('Start Scanning', self.start)
        self.msgwin.showtext('  To start updating again')
        self.msgwin.showtext('\nAdjust the time axis using the slider ')
        if self.adc_scale != None:
            self.adc_scale.destroy()
        self.adc_scale =  Scale(None, command = self.set_adc_delay, \
          from_ = 0, to=6, orient=HORIZONTAL, length=100, showvalue=0)
        for i in range(len(self.delay_vals)):
          if self.delay_vals[i] == self.adc_delay:
            self.adc_scale.set(i)
        self.msgwin.showwindow(self.adc_scale)
        self.msgwin.showtext('. What your are changing is the time interval '+\
         'between digitizations.')
Example #50
0
class diode:
    NP = 500
    looping = False
    xlabel = 'Volts'
    ylabel = 'mA'
    size = [100,100]
    dac_voltage = 0.0
    adc_voltage = 0.0
    step = 39.0
    
    def __init__(self, parent, size, plot, msg):
        self.parent = parent
        self.plot2d = plot
        self.msgwin = msg
        x = Image.open(abs_path(photograph))
        im = x.resize(size)
        self.size[0] = float(size[0])
        self.size[1] = float(size[1])
        self.image = ImageTk.PhotoImage(im)
        self.canvas = Canvas(parent, width = size[0], height = size[1])
        self.canvas.create_image(0,0,image = self.image, anchor = NW)
        self.data = Data()

    def enter(self, p):	
      self.ph = p 
      self.ph.select_adc(0)
      self.ph.set_adc_size(2)
      self.plot2d.setWorld(0, 0, 5, 5)
      self.plot2d.mark_axes(self.xlabel, self.ylabel)
      self.intro_msg()

    def exit(self):
      self.looping = False

    def clear(self,e):
        self.data.clear()
        self.plot2d.delete_lines()  
              
    def save(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save(fname)
        self.msgwin.msg('Data saved to '+ fname)

    def analyze(self,e):
        self.data.analyze(self.xlabel, self.ylabel)

    def save_all(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save_all(fname)
        self.msgwin.msg('Data saved to '+ fname)

    def show_all(self,e):
        self.plot2d.delete_lines()  
        self.data.index = 0
        for tr in self.data.traces:
          self.plot2d.line(tr,self.data.get_col())
#------------------------------------------------------------------        
    def start(self,e):
        if self.ph == None:
            self.msgwin.msg('Connection not made yet', 'red')
            return
        if self.looping == False:    # Same color if restarted in between
        	self.col = self.data.get_col()
        self.msgwin.msg('Starting Diode IV measurement')
        self.data.points = []
	self.looping = True
	self.dac_voltage = 0.0
	
    def accept_trace(self):
        self.data.traces.append(self.data.points)

    def update(self):
        if self.ph == None:
          return
        if self.looping != True:
          return
	self.ph.set_voltage(self.dac_voltage)
	time.sleep(0.05)
	self.adc_voltage = self.ph.get_voltage()[1]
	      # voltage and current converted into volts
	current = (self.dac_voltage - self.adc_voltage)/1000.0
        self.data.points.append( (self.adc_voltage/1000.0, current))
        if len(self.data.points) < 2:
          return
        self.plot2d.delete_lines()
        self.plot2d.line(self.data.points, self.col)
        self.dac_voltage = self.dac_voltage + self.step
        if self.dac_voltage > 5000:
          self.msgwin.msg('IV Plot Done')
          self.accept_trace()
          self.looping = False

    def intro_msg(self):
        self.clear(None)
        self.msgwin.clear()
        self.msgwin.showtext('Make the connections as shown above. The '+\
        'software changes the DAC voltage from 0 to 5V in steps and '+\
        'measure the resulting voltage across the diode. The current is '+\
        'calculated from the voltages at DAC and CH0, both are known. '+\
        'Connect a 1 uF capacitor from CH0 to ground for better results.'+\
        'You can take multiple traces. The operations are:\n')
        
        self.msgwin.showlink('Start', self.start)
        self.msgwin.showtext(' Start making the IV plot\n')
        self.msgwin.showlink('Analyze', self.analyze)
        self.msgwin.showtext(' the data using Xmgrace.\n')

        self.msgwin.showlink('Save the latest trace', self.save)
        self.msgwin.showtext(' or ')
        self.msgwin.showlink('Save all Traces', self.save_all)
        self.msgwin.showtext(' to a file named ')
        self.fntext =  Entry(None, width =20, fg = 'blue')
        self.msgwin.showwindow(self.fntext)
        self.fntext.insert(END,'iv.dat')

        self.msgwin.showlink('Display all Traces', self.show_all)
        self.msgwin.showtext(' Shows the data collected so far\n')
        self.msgwin.showlink('Clear all Traces', self.clear)
        self.msgwin.showtext(' Clears all the data ')
Example #51
0
class tran:
    NP = 200
    adc_delay = 250
    numchans = 2
    xlabel = 'milli seconds'
    ylabel = 'Volts'
    size = [350.0, 272.0]

    def __init__(self, parent, size, plot, msg):
        self.parent = parent
        self.plot2d = plot
        self.msgwin = msg
        x = Image.open(abs_path(photograph))
        im = x.resize(size)        

        self.size[0] = float(im.size[0])
        self.size[1] = float(im.size[1])
        self.image = ImageTk.PhotoImage(im)
        self.canvas = Canvas(parent, width = im.size[0], height = im.size[1])
        self.canvas.create_image(0,0,image = self.image, anchor = NW)
        self.data = Data()

    def enter(self, fd):
        self.ph = fd
        self.ph.set_adc_delay(self.adc_delay)
        self.plot2d.setWorld(0, -5, self.NP * self.adc_delay/1000.0, 5)
        self.plot2d.mark_axes(self.xlabel, self.ylabel, self.numchans)
        
        self.ph.add_channel(0)
        self.ph.add_channel(1)
        self.ph.del_channel(2)
        self.ph.del_channel(3)

        self.msg_intro()
	v = []
        for i in range(100):
	    x = 127.5 + 127.5 * math.sin(2.0*math.pi*i/100)
	    x = int(x+0.5)
	    v.append(x)
	self.ph.load_wavetable(v)	
	res = self.ph.start_wave(20)
	s = 'DAC is set to generate %3.1f Hz Sine Wave'%(res)
	self.msgwin.msg(s)
        self.updating = True

    def exit(self):
       self.updating = False
       self.ph.stop_wave()
       for k in range(4):
           self.ph.del_channel(k)

#--------------------------------------------------------------------

    def update(self):
        if self.ph == None:
            return
        phdata = self.ph.multi_read_block(self.NP, self.adc_delay, 0)
        if phdata == None:
            return

        self.data.points = []
        self.data.points2 = []
        for pt in phdata:
            self.data.points.append( (pt[0]/1000.0, 5.0 - 2.0 * pt[1]/1000.0) )
            self.data.points2.append((pt[0]/1000.0, 5.0 - 2.0 * pt[2]/1000.0) )
        self.plot2d.delete_lines()
        self.plot2d.line(self.data.points, 'black')
        self.plot2d.line(self.data.points2, 'red')

    def save_all(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.traces = []
        self.data.traces.append(self.data.points)
        self.data.traces.append(self.data.points2)
        self.data.save_all(fname)
        self.msgwin.msg('Data saved to '+ fname)
          
#-------------------------- Message routines -------------
    def msg_intro(self):
        self.msgwin.clear()
        self.msgwin.showtext('Primary Coil is connected to DAC (producing '+\
        'a 50 Hz signal) through a series capacitor. Primary is monitored by CH1. '+\
        'and secondary coil is monitored by CH0. The primary waveform will be seen as a'+\
        'sinewave. Watch the changes in the secondary waveform by changing the distance between '+\
        'coils and adding the ferrite core.\n')
        self.msgwin.showlink('Save Traces', self.save_all)
        self.msgwin.showtext('  Saves both the voltage waveforms to text file named ') 
        self.fntext =  Entry(None, width =20, fg = 'red')
        self.msgwin.showwindow(self.fntext)
        self.fntext.insert(END,'trans.dat')
Example #52
0
class rad:
    xmax  = 255
    ymax  = 100
    xlabel = 'Channel Number'
    ylabel = 'Count'
    size = [100,100]
    running = False
    scale = None
      
    def __init__(self, parent, size, plot, msg):
        self.parent = parent
        self.plot2d = plot
        self.msgwin = msg
        x = Image.open(abs_path(photograph))
        im = x.resize(size)
        self.size[0] = float(size[0])
        self.size[1] = float(size[1])
        self.image = ImageTk.PhotoImage(im)
        self.canvas = Canvas(parent, width = size[0], height = size[1])
        self.canvas.create_image(0,0,image = self.image, anchor = NW)
        self.data = Data()

    def enter(self, p):	
      self.ph = p 
      self.ph.clear_hist()
      self.intro_msg()
      self.plot2d.setWorld(0, 0, self.xmax, self.ymax)	
      self.plot2d.mark_axes(self.xlabel,self.ylabel)
      self.plot2d.markerval = None	# Clear Markers
      self.calibrated = False
      try:
         self.plot2d.canvas.delete(self.plot2d.markertext)
      except:
         pass
      

    def exit(self):
      self.running = False
      
    def clear(self,e):
        self.plot2d.delete_lines()  
        self.data.clear()

    def set_ymax(self,w):
        d = self.scale.get()    
        self.ymax = float(d)
        self.plot2d.setWorld(0, 0, self.xmax, self.ymax)	
        self.plot2d.mark_axes(self.xlabel,self.ylabel)

    def collect_hist(self, e):
        if self.running == False:
            return
        phdata = self.ph.read_hist()
        self.plot2d.setWorld(0, 0, self.xmax, self.ymax)
        self.plot2d.mark_axes(self.xlabel, self.ylabel)
        self.data.points = []
        for k in phdata:
          energy = k[0] * self.xmax / 255.0
          self.data.points.append( (energy, k[1]) )
        self.plot2d.delete_lines()  
        self.plot2d.line(self.data.points, 'black')
#        self.data.traces.append(self.data.points)
        self.last_read_time = time.time()
        
    def update(self):
        if self.running == False:
            return
        self.collect_hist(0)

    def start(self,w):
        self.ph.start_hist()
        self.running = True
        self.msgwin.msg('Started Collecting Data')
        
    def stop(self,w):
        self.running = False
        self.ph.stop_hist()
        self.msgwin.msg('Stopped Collecting Data')
        
    def clear_hist(self,w):
        self.ph.clear_hist()
        self.plot2d.delete_lines()  
        self.msgwin.msg('Cleared Histogram Data')
        
    def save(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save(fname)
        self.msgwin.msg('Data saved to '+ fname)

    def calibrate(self,e):
        if self.plot2d.markerval == None:
          self.msgwin.msg('Mark a Peak before calibration','red')
          return  
        try:
          chan = self.plot2d.markerval[0]
          s = self.energytext.get()
          energy = float(s)
          self.xmax = self.xmax * energy / chan
          self.xlabel = 'Energy (MeV)'
          self.plot2d.setWorld(0, 0, self.xmax, self.ymax)	
          self.plot2d.mark_axes(self.xlabel,self.ylabel)
          self.calibrated = True
          self.plot2d.canvas.delete(self.plot2d.markertext)
          self.plot2d.markerval = None
          self.msgwin.msg('Calibration done')
        except:
          self.msgwin.msg('Error occured during calibration','red')

    def autoscale(self,e):
        ymax = 10.0
        for pt in self.data.points:
          if pt[1] > ymax:
            ymax = pt[1]
        self.ymax = ymax * 1.1
        self.plot2d.setWorld(0, 0, self.xmax, self.ymax)	

    def intro_msg(self):
        self.clear(None)
        self.msgwin.clear()
        self.msgwin.showtext('Connections: (1) Blue - CH0 (2) Black - GND '+\
        '(3)Green - D3out (4) Yellow - CMP\n','red')
        self.msgwin.showtext('Connect the Radiation Detection Accessoy as '+\
        'shown in the figure.\n')
        self.msgwin.showlink('Start', self.start)
        self.msgwin.showtext(' Collecting. ')
        self.msgwin.showlink('Stop', self.stop)
        self.msgwin.showtext(' Stop Collecting. ')
        self.msgwin.showlink('Update', self.collect_hist)
        self.msgwin.showtext(' the Display. ')
        self.msgwin.showlink('Clear', self.clear_hist)
        self.msgwin.showtext(' data and Display. ')       
        self.msgwin.showlink('Save Histogram', self.save)
        self.msgwin.showtext(' to file ') 
        self.fntext =  Entry(None, width =15, fg = 'red')
        self.msgwin.showwindow(self.fntext)
        self.fntext.insert(END,'hist.dat')
        self.msgwin.showtext('\n')
        
        self.scale =  Scale(None, command = self.set_ymax, resolution = 100,\
          from_ = 100, to=10000, orient=HORIZONTAL, length=100, showvalue=0)
        self.scale.set(self.ymax)
        self.msgwin.showwindow(self.scale)
        self.msgwin.showtext(' Change the Vertical Axis. For auto scaling ')
        self.msgwin.showlink('Click Here\n', self.autoscale)

        self.msgwin.showtext(' To calibrate, click on a known peak, enter '+\
        ' the corresponding energy ')       
        self.energytext =  Entry(None, width =10, fg = 'red')
        self.msgwin.showwindow(self.energytext)
        self.energytext.insert(END,'5.485')
        self.msgwin.showtext(' MeV and ')
        self.msgwin.showlink('Click Here\n', self.calibrate)
Example #53
0
class rodpend:
    NP = 400
    tmax = 10.0		# We digitize oscillations for 10 seconds
    maxcount = 10
    looping = False
    xlabel = 'Number'
    ylabel = 'g'
    size = [100,100]
    scale = None    
    
    def __init__(self, parent, size, plot, msg):
        self.parent = parent
        self.plot2d = plot
        self.msgwin = msg
        x = Image.open(abs_path(photograph))
        im = x.resize(size)
        self.size[0] = float(size[0])
        self.size[1] = float(size[1])
        self.image = ImageTk.PhotoImage(im)
        self.canvas = Canvas(parent, width = size[0], height = size[1])
        self.canvas.create_image(0,0,image = self.image, anchor = NW)
        self.data = Data()

    def enter(self, p):	
      self.ph = p 
      self.ph.select_adc(0)
      self.ph.set_adc_size(2)
      self.intro_msg()
      self.plot2d.setWorld(0, -5, self.tmax, 5)	
      self.plot2d.mark_axes(self.xlabel,self.ylabel)

    def exit(self):
      self.looping = False
      
    def clear(self,e):
        self.plot2d.delete_lines()  
        self.data.clear()

    def save(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save(fname)
        self.msgwin.msg('Data saved to '+ fname)

    def analyze(self,e):
        self.data.analyze(self.xlabel, self.ylabel)

    def save_all(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save_all(fname)
        self.msgwin.msg('Data saved to '+ fname)

    def show_all(self,e):
        self.plot2d.delete_lines()  
        self.data.index = 0
        for tr in self.data.traces:
          self.plot2d.line(tr,self.data.get_col())
#------------------------------------------------------------------        
        
    def start(self,e):
        x = self.lentext.get()
        try:
            self.length = float(x)
        except:
            self.msgwin.msg('Set the length of the Pendulum', 'red')
            return

        x = self.numtext.get()
        try:
            self.maxcount = int(x)
        except:
            self.maxcount = 10
        self.tmax = self.maxcount

        if self.ph == None:
            self.msgwin.msg('Connection not made yet', 'red')
            return
        if self.looping == False:    		# Same color if restarted
        	self.col = self.data.get_col()
        self.msgwin.msg('Starting Measurement of gravity using Rod Pendulum')
	self.count = 0
        t = self.ph.pendulum_period(3)	# Every alternate rising edge
#	t = self.ph.multi_r2rtime(3,1)
	if t < 0:
          self.msgwin.msg('Pendulum Period Timeout Error', 'red')
          return
        t = t/1000000.0
        self.pisqr = math.pi ** 2
        g = 4.0 * self.pisqr * 2.0 * self.length / (3.0 *  t * t)
        self.msgwin.showtext('\nPeriod    Gravity')
        self.msgwin.showtext('\n%6.5f\t%4.1f'%(t,g))
#        print self.pisqr, self.length, t, g
        self.data.points = [(self.count, g)]
        self.plot2d.setWorld(0, 0, self.tmax, g*1.2)	
        self.plot2d.mark_axes(self.xlabel,self.ylabel)
	self.looping = True

    def accept_trace(self):
        self.data.traces.append(self.data.points)

    def update(self):
        if self.looping == False:
            return
        t = self.ph.pendulum_period(3)	# Every alternate rising edge
        if t < 0:
          self.msgwin.msg('Pendulum Period Timeout Error', 'red')
          self.looping = False
          return
        print t
        t = t/1000000.0
        self.pisqr = math.pi ** 2
        g = 4.0 * self.pisqr * 2.0 * self.length / (3.0 *  t * t)
        self.data.points.append( (self.count, g) )
        self.plot2d.delete_lines()
        self.plot2d.line(self.data.points, self.col)
        self.msgwin.showtext('\n%6.5f\t%4.1f'%(t,g))
        self.count = self.count + 1
        if self.count >= self.maxcount:
          self.looping = False
          self.msgwin.msg('Measured gravity %d times'%self.count)
          self.accept_trace()
        time.sleep(t/2)		# Always measure in the same direction

    def refresh(self,e):
        self.intro_msg()

    def intro_msg(self):
#        self.clear(None)
        self.msgwin.clear()
        self.msgwin.showtext('Connect the Light Barrier as shown in the '+\
        'photograph and set the pendulum oscillating. Phoenix will measure '+\
        'the Period of the pendulum and calculate the value of acceleration '+\
        'due to gravity. Measure the length of the pendulum and enter it '+\
        'The pendulum must be vertical in its resting position.\n')

        self.msgwin.showtext('Oscillate the Pendulum and ')
        self.msgwin.showlink('Click Here', self.start)
        self.msgwin.showtext(' to start the measurements.')
        
        self.msgwin.showtext(' Length = ')
        self.lentext =  Entry(None, width = 5, fg = 'red')
        self.msgwin.showwindow(self.lentext)
        self.lentext.insert(END,'7.0')
        self.msgwin.showtext(' cm. Number of measurements = ')
        self.numtext =  Entry(None, width = 5, fg = 'red')
        self.msgwin.showwindow(self.numtext)
        self.numtext.insert(END,str(self.maxcount))

        self.msgwin.showlink('\nSave the latest trace', self.save)
        self.msgwin.showtext(' or ')
        self.msgwin.showlink('Save all Traces', self.save_all)
        self.msgwin.showtext(' to a text file named ')
        self.fntext =  Entry(None, width =15, fg = 'red')
        self.msgwin.showwindow(self.fntext)
        self.fntext.insert(END,'rodpend.dat')
        self.msgwin.showtext(' or ')
        self.msgwin.showlink('Analyze', self.analyze)
        self.msgwin.showtext(' data online using Xmgrace.\n')
        self.msgwin.showlink('Display all Traces', self.show_all)
        self.msgwin.showtext(' , ')
        self.msgwin.showlink('Clear all Traces', self.clear)
        self.msgwin.showtext(' or ')
        self.msgwin.showlink('Refresh This Window', self.refresh)
        self.msgwin.showtext('\nCalculated g will be plotted.')
Example #54
0
class tran:
    NP = 200
    adc_delay = 250
    numchans = 2
    xlabel = 'milli seconds'
    ylabel = 'Volts'
    size = [350.0, 272.0]

    def __init__(self, parent, size, plot, msg):
        self.parent = parent
        self.plot2d = plot
        self.msgwin = msg
        x = Image.open(abs_path(photograph))
        im = x.resize(size)        

        self.size[0] = float(im.size[0])
        self.size[1] = float(im.size[1])
        self.image = ImageTk.PhotoImage(im)
        self.canvas = Canvas(parent, width = im.size[0], height = im.size[1])
        self.canvas.create_image(0,0,image = self.image, anchor = NW)
        self.data = Data()

    def enter(self, fd):
        self.ph = fd
        self.ph.set_adc_delay(self.adc_delay)
        self.plot2d.setWorld(0, -5, self.NP * self.adc_delay/1000.0, 5)
        self.plot2d.mark_axes(self.xlabel, self.ylabel, self.numchans)
        
        self.ph.add_channel(0)
        self.ph.add_channel(1)
        self.ph.del_channel(2)
        self.ph.del_channel(3)

        self.msg_intro()
	v = []
        for i in range(100):
	    x = 127.5 + 127.5 * math.sin(2.0*math.pi*i/100)
	    x = int(x+0.5)
	    v.append(x)
	self.ph.load_wavetable(v)	
	res = self.ph.start_wave(20)
	s = 'DAC is set to generate %3.1f Hz Sine Wave'%(res)
	self.msgwin.msg(s)
        self.updating = True

    def exit(self):
       self.updating = False
       self.ph.stop_wave()
       for k in range(4):
           self.ph.del_channel(k)

#--------------------------------------------------------------------

    def update(self):
        if self.ph == None:
            return
        phdata = self.ph.multi_read_block(self.NP, self.adc_delay, 0)
        if phdata == None:
            return

        self.data.points = []
        self.data.points2 = []
        for pt in phdata:
            self.data.points.append( (pt[0]/1000.0, 5.0 - 2.0 * pt[1]/1000.0) )
            self.data.points2.append((pt[0]/1000.0, 5.0 - 2.0 * pt[2]/1000.0) )
        self.plot2d.delete_lines()
        self.plot2d.line(self.data.points, 'black')
        self.plot2d.line(self.data.points2, 'red')

    def save_all(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.traces = []
        self.data.traces.append(self.data.points)
        self.data.traces.append(self.data.points2)
        self.data.save_all(fname)
        self.msgwin.msg('Data saved to '+ fname)
          
#-------------------------- Message routines -------------
    def msg_intro(self):
        self.msgwin.clear()
        self.msgwin.showtext('In this experiment, the DAC socket (producing '+\
        'a 50 Hz signal) is connected one end of a coil (Other end to GND) '+\
        'through a series capacitor. The same is also connected to CH1 via '+\
        'a level shifter for monitoring the primary waveform. '+\
        'Another coil (Secondary) is connected between Ground '+\
        'and the input of another level shifter, and its output '+\
        'is connected to CH0. Now you should see the primary waveform '+\
        'as a sinewave and the secondary as a horizontal line. Keep the '+\
        'coils close and note changes in secondary waveform. Pack them '+\
        'with ferrite core to see the effect of increased magnetic coupling\n')
        self.msgwin.showlink('Save Traces', self.save_all)
        self.msgwin.showtext('  Saves both the voltage waveforms to text file named ') 
        self.fntext =  Entry(None, width =20, fg = 'red')
        self.msgwin.showwindow(self.fntext)
        self.fntext.insert(END,'trans.dat')
        self.msgwin.showtext('\n\nTry changing the orientation of the coils, removing the ferrite '+\
        'etc. to study the effect of them')
Example #55
0
class pt100:
    NP = 400
    delay = 0.03	# minimum delay between voltage reads
    tmax = 12.0		# Number of seconds for NP reads
    looping = False
    xlabel = 'Seconds'
    ylabel = 'Kelvin'
    gain = 11.0		# Assume PT100 output is amplified by 11
    ccval = 1.0		# CCS nominal value is 1 mA
    maxtemp = 800.0
    size = [100,100]
    del_scale = None    
    np_scale = None    
    
    def __init__(self, parent, size, plot, msg):
        self.parent = parent
        self.plot2d = plot
        self.msgwin = msg
        x = Image.open(abs_path(photograph))
        im = x.resize(size)
        self.size[0] = float(size[0])
        self.size[1] = float(size[1])
        self.image = ImageTk.PhotoImage(im)
        self.canvas = Canvas(parent, width = size[0], height = size[1])
        self.canvas.create_image(0,0,image = self.image, anchor = NW)
        self.data = Data()

    def enter(self, p):	
      self.ph = p 
      self.ph.select_adc(0)
      self.ph.set_adc_size(2)
      self.intro_msg()
      self.tmax = self.delay * self.NP
      self.plot2d.setWorld(0, 0, self.tmax, self.maxtemp)	
      self.plot2d.mark_axes(self.xlabel,self.ylabel)

    def exit(self):
      self.looping = False
      
    def set_delay(self,w):
        if self.looping:
          return
        d = self.del_scale.get()    
        self.delay = float(d)/1000.0
        self.plot2d.setWorld(0, 0, self.NP * self.delay, self.maxtemp)	
        self.plot2d.mark_axes(self.xlabel,self.ylabel)
        if len(self.data.points) < 2:
          return
        self.plot2d.delete_lines()
        self.plot2d.line(self.data.points, self.col)
        
    def set_NP(self,w):
        d = self.np_scale.get()    
        self.NP = d
        self.plot2d.setWorld(0, 0, self.NP * self.delay, self.maxtemp)	
        self.plot2d.mark_axes(self.xlabel,self.ylabel)
        if len(self.data.points) < 2:
          return
        self.plot2d.delete_lines()
        self.plot2d.line(self.data.points, self.col)
        
    def clear(self,e):
        self.plot2d.delete_lines()  
        self.data.clear()

    def save(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save(fname)
        self.msgwin.msg('Data saved to '+ fname)

    def analyze(self,e):
        self.data.analyze(self.xlabel, self.ylabel)

    def save_all(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save_all(fname)
        self.msgwin.msg('Data saved to '+ fname)

    def show_all(self,e):
        self.plot2d.delete_lines()  
        self.data.index = 0
        for tr in self.data.traces:
          self.plot2d.line(tr,self.data.get_col())
#------------------------------------------------------------------        
        
    def start(self,e):
        if self.ph == None:
            self.msgwin.msg('Connection not made yet', 'red')
            return
        if self.looping == False:    		# Same color if restarted
        	self.col = self.data.get_col()
        self.msgwin.msg('Started Temperature Recording')
	self.looping = True
	self.data.points = []
	
    def stop(self,e):
        self.msgwin.msg('Stopped Recording')
        self.accept_trace()
        self.looping = False

    def accept_trace(self):
        self.data.traces.append(self.data.points)

    def update(self):
        if self.looping == False:
            return
        if self.delay > 0:
          time.sleep(self.delay)
        val = self.ph.get_voltage()
        if self.data.points == []:
          self.start_time = val[0]
        temp = self.mv2cel(val[1])
        self.data.points.append( (val[0]-self.start_time, temp) )
        if len(self.data.points) < 2:
          return
        self.plot2d.delete_lines()
        self.plot2d.line(self.data.points, self.col)
        if len(self.data.points) >= self.NP:
          self.msgwin.msg('Temperature recording finished')
          self.accept_trace()
          self.looping = False

    def mv2cel(self,mv):
        self.offset = 0.0
	mv = (mv - self.offset)/self.gain
	r = mv / self.ccval
        # Convert resistance to temperature for PT100
	r0 = 100.0
	A = 3.9083e-3
	B = -5.7750e-7
	c = 1 - r/r0
	b4ac = math.sqrt( A*A - 4 * B * c)
	t = (-A + b4ac) / (2.0 * B)
	print t 
	return t + 273

    def calibrate(self,e):
        try:
          s = self.gaintext.get()
          self.gain = float(s)
          s = self.cctext.get()
          self.ccval = float(s)
        except:
          self.msgwin.msg('Error occured during calibration','red')

    def intro_msg(self):
        self.clear(None)
        self.msgwin.clear()
        self.msgwin.showtext('Resistance of the PT100 sensor is a function '+\
        'of temperature. The sonsor is connected to a 1mA constant current '+\
        'source an the voltage across the sensor is measured after '+\
        'sufficient amplification.')
        self.msgwin.showlink('Start', self.start)
        self.msgwin.showtext(' Start Recording Temperature')
        self.msgwin.showlink('Stop', self.stop)
        self.msgwin.showtext(' Stop Recording Temperature')

        self.msgwin.showlink('Save the latest trace', self.save)
        self.msgwin.showtext(' or ')
        self.msgwin.showlink('Save all Traces', self.save_all)
        self.msgwin.showtext(' to a file named ')
        self.fntext =  Entry(None, width =20, fg = 'red')
        self.msgwin.showwindow(self.fntext)
        self.fntext.insert(END,'pt100.dat')
        self.msgwin.showtext(' (You can change the filename)\n')

        self.msgwin.showlink('Analyze', self.analyze)
        self.msgwin.showtext(' Process the data using Xmgrace.\n')
        self.msgwin.showlink('Display all Traces', self.show_all)
        self.msgwin.showtext(' to see all the data collected and ')
        self.msgwin.showlink('Clear all Traces', self.clear)
        self.msgwin.showtext(' Clear Everything ')

        self.msgwin.showtext('\nSet Maximum Number of Readings')
        if self.np_scale != None:
            self.np_scale.destroy()
        self.np_scale =  Scale(None, command = self.set_NP, \
          from_ = 10, to=1000, orient=HORIZONTAL, length=100, showvalue = 1)
        self.np_scale.set(self.NP)
        self.msgwin.showwindow(self.np_scale)

        self.msgwin.showtext(' and the Interval between readings ')
        if self.del_scale != None:
            self.del_scale.destroy()
        self.del_scale =  Scale(None, command = self.set_delay, \
          from_ = 30, to=1000, orient=HORIZONTAL, length=100, showvalue=1)
        self.del_scale.set(self.delay)
        self.msgwin.showwindow(self.del_scale)
        self.msgwin.showtext(' milli seconds.\n')

        self.msgwin.showtext('For calibration enter the amplifier gain ')
        self.gaintext =  Entry(None, width =10, fg = 'red')
        self.msgwin.showwindow(self.gaintext)
        self.gaintext.insert(END,'11.0')
        self.msgwin.showtext(' and the CCS value')
        self.cctext =  Entry(None, width =10, fg = 'red')
        self.msgwin.showwindow(self.cctext)
        self.cctext.insert(END,'1.0')
        self.msgwin.showtext('mA and ')
        self.msgwin.showlink('Click Here\n', self.calibrate)
Example #56
0
class induction:
    NP = 200
    adc_delay = 500
    delay_vals = [200,500,1000]
    looping = False
    xlabel = 'milli seconds'
    ylabel = 'Volts'
    size = [100,100]
    
    def __init__(self, parent, size, plot, msg):
        self.parent = parent
        self.plot2d = plot
        self.msgwin = msg
        x = Image.open(abs_path(photograph))
        im = x.resize(size)
        self.size[0] = float(size[0])
        self.size[1] = float(size[1])
        self.image = ImageTk.PhotoImage(im)
        self.canvas = Canvas(parent, width = size[0], height = size[1])
        self.canvas.create_image(0,0,image = self.image, anchor = NW)
        self.data = Data()

    def enter(self,fd):
        self.plot2d.setWorld(0, -5, self.NP * self.adc_delay/1000.0, 5)
        self.plot2d.mark_axes(self.xlabel, self.ylabel)
        self.intro_msg()
        self.ph = fd
        try:
            self.ph.select_adc(0)
            self.ph.set_adc_size(2)
        except:
            self.msgwin.msg('Connection NOT Established','red')
        
    def exit(self):
        self.looping = False
      
    def clear(self,e):
        self.data.clear()
        self.plot2d.delete_lines()  
              
    def save(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save(fname)
        self.msgwin.msg('Data saved to '+ fname)

    def analyze(self,e):
        self.data.analyze(self.xlabel, self.ylabel)

    def save_all(self,e):
        fname = self.fntext.get()
        if fname == None:
          return
        self.data.save_all(fname)
        self.msgwin.msg('Data saved to '+ fname)
        
    def show_all(self,e):
        self.plot2d.delete_lines()  
        self.data.index = 0
        for tr in self.data.traces:
          self.plot2d.line(tr,self.data.get_col())

    def set_adc_delay(self,w):
        d = self.adc_scale.get()    
        self.adc_delay = self.delay_vals[d]
        if self.ph == None:
            return
        self.ph.set_adc_delay(self.adc_delay)
        self.plot2d.setWorld(0, -5, self.NP * self.adc_delay/1000, 5)
        self.plot2d.mark_axes(self.xlabel, self.ylabel)
#------------------------------------------------------------------        
        
    def start(self,e):
        if self.ph == None:
          self.msgwin.msg('Connection not made yet', 'red')
          return
        phdata = self.ph.read_block(self.NP, self.adc_delay, 1)
        if phdata == None: 
            return

        self.data.points = []
        for k in phdata:
          self.data.points.append( (k[0]/1000.0, k[1]/1000.0) ) # scale & copy
        
	self.limit = 0.0		# Find the present signal level
	for p in self.data.points:
          if abs(p[1]) > self.limit:
	    self.limit = abs(p[1]) + 0.1
	self.looping = True
        self.msgwin.msg('Scanning for Waveform (amplitude > %4.3f V)'%self.limit)
#	print self.limit
	
    def get_peaks(self):
        vmin = 5.0
        vmax = -5.0
        t1 = t2 = 0
        for p in self.data.points:
          if p[1] < vmin:
            vmin = p[1]
            t1 = p[0]
	  if p[1] > vmax:
	    vmax = p[1]
	    t2 = p[0]
#        print vmin, vmax
        if t1 < t2:			# First peak is first
            return (t1,vmin), (t2,vmax)
        else:
            return (t2,vmax),(t1,vmin)

    def accept_trace(self):
#        print self.data.points
        self.data.traces.append(self.data.points)
        self.msgwin.msg('Waveform Captured. Click on Scan CH0 to repeat')
        
    def update(self):
        if self.ph == None:
          return
        if self.looping != True:
          return
        try:
          data = self.ph.read_block(self.NP, self.adc_delay, 1)
          if data == None: return
        except: 
          return
        self.data.points = []
        for k in data:
          self.data.points.append( (k[0]/1000.0, k[1]/1000.0) )
        self.plot2d.delete_lines()
        self.plot2d.line(self.data.points,'black')
	for p in self.data.points:
	  if abs(p[1]) > self.limit:
	    self.peaks = self.get_peaks()
	    tmax = self.NP * self.adc_delay / 1000.0 # micro to milli secs
            if self.peaks[0][0] > 0.2*tmax and self.peaks[1][0] < 0.8*tmax:
  	      self.looping = False
              self.accept_trace()
              break
 
    def intro_msg(self):
        self.clear(None)
        self.msgwin.clear()
        self.msgwin.showtext('Connect the coil as shown in the figure.\n')
        self.msgwin.showlink('Click Here', self.start)
        self.msgwin.showtext(' to start scanning.\n'+\
        'Drop the magnet into the coil from some height, until a trace is captured.\n'+\
        'You can repeat the whole process to acquire several waveforms.\n')

        self.msgwin.showlink('Save', self.save)
        self.msgwin.showtext(' the latest trace or ')    
        self.msgwin.showlink('Save all Traces', self.save_all)
        self.msgwin.showtext(' to a file named ')
        self.fntext =  Entry(None, width =20, fg = 'red')
        self.msgwin.showwindow(self.fntext)
        self.fntext.insert(END,'ind.dat')
        self.msgwin.showtext(' Send the data to ')
        self.msgwin.showlink('XmGrace', self.analyze)
        self.msgwin.showtext('\n')

        self.msgwin.showlink('View All', self.show_all)
        self.msgwin.showtext(' Traces captured so far. ')
        self.msgwin.showlink('Clear all Traces', self.clear)