Beispiel #1
0
def __generate_movie__(movie_specification):
    """
    function generate a movie using mencoder based on frame's files extension
    or a file listing
    """
    m = movie_specification
    if m.idx == None:
        output_file = as_path(m.directory, '%s.avi' % (m.name))
    else:
        output_file = as_path(m.directory,
                              '%s_part_%02i.avi' % (m.name, m.idx))

    #frames_file if not null contains frames listing
    frames_source = 'mf://' + (('@' + m.frames_file) if m.frames_file else
                               as_path(m.movie_directory, m.movie_frames))
    command = ('mencoder',
               frames_source,
               #'mf://@' + m.frames_file,
               '-mf',
               #'type=png:w=1024:h=800:fps=30',
               'type=png:w=' + str(m.width) + \
                    ':h=' + str(m.height) + \
                    ':fps=' + str(m.fps),
               '-ovc',
               'lavc',
               '-lavcopts',
               'vcodec=mpeg4:mbd=2:trell',
               '-oac',
               'copy',
               '-o',
               '%s' % (output_file))
    os.spawnvp(os.P_WAIT, 'mencoder', command)
    return output_file
Beispiel #2
0
def __generate_movie__(movie_specification):
    """
    function generate a movie using mencoder based on frame's files extension
    or a file listing
    """
    m = movie_specification
    if m.idx == None:
        output_file = as_path(m.directory, '%s.avi' % (m.name))
    else:
        output_file = as_path(m.directory,
                              '%s_part_%02i.avi' % (m.name, m.idx))

    #frames_file if not null contains frames listing
    frames_source = 'mf://' + (('@' + m.frames_file)
            if m.frames_file else as_path(m.movie_directory, m.movie_frames))
    command = ('mencoder',
               frames_source,
               #'mf://@' + m.frames_file,
               '-mf',
               #'type=png:w=1024:h=800:fps=30',
               'type=png:w=' + str(m.width) + \
                    ':h=' + str(m.height) + \
                    ':fps=' + str(m.fps),
               '-ovc',
               'lavc',
               '-lavcopts',
               'vcodec=mpeg4:mbd=2:trell',
               '-oac',
               'copy',
               '-o',
               '%s' % (output_file))
    os.spawnvp(os.P_WAIT, 'mencoder', command)
    return output_file
    def __init__(self, pp_specs_manager):
        super(PoincarePlotAnimation, self).__init__(pp_specs_manager)
        interval = int(1000.0 / self.manager.movie_fps)
        ani = animation.FuncAnimation(self.fig,
                                      self.update_plot,
                                      init_func=self.setup_plot,
                                      frames=len(self.pp_specs),
                                      interval=interval,
                                      blit=True,
                                      repeat_delay=0,
                                      repeat=False)

        p0 = self.pp_specs[0]  # alias
        output_movie_file = as_path(
            self.manager.movie_dir,
            '%s_%06d.mp4' % (self.manager.movie_name, p0.idx))
        ani.save(
            output_movie_file,
            fps=self.manager.movie_fps,
            dpi=self.manager.movie_dpi,
            #extra_args=['-vcodec', 'libx264']
            #extra_args=['-vcodec', 'mpeg4']
            #extra_args=['-vcodec', 'h264']
            #, '-lavcopts', 'vcodec=avi']
        )
    def save_movie(self):
        if not self.__prefixed_movie_name__ == None and self.p.movie_not_save == False:  # @IgnorePep8

            output_file = generate_movie(self.__prefixed_movie_name__,
                                    self.__prefixed_movie_dir__,
                                    self.p.movie_width, self.p.movie_height,
                                    self.p.movie_fps,
                                    movie_clean_frames=self.p.movie_clean_frames)
            self.message = "Poincare plot movie %s is created !" \
                            % as_path(self.__prefixed_movie_dir__, output_file)
    def __get_prefixed_movie_dir__(self):
        """
        method returns prefixed movie_dir by source name and output_prefix
        """
        movie_dir = self.p.movie_dir

        prefix = self.p.output_prefix + '_' \
                if not is_empty(self.p.output_prefix) else ''

        return as_path(movie_dir, prefix + 'm_' + self.source) \
                if self.p.movie_prefixed_by_source else movie_dir
Beispiel #6
0
    def __get_prefixed_movie_dir__(self):
        """
        method returns prefixed movie_dir by source name and output_prefix
        """
        movie_dir = self.p.movie_dir

        prefix = self.p.output_prefix + '_' \
                if not is_empty(self.p.output_prefix) else ''

        return as_path(movie_dir, prefix + 'm_' + self.source) \
                if self.p.movie_prefixed_by_source else movie_dir
Beispiel #7
0
    def save_movie(self):
        if not self.__prefixed_movie_name__ == None and self.p.movie_not_save == False:  # @IgnorePep8

            output_file = generate_movie(
                self.__prefixed_movie_name__,
                self.__prefixed_movie_dir__,
                self.p.movie_width,
                self.p.movie_height,
                self.p.movie_fps,
                movie_clean_frames=self.p.movie_clean_frames)
            self.message = "Poincare plot movie %s is created !" \
                            % as_path(self.__prefixed_movie_dir__, output_file)
Beispiel #8
0
 def __save_members__(self):
     """
     [optional]
     save members values
     """
     if self.save_parameters:
         _filename = as_path(self.output_dir, "pp_parameters.txt")
         create_dir(self.output_dir)
         with open(_filename, 'w') as _file:
             for info_line in self.__p__.get_infos_lines(valued_only=True):
                 _file.write(info_line + "\n")
             print('Poincare plot parameters saved into a file: %s ' %
                   (_filename))
Beispiel #9
0
 def __save_members__(self):
     """
     [optional]
     save members values
     """
     if self.save_parameters:
         _filename = as_path(self.output_dir, "pp_parameters.txt")
         create_dir(self.output_dir)
         with open(_filename, 'w') as _file:
             for info_line in self.__p__.get_infos_lines(valued_only=True):
                 _file.write(info_line + "\n")
             print('Poincare plot parameters saved into a file: %s '
                   % (_filename))
Beispiel #10
0
    def __process__(self, _file_handler, disp=True, **params):
        """
        the method which starts to generate Poincare Plot parameters
        """
        sign_multiplicator = 80
        file_counter = 0
        if disp:
            print('*' * sign_multiplicator)

        if self.group_data_filename and self.data_file == None:
            #create a group data input file
            outfilename = as_path(self.output_dir,
                                  "grp_" + self.group_data_filename)
            joined = join_files(self.__data_filenames__(),
                                    headers_count=self.headers_count,
                                    outfilename=outfilename)
            if joined:
                self.data_file = outfilename
                if disp:
                    print('Using group data file: ' + self.data_file)

        if self.data_file:  # data_file parameter is superior to data_dir parameter @IgnorePep8
            _data_file = self.__shuffle_file__(self.data_file)
            if os.path.exists(_data_file) == False:
                if disp:
                    print('The file: ' + _data_file + " doesn't exist")
            else:
                file_counter = 1
                if self.__p__.check_data_indexes(_data_file, disp):
                    _file_handler(_data_file, disp=disp, **params)
        else:
            for _file in self.__data_filenames__():
                if os.path.isfile(_file):
                    file_counter = file_counter + 1
                    if disp:
                        print('=' * sign_multiplicator)
                    if not self.__p__.check_data_indexes(_file, disp):
                        continue
                    if _file_handler(_file, disp=disp, **params) == False:
                        break
        if disp:
            print('Processing finished')
            if file_counter == 0:
                print('No files to process [' + self.data_dir
                                            + self.extension + ']')
            else:
                print('Number of processed files: ' + str(file_counter))
Beispiel #11
0
    def __process__(self, _file_handler, disp=True, **params):
        """
        the method which starts to generate Poincare Plot parameters
        """
        sign_multiplicator = 80
        file_counter = 0
        if disp:
            print('*' * sign_multiplicator)

        if self.group_data_filename and self.data_file == None:
            #create a group data input file
            outfilename = as_path(self.output_dir,
                                  "grp_" + self.group_data_filename)
            joined = join_files(self.__data_filenames__(),
                                headers_count=self.headers_count,
                                outfilename=outfilename)
            if joined:
                self.data_file = outfilename
                if disp:
                    print('Using group data file: ' + self.data_file)

        if self.data_file:  # data_file parameter is superior to data_dir parameter @IgnorePep8
            _data_file = self.__shuffle_file__(self.data_file)
            if os.path.exists(_data_file) == False:
                if disp:
                    print('The file: ' + _data_file + " doesn't exist")
            else:
                file_counter = 1
                if self.__p__.check_data_indexes(_data_file, disp):
                    _file_handler(_data_file, disp=disp, **params)
        else:
            for _file in self.__data_filenames__():
                if os.path.isfile(_file):
                    file_counter = file_counter + 1
                    if disp:
                        print('=' * sign_multiplicator)
                    if not self.__p__.check_data_indexes(_file, disp):
                        continue
                    if _file_handler(_file, disp=disp, **params) == False:
                        break
        if disp:
            print('Processing finished')
            if file_counter == 0:
                print('No files to process [' + self.data_dir +
                      self.extension + ']')
            else:
                print('Number of processed files: ' + str(file_counter))
Beispiel #12
0
def create_many_plots(_source_file, headers, separator, window_size=None):
    """
    draws many plots base on data from _source_file and collection of headers
    grouped by a semicolon, elements in a group are separated by a comma
    """
    colors = ['red', 'green', 'blue', 'yellow', 'black', 'cyan', 'magenta']

    first_line = get_first_lines(_source_file)
    if len(first_line) == 0:
        return
    file_headers = [
        header.strip().lower() for header in first_line[0].split(separator)
    ]
    for sub_headers in get_as_list(headers, separator=';'):
        labels = [label.strip().lower() for label in get_as_list(sub_headers)]
        try:
            cols = tuple([file_headers.index(label) for label in labels])
        except ValueError:
            continue
        if separator == None:
            values = np.loadtxt(_source_file,
                                skiprows=1,
                                unpack=True,
                                usecols=cols)
        else:
            values = np.loadtxt(_source_file,
                                skiprows=1,
                                unpack=True,
                                usecols=cols,
                                delimiter=separator)

        plt.gca().set_color_cycle(colors[:len(labels)])

        filename = get_filename(_source_file)
        title = "%s %s" % (filename, nvl(window_size + " window size", ''))
        plt.title(title)

        for value in values:
            plt.plot(value)

        plt.legend(labels, loc='upper right')
        plt.axes().set_xlabel('Time')

        plot_filename = "%s_%s.png" % (filename, '_'.join(labels))
        _file = as_path(fs.dirname(_source_file), plot_filename)
        plt.savefig(_file)
        plt.cla()
Beispiel #13
0
def create_many_plots(_source_file, headers, separator, window_size=None):
    """
    draws many plots base on data from _source_file and collection of headers
    grouped by a semicolon, elements in a group are separated by a comma
    """
    colors = ['red', 'green', 'blue', 'yellow', 'black', 'cyan', 'magenta']

    first_line = get_first_lines(_source_file)
    if len(first_line) == 0:
        return
    file_headers = [header.strip().lower()
            for header in first_line[0].split(separator)]
    for sub_headers in get_as_list(headers, separator=';'):
        labels = [label.strip().lower()
                  for label in get_as_list(sub_headers)]
        try:
            cols = tuple([file_headers.index(label) for label in labels])
        except ValueError:
            continue
        if separator == None:
            values = np.loadtxt(_source_file, skiprows=1, unpack=True,
                                usecols=cols)
        else:
            values = np.loadtxt(_source_file, skiprows=1, unpack=True,
                                usecols=cols, delimiter=separator)

        plt.gca().set_color_cycle(colors[:len(labels)])

        filename = get_filename(_source_file)
        title = "%s %s" % (filename, nvl(window_size + " window size", ''))
        plt.title(title)

        for value in values:
            plt.plot(value)

        plt.legend(labels, loc='upper right')
        plt.axes().set_xlabel('Time')

        plot_filename = "%s_%s.png" % (filename, '_'.join(labels))
        _file = as_path(fs.dirname(_source_file), plot_filename)
        plt.savefig(_file)
        plt.cla()
    def __init__(self, pp_specs_manager):
        super(PoincarePlotAnimation, self).__init__(pp_specs_manager)
        interval = int(1000.0 / self.manager.movie_fps)
        ani = animation.FuncAnimation(self.fig,
                            self.update_plot,
                            init_func=self.setup_plot,
                            frames=len(self.pp_specs),
                            interval=interval,
                            blit=True,
                            repeat_delay=0,
                            repeat=False)

        p0 = self.pp_specs[0]  # alias
        output_movie_file = as_path(self.manager.movie_dir,
                        '%s_%06d.mp4' % (self.manager.movie_name, p0.idx))
        ani.save(output_movie_file, fps=self.manager.movie_fps,
                 dpi=self.manager.movie_dpi,
                 #extra_args=['-vcodec', 'libx264']
                 #extra_args=['-vcodec', 'mpeg4']
                 #extra_args=['-vcodec', 'h264']
                 #, '-lavcopts', 'vcodec=avi']
                 )
Beispiel #15
0
from hra_core.io_utils import as_path
import hra_math.time_domain.poincare_plot.poincare_plot as poincare

root_dir = '/home/jurek/volumes/doctoral/monitor_do_impedancji_niccomo/'
result_dir = 'results'
groups = ['_healthy', '_team']
#groups = ['_healthy']
parameters = ['SV', 'SVR', 'CO', 'PEP', 'LVET']
#parameters = ['SV']
types = ['sliding', 'jumping']

for group in groups:
    for parameter in parameters:
        for _type in types:

            base_output_dir = as_path(root_dir, result_dir, group, _type) + fs.sep # @IgnorePep8

            #parameters
            parameter_dir = as_path(base_output_dir, parameter) + fs.sep
            pp = poincare.PoincarePlotManager()
            pp.window_size = '5m'
            #pp.data_file = as_path(root_dir, group, '001.res')
            pp.data_dir = as_path(root_dir, group) + fs.sep
            pp.output_dir = parameter_dir
            pp.signal_label = parameter
            pp.time_label = 'time'
            pp.extension = '*.res'
            pp.statistics_names = 'c1d, c1a, c2d, c2a, cd, ca, asymmetry'
            pp.output_separator = ';  '
            pp.override_existing_outcomes = True
            pp.headers_count = 2
Beispiel #16
0
def __generate_multiprocessing_movie__(movie_name, movie_dir, movie_width,
                                       movie_height, movie_fps, movie_frames,
                                       movie_clean_frames):
    """
    generating a movie with given specification using multiprocessing;
    all frames are divided into excluded sets of files, for each set
    a movie is create, in the next step all parts are joined to create
    one final movie
    """

    #delete previous partial listing files
    command = 'rm %s' % as_path(movie_dir, '%s_lst_*' % (movie_name))
    execute_command(command)

    #create listing of all files
    listing_file = str(as_path(movie_dir, movie_name + ".lst"))
    #command = 'ls %s > %s' % (as_path(movie_dir, movie_frames), listing_file)
    #a better version with no trouble with a limit of number of files of ls command
    command = '(cd %s; find $PWD -name \'*.png\' | sort > %s)' % (movie_dir,
                                                                  listing_file)
    execute_command(command)

    #get number of movie frames
    command = 'wc -l %s' % listing_file
    count = int(execute_command(command, 1).split()[0])

    #get bytes per line
    command = 'head %s' % listing_file
    bytes_per_line = int(len(execute_command(command, 1)))

    #divide number of files frames per processors count
    size = count / multiprocessing.cpu_count()

    #split listing file into equal parts prefixed <movie_name>_lst_
    command = '(cd %s; split -C %i %s %s_lst_)' % \
                (movie_dir, bytes_per_line * size, listing_file, movie_name)
    execute_command(command)

    #delete all previous movie parts
    command = 'rm %s' % as_path(movie_dir, '%s_part_*.avi' % (movie_name))
    execute_command(command)

    movie_specifications = []
    #iterate over splits to create movie_specifications objects
    command = 'ls ' + as_path(movie_dir, '%s_lst_*' % (movie_name))
    for idx, frames_file in enumerate(run_command(command)):
        frames_file = frames_file.replace('\n', '')
        movie_specification = MovieSpecification(movie_name, movie_dir,
                                                 movie_width, movie_height,
                                                 movie_fps, frames_file, idx,
                                                 movie_frames)
        movie_specifications.append(movie_specification)

    #run generation of partial movies
    pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
    #if not self.params.info_message_handler == None:
    #    self.params.info_message_handler('Generating frames'
    #                                 + (' ' * 20))
    pool.map(__generate_movie__, movie_specifications)
    pool.close()

    #join all video part movies
    movie_draft = movie_name + '_draft.avi'
    command = '(cd %s; cat %s_part_*.avi  > %s)' % (movie_dir, movie_name,
                                                    movie_draft)
    execute_command(command)

    #convert draft file into final movie file
    output_file = '%s.avi' % (movie_name)
    command = '(cd %s; mencoder %s -o %s -forceidx -ovc copy -oac copy)' \
                 % (movie_dir, movie_draft, output_file)
    execute_command(command)

    #delete draft movie
    command = 'rm %s' % as_path(movie_dir, movie_draft)
    execute_command(command)

    #delete all movie parts
    command = 'rm %s' % as_path(movie_dir, '%s_part_*.avi' % (movie_name))
    execute_command(command)

    #delete partial listing files
    command = 'rm %s' % as_path(movie_dir, '%s_lst_*' % (movie_name))
    execute_command(command)

    # delete frames files
    if movie_clean_frames:
        command = 'cat %s | xargs rm ' % as_path(movie_dir, '%s.lst' %
                                                 (movie_name))
        execute_command(command)

    #delete listing of all files
    command = 'rm %s' % as_path(movie_dir, '%s.lst' % (movie_name))
    execute_command(command)

    return output_file
Beispiel #17
0
    def add_data_vector_segment(self, data_vector_segment, last_segment=False):
        self.message = None
        if self.__prefixed_movie_name__ == None:
            return

        if last_segment:
            self.__save_frames__()
            return

        frame_name = '%s_%06d' % (self.__prefixed_movie_name__, self.idx)
        frame_file = as_path(self.__prefixed_movie_dir__, frame_name + '.png')
        #skip_frame = True if self.idx < self.p.movie_skip_to_frame or \
        #    (self.p.movie_skip_frames and os.path.exists(frame_file)) \
        #    else False
        skip_frame = False

        mean_plus = pl.mean(data_vector_segment.signal_plus)
        mean_minus = pl.mean(data_vector_segment.signal_minus)

        s_plus = len(data_vector_segment.signal_plus)

        _pp_spec = MiniPoincarePlotSpec()
        _pp_spec.idx = self.idx
        _pp_spec.s_plus = s_plus
        _pp_spec.mean_plus = mean_plus
        _pp_spec.mean_minus = mean_minus
        _pp_spec.range = self.range
        _pp_spec.frame_file = frame_file

        if self.idx == 0:

            self.s_size = s_plus
            self.x_data.put(pl.arange(s_plus), data_vector_segment.signal_plus)
            self.y_data.put(pl.arange(s_plus),
                            data_vector_segment.signal_minus)

            ok = True
            old_s_plus = 0
            _pp_spec.level = 0
            _pp_spec.active_start = 0
            _pp_spec.active_stop = s_plus
        else:
            old_s_plus = len(self.old_signal_plus)
            ok = False
            if s_plus >= old_s_plus:
                if pl.all(self.old_signal_plus \
                            == data_vector_segment.signal_plus[:old_s_plus]):
                    old_size = self.s_size
                    new_size = old_size + s_plus - old_s_plus
                    if new_size > old_size:
                        _pp_spec.active_start = old_size
                        _pp_spec.active_stop = new_size

                        if new_size > len(self.x_data):
                            raise Exception(
                                'New size is greater then the signal size !')

                        self.x_data.put(
                            pl.arange(old_size, new_size),
                            data_vector_segment.signal_plus[old_s_plus -
                                                            s_plus:])

                        self.y_data.put(
                            pl.arange(old_size, new_size),
                            data_vector_segment.signal_minus[old_s_plus -
                                                             s_plus:])

                        _pp_spec.inactive_stop = \
                                        self._pp_spec_old.inactive_stop
                        self.s_size = new_size
                    _pp_spec.level = 1
                    ok = True
                else:
                    for idx in xrange(1, old_s_plus):
                        if pl.all(self.old_signal_plus[idx:] \
                                    == data_vector_segment.signal_plus[idx - 1:
                                                            old_s_plus - idx]):
                            old_size = self.s_size
                            new_size = old_size + s_plus - (old_s_plus - idx)

                            if new_size > len(self.x_data):
                                raise Exception(
                                    'New size is greater then the signal size !'
                                )

                            if new_size > old_size:
                                _pp_spec.active_start = old_size
                                _pp_spec.active_stop = new_size

                                self.x_data.put(
                                    pl.arange(old_size,
                                              new_size), data_vector_segment.
                                    signal_plus[old_s_plus - idx:])

                                self.y_data.put(
                                    pl.arange(old_size,
                                              new_size), data_vector_segment.
                                    signal_minus[old_s_plus - idx:])
                                self.s_size = new_size

                            _d = self.s_size - s_plus
                            _pp_spec.inactive_start = _d - idx
                            _pp_spec.inactive_stop = _d

                            _pp_spec.level = 3

                            ok = True
                            break
            else:
                for idx in xrange(1, old_s_plus):
                    if idx + s_plus <= old_s_plus \
                        and pl.all(
                            self.old_signal_plus[idx:idx + s_plus] \
                                    == data_vector_segment.signal_plus):

                        _d = self.s_size - old_s_plus
                        _pp_spec.inactive_start = _d
                        _pp_spec.inactive_stop = _d + idx

                        if _pp_spec.inactive_stop + s_plus < self.s_size:
                            _pp_spec.inactive_start_2 = \
                                        _pp_spec.inactive_stop + s_plus
                            _pp_spec.inactive_stop_2 = self.s_size
                        _pp_spec.level = 2

                        ok = True
                        break
        if ok == True and skip_frame == False:
            _pp_spec.x_data = self.x_data
            _pp_spec.y_data = self.y_data
            _pp_spec.cum_inactive = self.cum_inactive
            _pp_spec.s_size = self.s_size
            #print('PP_SPEC: ' + str(_p))

            self.pp_spec_manager.addMiniPoincarePlotSpec(_pp_spec)
            if self.idx > 0 and \
                (self.p.movie_bin_size > 0
                    and ((self.idx % self.p.movie_bin_size) == 0)):
                if len(self.pp_specs_managers) >= self.core_nums:
                    if self.p.movie_calculate_all_frames == False:
                        self.__save_frames__()
                        self.pp_specs_managers = []

                old_pp_spec_manager = self.pp_spec_manager
                self.pp_spec_manager = MiniPoincarePlotSpecManager()
                self.pp_spec_manager.movie_dir = self.__prefixed_movie_dir__
                self.pp_spec_manager.movie_name = self.__prefixed_movie_name__
                self.pp_spec_manager.movie_dpi = self.p.movie_dpi
                self.pp_spec_manager.movie_fps = self.p.movie_fps
                self.pp_spec_manager.movie_height = self.p.movie_height
                self.pp_spec_manager.movie_width = self.p.movie_width
                self.pp_spec_manager.active_color = self.active_color
                self.pp_spec_manager.inactive_color = self.inactive_color
                self.pp_spec_manager.centroid_color = self.centroid_color
                self.pp_spec_manager.active_point_size = \
                                                self.p.movie_active_size
                self.pp_spec_manager.inactive_point_size = \
                                                self.p.movie_inactive_size
                self.pp_spec_manager.centroid_point_size = \
                                                self.p.movie_centroid_size
                self.pp_spec_manager.show_plot_legends = \
                                            self.p.movie_show_plot_legends
                self.pp_spec_manager.x_label = self.p.x_label
                self.pp_spec_manager.y_label = self.p.y_label
                self.pp_spec_manager.clean_frames = self.p.movie_clean_frames
                self.pp_spec_manager.movie_title = self.p.movie_title
                self.pp_spec_manager.movie_frame_step = self.p.movie_frame_step
                self.pp_spec_manager.movie_identity_line = self.p.movie_identity_line
                self.pp_spec_manager.movie_hour_label = self.p.movie_hour_label
                self.pp_spec_manager.movie_minute_label = self.p.movie_minute_label
                self.pp_spec_manager.movie_second_label = self.p.movie_second_label
                self.pp_spec_manager.movie_time_label_in_line = self.p.movie_time_label_in_line
                self.pp_spec_manager.movie_time_label_font_size = self.p.movie_time_label_font_size
                self.pp_spec_manager.movie_time_label_prefix = self.p.movie_time_label_prefix
                self.pp_spec_manager.movie_title_font_size = self.p.movie_title_font_size
                self.pp_spec_manager.movie_axis_font_size = self.p.movie_axis_font_size
                self.pp_spec_manager.movie_axis_font = self.p.movie_axis_font
                self.pp_spec_manager.movie_title_font = self.p.movie_title_font
                self.pp_spec_manager.movie_tick_font = self.p.movie_tick_font
                self.pp_spec_manager.movie_frame_pad = self.p.movie_frame_pad
                self.pp_spec_manager.movie_create_time_label = self.p.movie_create_time_label
                self.pp_spec_manager.movie_frame_filename_with_time = self.p.movie_frame_filename_with_time

                #add all previous pp specs
                for pp_spec in old_pp_spec_manager.getMiniPoincarePlotSpecs():
                    self.pp_spec_manager.addPreviousPoincarePlotSpecMinimum(
                        pp_spec)
                old_pp_spec_manager = None

                self.pp_specs_managers.append(self.pp_spec_manager)
            self.message = 'Prepare frame: %s' % (frame_name)
        elif ok == True and skip_frame == True:
            self.message = 'Skip frame %s' % (frame_name)
        elif ok == False:
            print('s_plus: ' + str(s_plus) + ' old_s_plus: ' + str(old_s_plus))
            print('old_signal_plus: ' + str(self.old_signal_plus))
            print('signal_plus:     ' + str(data_vector_segment.signal_plus))
            raise Exception('Error for idx ' + str(self.idx))
        if _pp_spec.inactive_start >= 0 and _pp_spec.inactive_stop >= 0:
            #if time array is not None use it as array for cumulative time
            if not self.time == None:
                self.cum_inactive += pl.sum(
                    self.time[_pp_spec.inactive_start:_pp_spec.inactive_stop])
            else:
                self.cum_inactive += pl.sum(
                    self.x_data[_pp_spec.inactive_start:_pp_spec.
                                inactive_stop])

        self.old_signal_plus = data_vector_segment.signal_plus
        self.idx = self.idx + 1
        self._pp_spec_old = _pp_spec
Beispiel #18
0
root_dir = '/home/jurek/volumes/doctoral/monitor_do_impedancji_niccomo/'
result_dir = 'results_whole_for_R'
groups = ['_healthy', '_team']
#groups = ['_healthy']
parameters = ['SV', 'SVR', 'CO', 'PEP', 'LVET']
#parameters = ['SV']
#types = ['sliding', 'jumping']
#types = ['jumping']
types = ['whole']

for group in groups:
    for parameter in parameters:
        for _type in types:

            base_output_dir = as_path(root_dir, result_dir, group, _type) + fs.sep # @IgnorePep8

            #parameters
            parameter_dir = as_path(base_output_dir, parameter) + fs.sep
            pp = poincare.PoincarePlotManager()
            #if not _type == None:
            #pp.window_size = '5m'
            #pp.data_file = as_path(root_dir, group, '001.res')
            pp.data_dir = as_path(root_dir, group) + fs.sep
            pp.output_dir = parameter_dir
            pp.signal_label = parameter
            pp.time_label = 'time'
            pp.extension = '*.res'
            pp.statistics_names = 'sd1, sd2, sd1d, sd1a, sd2d, sd2a, sdnnd, sdnna, sdnn, c1d, c1a, c2a, c2d, ca, cd' # @IgnorePep8
            pp.output_separator = ';  '
            pp.override_existing_outcomes = True
Beispiel #19
0
root_dir = '/home/jurek/volumes/doctoral/monitor_do_impedancji_niccomo/'
result_dir = 'results_whole_for_R'
groups = ['_healthy', '_team']
#groups = ['_healthy']
parameters = ['SV', 'SVR', 'CO', 'PEP', 'LVET']
#parameters = ['SV']
#types = ['sliding', 'jumping']
#types = ['jumping']
types = ['whole']

for group in groups:
    for parameter in parameters:
        for _type in types:

            base_output_dir = as_path(root_dir, result_dir, group,
                                      _type) + fs.sep  # @IgnorePep8

            #parameters
            parameter_dir = as_path(base_output_dir, parameter) + fs.sep
            pp = poincare.PoincarePlotManager()
            #if not _type == None:
            #pp.window_size = '5m'
            #pp.data_file = as_path(root_dir, group, '001.res')
            pp.data_dir = as_path(root_dir, group) + fs.sep
            pp.output_dir = parameter_dir
            pp.signal_label = parameter
            pp.time_label = 'time'
            pp.extension = '*.res'
            pp.statistics_names = 'sd1, sd2, sd1d, sd1a, sd2d, sd2a, sdnnd, sdnna, sdnn, c1d, c1a, c2a, c2d, ca, cd'  # @IgnorePep8
            pp.output_separator = ';  '
            pp.override_existing_outcomes = True
    def add_data_vector_segment(self, data_vector_segment, last_segment=False):
        self.message = None
        if self.__prefixed_movie_name__ == None:
            return

        if last_segment:
            self.__save_frames__()
            return

        frame_name = '%s_%06d' % (self.__prefixed_movie_name__, self.idx)
        frame_file = as_path(self.__prefixed_movie_dir__, frame_name + '.png')
        #skip_frame = True if self.idx < self.p.movie_skip_to_frame or \
        #    (self.p.movie_skip_frames and os.path.exists(frame_file)) \
        #    else False
        skip_frame = False

        mean_plus = pl.mean(data_vector_segment.signal_plus)
        mean_minus = pl.mean(data_vector_segment.signal_minus)

        s_plus = len(data_vector_segment.signal_plus)

        _pp_spec = MiniPoincarePlotSpec()
        _pp_spec.idx = self.idx
        _pp_spec.s_plus = s_plus
        _pp_spec.mean_plus = mean_plus
        _pp_spec.mean_minus = mean_minus
        _pp_spec.range = self.range
        _pp_spec.frame_file = frame_file

        if self.idx == 0:

            self.s_size = s_plus
            self.x_data.put(pl.arange(s_plus), data_vector_segment.signal_plus)
            self.y_data.put(pl.arange(s_plus),
                                            data_vector_segment.signal_minus)

            ok = True
            old_s_plus = 0
            _pp_spec.level = 0
            _pp_spec.active_start = 0
            _pp_spec.active_stop = s_plus
        else:
            old_s_plus = len(self.old_signal_plus)
            ok = False
            if s_plus >= old_s_plus:
                if pl.all(self.old_signal_plus \
                            == data_vector_segment.signal_plus[:old_s_plus]):
                    old_size = self.s_size
                    new_size = old_size + s_plus - old_s_plus
                    if new_size > old_size:
                        _pp_spec.active_start = old_size
                        _pp_spec.active_stop = new_size

                        if new_size > len(self.x_data):
                            raise Exception(
                                'New size is greater then the signal size !')

                        self.x_data.put(pl.arange(old_size, new_size),
                            data_vector_segment.signal_plus[old_s_plus
                                                            - s_plus:])

                        self.y_data.put(pl.arange(old_size, new_size),
                            data_vector_segment.signal_minus[old_s_plus
                                                             - s_plus:])

                        _pp_spec.inactive_stop = \
                                        self._pp_spec_old.inactive_stop
                        self.s_size = new_size
                    _pp_spec.level = 1
                    ok = True
                else:
                    for idx in xrange(1, old_s_plus):
                        if pl.all(self.old_signal_plus[idx:] \
                                    == data_vector_segment.signal_plus[idx - 1:
                                                            old_s_plus - idx]):
                            old_size = self.s_size
                            new_size = old_size + s_plus - (old_s_plus - idx)

                            if new_size > len(self.x_data):
                                raise Exception(
                                'New size is greater then the signal size !')

                            if new_size > old_size:
                                _pp_spec.active_start = old_size
                                _pp_spec.active_stop = new_size

                                self.x_data.put(pl.arange(old_size, new_size),
                                    data_vector_segment.signal_plus[
                                                           old_s_plus - idx:])

                                self.y_data.put(pl.arange(old_size, new_size),
                                    data_vector_segment.signal_minus[
                                                           old_s_plus - idx:])
                                self.s_size = new_size

                            _d = self.s_size - s_plus
                            _pp_spec.inactive_start = _d - idx
                            _pp_spec.inactive_stop = _d

                            _pp_spec.level = 3

                            ok = True
                            break
            else:
                for idx in xrange(1, old_s_plus):
                    if idx + s_plus <= old_s_plus \
                        and pl.all(
                            self.old_signal_plus[idx:idx + s_plus] \
                                    == data_vector_segment.signal_plus):

                        _d = self.s_size - old_s_plus
                        _pp_spec.inactive_start = _d
                        _pp_spec.inactive_stop = _d + idx

                        if _pp_spec.inactive_stop + s_plus < self.s_size:
                            _pp_spec.inactive_start_2 = \
                                        _pp_spec.inactive_stop + s_plus
                            _pp_spec.inactive_stop_2 = self.s_size
                        _pp_spec.level = 2

                        ok = True
                        break
        if ok == True and skip_frame == False:
            _pp_spec.x_data = self.x_data
            _pp_spec.y_data = self.y_data
            _pp_spec.cum_inactive = self.cum_inactive
            _pp_spec.s_size = self.s_size
            #print('PP_SPEC: ' + str(_p))

            self.pp_spec_manager.addMiniPoincarePlotSpec(_pp_spec)
            if self.idx > 0 and \
                (self.p.movie_bin_size > 0
                    and ((self.idx % self.p.movie_bin_size) == 0)):
                if len(self.pp_specs_managers) >= self.core_nums:
                    if self.p.movie_calculate_all_frames == False:
                        self.__save_frames__()
                        self.pp_specs_managers = []

                old_pp_spec_manager = self.pp_spec_manager
                self.pp_spec_manager = MiniPoincarePlotSpecManager()
                self.pp_spec_manager.movie_dir = self.__prefixed_movie_dir__
                self.pp_spec_manager.movie_name = self.__prefixed_movie_name__
                self.pp_spec_manager.movie_dpi = self.p.movie_dpi
                self.pp_spec_manager.movie_fps = self.p.movie_fps
                self.pp_spec_manager.movie_height = self.p.movie_height
                self.pp_spec_manager.movie_width = self.p.movie_width
                self.pp_spec_manager.active_color = self.active_color
                self.pp_spec_manager.inactive_color = self.inactive_color
                self.pp_spec_manager.centroid_color = self.centroid_color
                self.pp_spec_manager.active_point_size = \
                                                self.p.movie_active_size
                self.pp_spec_manager.inactive_point_size = \
                                                self.p.movie_inactive_size
                self.pp_spec_manager.centroid_point_size = \
                                                self.p.movie_centroid_size
                self.pp_spec_manager.show_plot_legends = \
                                            self.p.movie_show_plot_legends
                self.pp_spec_manager.x_label = self.p.x_label
                self.pp_spec_manager.y_label = self.p.y_label
                self.pp_spec_manager.clean_frames = self.p.movie_clean_frames
                self.pp_spec_manager.movie_title = self.p.movie_title
                self.pp_spec_manager.movie_frame_step = self.p.movie_frame_step
                self.pp_spec_manager.movie_identity_line = self.p.movie_identity_line
                self.pp_spec_manager.movie_hour_label = self.p.movie_hour_label
                self.pp_spec_manager.movie_minute_label = self.p.movie_minute_label
                self.pp_spec_manager.movie_second_label = self.p.movie_second_label
                self.pp_spec_manager.movie_time_label_in_line = self.p.movie_time_label_in_line
                self.pp_spec_manager.movie_time_label_font_size = self.p.movie_time_label_font_size
                self.pp_spec_manager.movie_time_label_prefix = self.p.movie_time_label_prefix
                self.pp_spec_manager.movie_title_font_size = self.p.movie_title_font_size
                self.pp_spec_manager.movie_axis_font_size = self.p.movie_axis_font_size
                self.pp_spec_manager.movie_axis_font = self.p.movie_axis_font
                self.pp_spec_manager.movie_title_font = self.p.movie_title_font
                self.pp_spec_manager.movie_tick_font = self.p.movie_tick_font
                self.pp_spec_manager.movie_frame_pad = self.p.movie_frame_pad
                self.pp_spec_manager.movie_create_time_label = self.p.movie_create_time_label
                self.pp_spec_manager.movie_frame_filename_with_time = self.p.movie_frame_filename_with_time

                #add all previous pp specs
                for pp_spec in old_pp_spec_manager.getMiniPoincarePlotSpecs():
                    self.pp_spec_manager.addPreviousPoincarePlotSpecMinimum(
                                                                    pp_spec)
                old_pp_spec_manager = None

                self.pp_specs_managers.append(self.pp_spec_manager)
            self.message = 'Prepare frame: %s' % (frame_name)
        elif ok == True and skip_frame == True:
            self.message = 'Skip frame %s' % (frame_name)
        elif ok == False:
            print('s_plus: ' + str(s_plus) + ' old_s_plus: ' + str(old_s_plus))
            print('old_signal_plus: ' + str(self.old_signal_plus))
            print('signal_plus:     ' + str(data_vector_segment.signal_plus))
            raise Exception('Error for idx ' + str(self.idx))
        if _pp_spec.inactive_start >= 0 and _pp_spec.inactive_stop >= 0:
            #if time array is not None use it as array for cumulative time
            if not self.time == None:
                self.cum_inactive += pl.sum(
                    self.time[
                            _pp_spec.inactive_start:_pp_spec.inactive_stop])
            else:
                self.cum_inactive += pl.sum(
                    self.x_data[
                            _pp_spec.inactive_start:_pp_spec.inactive_stop])

        self.old_signal_plus = data_vector_segment.signal_plus
        self.idx = self.idx + 1
        self._pp_spec_old = _pp_spec
Beispiel #21
0
def __generate_multiprocessing_movie__(movie_name, movie_dir,
                                   movie_width, movie_height,
                                   movie_fps, movie_frames,
                                   movie_clean_frames):
    """
    generating a movie with given specification using multiprocessing;
    all frames are divided into excluded sets of files, for each set
    a movie is create, in the next step all parts are joined to create
    one final movie
    """

    #delete previous partial listing files
    command = 'rm %s' % as_path(movie_dir, '%s_lst_*' % (movie_name))
    execute_command(command)

    #create listing of all files
    listing_file = str(as_path(movie_dir, movie_name + ".lst"))
    #command = 'ls %s > %s' % (as_path(movie_dir, movie_frames), listing_file)
    #a better version with no trouble with a limit of number of files of ls command
    command = '(cd %s; find $PWD -name \'*.png\' | sort > %s)' % (movie_dir, listing_file)
    execute_command(command)

    #get number of movie frames
    command = 'wc -l %s' % listing_file
    count = int(execute_command(command, 1).split()[0])

    #get bytes per line
    command = 'head %s' % listing_file
    bytes_per_line = int(len(execute_command(command, 1)))

    #divide number of files frames per processors count
    size = count / multiprocessing.cpu_count()

    #split listing file into equal parts prefixed <movie_name>_lst_
    command = '(cd %s; split -C %i %s %s_lst_)' % \
                (movie_dir, bytes_per_line * size, listing_file, movie_name)
    execute_command(command)

    #delete all previous movie parts
    command = 'rm %s' % as_path(movie_dir, '%s_part_*.avi' % (movie_name))
    execute_command(command)

    movie_specifications = []
    #iterate over splits to create movie_specifications objects
    command = 'ls ' + as_path(movie_dir, '%s_lst_*' % (movie_name))
    for idx, frames_file in enumerate(run_command(command)):
        frames_file = frames_file.replace('\n', '')
        movie_specification = MovieSpecification(movie_name, movie_dir,
                            movie_width, movie_height, movie_fps, frames_file,
                            idx, movie_frames)
        movie_specifications.append(movie_specification)

    #run generation of partial movies
    pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
    #if not self.params.info_message_handler == None:
    #    self.params.info_message_handler('Generating frames'
    #                                 + (' ' * 20))
    pool.map(__generate_movie__, movie_specifications)
    pool.close()

    #join all video part movies
    movie_draft = movie_name + '_draft.avi'
    command = '(cd %s; cat %s_part_*.avi  > %s)' % (
                                    movie_dir, movie_name, movie_draft)
    execute_command(command)

    #convert draft file into final movie file
    output_file = '%s.avi' % (movie_name)
    command = '(cd %s; mencoder %s -o %s -forceidx -ovc copy -oac copy)' \
                 % (movie_dir, movie_draft, output_file)
    execute_command(command)

    #delete draft movie
    command = 'rm %s' % as_path(movie_dir, movie_draft)
    execute_command(command)

    #delete all movie parts
    command = 'rm %s' % as_path(movie_dir, '%s_part_*.avi' % (movie_name))
    execute_command(command)

    #delete partial listing files
    command = 'rm %s' % as_path(movie_dir, '%s_lst_*' % (movie_name))
    execute_command(command)

    # delete frames files
    if movie_clean_frames:
        command = 'cat %s | xargs rm ' % as_path(movie_dir, '%s.lst' % (movie_name))
        execute_command(command)

    #delete listing of all files
    command = 'rm %s' % as_path(movie_dir, '%s.lst' % (movie_name))
    execute_command(command)

    return output_file