if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description=
        "Characterises orthogonality of the axis and finds step size")
    parser.add_argument("side_length",
                        type=int,
                        help="Total displacement along each axis")
    parser.add_argument("--points",
                        type=int,
                        default=10,
                        help="Number of measurments to record")
    args = parser.parse_args()

    with load_microscope("microscope_settings.npz", dummy_stage = False) as ms, \
         closing(data_file.Datafile(filename = "step_size.hdf5")) as df:

        assert picamera_supports_lens_shading(
        ), "You need the updated picamera module with lens shading!"

        camera = ms.camera
        stage = ms.stage

        side_length = args.side_length
        points = args.points
        backlash = 256

        camera.resolution = (640, 480)
        stage.backlash = backlash

        stage_pos = df.new_group("data_stage", "orthogonality")
Exemple #2
0
    parser.add_argument("step",
                        type=int,
                        nargs=3,
                        help="The displacement between each point, in steps")
    parser.add_argument("--n_frames",
                        type=int,
                        default=2000,
                        help="The number of frames to record for each run")
    parser.add_argument("--framerate",
                        type=int,
                        default=100,
                        help="Rate at which to run the camera (frames/second)")
    args = parser.parse_args()

    with load_microscope("microscope_settings.npz", dummy_stage = False) as ms, \
         closing(data_file.Datafile(filename = "precision.hdf5")) as df:

        assert picamera_supports_lens_shading(
        ), "You need the updated picamera module with lens shading!"

        camera = ms.camera
        stage = ms.stage

        N_frames = args.n_frames
        step = args.step
        framerate = args.framerate
        backlash = 256

        camera.resolution = (640, 480)
        camera.framerate = framerate
        stage.backlash = backlash
Exemple #3
0
                        help="Time (in seconds) to wait at each point")
    parser.add_argument(
        "--return_to_start",
        dest="return_to_start",
        action="store_true",
        help="Return to the origin at the beginning of each run")
    parser.add_argument("--output",
                        help="HDF5 file to save to",
                        default="linear_motion.hdf5")
    parser.add_argument("--settings_file",
                        help="File where the microscope settings are stored.",
                        default="microscope_settings.npz")
    args = parser.parse_args()

    with load_microscope(args.settings_file, dummy_stage = False) as ms, \
         closing(data_file.Datafile(filename = args.output)) as df:

        assert picamera_supports_lens_shading(
        ), "You need the updated picamera module with lens shading!"

        camera = ms.camera
        stage = ms.stage

        N_frames = args.n_frames
        N_repeats = args.n_repeats
        step = args.step
        framerate = args.framerate
        dwell_time = args.dwell_time
        backlash = 256
        return_to_start = args.return_to_start
from scipy import ndimage, signal
import sys
import time
from camera_stuff import get_numpy_image, find_template
import microscope
import picamera
import data_file
from openflexure_stage import OpenFlexureStage
import h5py
from contextlib import closing

if __name__ == "__main__":

    with picamera.PiCamera(resolution = (640, 480)) as camera, \
         OpenFlexureStage('/dev/ttyUSB0') as stage, \
         closing(data_file.Datafile(filename = "orthogonality_15123.hdf5")) as df:

        side_length = 4000
        points = 10

        stage.backlash = 256

        camera.start_preview()

        image = get_numpy_image(camera, greyscale=True).astype(np.float32)
        background = cv2.GaussianBlur(image, (41, 41), 0)
        templ8 = (image - background)[100:-100, 100:-100]

        stage_centre = stage.position

        data_stage = df.new_group(
Exemple #5
0
def image_capture(start_t, event, ms, q):
    while event.is_set():
        frame = ms.rgb_image().astype(np.float32)
        capture_t = time.time()
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        q.put(frame)
        tim = capture_t - start_t
        q.put(tim)
        print('Number of itms in the queue: {}'.format(q.qsize()))
        time.sleep(0.2)

if __name__ == "__main__":

    with load_microscope("microscope_settings.npz") as ms, \
         closing(data_file.Datafile(filename = "drift.hdf5")) as df:

        assert picamera_supports_lens_shading(), "You need the updated picamera module with lens shading!"

        camera = ms.camera
        stage = ms.stage

        camera.resolution=(640,480)

        cam_pos = df.new_group("data", "drift")

        N_frames = 500
        #need to be consistant between drift.py and drift_plot.py

        camera.start_preview(resolution=(640,480))
        gains = stage.light_sensor_gain_values
        stage.light_sensor_gain = gains[
            -1]  #maximum gain to detect faint signal
        length = 500  #initial side length of spiral
        step = 100  #move distance per step
        move = 0  #distance already moved
        i = 0  #counter
        try:
            threshold = int(sys.argv[1])
        except:
            threshold = 500

        backlash = None

        df = data_file.Datafile(filename="raster.hdf5")
        data_intensity = df.new_group("intensity reading", "raster scan")
        data_stage = df.new_group("stage position", "raster scan")

        intensity_data = np.zeros((1, 5000))
        stage_data = np.zeros((3, 5000))

        reading = stage.light_sensor_intensity
        stage_position = stage.position
        intensity_data[0, i] = reading
        stage_data[0:, i] = stage_position
        i = i + 1
        print reading

        while (reading < threshold):  #avoids background noise
            while (move <= length and reading < threshold):
Exemple #7
0
    parser.add_argument("area",
                        type=int,
                        nargs=2,
                        help="Area of scan measured in steps")
    parser.add_argument(
        "step",
        type=int,
        help="Displacement between measurments measured in steps")
    parser.add_argument("--backlash",
                        type=int,
                        default=256,
                        help="Backlash correction on or off")
    args = parser.parse_args()

    with load_microscope("microscope_settings.npz", dummy_stage = False) as ms, \
         closing(data_file.Datafile(filename = "raster.hdf5")) as df:

        assert picamera_supports_lens_shading(
        ), "You need the updated picamera module with lens shading!"

        camera = ms.camera
        stage = ms.stage

        area = args.area
        step = args.step
        backlash = args.backlash

        camera.resolution = (640, 480)
        camera.start_preview(resolution=(640, 480))

        initial_stage_position = stage.position
from openflexure_microscope.microscope import picamera_supports_lens_shading
import scipy
from scipy import ndimage, signal
import matplotlib.pyplot as plt
from contextlib import contextmanager, closing
import data_file
import cv2
from camera_stuff import find_template
#import h5py
import threading
import queue

if __name__ == "__main__":

    with load_microscope("microscope_settings.npz", dummy_stage = False) as ms, \
         closing(data_file.Datafile(filename = "fatigue_tests.hdf5")) as df:

        assert picamera_supports_lens_shading(
        ), "You need the updated picamera module with lens shading!"

        camera = ms.camera
        stage = ms.stage

        step_size = 1000
        n_steps = 20
        backlash = 0

        camera.resolution = (1640, 1232)
        stage.backlash = backlash

        data_group = df.new_group("fatigue_test")
        #loads camera from picamera and set the stage as the arduino, lower resolution can speed up the programme
        camera.start_preview()  #shows preview of camera
        ms = microscope.Microscope(camera, stage)
        ms.freeze_camera_settings()
        frame = get_numpy_image(camera, True)
        print "Frame is {}".format(frame.shape)  #print resolution

        backlash = 128  #counters backlash
        stage.move_rel([-backlash, -backlash, -backlash])
        stage.move_rel([backlash, backlash, backlash])  #energise motors
        stage.backlash = backlash

        stage_position = stage.position  #store the initial position of the lens

        df = data_file.Datafile(
            filename="accuracy.hdf5")  #creates the .hdf5 file to save the data
        data_gr = df.new_group(
            "test_data", "A file of data collected to test this code works")
        #puts the data file into a group with description

        data = np.zeros(
            (3, N_frames))  #creates the array in which data is stored

        outputs = [io.BytesIO() for j in range(N_frames)
                   ]  #creates a list containing RAM locations for 100 images

        event = threading.Event()  #creates the event for the thread

        def movement():  #defines the function for moving the stage
            global event, stage
            while not event.wait(2):
import data_file
import h5py

if __name__ == "__main__":

    with picamera.PiCamera(resolution=(
            640, 480)) as camera, OpenFlexureStage('/dev/ttyUSB0') as stage:

        N_frames = 100

        #loads camera from picamera and set the stage as the arduino, lower resolution can speed up the programme
        ms = microscope.Microscope(camera, stage)
        ms.freeze_camera_settings()
        frame = get_numpy_image(camera, True)

        df = data_file.Datafile(filename="positions.hdf5"
                                )  #creates the .hdf5 file to save the data
        data_gr = df.new_group(
            "test_data", "A file of data collected to test this code works")
        #puts the data file into a group with description

        event = threading.Event()  #creates the event for the thread

        def picture(
                frame
        ):  #defines the function for saving an image every 30 seconds
            global event
            while not event.wait(10):
                cv2.imwrite(
                    "/home/pi/microscope/frames/drift_%s.jpg" %
                    time.strftime("%Y%m%d_%H%M%S"), frame)
Exemple #11
0
    for i in range(step):  #moves bottom right to bottom left in step steps
        stage.move_rel([side_length / (step + 1), 0, 0])
        time.sleep(1)
        np.append(data, measure_txy(1, start_time, camera, templ8))
    for i in range(step):  #moves bottom left to top left
        stage.move_rel([0, side_length / (step + 1), 0])
        time.sleep(1)
        np.append(data, measure_txy(1, start_time, camera, templ8))
    return data


if __name__ == "__main__":

    with picamera.PiCamera(resolution=(640,480), framerate = 100) as camera, \
        OpenFlexureStage('/dev/ttyUSB0') as stage, \
        closing(data_file.Datafile(filename="orthogonality.hdf5")) as df: #opens camera, stage and datafile

        stage.backlash = 248  #corrects for backlash

        side_length = 500  #defines distance moved for each side of the sqaure

        camera.start_preview()  #shows preview of camera

        stage_centre = stage.position  #saves the starting position of stage

        stage.move_rel(
            [side_length / 2, side_length / 2,
             0])  #moves the stage to the 'top left' corner of the square

        top_left = stage.position
        stage.move_rel(np.dot(step_size, axis))
        for j in range(2):
            dump = stage.light_sensor_intensity #throw away readings
        for j in range(samples):
            averages[j, 0] = stage.light_sensor_intensity
            time.sleep(0.1)
        data[i, 0] = np.mean(averages, axis = 0)
        data[i, 1] = stage.light_sensor_gain
        data[i, 2] = time.time() - start_time
        data[i, 3:] = stage.position
    return data

if __name__ == "__main__":
    #with OpenFlexureStage("/dev/ttyUSB0") as stage, \
    with OpenFlexureStage("COM3") as stage, \
         closing(data_file.Datafile(filename = "hillwalk_maria.hdf5")) as df:

        gain = [1, 2, 4, 8, 16]
        stage.light_sensor_gain = gain[-1]
        step_size = 200
        min_step = 20
        points = 5
        samples = 5

        stage.backlash = 256

        raw_data = df.new_group("data", "hill walk")

        start_time = time.time()

        while step_size > min_step:
Exemple #13
0
#import libraries
import numpy as np
from openflexure_stage import OpenFlexureStage
import time
from contextlib import closing
import h5py
import data_file

if __name__ == "__main__":

    with OpenFlexureStage('/dev/ttyUSB0') as stage, closing(
            data_file.Datafile(filename="background.hdf5")) as df:
        n = 200
        output = np.zeros((2, n))
        data_gr = df.new_group("data", "A file of data collected")
        for gain in [1, 25, 428, 9876]:
            gain_group = df.new_group("Gain",
                                      "Measurements for one gain",
                                      parent=data_gr)
            stage.light_sensor_gain = gain
            data = np.zeros(n)
            for i in range(n):
                data[i] = stage.light_sensor_fullspectrum
                time.sleep(5)

            mean = np.mean(data)
            error = np.std(data)
            print "When gain = %d: \nMean background signal = %d +/- %d \n" % (
                gain, mean, error)
            df.add_data(data, gain_group, "data")

def random_point(move_dist):
    #generate a random number in the range 1 to 360 degrees, and move in this direction by a specified amount
    angle = random.randrange(0, 360) * np.pi / 180
    vector = np.array(
        [move_dist * np.cos(angle), move_dist * np.sin(angle), 0])
    vector = np.rint(vector)  #round to the nearest integer
    return vector


if __name__ == "__main__":

    with picamera.PiCamera(resolution=(640,480)) as camera, \
         OpenFlexureStage('/dev/ttyUSB0') as stage, \
         closing(data_file.Datafile(filename="repeat_3.hdf5")) as df: #closes once the programme completes, avoids try-finally
        #loads camera from picamera and set the stage as the arduino, lower resolution can speed up the programme

        n_moves = 10
        samples = 5

        stage.backlash = 0
        bi = [128, 128, 0]
        bo = [-128, -128, 0]

        camera.start_preview()  #shows preview of camera

        for dist in [
                1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 1500, 2048, 3000,
                4096
        ]:
Exemple #15
0
                                      fraction=0.5)
    return data


def random_point(move_dist):
    angle = random.randrange(0, 360) * np.pi / 180
    vector = np.array(
        [move_dist * np.cos(angle), 0, move_dist * np.sin(angle)])
    vector = np.rint(vector)
    return vector


if __name__ == "__main__":

    with load_microscope("microscope_settings.npz", dummy_stage = False) as ms, \
         closing(data_file.Datafile(filename = "repeat.hdf5")) as df:

        assert picamera_supports_lens_shading(
        ), "You need the updated picamera module with lens shading!"

        camera = ms.camera
        stage = ms.stage

        backlash = 256

        camera.resolution = (640, 480)
        stage.backlash = backlash

        n_moves = 10

        camera.start_preview(resolution=(640, 480))
import h5py

if __name__ == "__main__":

    with picamera.PiCamera(resolution=(
            640, 480)) as camera, OpenFlexureStage('/dev/ttyUSB0') as stage:

        stage.backlash = 0
        #increased backlash correction value to get more accurate data
        move = 5000
        #maximum move distance without the templete travelling off field of view of the lens
        #larger move distance can reduce error

        stage_position = stage.position

        df = data_file.Datafile(filename="step_size.hdf5")
        data_step = df.new_group("step size", "step size measurments")

        data = np.zeros((2, 50))

        camera.start_preview()

        image = get_numpy_image(camera, greyscale=True)
        templ8 = image[100:-100, 100:-100]

        stage.move_rel([(-move), 0, 0])

        frame = get_numpy_image(camera, True)
        spot_coord = find_template(templ8, frame)
        x = spot_coord[0]
        y = spot_coord[1]
Exemple #17
0
        #authorises the program to acces the twitter account
        auth = tweepy.OAuthHandler(twitter_keys.consumer_key,
                                   twitter_keys.consumer_secret)
        auth.set_access_token(twitter_keys.access_token,
                              twitter_keys.access_token_secret)
        api = tweepy.API(auth)

        N_frames = 100
        counter = 0

        #loads camera from picamera and set the stage as the arduino, lower resolution can speed up the programme
        ms = microscope.Microscope(camera, stage)
        ms.freeze_camera_settings()
        frame = get_numpy_image(camera, True)

        df = data_file.Datafile(filename="drift_81217.hdf5"
                                )  #creates the .hdf5 file to save the data
        data_gr = df.new_group(
            "test_data", "A file of data collected to test this code works")
        #puts the data file into a group with description

        image = get_numpy_image(camera, greyscale=True)  #takes template photo
        templ8 = image[100:-100, 100:-100]  #crops image

        loop = True
        tweet = True

        start_t = time.time()  #measure starting time

        try:
            while loop:
                data = np.zeros(
Exemple #18
0
    global pos
    time.sleep(0.01)
    data = np.zeros((points, 6), dtype=np.float)
    pos += (np.dot(-(step_size * math.trunc(points / 2)), axis))
    for i in range(points):
        pos += (np.dot(step_size, axis))
        data[i, 0] = np.exp(-np.sum(
            (pos - maxpos)**2 / np.array([1000., 1000., 3000.])**2)) * 1000.
        data[i, 1] = 1
        data[i, 2] = time.time() - start_time
        data[i, 3:] = pos
    return data


if __name__ == "__main__":
    with closing(data_file.Datafile(filename="hillwalk.hdf5")) as df:

        gain = [1, 25, 428, 9876]
        #stage.light_sensor_gain = 9876
        step_size = 500
        min_step = 50
        points = 5
        samples = 5

        #        stage.backlash = 256

        raw_data = df.new_group("data", "hill walk")

        start_time = time.time()

        while step_size > min_step: