def __compute__(self):
        self.holidays = dict()
        for x in Database.team():
            self.holidays[x.id] = x.__holidays__(self)  # computing holidays for nephrologists

        for (yesterday, today) in self.iterate():
            if yesterday is not None:
                yesterday_profile = self.daily_plannings[date(yesterday.year, yesterday.month, yesterday.day)].profile
            else:
                yesterday_profile = None

            current_daily_planning = self.daily_plannings[
                date(today.year, today.month, today.day)
            ]  # the daily planning for the current day

            if current_daily_planning.weekday == 0:
                # recomputing holidays to take obligation recovery into account
                self.holidays = dict()
                for x in Database.team():
                    self.holidays[x.id] = x.__holidays__(self)  # computing holidays for nephrologists

            # if today_date is weekend day or today_date is holiday
            if current_daily_planning.weekday in [4, 5, 6] or not current_daily_planning.is_working_day:
                # allocate all damn day in the same time
                current_daily_planning.__allocate_whole_day__(
                    ConstraintStrategy.ALLOCATE_WEEKEND_DAYS.value, yesterday_profile, self.holidays
                )
                current_daily_planning.__allocate_whole_day__(
                    ConstraintStrategy.ALLOCATE_HOLIDAYS.value, yesterday_profile, self.holidays
                )

            # allocate all day's timeslots separately
            for current_timeslot in current_daily_planning.profile:
                if current_daily_planning.weekday in [0, 5, 6]:
                    current_daily_planning.__allocate_timeslot__(
                        ConstraintStrategy.ALLOCATE_MORNING_DIALYSIS.value,
                        yesterday_profile,
                        current_timeslot,
                        self.holidays,
                    )
                current_daily_planning.__allocate_timeslot__(
                    ConstraintStrategy.FOCUS_ON_PREFERENCES.value + ConstraintStrategy.DISCARD_COUNTERS.value,
                    yesterday_profile,
                    current_timeslot,
                    self.holidays,
                )
                current_daily_planning.__allocate_timeslot__(
                    ConstraintStrategy.NONE.value, yesterday_profile, current_timeslot, self.holidays
                )
Esempio n. 2
0
    def __compute__(self):
        self.holidays = dict()
        for x in Database.team():
            self.holidays[x.id] = x.__holidays__(
                self)  # computing holidays for nephrologists

        for (yesterday, today) in self.iterate():
            if yesterday is not None:
                yesterday_profile = self.daily_plannings[date(
                    yesterday.year, yesterday.month, yesterday.day)].profile
            else:
                yesterday_profile = None

            current_daily_planning = self.daily_plannings[date(
                today.year, today.month,
                today.day)]  # the daily planning for the current day

            if current_daily_planning.weekday == 0:
                # recomputing holidays to take obligation recovery into account
                self.holidays = dict()
                for x in Database.team():
                    self.holidays[x.id] = x.__holidays__(
                        self)  # computing holidays for nephrologists

            # if today_date is weekend day or today_date is holiday
            if current_daily_planning.weekday in [
                    4, 5, 6
            ] or not current_daily_planning.is_working_day:
                # allocate all damn day in the same time
                current_daily_planning.__allocate_whole_day__(
                    ConstraintStrategy.ALLOCATE_WEEKEND_DAYS.value,
                    yesterday_profile, self.holidays)
                current_daily_planning.__allocate_whole_day__(
                    ConstraintStrategy.ALLOCATE_HOLIDAYS.value,
                    yesterday_profile, self.holidays)

            # allocate all day's timeslots separately
            for current_timeslot in current_daily_planning.profile:
                if current_daily_planning.weekday in [0, 5, 6]:
                    current_daily_planning.__allocate_timeslot__(
                        ConstraintStrategy.ALLOCATE_MORNING_DIALYSIS.value,
                        yesterday_profile, current_timeslot, self.holidays)
                current_daily_planning.__allocate_timeslot__(
                    ConstraintStrategy.FOCUS_ON_PREFERENCES.value +
                    ConstraintStrategy.DISCARD_COUNTERS.value,
                    yesterday_profile, current_timeslot, self.holidays)
                current_daily_planning.__allocate_timeslot__(
                    ConstraintStrategy.NONE.value, yesterday_profile,
                    current_timeslot, self.holidays)
Esempio n. 3
0
 def __get__(cls, nephrologist_id):
     candidate = [x for x in Database.team() if x.id == nephrologist_id]
     if len(candidate) == 0:
         raise UserWarning(
             "nephrologist id provided does not match any existing nephrologist."
         )
     else:
         return candidate[0]
Esempio n. 4
0
    def __team_problem__(self, current_timeslot, holidays, offset_range=[]):
        current_team = [x for x in Database.team() if x.id not in self.__currently_allocated_nephrologists__(current_timeslot)]

        '''
        if yesterday_profile is not None:
            offset_range = range(1, 4)
            # The eligible nephrologist
            if TimeSlot.THIRD_SHIFT in yesterday_profile:
                yesterday_obligations = list(set(self.obligation_activities) & set([x for x in yesterday_profile[TimeSlot.THIRD_SHIFT]]))
                if len(yesterday_obligations) == 1 and yesterday_profile[TimeSlot.THIRD_SHIFT][yesterday_obligations[0]] is not None:
                    current_team = [x for x in current_team if x.id is not yesterday_profile[TimeSlot.THIRD_SHIFT][yesterday_obligations[0]].id]
        else:
            offset_range = []
        '''

        # check whether nephrologist is scheduled for vacation for current day + days before/after in the offset range
        for offset in list(set([0]) | set(offset_range)):
            current_team = [x for x in current_team if not self.__is_in_holiday__(x, current_timeslot, holidays, offset)]

        current_activities = [x for x in self.profile[current_timeslot] if not self.__is_currently_allocated_activity__(current_timeslot, x)]

        problem = None
        if len(current_activities) > 0:

            '''
            shuffle(current_team)
            shuffle(current_activities)
            '''

            # get rid of nephrologist that have declared all current activities as aversions
            current_team = [x for x in current_team if any([x.score(self.weekday, current_timeslot, act) >= 0 for act in current_activities])]

            if len(current_team) > 0:
                # instantiate a new problem
                problem = Problem()

                # add the different activities to allocate
                problem.addVariable(self.nephrologist_key, current_team)  # the whole nephrologists minus nephrologists in vacation on this specific day/shift
                problem.addVariable(self.activity_key, current_activities)  # the whole nephrologists that can be allocated on this specific day/shift

                # constraints declaration
                problem.addConstraint(AllDifferentConstraint())
                problem.addConstraint(lambda nep, act: act in nep.activities, (self.nephrologist_key, self.activity_key))

        return current_team, problem
    def insert_data_into_sql(self):
        db = Database()

        # Create the Train Table
        create_command = 'create table if not exists train (trainid INT AUTO_INCREMENT,'
        collist = ''
        for column in self.df.columns:
            create_command += column + ' TEXT,'
            collist += column + ','
        collist = collist[:-1] + ");"

        create_command += 'primary key (trainid));'
        db.execute_query_with_params(create_command, {})

        # Load the Training File
        base_path = os.getcwd().replace('\\', '/') + '/'
        load_command = "LOAD DATA LOCAL INFILE '" + base_path + self.input_file + \
                       "' INTO TABLE train FIELDS TERMINATED BY ',' " \
                       "LINES TERMINATED BY '\\n' " \
                       "IGNORE 1 LINES (" + collist

        db.execute_query_with_params(load_command, {})
        return
Esempio n. 6
0
 def __get__(cls, nephrologist_id):
     candidate = [x for x in Database.team() if x.id == nephrologist_id]
     if len(candidate) == 0:
         raise UserWarning("nephrologist id provided does not match any existing nephrologist.")
     else:
         return candidate[0]
def main():
    log.info("*****Clustering Application*****")

    # Create a model for clustering patents
    model = Clustering(method="rbr",
                       criterion="i2",
                       similarity="cos",
                       cluster_choice="best",
                       rowmodel="none",
                       colmodel="none",
                       trials=10,
                       showfeatures=False,
                       showsummaries=True,
                       summary_method="cliques",
                       showtree=False,
                       zscores=False,
                       plotclusters=True,
                       plotformat="ps")

    # Create an object of 'Database'
    db = Database(verbose=True)

    # Connect to SQLite database
    db.connect(in_memory=True, load_from=PATENT_EMBEDDING_DATABASE)

    # Dummy document collection
    documents = []
    for root, folders, files in os.walk(config.CLUSTERING_BENCHMARK_DATA):
        for file in files:
            if not file.startswith('.'):
                if file.endswith(""):
                    document_name = file
                    document_category = root.rsplit(os.sep, 1)[1]
                    document_label = document_category + "." + document_name
                    documents.append(document_label)

    # Generate matrix of document embeddings
    model.patent2mat(documents,
                     rows=len(documents),
                     columns=300,
                     database=db,
                     search_on=PRIMARY_KEY,
                     matrix=PATENT_MATRIX,
                     labels=LABELS,
                     classes=CLASSES,
                     path=PATENT_CLUSTERING_PATH)

    # Close connection to SQLite database
    db.close()

    # Cluster documents
    model.train(matrix=PATENT_MATRIX,
                labels=LABELS,
                classes=CLASSES,
                use_patent_classes=True,
                k=20,
                iterations=20,
                patent_clusters=PATENT_CLUSTER,
                plot=PATENT_CLUSTER_PLOT,
                path=PATENT_CLUSTERING_PATH)

    # Clean all un-necessary files
    clean(
        cleanSample=True,
        cleanModel=False,
        cleanDocvecs=True,
        cleanDatabase=False,
        cleanClusters=True,
        filter=["PatentCluster", "PatentCluster.ps", "PatentEmbedding.rclass"])
Esempio n. 8
0
def main():
    log.info("*****Patent2Vec Application*****")

    # Preprocess patent documents
    log.info("Preprocessing patent documents")
    patents = PatentDocument(SOURCE_DATASET,
                             extension="",
                             use_conceptualizer=True,
                             transform_conceptualizer=True,
                             enable_sampling=True,
                             train_ratio=1.0,
                             test_ratio=0.0)

    # Create Patent2Vec model
    models = OrderedDict()

    # PV-DM with average
    models["PV_DM_Mean"] = \
        Patent2Vec(dm=1, dm_mean=1, dm_concat=0, min_word_count=5, size=500,
                   context_window_size=8, negative=2, iter=50, workers=CPU_CORE,
                   use_less_memory=False, docvecs_mapfile=DOCVECS_MAP)
    models["PV_DM_Mean"].build(patents)
    models["PV_DM_Mean"].intersect_with_pretrained_embedding(
        PRETRAINED_EMBEDDING, binary=False)
    #     models["PV_DM_Mean"].load(PATENT2VEC_MODEL)

    #     # PV-DM with concatenation
    #     models["PV_DM_Concatenation"] = \
    #         Patent2Vec(dm=1, dm_mean=0, dm_concat=1, min_word_count=5, size=500,
    #                    context_window_size=8, negative=2, iter=50, workers=CPU_CORE,
    #                    use_less_memory=False, docvecs_mapfile=DOCVECS_MAP)
    #     models["PV_DM_Concatenation"].reuse_from(models["PV_DM_Mean"])
    # #     models["PV_DM_Concatenation"].build(patents)
    # #     models["PV_DM_Concatenation"].intersect_with_pretrained_embedding(PRETRAINED_EMBEDDING,
    # #                                                                       binary=False)
    # # #     models["PV_DM_Concatenation"].load(PATENT2VEC_MODEL)

    #     # PV-DBOW
    #     models["PV_DBOW"] = \
    #         Patent2Vec(dm=0, dm_mean=0, dm_concat=0, min_word_count=5, size=500,
    #                    context_window_size=8, negative=2, iter=50, workers=CPU_CORE,
    #                    use_less_memory=False, docvecs_mapfile=DOCVECS_MAP)
    #     models["PV_DBOW"].reuse_from(models["PV_DM_Mean"])
    # #     models["PV_DBOW"].build(patents)
    # #     models["PV_DBOW"].intersect_with_pretrained_embedding(PRETRAINED_EMBEDDING,
    # #                                                           binary=False)
    # # #     models["PV_DBOW"].load(PATENT2VEC_MODEL)

    #     # Mixed models
    #     models["DBOW + DM with average"] = ConcatenatedPatent2Vec([models["PV_DBOW"],
    #                                                                models["PV_DM_Mean"]])
    #     models["DBOW + DM with concatenation"] = ConcatenatedPatent2Vec([models["PV_DBOW"],
    #                                                                      models["PV_DM_Concatenation"]])

    for name, model in models.items():
        # Train Patent2Vec model
        start_time = time.time()
        model.train(patents,
                    alpha=0.1,
                    min_alpha=0.0001,
                    passes=10,
                    fixed_alpha=False)
        end_time = time.time()
        log.info("Total time elapsed: %r", (end_time - start_time))

        # Evaluate Patent2Vec model
        model.evaluate()

        # Save Patent2Vec model
        model.save(model=PATENT2VEC_MODEL)

        # Create a database object
        db = Database(verbose=True)

        # Connect to database
        db.connect(in_memory=True)

        # Create a new table for storing document embeddings
        db.create_table(table=PATENT_EMBEDDING_TABLE,
                        primary_column=PRIMARY_KEY,
                        other_columns=FIELDS)

        # Save document embeddings
        model.save_document_embeddings(document_embeddings=PATENT_EMBEDDING,
                                       rows=len(patents),
                                       columns=500,
                                       database=db,
                                       table_name=PATENT_EMBEDDING_TABLE,
                                       save_patent_category=True,
                                       prepend_document_category=True)

        # Test documents
        if not os.path.exists(TESTING_DATA):
            raise PathNotFoundError("Path does not exist: %s" % TESTING_DATA)

        with open(TESTING_DATA, "r") as t:
            test_documents = t.readlines()
            test_documents = map(lambda x: x.strip(), test_documents)
            test_documents = filter(None, test_documents)

        # Preprocessed test documents
        preprocessed_test_documents = patents.get_preprocessed_corpus(
            test_documents)

        # Predict document embeddings
        model.predict(preprocessed_test_documents,
                      alpha=0.1,
                      min_alpha=0.0001,
                      steps=50,
                      save=True,
                      database=db,
                      table_name=PATENT_EMBEDDING_TABLE,
                      save_patent_category=True,
                      prepend_document_category=True)

        # Create an index on document embedding table
        db.create_index(index=PATENT_EMBEDDING_INDEX,
                        table=PATENT_EMBEDDING_TABLE,
                        index_by_column=PRIMARY_KEY[0])

        # Close database connection
        db.close(save_to=PATENT_EMBEDDING_DATABASE)

        # Delete temporary training data
        model.clean()

    # Test document for checking the quality of Patent2Vec model
    patents.set_token_only(True)
    preprocessed_test_document = patents.get_preprocessed_document(
        TEST_DOCUMENT)
    patents.set_token_only(False)

    # Check quality of Patent2Vec model
    if preprocessed_test_document is not None:
        log.info("Check quality of Patent2Vec model")
        log.info("Top matches for test document: %s", TEST_DOCUMENT)

        for name, model in models.items():
            embedding = model.infer(preprocessed_test_document)

            top_matches = model.model.docvecs.most_similar(
                positive=[embedding], negative=[], topn=10)
            top_matches = map(lambda x: x[0] + "\t\t" + str(x[1]), top_matches)

            for top_match in top_matches:
                log.info(top_match)

    # Clean all un-necessary files
    clean(cleanSample=True,
          cleanModel=False,
          cleanDocvecs=True,
          cleanDatabase=False,
          cleanClusters=False,
          filter=[])
Esempio n. 9
0
    def __team_problem__(self, current_timeslot, holidays, offset_range=[]):
        current_team = [
            x for x in Database.team() if x.id not in
            self.__currently_allocated_nephrologists__(current_timeslot)
        ]
        '''
        if yesterday_profile is not None:
            offset_range = range(1, 4)
            # The eligible nephrologist
            if TimeSlot.THIRD_SHIFT in yesterday_profile:
                yesterday_obligations = list(set(self.obligation_activities) & set([x for x in yesterday_profile[TimeSlot.THIRD_SHIFT]]))
                if len(yesterday_obligations) == 1 and yesterday_profile[TimeSlot.THIRD_SHIFT][yesterday_obligations[0]] is not None:
                    current_team = [x for x in current_team if x.id is not yesterday_profile[TimeSlot.THIRD_SHIFT][yesterday_obligations[0]].id]
        else:
            offset_range = []
        '''

        # check whether nephrologist is scheduled for vacation for current day + days before/after in the offset range
        for offset in list(set([0]) | set(offset_range)):
            current_team = [
                x for x in current_team if not self.__is_in_holiday__(
                    x, current_timeslot, holidays, offset)
            ]

        current_activities = [
            x for x in self.profile[current_timeslot] if
            not self.__is_currently_allocated_activity__(current_timeslot, x)
        ]

        problem = None
        if len(current_activities) > 0:
            '''
            shuffle(current_team)
            shuffle(current_activities)
            '''

            # get rid of nephrologist that have declared all current activities as aversions
            current_team = [
                x for x in current_team if any([
                    x.score(self.weekday, current_timeslot, act) >= 0
                    for act in current_activities
                ])
            ]

            if len(current_team) > 0:
                # instantiate a new problem
                problem = Problem()

                # add the different activities to allocate
                problem.addVariable(
                    self.nephrologist_key, current_team
                )  # the whole nephrologists minus nephrologists in vacation on this specific day/shift
                problem.addVariable(
                    self.activity_key, current_activities
                )  # the whole nephrologists that can be allocated on this specific day/shift

                # constraints declaration
                problem.addConstraint(AllDifferentConstraint())
                problem.addConstraint(
                    lambda nep, act: act in nep.activities,
                    (self.nephrologist_key, self.activity_key))

        return current_team, problem
Esempio n. 10
0
    def output(self):
        # from xlrd import open_workbook
        from xlwt import Workbook, XFStyle, Borders, Alignment, Font, Pattern, Style, easyxf

        # from xlutils.copy import copy

        """
        rb = open_workbook(r"Templates\template.xls")
        wb = copy(rb)

        s = wb.get_sheet(0)
        s.write(0, 0, 'A1')
        wb.save(r"C:\Temp\nephro-planner\new.xls")
        """

        book = Workbook(encoding="utf-8")
        sheet = book.add_sheet(r"Feuille1")

        style_title = XFStyle()
        font_title = Font()
        font_title.name = "Comic Sans MS"
        font_title.height = 280
        style_title.font = font_title
        style_title.alignment.horz = Alignment.HORZ_CENTER
        style_title.alignment.vert = Alignment.VERT_CENTER

        style_cell_bottom = XFStyle()
        borders_cell_bottom = Borders()
        borders_cell_bottom.bottom = Borders.MEDIUM
        style_cell_bottom.borders = borders_cell_bottom

        style_header = XFStyle()
        font_header = Font()
        font_header.bold = 1
        font_header.name = "Arial Narrow"
        font_header.height = 240
        style_header.font = font_header
        style_header.alignment.horz = Alignment.HORZ_CENTER
        style_header.alignment.vert = Alignment.VERT_CENTER
        borders_header = Borders()
        borders_header.top = Borders.MEDIUM
        borders_header.left = Borders.MEDIUM
        borders_header.bottom = Borders.MEDIUM
        borders_header.right = Borders.MEDIUM
        style_header.borders = borders_header

        style_sub_header = XFStyle()
        font_sub_header = Font()
        font_sub_header.name = "Arial Narrow"
        font_sub_header.height = 240
        style_sub_header.font = font_sub_header
        style_sub_header.alignment.horz = Alignment.HORZ_CENTER
        style_sub_header.alignment.vert = Alignment.VERT_CENTER
        borders_sub_header = Borders()
        borders_sub_header.top = Borders.MEDIUM
        borders_sub_header.left = Borders.MEDIUM
        borders_sub_header.bottom = Borders.MEDIUM
        borders_sub_header.right = Borders.MEDIUM
        style_sub_header.borders = borders_sub_header

        style_date = XFStyle()
        font_date = Font()
        font_date.name = "Arial Narrow"
        font_date.height = 240
        style_date.font = font_date
        style_date.alignment.horz = Alignment.HORZ_RIGHT
        style_date.alignment.vert = Alignment.VERT_CENTER
        borders_date = Borders()
        borders_date.left = Borders.MEDIUM
        style_date.borders = borders_date

        style_date_status = XFStyle()
        font_date_status = Font()
        font_date_status.name = "Arial Narrow"
        font_date_status.height = 240
        style_date_status.font = font_date_status
        style_date_status.alignment.horz = Alignment.HORZ_LEFT
        style_date_status.alignment.vert = Alignment.VERT_CENTER
        borders_date_status = Borders()
        borders_date_status.right = Borders.MEDIUM
        style_date_status.borders = borders_date_status

        style_cell_normal = XFStyle()
        font_cell_normal = Font()
        font_cell_normal.name = "Arial Narrow"
        font_cell_normal.height = 220
        style_cell_normal.font = font_cell_normal
        style_cell_normal.alignment.horz = Alignment.HORZ_CENTER
        style_cell_normal.alignment.vert = Alignment.VERT_CENTER

        style_cell_right = XFStyle()
        font_cell_right = Font()
        font_cell_right.name = "Arial Narrow"
        font_cell_right.height = 220
        style_cell_right.font = font_cell_right
        style_cell_right.alignment.horz = Alignment.HORZ_CENTER
        style_cell_right.alignment.vert = Alignment.VERT_CENTER
        borders_cell_right = Borders()
        borders_cell_right.right = Borders.MEDIUM
        style_cell_right.borders = borders_cell_right

        style_cell_top = XFStyle()
        borders_cell_top = Borders()
        borders_cell_top.top = Borders.MEDIUM
        style_cell_top.borders = borders_cell_top

        column_offset = 1
        date_column_offset = 2
        row_offset = 1
        table_width = 1 + 3 * len(Database.team())

        # build titles
        sheet.write_merge(row_offset, row_offset, column_offset, table_width + 1, "POLE MEDECINE INTERNE", style_title)
        sheet.write_merge(
            row_offset + 1,
            row_offset + 1,
            column_offset,
            table_width + 1,
            "Service NEPHROLOGIE – HEMODIALYSE",
            style_title,
        )
        sheet.write_merge(
            row_offset + 2,
            row_offset + 2,
            column_offset,
            table_width + 1,
            "Planning de {0} {1}".format(self.human_readable_months[self.month - 1], self.year),
            style_title,
        )

        # patch date columns top borders
        sheet.write(row_offset + 5, column_offset, "", style_cell_bottom)
        sheet.write(row_offset + 5, column_offset + 1, "", style_cell_bottom)

        # build header and sub header
        for x in Database.team():
            sheet.write_merge(
                row_offset + 4,
                row_offset + 4,
                column_offset + date_column_offset + 3 * (x.id - 1),
                column_offset + date_column_offset + 3 * x.id - 1,
                x.name,
                style_header,
            )
            sheet.write(row_offset + 5, column_offset + date_column_offset + 3 * (x.id - 1), "M", style_sub_header)
            sheet.write(row_offset + 5, column_offset + date_column_offset + 3 * (x.id - 1) + 1, "AM", style_sub_header)
            sheet.write(row_offset + 5, column_offset + date_column_offset + 3 * (x.id - 1) + 2, "N", style_sub_header)

        """
        i = 40
        for x in sorted(Style.colour_map):
            style = XFStyle()
            pattern = Pattern()
            pattern.pattern = Pattern.SOLID_PATTERN
            pattern.pattern_fore_colour = Style.colour_map[x]
            style.pattern = pattern
            sheet.write(i, 1, x, style)
            i += 1
        """

        pattern_pale_blue = Pattern()
        pattern_pale_blue.pattern = Pattern.SOLID_PATTERN
        pattern_pale_blue.pattern_fore_colour = Style.colour_map["pale_blue"]

        pattern_light_yellow = Pattern()
        pattern_light_yellow.pattern = Pattern.SOLID_PATTERN
        pattern_light_yellow.pattern_fore_colour = Style.colour_map["light_yellow"]

        pattern_ice_blue = Pattern()
        pattern_ice_blue.pattern = Pattern.SOLID_PATTERN
        pattern_ice_blue.pattern_fore_colour = Style.colour_map["ice_blue"]

        pattern_light_green = Pattern()
        pattern_light_green.pattern = Pattern.SOLID_PATTERN
        pattern_light_green.pattern_fore_colour = Style.colour_map["light_green"]

        pattern_ivory = Pattern()
        pattern_ivory.pattern = Pattern.SOLID_PATTERN
        pattern_ivory.pattern_fore_colour = Style.colour_map["ivory"]

        pattern_tan = Pattern()
        pattern_tan.pattern = Pattern.SOLID_PATTERN
        pattern_tan.pattern_fore_colour = Style.colour_map["tan"]

        pattern_gold = Pattern()
        pattern_gold.pattern = Pattern.SOLID_PATTERN
        pattern_gold.pattern_fore_colour = Style.colour_map["gold"]

        def __cell_colouration__(style, current_activity):
            if current_activity is Activity.CONSULTATION:
                style.pattern = pattern_pale_blue
            elif current_activity is Activity.DIALYSIS:
                style.pattern = pattern_ice_blue
            elif current_activity is Activity.NEPHROLOGY:
                style.pattern = pattern_light_green
            elif current_activity is Activity.OTHERS:
                style.pattern = pattern_tan
            elif current_activity is Activity.OBLIGATION:
                style.pattern = pattern_ivory
            elif current_activity is Activity.OBLIGATION_WEEKEND:
                style.pattern = pattern_light_yellow
            elif current_activity is Activity.OBLIGATION_HOLIDAY:
                style.pattern = pattern_gold
            else:
                style.pattern = Pattern()
            return style

        last_day = calendar.monthrange(self.year, self.month)[1]
        for x in range(1, last_day + 1):
            current_date = date(self.year, self.month, x)
            current_daily_planning = self.daily_plannings[current_date]

            # build date and date status columns
            sheet.write(
                row_offset + 5 + x,
                column_offset,
                "{0}. {1}".format(self.human_readable_days[current_daily_planning.weekday][0:3].lower(), x),
                style_date,
            )
            if current_daily_planning.weekday in [5, 6]:
                sheet.write(row_offset + 5 + x, column_offset + 1, "WK", style_date_status)
            elif not current_daily_planning.is_working_day:
                sheet.write(row_offset + 5 + x, column_offset + 1, "Férié", style_date_status)
            else:
                sheet.write(row_offset + 5 + x, column_offset + 1, "", style_date_status)

            """
            easyxf(
                 'font: bold 1, name Tahoma, height 160;'
                 'align: vertical center, horizontal center, wrap on;'
                 'borders: left thin, right thin, top thin, bottom thin;'
                 'pattern: pattern solid, pattern_fore_colour green, pattern_back_colour green'
                 )
            """

            # fill in month planning
            for y in Database.team():
                current_activity = current_daily_planning.__get_activity__(TimeSlot.FIRST_SHIFT, y)
                sheet.write(
                    row_offset + 5 + x,
                    column_offset + date_column_offset + 3 * (y.id - 1),
                    self.human_readable_activities[current_activity] if current_activity else "",
                    __cell_colouration__(style_cell_normal, current_activity),
                )
                current_activity = current_daily_planning.__get_activity__(TimeSlot.SECOND_SHIFT, y)
                sheet.write(
                    row_offset + 5 + x,
                    column_offset + date_column_offset + 1 + 3 * (y.id - 1),
                    self.human_readable_activities[current_activity] if current_activity else "",
                    __cell_colouration__(style_cell_normal, current_activity),
                )
                current_activity = current_daily_planning.__get_activity__(TimeSlot.THIRD_SHIFT, y)
                sheet.write(
                    row_offset + 5 + x,
                    column_offset + date_column_offset + 2 + 3 * (y.id - 1),
                    self.human_readable_activities[current_activity] if current_activity else "",
                    __cell_colouration__(style_cell_right, current_activity),
                )

        # patch table bottom border
        for x in range(0, table_width + 1):
            sheet.write(row_offset + 5 + (last_day + 1), column_offset + x, "", style_cell_top)

        book.save(r"C:\Temp\nephro-planner\new.xls")
Esempio n. 11
0
    def output(self):
        # from xlrd import open_workbook
        from xlwt import Workbook, XFStyle, Borders, Alignment, Font, Pattern, Style, easyxf
        # from xlutils.copy import copy
        '''
        rb = open_workbook(r"Templates\template.xls")
        wb = copy(rb)

        s = wb.get_sheet(0)
        s.write(0, 0, 'A1')
        wb.save(r"C:\Temp\nephro-planner\new.xls")
        '''

        book = Workbook(encoding="utf-8")
        sheet = book.add_sheet(r"Feuille1")

        style_title = XFStyle()
        font_title = Font()
        font_title.name = "Comic Sans MS"
        font_title.height = 280
        style_title.font = font_title
        style_title.alignment.horz = Alignment.HORZ_CENTER
        style_title.alignment.vert = Alignment.VERT_CENTER

        style_cell_bottom = XFStyle()
        borders_cell_bottom = Borders()
        borders_cell_bottom.bottom = Borders.MEDIUM
        style_cell_bottom.borders = borders_cell_bottom

        style_header = XFStyle()
        font_header = Font()
        font_header.bold = 1
        font_header.name = "Arial Narrow"
        font_header.height = 240
        style_header.font = font_header
        style_header.alignment.horz = Alignment.HORZ_CENTER
        style_header.alignment.vert = Alignment.VERT_CENTER
        borders_header = Borders()
        borders_header.top = Borders.MEDIUM
        borders_header.left = Borders.MEDIUM
        borders_header.bottom = Borders.MEDIUM
        borders_header.right = Borders.MEDIUM
        style_header.borders = borders_header

        style_sub_header = XFStyle()
        font_sub_header = Font()
        font_sub_header.name = "Arial Narrow"
        font_sub_header.height = 240
        style_sub_header.font = font_sub_header
        style_sub_header.alignment.horz = Alignment.HORZ_CENTER
        style_sub_header.alignment.vert = Alignment.VERT_CENTER
        borders_sub_header = Borders()
        borders_sub_header.top = Borders.MEDIUM
        borders_sub_header.left = Borders.MEDIUM
        borders_sub_header.bottom = Borders.MEDIUM
        borders_sub_header.right = Borders.MEDIUM
        style_sub_header.borders = borders_sub_header

        style_date = XFStyle()
        font_date = Font()
        font_date.name = "Arial Narrow"
        font_date.height = 240
        style_date.font = font_date
        style_date.alignment.horz = Alignment.HORZ_RIGHT
        style_date.alignment.vert = Alignment.VERT_CENTER
        borders_date = Borders()
        borders_date.left = Borders.MEDIUM
        style_date.borders = borders_date

        style_date_status = XFStyle()
        font_date_status = Font()
        font_date_status.name = "Arial Narrow"
        font_date_status.height = 240
        style_date_status.font = font_date_status
        style_date_status.alignment.horz = Alignment.HORZ_LEFT
        style_date_status.alignment.vert = Alignment.VERT_CENTER
        borders_date_status = Borders()
        borders_date_status.right = Borders.MEDIUM
        style_date_status.borders = borders_date_status

        style_cell_normal = XFStyle()
        font_cell_normal = Font()
        font_cell_normal.name = "Arial Narrow"
        font_cell_normal.height = 220
        style_cell_normal.font = font_cell_normal
        style_cell_normal.alignment.horz = Alignment.HORZ_CENTER
        style_cell_normal.alignment.vert = Alignment.VERT_CENTER

        style_cell_right = XFStyle()
        font_cell_right = Font()
        font_cell_right.name = "Arial Narrow"
        font_cell_right.height = 220
        style_cell_right.font = font_cell_right
        style_cell_right.alignment.horz = Alignment.HORZ_CENTER
        style_cell_right.alignment.vert = Alignment.VERT_CENTER
        borders_cell_right = Borders()
        borders_cell_right.right = Borders.MEDIUM
        style_cell_right.borders = borders_cell_right

        style_cell_top = XFStyle()
        borders_cell_top = Borders()
        borders_cell_top.top = Borders.MEDIUM
        style_cell_top.borders = borders_cell_top

        column_offset = 1
        date_column_offset = 2
        row_offset = 1
        table_width = 1 + 3 * len(Database.team())

        # build titles
        sheet.write_merge(row_offset, row_offset, column_offset,
                          table_width + 1, "POLE MEDECINE INTERNE",
                          style_title)
        sheet.write_merge(row_offset + 1, row_offset + 1, column_offset,
                          table_width + 1, "Service NEPHROLOGIE – HEMODIALYSE",
                          style_title)
        sheet.write_merge(
            row_offset + 2, row_offset + 2, column_offset, table_width + 1,
            "Planning de {0} {1}".format(
                self.human_readable_months[self.month - 1],
                self.year), style_title)

        # patch date columns top borders
        sheet.write(row_offset + 5, column_offset, "", style_cell_bottom)
        sheet.write(row_offset + 5, column_offset + 1, "", style_cell_bottom)

        # build header and sub header
        for x in Database.team():
            sheet.write_merge(
                row_offset + 4, row_offset + 4,
                column_offset + date_column_offset + 3 * (x.id - 1),
                column_offset + date_column_offset + 3 * x.id - 1, x.name,
                style_header)
            sheet.write(row_offset + 5,
                        column_offset + date_column_offset + 3 * (x.id - 1),
                        "M", style_sub_header)
            sheet.write(
                row_offset + 5,
                column_offset + date_column_offset + 3 * (x.id - 1) + 1, "AM",
                style_sub_header)
            sheet.write(
                row_offset + 5,
                column_offset + date_column_offset + 3 * (x.id - 1) + 2, "N",
                style_sub_header)
        '''
        i = 40
        for x in sorted(Style.colour_map):
            style = XFStyle()
            pattern = Pattern()
            pattern.pattern = Pattern.SOLID_PATTERN
            pattern.pattern_fore_colour = Style.colour_map[x]
            style.pattern = pattern
            sheet.write(i, 1, x, style)
            i += 1
        '''

        pattern_pale_blue = Pattern()
        pattern_pale_blue.pattern = Pattern.SOLID_PATTERN
        pattern_pale_blue.pattern_fore_colour = Style.colour_map['pale_blue']

        pattern_light_yellow = Pattern()
        pattern_light_yellow.pattern = Pattern.SOLID_PATTERN
        pattern_light_yellow.pattern_fore_colour = Style.colour_map[
            'light_yellow']

        pattern_ice_blue = Pattern()
        pattern_ice_blue.pattern = Pattern.SOLID_PATTERN
        pattern_ice_blue.pattern_fore_colour = Style.colour_map['ice_blue']

        pattern_light_green = Pattern()
        pattern_light_green.pattern = Pattern.SOLID_PATTERN
        pattern_light_green.pattern_fore_colour = Style.colour_map[
            'light_green']

        pattern_ivory = Pattern()
        pattern_ivory.pattern = Pattern.SOLID_PATTERN
        pattern_ivory.pattern_fore_colour = Style.colour_map['ivory']

        pattern_tan = Pattern()
        pattern_tan.pattern = Pattern.SOLID_PATTERN
        pattern_tan.pattern_fore_colour = Style.colour_map['tan']

        pattern_gold = Pattern()
        pattern_gold.pattern = Pattern.SOLID_PATTERN
        pattern_gold.pattern_fore_colour = Style.colour_map['gold']

        def __cell_colouration__(style, current_activity):
            if current_activity is Activity.CONSULTATION:
                style.pattern = pattern_pale_blue
            elif current_activity is Activity.DIALYSIS:
                style.pattern = pattern_ice_blue
            elif current_activity is Activity.NEPHROLOGY:
                style.pattern = pattern_light_green
            elif current_activity is Activity.OTHERS:
                style.pattern = pattern_tan
            elif current_activity is Activity.OBLIGATION:
                style.pattern = pattern_ivory
            elif current_activity is Activity.OBLIGATION_WEEKEND:
                style.pattern = pattern_light_yellow
            elif current_activity is Activity.OBLIGATION_HOLIDAY:
                style.pattern = pattern_gold
            else:
                style.pattern = Pattern()
            return style

        last_day = calendar.monthrange(self.year, self.month)[1]
        for x in range(1, last_day + 1):
            current_date = date(self.year, self.month, x)
            current_daily_planning = self.daily_plannings[current_date]

            # build date and date status columns
            sheet.write(
                row_offset + 5 + x, column_offset, "{0}. {1}".format(
                    self.human_readable_days[current_daily_planning.weekday]
                    [0:3].lower(), x), style_date)
            if current_daily_planning.weekday in [5, 6]:
                sheet.write(row_offset + 5 + x, column_offset + 1, "WK",
                            style_date_status)
            elif not current_daily_planning.is_working_day:
                sheet.write(row_offset + 5 + x, column_offset + 1, "Férié",
                            style_date_status)
            else:
                sheet.write(row_offset + 5 + x, column_offset + 1, "",
                            style_date_status)
            '''
            easyxf(
                 'font: bold 1, name Tahoma, height 160;'
                 'align: vertical center, horizontal center, wrap on;'
                 'borders: left thin, right thin, top thin, bottom thin;'
                 'pattern: pattern solid, pattern_fore_colour green, pattern_back_colour green'
                 )
            '''

            # fill in month planning
            for y in Database.team():
                current_activity = current_daily_planning.__get_activity__(
                    TimeSlot.FIRST_SHIFT, y)
                sheet.write(
                    row_offset + 5 + x,
                    column_offset + date_column_offset + 3 * (y.id - 1),
                    self.human_readable_activities[current_activity]
                    if current_activity else "",
                    __cell_colouration__(style_cell_normal, current_activity))
                current_activity = current_daily_planning.__get_activity__(
                    TimeSlot.SECOND_SHIFT, y)
                sheet.write(
                    row_offset + 5 + x,
                    column_offset + date_column_offset + 1 + 3 * (y.id - 1),
                    self.human_readable_activities[current_activity]
                    if current_activity else "",
                    __cell_colouration__(style_cell_normal, current_activity))
                current_activity = current_daily_planning.__get_activity__(
                    TimeSlot.THIRD_SHIFT, y)
                sheet.write(
                    row_offset + 5 + x,
                    column_offset + date_column_offset + 2 + 3 * (y.id - 1),
                    self.human_readable_activities[current_activity]
                    if current_activity else "",
                    __cell_colouration__(style_cell_right, current_activity))

        # patch table bottom border
        for x in range(0, table_width + 1):
            sheet.write(row_offset + 5 + (last_day + 1), column_offset + x, "",
                        style_cell_top)

        book.save(r"C:\Temp\nephro-planner\new.xls")
Esempio n. 12
0
def main():
    try:
        # Database.__create__()
        # Nephrologist.__load__()

        import codecs
        import json
        from Utils.custom_encoder import CustomEncoder
        from Utils.python_object_encoder import PythonObjectEncoder
        from collections import Counter

        # print(json.dumps(Counter({Activity.OBLIGATION: 5}), cls=CustomEncoder, indent=4, sort_keys=True, ensure_ascii=False))

        # from pprint import pprint

        import io
        with io.open("data.txt", "wb") as outfile:
        # with codecs.open("data.txt", "w", encoding="utf8") as outfile:
            s = json.dumps(Database.team(), cls=CustomEncoder, indent=4, sort_keys=True, encoding="latin-1")
            # print s
            # s2u = u"%s" % s  # incorrect
            s2u = unicode(s, "latin-1")
            # print s2u
            sutf8 = s2u.encode("latin-1")
            # print sutf8
            outfile.write(sutf8)

        '''
        for n in Database.team():
            print repr(n)
        '''



        from Models.preferences import Preferences
        from Models.aversions import Aversions
        def __recursive_object_hook__(o):
            def __regular_t2a__(p):
                return dict([(int(key), dict([(TimeSlot.__from_string__(timeslot), [Activity.__from_string__(activity) for activity in activities]) for timeslot, activities in timeslot2activites.iteritems()])) for key, timeslot2activites in p.iteritems()])

            if isinstance(o, list):
                return [__recursive_object_hook__(value) for value in o]
            elif "__nephrologist__" in o:
                nephrologist = o["__nephrologist__"]
                id = int(nephrologist["id"]) if "id" in nephrologist else None
                name = str(nephrologist["name"].encode("latin-1")) if "name" in nephrologist else None
                activities = [Activity.__from_string__(flag) for flag in nephrologist["activities"] if "activities" in nephrologist]
                holidays = [__recursive_object_hook__(value) for value in nephrologist["holidays"] if "holidays" in nephrologist]
                preferences = __recursive_object_hook__(nephrologist["preferences"]) if "preferences" in nephrologist else None
                aversions = __recursive_object_hook__(nephrologist["aversions"]) if "aversions" in nephrologist else None
                counters = __recursive_object_hook__(nephrologist["initial_counters"]) if "initial_counters" in nephrologist else None
                return Nephrologist(id, name, activities, holidays, preferences, aversions, counters)
            elif "__date__" in o:
                return datetime.strptime(o["__date__"], "%Y-%m-%d").date()
            elif "__preferences__" in o:
                return Preferences(__regular_t2a__(o["__preferences__"]))
            elif "__aversions__" in o:
                return Aversions(__regular_t2a__(o["__aversions__"]))
            elif "__counter__" in o:
                return Counter(dict([(Activity.__from_string__(key), value) for key, value in o["__counter__"].iteritems()]))
            else:
                return o

        from Utils.custom_decoder import CustomDecoder
        with io.open("data.txt", "rb") as infile:
            # reads = unicode(infile.read(), 'utf-8')
            r = infile.read().decode("unicode-escape")
            print("Séver")
            print(u"Séver")
            print(str("Séver"))
            print(r[3990:3995])
            # r2u = unicode(r, "utf-8")
            # print(r)
            # print(reads)
            # data = CustomDecoder().decode(reads)
            data = json.loads(r, object_hook=__recursive_object_hook__)
            print(unicode(data[2].name, "utf-8"))
            print(unicode(Database.team()[2].name, "utf-8"))
            # print(type(data))
            # print pprint(data)
        return






        year = 2015
        month = 5
        month_planning = MonthlyPlanning(year, month)
        month_planning.__compute__()

        '''
        print("--------------------------------------------------")
        for x in Database.team():
            print(repr(x) + ": " + str(x.counters()))
        '''

        '''
        for y in current_daily_planning.profile:
            print(str(today) + "|" + str(y) + ": " + "-".join([str(x) for x in sorted(current_daily_planning.currentlyAllocatedNephrologists(y))]))
        '''

        '''
        for x in Database.team():
            print(repr(x) + ": ")
            for holiday in sorted(month_planning.holidays[x.id]):
                print("\t" + str(holiday) + ": " + "|".join([y.name[0] for y in month_planning.holidays[x.id][holiday]]))

        print("------------------------------------------------------------")
        '''

        print(month_planning)

        month_planning.output()
    finally:
        pass