Beispiel #1
0
 def _capture_frames_dark(self, storage, prefix, frame_shape, dark_frames_count, dark_frame_store_stats):
     if dark_frames_count == 0:
         return None, None
     #Close shutter
     self._redis_client.publish('hics:camera:shutter_open', 0)
     
     #Create dark frames
     s = time.time()
     if not dark_frame_store_stats:
         storage[prefix] = EmptyNDArray((frame_shape[0], dark_frames_count, frame_shape[2]))
     s = 2 - time.time() + s
     if s > 0:
         time.sleep(s)
         
     start_time = datetime.datetime.now()
     
     if not dark_frame_store_stats:
         frames = storage[prefix]
     else:
         frames = numpy.empty((frame_shape[0], dark_frames_count, frame_shape[2]))
     
     self._flush_queue()
     idx = 0
     while idx < dark_frames_count:
         frames[:, idx, :] = self.get_frame()[:, 0, :]
         idx += 1
         
     if dark_frame_store_stats:
         storage[prefix+'-avg'] = numpy.average(frames, 1)
         storage[prefix+'-var'] = numpy.var(frames, 1)
         
     end_time = datetime.datetime.now()
     
     return start_time, end_time
Beispiel #2
0
    def add_data(self, mhdr_file, label):
        d = mmapdict(mhdr_file, True)

        data = d['hdr']
        mask = d['hdr'].mask.all(2)

        valid_pixels = numpy.array(numpy.nonzero(~(d['hdr'].mask.all(2)))).T

        if label not in self.labels:
            self._mm['labels'] = self.labels + [label]

        if self.wavelengths is None:
            self._mm['wavelengths'] = numpy.array(d['wavelengths'])
        else:
            if list(self.wavelengths) != list(d['wavelengths']):
                raise ValueError("Wavelengths don't match")

        label_id = self.labels.index(label)
        label_key = 'data-{:05d}'.format(label_id)

        if label_key not in self._mm:
            self._mm[label_key] = EmptyNDArray(
                (valid_pixels.shape[0], len(self._mm['wavelengths'])),
                dtype=numpy.float32)
            offset = 0
        else:
            old = self._mm[label_key]
            self._mm[label_key] = EmptyNDArray(
                (valid_pixels.shape[0] + old.shape[0],
                 len(self._mm['wavelengths'])),
                dtype=numpy.float32)
            self._mm[label_key][:old.shape[0]] = old
            offset = old.shape[0]

        out_data = self._mm[label_key]
        for y, x in valid_pixels:
            out_data[offset] = data[y, x].filled(numpy.nan)
            offset += 1

        self._mm.vacuum()
Beispiel #3
0
    def blit_images(self):
        im_list = self.merge_greedy()
        min_pos_y = 0
        max_pos_y = 0
        min_pos_x = 0
        max_pos_x = 0
        for im_list_entry in im_list:
            im_id, pos_y, pos_x, flip_y, flip_x = im_list_entry
            im = self.get_im(im_id)
            min_pos_y = min(pos_y, min_pos_y)
            min_pos_x = min(pos_x, min_pos_x)
            max_pos_y = max(pos_y + im.shape[0], max_pos_y)
            max_pos_x = max(pos_x + im.shape[1], max_pos_x)

        shape = (max_pos_y - min_pos_y, max_pos_x - min_pos_x,
                 len(input_data['wavelengths']))
        self._output_data['hdr'] = EmptyNDArray(shape)
        target = self._output_data['hdr']
        self._output_data['hdr-coeff'] = EmptyNDArray(shape)
        target_coeff = self._output_data['hdr-coeff']
        for im_list_entry in im_list:
            im_id, pos_y, pos_x, flip_y, flip_x = im_list_entry
            im = numpy.ma.masked_invalid(self.get_im(im_id))[::{
                1: -1,
                0: 1
            }[flip_y], ::{
                1: -1,
                0: 1
            }[flip_x]]
            coeff_matrix = HDRMakerCorr.make_mask(im)
            coeff_matrix *= ~(im.mask)
            target[pos_y - min_pos_y:pos_y - min_pos_y + im.shape[0],
                   pos_x - min_pos_x:pos_x - min_pos_x +
                   im.shape[1]] += (im.filled(0) * coeff_matrix)
            target_coeff[pos_y - min_pos_y:pos_y - min_pos_y + im.shape[0],
                         pos_x - min_pos_x:pos_x - min_pos_x +
                         im.shape[1]] += coeff_matrix

        target /= target_coeff
        target[target_coeff == 0.] = numpy.nan
Beispiel #4
0
    def _capture_frames(self,
                        storage,
                        prefix,
                        frame_shape,
                        dark_frames_count_before,
                        dark_frames_count_after,
                        data_frames_count=None,
                        capture_end_position=None,
                        reversed_scan=False,
                        dark_frame_store_stats=False):
        d0_start_time = None
        d0_end_time = None
        d1_start_time = None
        d1_end_time = None

        d0_start_time, d0_end_time = self._capture_frames_dark(
            storage, prefix + '-d0', frame_shape, dark_frames_count_before,
            dark_frame_store_stats)

        #Capture data
        data_positions = {}

        if data_frames_count is not None:
            self._redis_client.publish('hics:camera:shutter_open', 1)
            s = time.time()
            storage[prefix] = EmptyNDArray(
                (frame_shape[0], data_frames_count, frame_shape[2]))
            s = 2 - time.time() + s
            if s > 0:
                time.sleep(s)
            while not self._queue.empty():
                _dummy = self._queue.get()
            data_start_time = datetime.datetime.now()
            idx = 0
            while idx < data_frames_count:
                key, data = self._queue.get()
                if key == 'hics:framegrabber:frame_raw':
                    storage[prefix][:, idx, :] = pickle.loads(data)[:, 0, :]
                    idx += 1
                elif key == 'hics:scanner:state':
                    state = [int(x) for x in data.split(b':')]
                    moving = state[0] == 1
                    position = state[1]
                    data_positions[datetime.datetime.now()] = (position,
                                                               moving)

            data_end_time = datetime.datetime.now()

        elif capture_end_position is not None:
            self._redis_client.publish('hics:camera:shutter_open', 1)
            time.sleep(2)
            #Size is not known...
            self._redis_client.publish('hics:scanner:move_absolute',
                                       capture_end_position)

            #Clear queue
            self.clear_queue()

            frames = []
            moving = False
            position = None
            while len(frames) == 0 or moving:
                key, data = self._queue.get()
                if key == 'hics:framegrabber:frame_raw' and (
                        moving or capture_end_position == position):
                    frames.append(pickle.loads(data))
                elif key == 'hics:scanner:state':
                    state = [int(x) for x in data.split(b':')]
                    moving = state[0] == 1
                    position = state[1]
                    data_positions[datetime.datetime.now()] = (position,
                                                               moving)

            if not reversed_scan:
                frames.reverse()
            storage[prefix] = numpy.concatenate(frames, 1)

        d1_start_time, d1_end_time = self._capture_frames_dark(
            storage, prefix + '-d1', frame_shape, dark_frames_count_after,
            dark_frame_store_stats)

        properties = {
            'integration_time':
            float(
                self._redis_client.get('hics:camera:integration_time').decode(
                    'utf8')),
            'd0_start_time':
            d0_start_time,
            'd0_end_time':
            d0_end_time,
            'd1_start_time':
            d1_start_time,
            'd1_end_time':
            d1_end_time,
            'data_positions':
            data_positions
        }

        focus_state = self._redis_client.get('hics:focus:state')
        if focus_state is not None:
            properties['focus_position'] = float(
                focus_state.decode('utf8').split(':')[1])
        storage[prefix + '-p'] = properties
Beispiel #5
0
    for im_id in im_ids:
        hdr.get_descriptors(im_id)

    #Match descriptors
    for im_i, im_j in itertools.product(im_ids, im_ids):
        hdr.get_match(im_i, im_j)

    for i in im_ids:
        hdr.get_transformed_image(i)

    from matplotlib import pyplot as plt

    minshape = min([hdr.get_transformed_image(im_id)[0].shape for im_id in im_ids if hdr.get_transformed_image(im_id)[0] is not None])

    if 'hdr_data' not in output_data:
        output_data['hdr-data'] = EmptyNDArray(minshape)
        output_data['hdr-data_max'] = EmptyNDArray(minshape)
        output_data['hdr-data_min'] = EmptyNDArray(minshape)
        output_data['hdr-var'] = EmptyNDArray(minshape)
        output_data['hdr-mask'] = EmptyNDArray(minshape)
        output_data['hdr-coeff'] = EmptyNDArray(minshape)

    hdr_data = output_data['hdr-data']
    hdr_var = output_data['hdr-var']
    hdr_mask = output_data['hdr-mask']
    hdr_data_min = output_data['hdr-data_min']
    hdr_data_max = output_data['hdr-data_max']
    hdr_coeff = output_data['hdr-coeff']

    hdr_data[:, :, :] = 0
    hdr_data_min[:, :, :] = 0
Beispiel #6
0
    from multilstsq import ExprEvaluator
    import numpy
    import itertools

    d = mmapdict(sys.argv[1])
    d['model'] = 'b0 + b1*x0 + b2*(x0**2)'
    nval = 100000
    shape = (10000, )

    expr = ExprEvaluator(d['model'])

    explanatories = list(sorted(x for x in expr.variables
                                if x.startswith('x')))
    parameters = list(sorted(x for x in expr.variables if x.startswith('b')))

    d['explanatories'] = EmptyNDArray(shape + (nval, len(explanatories)))
    d['response'] = EmptyNDArray(shape + (nval, 1))
    d['parameters'] = numpy.random.normal(size=shape + (len(parameters), ))

    d.vacuum()

    m_explanatories = d['explanatories']
    m_response = d['response']
    m_parameters = d['parameters']

    for pos in itertools.product(*(range(x) for x in shape)):
        print(pos)
        m_explanatories[pos] = numpy.random.normal(size=(nval,
                                                         len(explanatories)))
        p = dict(zip(parameters, m_parameters[pos]))